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

test: add Mongo Asset's FindByID unit testing #139

Merged
merged 8 commits into from
May 11, 2022
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ jobs:
ci:
name: CI
runs-on: ubuntu-latest
services:
mongo:
image: mongo:4.4-focal
rot1024 marked this conversation as resolved.
Show resolved Hide resolved
ports:
- 27017:27017
steps:
- name: set up
uses: actions/setup-go@v3
Expand All @@ -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:
Expand Down
66 changes: 66 additions & 0 deletions internal/infrastructure/mongo/asset_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
}
}
43 changes: 43 additions & 0 deletions internal/infrastructure/mongo/mongo_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
}