Skip to content

Commit

Permalink
为组合模型增加集成测试 (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
flyhigher139 authored Dec 2, 2022
1 parent 0d6ca7a commit fa347f7
Show file tree
Hide file tree
Showing 7 changed files with 387 additions and 0 deletions.
1 change: 1 addition & 0 deletions .CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
89 changes: 89 additions & 0 deletions internal/integration/delete_composition_test.go
Original file line number Diff line number Diff line change
@@ -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",
},
})
}
81 changes: 81 additions & 0 deletions internal/integration/insert_combination_test.go
Original file line number Diff line number Diff line change
@@ -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",
},
})
}
92 changes: 92 additions & 0 deletions internal/integration/select_combination_test.go
Original file line number Diff line number Diff line change
@@ -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",
},
})
}
85 changes: 85 additions & 0 deletions internal/integration/update_combination_test.go
Original file line number Diff line number Diff line change
@@ -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",
},
})
}
26 changes: 26 additions & 0 deletions internal/test/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
}
Loading

0 comments on commit fa347f7

Please sign in to comment.