Skip to content

Commit

Permalink
chore: Increase domain package unit test coverage (event and schema)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkarel committed Nov 13, 2024
1 parent f7f15b6 commit a4af7f1
Show file tree
Hide file tree
Showing 3 changed files with 271 additions and 0 deletions.
4 changes: 4 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,11 @@ cloud.google.com/go/auth v0.7.2/go.mod h1:VEc4p5NNxycWQTMQEDQF0bd6aTMb6VgYDXEwiJ
cloud.google.com/go/auth v0.8.0/go.mod h1:qGVp/Y3kDRSDZ5gFD/XPUfYQ9xW1iI7q8RIRoCyBbJc=
cloud.google.com/go/auth v0.9.0/go.mod h1:2HsApZBr9zGZhC9QAXsYVYaWk8kNUt37uny+XVKi7wM=
cloud.google.com/go/auth v0.9.3/go.mod h1:7z6VY+7h3KUdRov5F1i8NDP5ZzWKYmEPO842BgCsmTk=
cloud.google.com/go/auth v0.9.4/go.mod h1:SHia8n6//Ya940F1rLimhJCjjx7KE17t0ctFEci3HkA=
cloud.google.com/go/auth v0.9.9/go.mod h1:xxA5AqpDrvS+Gkmo9RqrGGRh6WSNKKOXhY3zNOr38tI=
cloud.google.com/go/auth/oauth2adapt v0.2.2/go.mod h1:wcYjgpZI9+Yu7LyYBg4pqSiaRkfEK3GQcpb7C/uyF1Q=
cloud.google.com/go/auth/oauth2adapt v0.2.3/go.mod h1:tMQXOfZzFuNuUxOypHlQEXgdfX5cuhwU+ffUuXRJE8I=
cloud.google.com/go/auth/oauth2adapt v0.2.4/go.mod h1:jC/jOpwFP6JBxhB3P5Rr0a9HLMC/Pe3eaL4NmdvqPtc=
cloud.google.com/go/automl v1.5.0 h1:Av8bE5rMQxUl4SVfYsEczEJf1ftIKQOUGGzpzEYwodY=
cloud.google.com/go/automl v1.5.0/go.mod h1:34EjfoFGMZ5sgJ9EoLsRtdPSNZLcfflJR39VbVNS2M0=
cloud.google.com/go/automl v1.6.0 h1:U+kHmeKGXgBvTlrecPJhwkItWaIpIscG5DUpQxBQZZg=
Expand Down Expand Up @@ -3378,6 +3381,7 @@ google.golang.org/genproto/googleapis/bytestream v0.0.0-20240903143218-8af14fe29
google.golang.org/genproto/googleapis/bytestream v0.0.0-20240903143218-8af14fe29dc1/go.mod h1:q0eWNnCW04EJlyrmLT+ZHsjuoUiZ36/eAEdCCezZoco=
google.golang.org/genproto/googleapis/bytestream v0.0.0-20241015192408-796eee8c2d53 h1:mVZqGNBNN8C63iGnWgHZSGbT/vG7voylnp4atysmReg=
google.golang.org/genproto/googleapis/bytestream v0.0.0-20241015192408-796eee8c2d53/go.mod h1:T8O3fECQbif8cez15vxAcjbwXxvL2xbnvbQ7ZfiMAMs=
google.golang.org/genproto/googleapis/bytestream v0.0.0-20241021214115-324edc3d5d38/go.mod h1:T8O3fECQbif8cez15vxAcjbwXxvL2xbnvbQ7ZfiMAMs=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230526203410-71b5a4ffd15e/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA=
Expand Down
155 changes: 155 additions & 0 deletions server/pkg/integrationapi/event_test.go
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)
}
}
}
112 changes: 112 additions & 0 deletions server/pkg/integrationapi/schema_test.go
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{},
// },
// },
// }
// }

0 comments on commit a4af7f1

Please sign in to comment.