-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d6ca7a
commit fa347f7
Showing
7 changed files
with
387 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,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", | ||
}, | ||
}) | ||
} |
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,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", | ||
}, | ||
}) | ||
} |
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,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", | ||
}, | ||
}) | ||
} |
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,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", | ||
}, | ||
}) | ||
} |
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
Oops, something went wrong.