Postgresql - Using the snapshots example from the docs with TestMain() #2518
-
Hello there, I am trying to implement the Snapshots example from the docs using the TestMain() function. My repository looks like this:
For testing purposes, I created the following two files:
If I ran each test case separate, everything works as expected. However if I try to run both testcases one after another, it looks like the database snapshot is not working and the current table is still populated with data from my first test case. On the console, I got the following error messages:
My Go version is:
I am still a beginner with Go, so I am not 100 percent certain if this issue is related to testcontainers or if I just made a mistake when creating my testcases. Does someone have a working example which I could check out or could give me a hint about my problem? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
Hi @purpose-robot I think the error is in the cleanup function, which is executed in the outer t.Cleanup(func() {
assert.NoError(t, container.Restore(ctx), postgres.WithSnapshotName("test"))
}) Try moving it to the inner
Then the restore will execute for each subtest |
Beta Was this translation helpful? Give feedback.
-
Hi @mdelapenya I changed the cleanup function as suggested, however now I got the following error message:
|
Beta Was this translation helpful? Give feedback.
-
I checked the error message and it means that the Postgres database was accidentally terminated. As far as I can tell, this happens right after the call to I created a GitHub repository with a working example of my code which you can find here: Link to repo By the way, I checked the example mentioned here and in my case the second test case fails too, so not sure what's wrong with me. |
Beta Was this translation helpful? Give feedback.
-
Okay, I could solve my issue. |
Beta Was this translation helpful? Give feedback.
Okay, I could solve my issue.
It was not related to testcontainers-go but to the way I initialise the database pool.