-
Notifications
You must be signed in to change notification settings - Fork 6
test: add unit tests for Asset of mongodoc #129
base: main
Are you sure you want to change the base?
Conversation
Codecov Report
@@ Coverage Diff @@
## main #129 +/- ##
==========================================
+ Coverage 36.25% 36.34% +0.09%
==========================================
Files 326 326
Lines 29069 29069
==========================================
+ Hits 10538 10566 +28
+ Misses 17576 17545 -31
- Partials 955 958 +3
|
out := c.Consume(tt.args.raw) | ||
assert.Equal(t, out == nil, tt.wantErr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
out := c.Consume(tt.args.raw) | |
assert.Equal(t, out == nil, tt.wantErr) | |
err := c.Consume(tt.args.raw) | |
assert.Equal(t, err, tt.wantErr) |
- The variable name "out" is not appropriate because the return value of the consume method is an error.
- Use a dedicated method to test for nil, such as
assert.Nil
,assert.NoError
, etc. Here, however, we use Equal to test for both cases where an error occurs and where it does not. Try to add a test case that causes an error. - Refer to https://pkg.go.dev/github.com/stretchr/testify/assert to learn about assert functions
type fields struct { | ||
Rows []*asset.Asset | ||
} | ||
type args struct { | ||
raw bson.Raw | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
wantErr bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type fields struct { | |
Rows []*asset.Asset | |
} | |
type args struct { | |
raw bson.Raw | |
} | |
tests := []struct { | |
name string | |
fields fields | |
args args | |
wantErr bool | |
type args struct { | |
raw bson.Raw | |
} | |
tests := []struct { | |
name string | |
target *AssetConsumer | |
args args | |
want *AssetConsumer | |
wantErr bool |
- Code style: use
target
instead offields
- Since Consume is a method that mutates AssetConsumer itself, it must test itself expected after execution.
assert.Equal(t, tt.target, tt.want)
newAsset := asset.New(). | ||
ID(aid). | ||
CreatedAt(time.Time{}). | ||
Team(tid). | ||
Name("name"). | ||
Size(10). | ||
URL("test"). | ||
ContentType("content type"). | ||
MustBuild() | ||
|
||
tests := []struct { | ||
name string | ||
target *AssetDocument | ||
want *asset.Asset | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "asset model", | ||
target: &AssetDocument{ | ||
ID: "01f2r7kg1fvvffp0gmexgy5hxy", | ||
CreatedAt: time.Time{}, | ||
Team: "01f2r7kg1fvvffp0gmexgy5hxy", | ||
Name: "name", | ||
Size: 10, | ||
URL: "test", | ||
ContentType: "content type", | ||
}, | ||
want: newAsset, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newAsset := asset.New(). | |
ID(aid). | |
CreatedAt(time.Time{}). | |
Team(tid). | |
Name("name"). | |
Size(10). | |
URL("test"). | |
ContentType("content type"). | |
MustBuild() | |
tests := []struct { | |
name string | |
target *AssetDocument | |
want *asset.Asset | |
wantErr bool | |
}{ | |
{ | |
name: "asset model", | |
target: &AssetDocument{ | |
ID: "01f2r7kg1fvvffp0gmexgy5hxy", | |
CreatedAt: time.Time{}, | |
Team: "01f2r7kg1fvvffp0gmexgy5hxy", | |
Name: "name", | |
Size: 10, | |
URL: "test", | |
ContentType: "content type", | |
}, | |
want: newAsset, | |
now := time.Now() | |
assetID := id.NewAssetID() | |
teamID := id.NewTeamID() | |
tests := []struct { | |
name string | |
target *AssetDocument | |
want *asset.Asset | |
wantErr bool | |
}{ | |
{ | |
name: "asset model", | |
target: &AssetDocument{ | |
ID: assetID.String(), | |
CreatedAt: now, | |
Team: teamID.String(), | |
Name: "name", | |
Size: 10, | |
URL: "test", | |
ContentType: "content type", | |
}, | |
want: asset.New(). | |
ID(assetID). | |
CreatedAt(now). | |
Team(teamID). | |
Name("name"). | |
Size(10). | |
URL("test"). | |
ContentType("content type"). | |
MustBuild(), |
It is easier to duplicate test cases and add various test cases if Asset is initialized for each test case.
newAsset := asset.New(). | ||
NewID(). | ||
Team(id.NewTeamID()). | ||
Name("test"). | ||
Size(10). | ||
URL("test_url"). | ||
MustBuild() | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want *AssetDocument | ||
want1 string | ||
}{ | ||
{ | ||
name: "new asset", | ||
args: args{ | ||
asset: newAsset, | ||
}, | ||
want: &AssetDocument{ | ||
ID: newAsset.ID().String(), | ||
CreatedAt: newAsset.CreatedAt(), | ||
Team: newAsset.Team().String(), | ||
Name: newAsset.Name(), | ||
Size: newAsset.Size(), | ||
URL: newAsset.URL(), | ||
ContentType: newAsset.ContentType(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newAsset := asset.New(). | |
NewID(). | |
Team(id.NewTeamID()). | |
Name("test"). | |
Size(10). | |
URL("test_url"). | |
MustBuild() | |
tests := []struct { | |
name string | |
args args | |
want *AssetDocument | |
want1 string | |
}{ | |
{ | |
name: "new asset", | |
args: args{ | |
asset: newAsset, | |
}, | |
want: &AssetDocument{ | |
ID: newAsset.ID().String(), | |
CreatedAt: newAsset.CreatedAt(), | |
Team: newAsset.Team().String(), | |
Name: newAsset.Name(), | |
Size: newAsset.Size(), | |
URL: newAsset.URL(), | |
ContentType: newAsset.ContentType(), | |
assetID := id.NewAssetID() | |
teamID := id.NewTeamID() | |
now := time.Now() | |
tests := []struct { | |
name string | |
args args | |
want *AssetDocument | |
want1 string | |
}{ | |
{ | |
name: "new asset", | |
args: args{ | |
asset: asset.New(). | |
ID(assetID). | |
Team(teamID). | |
CreatedAt(now). | |
Name("test"). | |
Size(10). | |
URL("test_url"). | |
ContentType("application/json"). | |
MustBuild(), | |
}, | |
want: &AssetDocument{ | |
ID: assetID.String(), | |
CreatedAt: now, | |
Team: teamID.String(), | |
Name: "test", | |
Size: 10, | |
URL: "test_url", | |
ContentType: "application/json", |
It is easier to increase the number of test cases by generating IDs in advance and using them rather than using the Asset itself.
} | ||
|
||
func TestAssetDocument_Model(t *testing.T) { | ||
aid, _ := id.AssetIDFrom("01f2r7kg1fvvffp0gmexgy5hxy") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
id.Must*ID
is also available
Overview
reearth/reearth-visualizer#249
What I've done
add test for mongodoc
What I haven't done
How I tested
I did a following command
go test -v ./internal/infrastructure/mongo/mongodoc/.
Which point I want you to review particularly
Memo