Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
chore: refactor common db conn & cleanup test code for mongo
Browse files Browse the repository at this point in the history
Signed-off-by: KeisukeYamashita <[email protected]>
  • Loading branch information
KeisukeYamashita committed May 9, 2022
1 parent 0e0cb1a commit 7810ec8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
29 changes: 4 additions & 25 deletions internal/infrastructure/mongo/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,15 @@ package mongo

import (
"context"
"os"
"testing"
"time"

"github.com/reearth/reearth-backend/internal/infrastructure/mongo/mongodoc"
"github.com/reearth/reearth-backend/pkg/asset"
"github.com/reearth/reearth-backend/pkg/id"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/mongo/driver/uuid"
)

func TestFindByID(t *testing.T) {
// Skip unit testing if "REEARTH_DB" is not configured
// See details: https://github.com/reearth/reearth/issues/273
db := os.Getenv("REEARTH_DB")
if db == "" {
return
}

tests := []struct {
Name string
Expected struct {
Expand All @@ -48,31 +36,22 @@ func TestFindByID(t *testing.T) {
},
}

c, _ := mongo.Connect(
context.Background(),
options.Client().
ApplyURI(db).
SetConnectTimeout(time.Second*10),
)
initDB := connect(t)

for _, tc := range tests {
tc := tc

t.Run(tc.Name, func(t *testing.T) {
t.Parallel()

database, _ := uuid.New()
client := mongodoc.NewClient(string(database[:]), c)
repo := NewAsset(client)
client, dropDB := initDB()
defer dropDB()

repo := NewAsset(client)
ctx := context.Background()
err := repo.Save(ctx, tc.Expected.Asset)
assert.NoError(t, err)

defer func() {
_ = c.Database(string(database[:])).Drop(ctx)
}()

got, err := repo.FindByID(ctx, tc.Expected.Asset.ID())
assert.NoError(t, err)
assert.Equal(t, tc.Expected.Asset.ID(), got.ID())
Expand Down
42 changes: 42 additions & 0 deletions internal/infrastructure/mongo/mongo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package mongo

import (
"context"
"os"
"testing"
"time"

"github.com/reearth/reearth-backend/internal/infrastructure/mongo/mongodoc"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/mongo/driver/uuid"
)

func connect(t *testing.T) func() (*mongodoc.Client, func()) {
t.Helper()

// Skip unit testing if "REEARTH_DB" is not configured
// See details: https://github.com/reearth/reearth/issues/273
db := os.Getenv("REEARTH_DB")
if db == "" {
t.SkipNow()
return nil
}

c, _ := mongo.Connect(
context.Background(),
options.Client().
ApplyURI(db).
SetConnectTimeout(time.Second*10),
)

return func() (*mongodoc.Client, func()) {
database, _ := uuid.New()
databaseName := "reearth-test-" + string(database[:])
client := mongodoc.NewClient(databaseName, c)

return client, func() {
_ = c.Database(databaseName).Drop(context.Background())
}
}
}

0 comments on commit 7810ec8

Please sign in to comment.