diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4a14e47c..607d65e7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,6 +8,11 @@ jobs: ci: name: CI runs-on: ubuntu-latest + services: + mongo: + image: mongo:4.4-focal + ports: + - 27017:27017 steps: - name: set up uses: actions/setup-go@v3 @@ -30,6 +35,8 @@ jobs: args: --timeout=10m - name: test run: go test ./... -v -race -coverprofile=coverage.txt -covermode=atomic -timeout 10m + env: + REEARTH_DB: mongodb://localhost - name: Send coverage report uses: codecov/codecov-action@v2 with: diff --git a/internal/infrastructure/mongo/asset_test.go b/internal/infrastructure/mongo/asset_test.go new file mode 100644 index 00000000..135683df --- /dev/null +++ b/internal/infrastructure/mongo/asset_test.go @@ -0,0 +1,66 @@ +package mongo + +import ( + "context" + "testing" + "time" + + "github.com/reearth/reearth-backend/pkg/asset" + "github.com/reearth/reearth-backend/pkg/id" + "github.com/stretchr/testify/assert" +) + +func TestFindByID(t *testing.T) { + tests := []struct { + Name string + Expected struct { + Name string + Asset *asset.Asset + } + }{ + { + Expected: struct { + Name string + Asset *asset.Asset + }{ + Asset: asset.New(). + NewID(). + CreatedAt(time.Now()). + Team(id.NewTeamID()). + Name("name"). + Size(10). + URL("hxxps://https://reearth.io/"). + ContentType("json"). + MustBuild(), + }, + }, + } + + initDB := connect(t) + + for _, tc := range tests { + tc := tc + + t.Run(tc.Name, func(t *testing.T) { + t.Parallel() + + client, dropDB := initDB() + defer dropDB() + + repo := NewAsset(client) + ctx := context.Background() + err := repo.Save(ctx, tc.Expected.Asset) + assert.NoError(t, err) + + got, err := repo.FindByID(ctx, tc.Expected.Asset.ID()) + assert.NoError(t, err) + assert.Equal(t, tc.Expected.Asset.ID(), got.ID()) + assert.Equal(t, tc.Expected.Asset.CreatedAt(), got.CreatedAt()) + assert.Equal(t, tc.Expected.Asset.Team(), got.Team()) + assert.Equal(t, tc.Expected.Asset.URL(), got.URL()) + assert.Equal(t, tc.Expected.Asset.Size(), got.Size()) + assert.Equal(t, tc.Expected.Asset.Name(), got.Name()) + assert.Equal(t, tc.Expected.Asset.ContentType(), got.ContentType()) + }) + } +} diff --git a/internal/infrastructure/mongo/mongo_test.go b/internal/infrastructure/mongo/mongo_test.go new file mode 100644 index 00000000..ec132989 --- /dev/null +++ b/internal/infrastructure/mongo/mongo_test.go @@ -0,0 +1,43 @@ +package mongo + +import ( + "context" + "encoding/hex" + "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-" + hex.EncodeToString(database[:]) + client := mongodoc.NewClient(databaseName, c) + + return client, func() { + _ = c.Database(databaseName).Drop(context.Background()) + } + } +}