-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Increase domain package unit test coverage (event and schema)
- Loading branch information
1 parent
f7f15b6
commit a4af7f1
Showing
3 changed files
with
271 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,155 @@ | ||
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() | ||
// machineID := | ||
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 { | ||
result := NewOperator(test.input) | ||
if !assert.Equal(t, result, test.want) { | ||
t.Errorf("expected %+v but got %+v", test.want, result) | ||
} | ||
} | ||
} | ||
|
||
func TestNewEventWith(t *testing.T) { | ||
now := time.Now() | ||
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(now).Type(event.AssetCreate).Operator(operator.OperatorFromUser(u.ID())).Object(a).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, | ||
}, | ||
} | ||
for _, test := range tests { | ||
result, err := NewEventWith(test.args.event, test.args.override, test.args.v, test.args.urlResolver) | ||
if !assert.Equal(t, result, test.want) { | ||
t.Errorf("expected %+v but got %+v", test.want, result) | ||
} | ||
if !assert.Equal(t, err, test.wantErr) { | ||
t.Errorf("expected %+v but got %+v", test.wantErr, err) | ||
} | ||
} | ||
} |
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,112 @@ | ||
package integrationapi | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/reearth/reearth-cms/server/pkg/id" | ||
"github.com/reearth/reearth-cms/server/pkg/model" | ||
"github.com/reearth/reearth-cms/server/pkg/schema" | ||
"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 | ||
} | ||
timeNow := time.Now() | ||
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(timeNow).MustBuild() | ||
model2 := model.New().ID(id.NewModelID()).Metadata(s2.ID().Ref()).Project(pID).Schema(s2.ID()).Key(id.NewKey("mmm123")).UpdatedAt(timeNow).MustBuild() | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want Model | ||
}{ | ||
{ | ||
name: "success", | ||
args: args{ | ||
m: model1, | ||
sp: schemaPackage1, | ||
lastModified: timeNow, | ||
}, | ||
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(timeNow), | ||
}, | ||
}, | ||
{ | ||
name: "success with item field in schema", | ||
args: args{ | ||
m: model2, | ||
sp: schemaPackage2, | ||
lastModified: timeNow, | ||
}, | ||
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(timeNow), | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := NewModel(tt.args.m, tt.args.sp, tt.args.lastModified) | ||
if result != tt.want { | ||
assert.Equal(t, result, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// func TestNewItemFieldChanges(t *testing.T) { | ||
// type args struct { | ||
// change item.FieldChange | ||
// } | ||
|
||
// tests := []struct{ | ||
// name string | ||
// args args | ||
// want []FieldChange | ||
// }{ | ||
// { | ||
// name: "success", | ||
// args: args{ | ||
// change: item.FieldChanges{}, | ||
// }, | ||
// }, | ||
// } | ||
// } |