From 41feef3013e7574e7fdee8bd3a69241a3fed67f8 Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Sun, 8 May 2022 11:56:13 +0900 Subject: [PATCH 1/7] add initial mongo asset's FindByID unit testing Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- internal/infrastructure/mongo/asset_test.go | 108 ++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 internal/infrastructure/mongo/asset_test.go diff --git a/internal/infrastructure/mongo/asset_test.go b/internal/infrastructure/mongo/asset_test.go new file mode 100644 index 00000000..7982d5db --- /dev/null +++ b/internal/infrastructure/mongo/asset_test.go @@ -0,0 +1,108 @@ +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 + } + + type a struct { + id asset.ID + createdAt time.Time + team asset.TeamID + name string + size int64 + url string + contentType string + } + + tests := []struct { + Name string + Expected struct { + Name string + Asset *a + } + }{ + { + Expected: struct { + Name string + Asset *a + }{ + Asset: &a{ + id: id.NewAssetID(), + createdAt: time.Now(), + team: id.NewTeamID(), + name: "name", + size: 10, + url: "hxxps://xxx.com", + contentType: "json", + }, + }, + }, + } + + c, _ := mongo.Connect( + context.Background(), + options.Client(). + ApplyURI(db). + SetConnectTimeout(time.Second*10), + ) + + for _, tc := range tests { + tc := tc + + t.Run(tc.Name, func(t *testing.T) { + t.Parallel() + + want, err := asset.New(). + NewID(). + Team(tc.Expected.Asset.team). + Name(tc.Expected.Asset.name). + Size(tc.Expected.Asset.size). + URL(tc.Expected.Asset.url). + CreatedAt(tc.Expected.Asset.createdAt). + Build() + + assert.NoError(t, err) + + database, _ := uuid.New() + client := mongodoc.NewClient(string(database[:]), c) + repo := NewAsset(client) + + ctx := context.Background() + err = repo.Save(ctx, want) + assert.NoError(t, err) + + defer func() { + _ = c.Database(string(database[:])).Drop(ctx) + }() + + got, err := repo.FindByID(ctx, want.ID()) + assert.NoError(t, err) + assert.Equal(t, want.ID(), got.ID()) + assert.Equal(t, want.CreatedAt(), got.CreatedAt()) + assert.Equal(t, want.Team(), got.Team()) + assert.Equal(t, want.URL(), got.URL()) + assert.Equal(t, want.Size(), got.Size()) + assert.Equal(t, want.Name(), got.Name()) + assert.Equal(t, want.ContentType(), got.ContentType()) + }) + } +} From 804899589d83aafe4ea3baf368dba05cdd8fa01e Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Sun, 8 May 2022 12:09:46 +0900 Subject: [PATCH 2/7] add mongo service container Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- .github/workflows/ci.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4a14e47c..91066d9d 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 From 18d01e22a284ba2bdd581fe85c8f3193b63c2c2d Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Sun, 8 May 2022 12:25:47 +0900 Subject: [PATCH 3/7] fix must build for asset building Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- internal/infrastructure/mongo/asset_test.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/internal/infrastructure/mongo/asset_test.go b/internal/infrastructure/mongo/asset_test.go index 7982d5db..295f8891 100644 --- a/internal/infrastructure/mongo/asset_test.go +++ b/internal/infrastructure/mongo/asset_test.go @@ -71,23 +71,21 @@ func TestFindByID(t *testing.T) { t.Run(tc.Name, func(t *testing.T) { t.Parallel() - want, err := asset.New(). + want := asset.New(). NewID(). Team(tc.Expected.Asset.team). Name(tc.Expected.Asset.name). Size(tc.Expected.Asset.size). URL(tc.Expected.Asset.url). CreatedAt(tc.Expected.Asset.createdAt). - Build() - - assert.NoError(t, err) + MustBuild() database, _ := uuid.New() client := mongodoc.NewClient(string(database[:]), c) repo := NewAsset(client) ctx := context.Background() - err = repo.Save(ctx, want) + err := repo.Save(ctx, want) assert.NoError(t, err) defer func() { From 0e0cb1aa2de6c308d5f1c23f56d64e9525dc9328 Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Mon, 9 May 2022 23:00:01 +0900 Subject: [PATCH 4/7] chore: use asset.Asset as expected test data Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- internal/infrastructure/mongo/asset_test.go | 59 +++++++-------------- 1 file changed, 20 insertions(+), 39 deletions(-) diff --git a/internal/infrastructure/mongo/asset_test.go b/internal/infrastructure/mongo/asset_test.go index 295f8891..bc188fe0 100644 --- a/internal/infrastructure/mongo/asset_test.go +++ b/internal/infrastructure/mongo/asset_test.go @@ -23,37 +23,27 @@ func TestFindByID(t *testing.T) { return } - type a struct { - id asset.ID - createdAt time.Time - team asset.TeamID - name string - size int64 - url string - contentType string - } - tests := []struct { Name string Expected struct { Name string - Asset *a + Asset *asset.Asset } }{ { Expected: struct { Name string - Asset *a + Asset *asset.Asset }{ - Asset: &a{ - id: id.NewAssetID(), - createdAt: time.Now(), - team: id.NewTeamID(), - name: "name", - size: 10, - url: "hxxps://xxx.com", - contentType: "json", - }, + Asset: asset.New(). + NewID(). + CreatedAt(time.Now()). + Team(id.NewTeamID()). + Name("name"). + Size(10). + URL("hxxps://https://reearth.io/"). + ContentType("json"). + MustBuild(), }, }, } @@ -71,36 +61,27 @@ func TestFindByID(t *testing.T) { t.Run(tc.Name, func(t *testing.T) { t.Parallel() - want := asset.New(). - NewID(). - Team(tc.Expected.Asset.team). - Name(tc.Expected.Asset.name). - Size(tc.Expected.Asset.size). - URL(tc.Expected.Asset.url). - CreatedAt(tc.Expected.Asset.createdAt). - MustBuild() - database, _ := uuid.New() client := mongodoc.NewClient(string(database[:]), c) repo := NewAsset(client) ctx := context.Background() - err := repo.Save(ctx, want) + err := repo.Save(ctx, tc.Expected.Asset) assert.NoError(t, err) defer func() { _ = c.Database(string(database[:])).Drop(ctx) }() - got, err := repo.FindByID(ctx, want.ID()) + got, err := repo.FindByID(ctx, tc.Expected.Asset.ID()) assert.NoError(t, err) - assert.Equal(t, want.ID(), got.ID()) - assert.Equal(t, want.CreatedAt(), got.CreatedAt()) - assert.Equal(t, want.Team(), got.Team()) - assert.Equal(t, want.URL(), got.URL()) - assert.Equal(t, want.Size(), got.Size()) - assert.Equal(t, want.Name(), got.Name()) - assert.Equal(t, want.ContentType(), got.ContentType()) + 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()) }) } } From 7810ec8f93ace735bd7596837106a4164dd3eec4 Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Mon, 9 May 2022 23:06:33 +0900 Subject: [PATCH 5/7] chore: refactor common db conn & cleanup test code for mongo Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- internal/infrastructure/mongo/asset_test.go | 29 ++------------ internal/infrastructure/mongo/mongo_test.go | 42 +++++++++++++++++++++ 2 files changed, 46 insertions(+), 25 deletions(-) create mode 100644 internal/infrastructure/mongo/mongo_test.go diff --git a/internal/infrastructure/mongo/asset_test.go b/internal/infrastructure/mongo/asset_test.go index bc188fe0..135683df 100644 --- a/internal/infrastructure/mongo/asset_test.go +++ b/internal/infrastructure/mongo/asset_test.go @@ -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 { @@ -48,12 +36,7 @@ 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 @@ -61,18 +44,14 @@ func TestFindByID(t *testing.T) { 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()) diff --git a/internal/infrastructure/mongo/mongo_test.go b/internal/infrastructure/mongo/mongo_test.go new file mode 100644 index 00000000..0fb72e79 --- /dev/null +++ b/internal/infrastructure/mongo/mongo_test.go @@ -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()) + } + } +} From 6467524c96b9c80113d5520700324a304def608c Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Mon, 9 May 2022 23:09:40 +0900 Subject: [PATCH 6/7] add: REEARTH_DB env var in CI testing Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 91066d9d..607d65e7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,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: From 56a028d39c29d1552e5945470ffc5ee7fb8b7e39 Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Tue, 10 May 2022 00:46:23 +0900 Subject: [PATCH 7/7] fix: use hex encode instead of string cast Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- internal/infrastructure/mongo/mongo_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/infrastructure/mongo/mongo_test.go b/internal/infrastructure/mongo/mongo_test.go index 0fb72e79..ec132989 100644 --- a/internal/infrastructure/mongo/mongo_test.go +++ b/internal/infrastructure/mongo/mongo_test.go @@ -2,6 +2,7 @@ package mongo import ( "context" + "encoding/hex" "os" "testing" "time" @@ -32,7 +33,7 @@ func connect(t *testing.T) func() (*mongodoc.Client, func()) { return func() (*mongodoc.Client, func()) { database, _ := uuid.New() - databaseName := "reearth-test-" + string(database[:]) + databaseName := "reearth-test-" + hex.EncodeToString(database[:]) client := mongodoc.NewClient(databaseName, c) return client, func() {