From a4af7f10214b98a03bd16f4a1badb77e14d00417 Mon Sep 17 00:00:00 2001 From: jasonkarel Date: Wed, 13 Nov 2024 12:53:01 +0700 Subject: [PATCH] chore: Increase domain package unit test coverage (event and schema) --- go.work.sum | 4 + server/pkg/integrationapi/event_test.go | 155 +++++++++++++++++++++++ server/pkg/integrationapi/schema_test.go | 112 ++++++++++++++++ 3 files changed, 271 insertions(+) create mode 100644 server/pkg/integrationapi/event_test.go create mode 100644 server/pkg/integrationapi/schema_test.go diff --git a/go.work.sum b/go.work.sum index 4df069b56b..ab7fb719c0 100644 --- a/go.work.sum +++ b/go.work.sum @@ -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= @@ -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= diff --git a/server/pkg/integrationapi/event_test.go b/server/pkg/integrationapi/event_test.go new file mode 100644 index 0000000000..da5159cb6e --- /dev/null +++ b/server/pkg/integrationapi/event_test.go @@ -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("hoge@example.com").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) + } + } +} diff --git a/server/pkg/integrationapi/schema_test.go b/server/pkg/integrationapi/schema_test.go new file mode 100644 index 0000000000..374e72b7f3 --- /dev/null +++ b/server/pkg/integrationapi/schema_test.go @@ -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{}, +// }, +// }, +// } +// }