Skip to content

Commit

Permalink
making test db globally available to be accesed directly from test (t…
Browse files Browse the repository at this point in the history
…o reset it for example)
  • Loading branch information
lucasmenendez committed Sep 6, 2024
1 parent 69ea0f0 commit 7e1a7c0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 5 additions & 2 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const (
testPort = 7788
)

// testDB is the MongoDB storage for the tests. Make it global so it can be
// accessed by the tests directly.
var testDB *db.MongoStorage

// testURL helper function returns the full URL for the given path using the
// test host and port.
func testURL(path string) string {
Expand Down Expand Up @@ -111,8 +115,7 @@ func TestMain(m *testing.M) {
// set reset db env var to true
_ = os.Setenv("VOCDONI_MONGO_RESET_DB", "true")
// create a new MongoDB connection with the test database
testDB, err := db.New(mongoURI, test.RandomDatabaseName())
if err != nil {
if testDB, err = db.New(mongoURI, test.RandomDatabaseName()); err != nil {
panic(err)
}
defer testDB.Close()
Expand Down
5 changes: 5 additions & 0 deletions api/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (

func Test_registerHandler(t *testing.T) {
c := qt.New(t)
defer func() {
if err := testDB.Reset(); err != nil {
c.Logf("error resetting test database: %v", err)
}
}()

registerURL := testURL(usersEndpoint)
testCases := []apiTestCase{
Expand Down

0 comments on commit 7e1a7c0

Please sign in to comment.