loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Test query must have "order by" explicitly to avoid unstable results (#24963)

The query must have the "ORDER BY", otherwise the result is not
deterministic.

Replace "interface{}" with "any" by the way.

authored by

wxiaoguang and committed by
GitHub
ad13df36 84c8ab9f

+27 -17
+27 -17
models/unittest/unit_tests.go
··· 19 19 const NonexistentID = int64(math.MaxInt64) 20 20 21 21 type testCond struct { 22 - query interface{} 23 - args []interface{} 22 + query any 23 + args []any 24 24 } 25 + 26 + type testOrderBy string 25 27 26 28 // 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 { 28 30 return &testCond{query: query, args: args} 29 31 } 30 32 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 32 40 for _, condition := range conditions { 33 41 switch cond := condition.(type) { 34 42 case *testCond: 35 43 e = e.Where(cond.query, cond.args...) 44 + case testOrderBy: 45 + orderBy = string(cond) 36 46 default: 37 47 e = e.Where(cond) 38 48 } 39 49 } 40 - return e 50 + return e.OrderBy(orderBy) 41 51 } 42 52 43 53 // 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) { 45 55 e := db.GetEngine(db.DefaultContext) 46 - return whereConditions(e, conditions).Get(bean) 56 + return whereOrderConditions(e, conditions).Get(bean) 47 57 } 48 58 49 59 // 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 { 51 61 exists, err := LoadBeanIfExists(bean, conditions...) 52 62 assert.NoError(t, err) 53 63 return exists 54 64 } 55 65 56 66 // 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 { 58 68 exists, err := LoadBeanIfExists(bean, conditions...) 59 69 assert.NoError(t, err) 60 70 assert.True(t, exists, ··· 64 74 } 65 75 66 76 // 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 { 68 78 e := db.GetEngine(db.DefaultContext).Table(table) 69 - res, err := whereConditions(e, conditions).Query() 79 + res, err := whereOrderConditions(e, conditions).Query() 70 80 assert.NoError(t, err) 71 81 assert.True(t, len(res) == 1, 72 82 "Expected to find one row in %s (with conditions %+v), but found %d", ··· 84 94 } 85 95 86 96 // 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 { 88 98 e := db.GetEngine(db.DefaultContext) 89 - count, err := whereConditions(e, conditions).Count(bean) 99 + count, err := whereOrderConditions(e, conditions).Count(bean) 90 100 assert.NoError(t, err) 91 101 return int(count) 92 102 } 93 103 94 104 // 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) { 96 106 exists, err := LoadBeanIfExists(bean, conditions...) 97 107 assert.NoError(t, err) 98 108 assert.False(t, exists) ··· 100 110 101 111 // AssertExistsIf asserts that a bean exists or does not exist, depending on 102 112 // 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) { 104 114 exists, err := LoadBeanIfExists(bean, conditions...) 105 115 assert.NoError(t, err) 106 116 assert.Equal(t, expected, exists) 107 117 } 108 118 109 119 // AssertSuccessfulInsert assert that beans is successfully inserted 110 - func AssertSuccessfulInsert(t assert.TestingT, beans ...interface{}) { 120 + func AssertSuccessfulInsert(t assert.TestingT, beans ...any) { 111 121 err := db.Insert(db.DefaultContext, beans...) 112 122 assert.NoError(t, err) 113 123 } 114 124 115 125 // 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) { 117 127 assert.EqualValues(t, expected, GetCount(t, bean)) 118 128 } 119 129