-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(server): Increase domain package unit test coverage (integrationapi part 2) #1307
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
788ada2
chore: Increase domain package unit test coverage (event and schema)
jasonkarel cfb1d99
add ut for schema_test
jasonkarel 588a865
fix mocTime for UT
jasonkarel 27ed976
fix ut mechanism
jasonkarel dfb75cb
add error case in NewEvenWith
jasonkarel f8b59f8
add item package ut
jasonkarel 6f68ae0
Merge branch 'main' into chore-increase-package-ut-coverage-2
jasonkarel 5cda658
fix ut case format with removing logs after assert
jasonkarel d5b9626
fix asset type of view
jasonkarel 185839a
Merge branch 'main' into chore-increase-package-ut-coverage-2
jasonkarel 65aaea9
Merge branch 'main' into chore-increase-package-ut-coverage-2
jasonkarel de1aa0a
Merge branch 'main' into chore-increase-package-ut-coverage-2
jasonkarel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,168 @@ | ||
package integrationapi | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/reearth/reearth-cms/server/pkg/asset" | ||
"github.com/reearth/reearth-cms/server/pkg/event" | ||
"github.com/reearth/reearth-cms/server/pkg/id" | ||
"github.com/reearth/reearth-cms/server/pkg/operator" | ||
"github.com/reearth/reearth-cms/server/pkg/project" | ||
"github.com/reearth/reearthx/account/accountdomain" | ||
"github.com/reearth/reearthx/account/accountdomain/user" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_NewOperator(t *testing.T) { | ||
|
||
uid := accountdomain.NewUserID() | ||
integrationID := id.NewIntegrationID() | ||
opUser := operator.OperatorFromUser(uid) | ||
opIntegration := operator.OperatorFromIntegration(integrationID) | ||
opMachine := operator.OperatorFromMachine() | ||
tests := []struct { | ||
name string | ||
input operator.Operator | ||
want Operator | ||
}{ | ||
{ | ||
name: "success user operator", | ||
input: opUser, | ||
want: Operator{ | ||
User: &OperatorUser{ | ||
ID: uid.String(), | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "success integration operator", | ||
input: opIntegration, | ||
want: Operator{ | ||
Integration: &OperatorIntegration{ | ||
ID: integrationID.String(), | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "success machine operator", | ||
input: opMachine, | ||
want: Operator{ | ||
Machine: &OperatorMachine{}, | ||
}, | ||
}, | ||
{ | ||
name: "success unknown operator", | ||
input: operator.Operator{}, | ||
want: Operator{}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
result := NewOperator(test.input) | ||
assert.Equal(t, result, test.want) | ||
}) | ||
|
||
} | ||
} | ||
|
||
func TestNewEventWith(t *testing.T) { | ||
mockTime := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) | ||
u := user.New().NewID().Email("[email protected]").Name("John").MustBuild() | ||
a := asset.New().NewID().Project(project.NewID()).Size(100).NewUUID(). | ||
CreatedByUser(u.ID()).Thread(id.NewThreadID()).MustBuild() | ||
eID1 := event.NewID() | ||
prj := event.Project{ | ||
ID: "testID", | ||
Alias: "testAlias", | ||
} | ||
|
||
ev := event.New[any]().ID(eID1).Timestamp(mockTime).Type(event.AssetCreate).Operator(operator.OperatorFromUser(u.ID())).Object(a).Project(&prj).MustBuild() | ||
ev1 := event.New[any]().ID(eID1).Timestamp(mockTime).Type(event.Type("test")).Operator(operator.OperatorFromUser(u.ID())).Object("test").Project(&prj).MustBuild() | ||
d1, _ := New(ev, "test", func(a *asset.Asset) string { | ||
return "test.com" | ||
}) | ||
d2, _ := New(ev.Object(), "test", func(a *asset.Asset) string { | ||
return "test.com" | ||
}) | ||
type args struct { | ||
event *event.Event[any] | ||
override any | ||
v string | ||
urlResolver asset.URLResolver | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want Event | ||
wantErr error | ||
}{ | ||
{ | ||
name: "success", | ||
args: args{ | ||
event: ev, | ||
override: ev, | ||
v: "test", | ||
urlResolver: func(a *asset.Asset) string { | ||
return "test.com" | ||
}, | ||
}, | ||
want: Event{ | ||
ID: ev.ID().String(), | ||
Type: string(ev.Type()), | ||
Timestamp: ev.Timestamp(), | ||
Data: d1, | ||
Project: &ProjectIdAlias{ | ||
ID: ev.Project().ID, | ||
Alias: ev.Project().Alias, | ||
}, | ||
Operator: NewOperator(ev.Operator()), | ||
}, | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "success when override is nil", | ||
args: args{ | ||
event: ev, | ||
override: nil, | ||
v: "test", | ||
urlResolver: func(a *asset.Asset) string { | ||
return "test.com" | ||
}, | ||
}, | ||
want: Event{ | ||
ID: ev.ID().String(), | ||
Type: string(ev.Type()), | ||
Timestamp: ev.Timestamp(), | ||
Data: d2, | ||
Project: &ProjectIdAlias{ | ||
ID: ev.Project().ID, | ||
Alias: ev.Project().Alias, | ||
}, | ||
Operator: NewOperator(ev.Operator()), | ||
}, | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "error new returns error", | ||
args: args{ | ||
event: ev, | ||
override: ev1, | ||
v: "", | ||
urlResolver: nil, | ||
}, | ||
want: Event{}, | ||
wantErr: ErrUnsupportedEntity, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
result, err := NewEventWith(test.args.event, test.args.override, test.args.v, test.args.urlResolver) | ||
assert.Equal(t, result, test.want) | ||
assert.Equal(t, err, test.wantErr) | ||
}) | ||
} | ||
} |
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,144 @@ | ||
package integrationapi | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/reearth/reearth-cms/server/pkg/id" | ||
"github.com/reearth/reearth-cms/server/pkg/item" | ||
"github.com/reearth/reearth-cms/server/pkg/model" | ||
"github.com/reearth/reearth-cms/server/pkg/schema" | ||
"github.com/reearth/reearth-cms/server/pkg/value" | ||
"github.com/reearth/reearthx/account/accountdomain" | ||
"github.com/reearth/reearthx/util" | ||
"github.com/samber/lo" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewModel(t *testing.T) { | ||
type args struct { | ||
m *model.Model | ||
sp *schema.Package | ||
lastModified time.Time | ||
} | ||
mockTime := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) | ||
pID := id.NewProjectID() | ||
sf1 := schema.NewField(schema.NewText(nil).TypeProperty()).NewID().RandomKey().MustBuild() | ||
sf2 := schema.NewField(lo.Must1(schema.NewInteger(nil, nil)).TypeProperty()).NewID().RandomKey().MustBuild() | ||
s1 := schema.New().NewID().Project(pID).Workspace(accountdomain.NewWorkspaceID()).Fields([]*schema.Field{sf1, sf2}).MustBuild() | ||
s2 := schema.New().NewID().Project(pID).Workspace(accountdomain.NewWorkspaceID()).Fields([]*schema.Field{sf1, sf2}).TitleField(sf1.ID().Ref()).MustBuild() | ||
schemaPackage1 := schema.NewPackage(s1, nil, nil, nil) | ||
schemaPackage2 := schema.NewPackage(s2, nil, nil, nil) | ||
model1 := model.New().ID(id.NewModelID()).Metadata(s1.ID().Ref()).Project(pID).Schema(s1.ID()).Key(id.NewKey("mmm123")).UpdatedAt(mockTime).MustBuild() | ||
model2 := model.New().ID(id.NewModelID()).Metadata(s2.ID().Ref()).Project(pID).Schema(s2.ID()).Key(id.NewKey("mmm123")).UpdatedAt(mockTime).MustBuild() | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want Model | ||
}{ | ||
{ | ||
name: "success", | ||
args: args{ | ||
m: model1, | ||
sp: schemaPackage1, | ||
lastModified: mockTime, | ||
}, | ||
want: Model{ | ||
Id: model1.ID().Ref(), | ||
Key: util.ToPtrIfNotEmpty(model1.Key().String()), | ||
Name: util.ToPtrIfNotEmpty(model1.Name()), | ||
Description: util.ToPtrIfNotEmpty(model1.Description()), | ||
Public: util.ToPtrIfNotEmpty(model1.Public()), | ||
ProjectId: model1.Project().Ref(), | ||
SchemaId: model1.Schema().Ref(), | ||
Schema: util.ToPtrIfNotEmpty(NewSchema(schemaPackage1.Schema())), | ||
MetadataSchemaId: model1.Metadata().Ref(), | ||
MetadataSchema: util.ToPtrIfNotEmpty(NewSchema(schemaPackage1.MetaSchema())), | ||
CreatedAt: lo.ToPtr(model1.ID().Timestamp()), | ||
UpdatedAt: lo.ToPtr(model1.UpdatedAt()), | ||
LastModified: util.ToPtrIfNotEmpty(mockTime), | ||
}, | ||
}, | ||
{ | ||
name: "success with item field in schema", | ||
args: args{ | ||
m: model2, | ||
sp: schemaPackage2, | ||
lastModified: mockTime, | ||
}, | ||
want: Model{ | ||
Id: model2.ID().Ref(), | ||
Key: util.ToPtrIfNotEmpty(model2.Key().String()), | ||
Name: util.ToPtrIfNotEmpty(model2.Name()), | ||
Description: util.ToPtrIfNotEmpty(model2.Description()), | ||
Public: util.ToPtrIfNotEmpty(model2.Public()), | ||
ProjectId: model2.Project().Ref(), | ||
SchemaId: model2.Schema().Ref(), | ||
Schema: util.ToPtrIfNotEmpty(NewSchema(schemaPackage2.Schema())), | ||
MetadataSchemaId: model2.Metadata().Ref(), | ||
MetadataSchema: util.ToPtrIfNotEmpty(NewSchema(schemaPackage2.MetaSchema())), | ||
CreatedAt: lo.ToPtr(model2.ID().Timestamp()), | ||
UpdatedAt: lo.ToPtr(model2.UpdatedAt()), | ||
LastModified: util.ToPtrIfNotEmpty(mockTime), | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
result := NewModel(tt.args.m, tt.args.sp, tt.args.lastModified) | ||
assert.Equal(t, tt.want, result) | ||
}) | ||
} | ||
} | ||
|
||
func TestNewItemFieldChanges(t *testing.T) { | ||
|
||
fID := id.NewFieldID() | ||
v0 := value.MultipleFrom(value.TypeBool, []*value.Value{ | ||
value.New(value.TypeBool, false), | ||
}) | ||
v1 := value.MultipleFrom(value.TypeBool, []*value.Value{ | ||
value.New(value.TypeBool, true), | ||
}) | ||
|
||
type args struct { | ||
change item.FieldChanges | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want []FieldChange | ||
}{ | ||
{ | ||
name: "success", | ||
args: args{ | ||
change: item.FieldChanges{ | ||
item.FieldChange{ | ||
ID: fID, | ||
Type: item.FieldChangeTypeAdd, | ||
CurrentValue: value.MultipleFrom(v1.Type(), []*value.Value{v1.First()}), | ||
PreviousValue: value.MultipleFrom(v0.Type(), []*value.Value{v0.First()}), | ||
}, | ||
}, | ||
}, | ||
want: []FieldChange{ | ||
{ | ||
ID: fID, | ||
Type: item.FieldChangeTypeAdd, | ||
CurrentValue: v1.Interface(), | ||
PreviousValue: v0.Interface(), | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(string(test.name), func(t *testing.T) { | ||
t.Parallel() | ||
result := NewItemFieldChanges(test.args.change) | ||
assert.Equal(t, test.want, result) | ||
}) | ||
} | ||
} | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🛠️ Refactor suggestion
Enhance test coverage with additional test cases.
While the test is well-structured, it could benefit from more comprehensive coverage:
Example additional test cases: