Skip to content

Commit 5c37dac

Browse files
committed
fix
1 parent 7314726 commit 5c37dac

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

models/unittest/unit_tests.go

+27-17
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,52 @@ import (
1919
const NonexistentID = int64(math.MaxInt64)
2020

2121
type testCond struct {
22-
query interface{}
23-
args []interface{}
22+
query any
23+
args []any
2424
}
2525

26+
type testOrderBy string
27+
2628
// Cond create a condition with arguments for a test
27-
func Cond(query interface{}, args ...interface{}) interface{} {
29+
func Cond(query any, args ...any) any {
2830
return &testCond{query: query, args: args}
2931
}
3032

31-
func whereConditions(e db.Engine, conditions []interface{}) db.Engine {
33+
// OrderBy creates "ORDER BY" a test query
34+
func OrderBy(orderBy string) any {
35+
return testOrderBy(orderBy)
36+
}
37+
38+
func whereOrderConditions(e db.Engine, conditions []any) db.Engine {
39+
orderBy := "id" // query must have the "ORDER BY", otherwise the result is not deterministic
3240
for _, condition := range conditions {
3341
switch cond := condition.(type) {
3442
case *testCond:
3543
e = e.Where(cond.query, cond.args...)
44+
case testOrderBy:
45+
orderBy = string(cond)
3646
default:
3747
e = e.Where(cond)
3848
}
3949
}
40-
return e
50+
return e.OrderBy(orderBy)
4151
}
4252

4353
// LoadBeanIfExists loads beans from fixture database if exist
44-
func LoadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {
54+
func LoadBeanIfExists(bean any, conditions ...any) (bool, error) {
4555
e := db.GetEngine(db.DefaultContext)
46-
return whereConditions(e, conditions).Get(bean)
56+
return whereOrderConditions(e, conditions).Get(bean)
4757
}
4858

4959
// BeanExists for testing, check if a bean exists
50-
func BeanExists(t assert.TestingT, bean interface{}, conditions ...interface{}) bool {
60+
func BeanExists(t assert.TestingT, bean any, conditions ...any) bool {
5161
exists, err := LoadBeanIfExists(bean, conditions...)
5262
assert.NoError(t, err)
5363
return exists
5464
}
5565

5666
// AssertExistsAndLoadBean assert that a bean exists and load it from the test database
57-
func AssertExistsAndLoadBean[T any](t assert.TestingT, bean T, conditions ...interface{}) T {
67+
func AssertExistsAndLoadBean[T any](t assert.TestingT, bean T, conditions ...any) T {
5868
exists, err := LoadBeanIfExists(bean, conditions...)
5969
assert.NoError(t, err)
6070
assert.True(t, exists,
@@ -64,9 +74,9 @@ func AssertExistsAndLoadBean[T any](t assert.TestingT, bean T, conditions ...int
6474
}
6575

6676
// AssertExistsAndLoadMap assert that a row exists and load it from the test database
67-
func AssertExistsAndLoadMap(t assert.TestingT, table string, conditions ...interface{}) map[string]string {
77+
func AssertExistsAndLoadMap(t assert.TestingT, table string, conditions ...any) map[string]string {
6878
e := db.GetEngine(db.DefaultContext).Table(table)
69-
res, err := whereConditions(e, conditions).Query()
79+
res, err := whereOrderConditions(e, conditions).Query()
7080
assert.NoError(t, err)
7181
assert.True(t, len(res) == 1,
7282
"Expected to find one row in %s (with conditions %+v), but found %d",
@@ -84,36 +94,36 @@ func AssertExistsAndLoadMap(t assert.TestingT, table string, conditions ...inter
8494
}
8595

8696
// GetCount get the count of a bean
87-
func GetCount(t assert.TestingT, bean interface{}, conditions ...interface{}) int {
97+
func GetCount(t assert.TestingT, bean any, conditions ...any) int {
8898
e := db.GetEngine(db.DefaultContext)
89-
count, err := whereConditions(e, conditions).Count(bean)
99+
count, err := whereOrderConditions(e, conditions).Count(bean)
90100
assert.NoError(t, err)
91101
return int(count)
92102
}
93103

94104
// AssertNotExistsBean assert that a bean does not exist in the test database
95-
func AssertNotExistsBean(t assert.TestingT, bean interface{}, conditions ...interface{}) {
105+
func AssertNotExistsBean(t assert.TestingT, bean any, conditions ...any) {
96106
exists, err := LoadBeanIfExists(bean, conditions...)
97107
assert.NoError(t, err)
98108
assert.False(t, exists)
99109
}
100110

101111
// AssertExistsIf asserts that a bean exists or does not exist, depending on
102112
// what is expected.
103-
func AssertExistsIf(t assert.TestingT, expected bool, bean interface{}, conditions ...interface{}) {
113+
func AssertExistsIf(t assert.TestingT, expected bool, bean any, conditions ...any) {
104114
exists, err := LoadBeanIfExists(bean, conditions...)
105115
assert.NoError(t, err)
106116
assert.Equal(t, expected, exists)
107117
}
108118

109119
// AssertSuccessfulInsert assert that beans is successfully inserted
110-
func AssertSuccessfulInsert(t assert.TestingT, beans ...interface{}) {
120+
func AssertSuccessfulInsert(t assert.TestingT, beans ...any) {
111121
err := db.Insert(db.DefaultContext, beans...)
112122
assert.NoError(t, err)
113123
}
114124

115125
// AssertCount assert the count of a bean
116-
func AssertCount(t assert.TestingT, bean, expected interface{}) {
126+
func AssertCount(t assert.TestingT, bean, expected any) {
117127
assert.EqualValues(t, expected, GetCount(t, bean))
118128
}
119129

0 commit comments

Comments
 (0)