This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add Mongo Asset's
FindByID
unit testing (#139)
- Loading branch information
1 parent
4f72b87
commit 35f9db5
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} | ||
} |