diff --git a/.CHANGELOG.md b/.CHANGELOG.md index 78e1a45..3c90d48 100644 --- a/.CHANGELOG.md +++ b/.CHANGELOG.md @@ -44,6 +44,7 @@ - [eorm: 补充实现组合定义模型后的测试用例](https://github.com/gotomicro/eorm/pull/104) - [eorm: LIKE 查询](https://github.com/gotomicro/eorm/pull/105) - [eorm: GetMulti功能](https://github.com/gotomicro/eorm/pull/109) +- [eorm: 为组合模型增加集成测试](https://github.com/gotomicro/eorm/pull/111) ### 文档, 代码质量以及文档 - [Add examples and docs for Aggregate and Assign](https://github.com/gotomicro/eorm/pull/50) diff --git a/internal/integration/delete_composition_test.go b/internal/integration/delete_composition_test.go new file mode 100644 index 0000000..41f978d --- /dev/null +++ b/internal/integration/delete_composition_test.go @@ -0,0 +1,89 @@ +// Copyright 2021 gotomicro +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build e2e + +package integration + +import ( + "context" + "testing" + + _ "github.com/go-sql-driver/mysql" + "github.com/gotomicro/eorm" + "github.com/gotomicro/eorm/internal/test" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +type DeleteCompositionTestSuite struct { + Suite +} + +func (s *DeleteCompositionTestSuite) SetupSuite() { + s.Suite.SetupSuite() + data1 := test.NewCombinedModel(1) + data2 := test.NewCombinedModel(2) + data3 := test.NewCombinedModel(3) + res := eorm.NewInserter[test.CombinedModel](s.orm).Values(data1, data2, data3).Exec(context.Background()) + if res.Err() != nil { + s.T().Fatal(res.Err()) + } +} + +func (i *DeleteCompositionTestSuite) TearDownTest() { + res := eorm.RawQuery[any](i.orm, "DELETE FROM `combined_model`").Exec(context.Background()) + if res.Err() != nil { + i.T().Fatal(res.Err()) + } +} + +func (i *DeleteCompositionTestSuite) TestDeleter() { + testCases := []struct { + name string + i *eorm.Deleter[test.CombinedModel] + rowsAffected int64 + wantErr error + }{ + { + name: "id only", + i: eorm.NewDeleter[test.CombinedModel](i.orm).From(&test.CombinedModel{}).Where(eorm.C("Id").EQ("1")), + rowsAffected: 1, + }, + { + name: "delete all", + i: eorm.NewDeleter[test.CombinedModel](i.orm).From(&test.CombinedModel{}), + rowsAffected: 2, + }, + } + for _, tc := range testCases { + i.T().Run(tc.name, func(t *testing.T) { + res := tc.i.Exec(context.Background()) + require.Equal(t, tc.wantErr, res.Err()) + affected, err := res.RowsAffected() + require.Nil(t, err) + assert.Equal(t, tc.rowsAffected, affected) + }) + } +} + +func TestMySQL8tDeleteComposition(t *testing.T) { + suite.Run(t, &DeleteCompositionTestSuite{ + Suite{ + driver: "mysql", + dsn: "root:root@tcp(localhost:13306)/integration_test", + }, + }) +} diff --git a/internal/integration/insert_combination_test.go b/internal/integration/insert_combination_test.go new file mode 100644 index 0000000..98cf094 --- /dev/null +++ b/internal/integration/insert_combination_test.go @@ -0,0 +1,81 @@ +// Copyright 2021 gotomicro +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build e2e + +package integration + +import ( + "context" + "testing" + + _ "github.com/go-sql-driver/mysql" + "github.com/gotomicro/eorm" + "github.com/gotomicro/eorm/internal/test" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +type InsertCompositionTestSuite struct { + Suite +} + +func (i *InsertCompositionTestSuite) TearDownTest() { + res := eorm.RawQuery[any](i.orm, "DELETE FROM `combined_model`").Exec(context.Background()) + if res.Err() != nil { + i.T().Fatal(res.Err()) + } +} + +func (i *InsertCompositionTestSuite) TestInsert() { + testCases := []struct { + name string + i *eorm.Inserter[test.CombinedModel] + rowsAffected int64 + wantErr error + }{ + { + name: "id only", + i: eorm.NewInserter[test.CombinedModel](i.orm).Values(&test.CombinedModel{Id: 1}), + rowsAffected: 1, + }, + { + name: "all field", + i: eorm.NewInserter[test.CombinedModel](i.orm).Values(test.NewCombinedModel(2)), + rowsAffected: 1, + }, + } + for _, tc := range testCases { + i.T().Run(tc.name, func(t *testing.T) { + res := tc.i.Exec(context.Background()) + assert.Equal(t, tc.wantErr, res.Err()) + if res.Err() != nil { + return + } + affected, err := res.RowsAffected() + require.NoError(t, err) + assert.Equal(t, tc.rowsAffected, affected) + }) + } +} + +func TestMySQL8InsertComposition(t *testing.T) { + suite.Run(t, &InsertCompositionTestSuite{ + Suite{ + driver: "mysql", + dsn: "root:root@tcp(localhost:13306)/integration_test", + }, + }) +} diff --git a/internal/integration/select_combination_test.go b/internal/integration/select_combination_test.go new file mode 100644 index 0000000..6b36c7f --- /dev/null +++ b/internal/integration/select_combination_test.go @@ -0,0 +1,92 @@ +// Copyright 2021 gotomicro +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build e2e + +package integration + +import ( + "context" + "testing" + + "github.com/gotomicro/eorm" + "github.com/gotomicro/eorm/internal/test" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" +) + +type SelectCombinationTestSuite struct { + Suite + data *test.CombinedModel +} + +func (s *SelectCombinationTestSuite) SetupSuite() { + s.Suite.SetupSuite() + s.data = test.NewCombinedModel(1) + res := eorm.NewInserter[test.CombinedModel](s.orm).Values(s.data).Exec(context.Background()) + if res.Err() != nil { + s.T().Fatal(res.Err()) + } +} + +func (s *SelectCombinationTestSuite) TearDownSuite() { + res := eorm.RawQuery[any](s.orm, "DELETE FROM `combined_model`").Exec(context.Background()) + if res.Err() != nil { + s.T().Fatal(res.Err()) + } +} + +func (s *SelectCombinationTestSuite) TestGet() { + testCases := []struct { + name string + s *eorm.Selector[test.CombinedModel] + wantErr error + wantRes *test.CombinedModel + }{ + { + name: "not found", + s: eorm.NewSelector[test.CombinedModel](s.orm). + From(&test.CombinedModel{}). + Where(eorm.C("Id").EQ(9)), + wantErr: eorm.ErrNoRows, + }, + { + name: "found", + s: eorm.NewSelector[test.CombinedModel](s.orm). + From(&test.CombinedModel{}). + Where(eorm.C("Id").EQ(1)), + wantRes: s.data, + }, + } + + for _, tc := range testCases { + s.T().Run(tc.name, func(t *testing.T) { + res, err := tc.s.Get(context.Background()) + assert.Equal(t, tc.wantErr, err) + if err != nil { + return + } + assert.Equal(t, tc.wantRes, res) + }) + } +} + +func TestMySQL8SelectCombination(t *testing.T) { + suite.Run(t, &SelectCombinationTestSuite{ + Suite: Suite{ + driver: "mysql", + dsn: "root:root@tcp(localhost:13306)/integration_test", + }, + }) +} diff --git a/internal/integration/update_combination_test.go b/internal/integration/update_combination_test.go new file mode 100644 index 0000000..25e506c --- /dev/null +++ b/internal/integration/update_combination_test.go @@ -0,0 +1,85 @@ +// Copyright 2021 gotomicro +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build e2e + +package integration + +import ( + "context" + "testing" + + "github.com/gotomicro/eorm" + "github.com/gotomicro/eorm/internal/test" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +type UpdateCombinationTestSuite struct { + Suite +} + +func (u *UpdateCombinationTestSuite) SetupSuite() { + u.Suite.SetupSuite() + data1 := test.NewCombinedModel(1) + res := eorm.NewInserter[test.CombinedModel](u.orm).Values(data1).Exec(context.Background()) + if res.Err() != nil { + u.T().Fatal(res.Err()) + } +} + +func (u *UpdateCombinationTestSuite) TearDownTest() { + res := eorm.RawQuery[any](u.orm, "DELETE FROM `combined_model`").Exec(context.Background()) + if res.Err() != nil { + u.T().Fatal(res.Err()) + } +} + +func (u *UpdateCombinationTestSuite) TestUpdate() { + testCases := []struct { + name string + u *eorm.Updater[test.CombinedModel] + rowsAffected int64 + wantErr error + }{ + { + name: "update columns", + u: eorm.NewUpdater[test.CombinedModel](u.orm).Update(&test.CombinedModel{Age: 18}). + Set(eorm.Columns("Age")).Where(eorm.C("Id").EQ(1)), + rowsAffected: 1, + }, + } + for _, tc := range testCases { + u.T().Run(tc.name, func(t *testing.T) { + res := tc.u.Exec(context.Background()) + assert.Equal(t, tc.wantErr, res.Err()) + if res.Err() != nil { + return + } + affected, err := res.RowsAffected() + require.NoError(t, err) + assert.Equal(t, tc.rowsAffected, affected) + }) + } +} + +func TestMySQL8UpdateCombination(t *testing.T) { + suite.Run(t, &UpdateCombinationTestSuite{ + Suite{ + driver: "mysql", + dsn: "root:root@tcp(localhost:13306)/integration_test", + }, + }) +} diff --git a/internal/test/types.go b/internal/test/types.go index f695f12..dbfbddd 100644 --- a/internal/test/types.go +++ b/internal/test/types.go @@ -178,3 +178,29 @@ func NewSimpleStruct(id uint64) *SimpleStruct { }, } } + +type BaseEntity struct { + CreateTime uint64 + UpdateTime uint64 +} + +type CombinedModel struct { + BaseEntity + Id int64 `eorm:"auto_increment,primary_key"` + FirstName string + Age int8 + LastName *string +} + +func NewCombinedModel(id int64) *CombinedModel { + return &CombinedModel{ + BaseEntity: BaseEntity{ + CreateTime: 10000, + UpdateTime: 10000, + }, + Id: id, + FirstName: "Tom" + fmt.Sprintln(id), + Age: 20, + LastName: ekit.ToPtr[string]("Jerry" + fmt.Sprintln(id)), + } +} diff --git a/script/mysql/init.sql b/script/mysql/init.sql index 798005e..2cbb5f8 100644 --- a/script/mysql/init.sql +++ b/script/mysql/init.sql @@ -39,3 +39,16 @@ create table if not exists `integration_test`.`simple_struct`( json_column varchar(2048), primary key (`id`) ); + +create table if not exists `integration_test`.`combined_model` +( + `id` bigint auto_increment + primary key, + `first_name` varchar(128) null, + `age` int null, + `last_name` varchar(128) null, + `create_time` bigint null, + `update_time` bigint null +); + +