code.gitea.io/gitea@v1.21.7/models/unittest/unit_tests.go (about) 1 // Copyright 2016 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package unittest 5 6 import ( 7 "math" 8 9 "code.gitea.io/gitea/models/db" 10 11 "github.com/stretchr/testify/assert" 12 "xorm.io/builder" 13 ) 14 15 // Code in this file is mainly used by unittest.CheckConsistencyFor, which is not in the unit test for various reasons. 16 // In the future if we can decouple CheckConsistencyFor into separate unit test code, then this file can be moved into unittest package too. 17 18 // NonexistentID an ID that will never exist 19 const NonexistentID = int64(math.MaxInt64) 20 21 type testCond struct { 22 query any 23 args []any 24 } 25 26 type testOrderBy string 27 28 // Cond create a condition with arguments for a test 29 func Cond(query any, args ...any) any { 30 return &testCond{query: query, args: args} 31 } 32 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 40 for _, condition := range conditions { 41 switch cond := condition.(type) { 42 case *testCond: 43 e = e.Where(cond.query, cond.args...) 44 case testOrderBy: 45 orderBy = string(cond) 46 default: 47 e = e.Where(cond) 48 } 49 } 50 return e.OrderBy(orderBy) 51 } 52 53 // LoadBeanIfExists loads beans from fixture database if exist 54 func LoadBeanIfExists(bean any, conditions ...any) (bool, error) { 55 e := db.GetEngine(db.DefaultContext) 56 return whereOrderConditions(e, conditions).Get(bean) 57 } 58 59 // BeanExists for testing, check if a bean exists 60 func BeanExists(t assert.TestingT, bean any, conditions ...any) bool { 61 exists, err := LoadBeanIfExists(bean, conditions...) 62 assert.NoError(t, err) 63 return exists 64 } 65 66 // AssertExistsAndLoadBean assert that a bean exists and load it from the test database 67 func AssertExistsAndLoadBean[T any](t assert.TestingT, bean T, conditions ...any) T { 68 exists, err := LoadBeanIfExists(bean, conditions...) 69 assert.NoError(t, err) 70 assert.True(t, exists, 71 "Expected to find %+v (of type %T, with conditions %+v), but did not", 72 bean, bean, conditions) 73 return bean 74 } 75 76 // AssertExistsAndLoadMap assert that a row exists and load it from the test database 77 func AssertExistsAndLoadMap(t assert.TestingT, table string, conditions ...any) map[string]string { 78 e := db.GetEngine(db.DefaultContext).Table(table) 79 res, err := whereOrderConditions(e, conditions).Query() 80 assert.NoError(t, err) 81 assert.True(t, len(res) == 1, 82 "Expected to find one row in %s (with conditions %+v), but found %d", 83 table, conditions, len(res), 84 ) 85 86 if len(res) == 1 { 87 rec := map[string]string{} 88 for k, v := range res[0] { 89 rec[k] = string(v) 90 } 91 return rec 92 } 93 return nil 94 } 95 96 // GetCount get the count of a bean 97 func GetCount(t assert.TestingT, bean any, conditions ...any) int { 98 e := db.GetEngine(db.DefaultContext) 99 for _, condition := range conditions { 100 switch cond := condition.(type) { 101 case *testCond: 102 e = e.Where(cond.query, cond.args...) 103 default: 104 e = e.Where(cond) 105 } 106 } 107 count, err := e.Count(bean) 108 assert.NoError(t, err) 109 return int(count) 110 } 111 112 // AssertNotExistsBean assert that a bean does not exist in the test database 113 func AssertNotExistsBean(t assert.TestingT, bean any, conditions ...any) { 114 exists, err := LoadBeanIfExists(bean, conditions...) 115 assert.NoError(t, err) 116 assert.False(t, exists) 117 } 118 119 // AssertExistsIf asserts that a bean exists or does not exist, depending on 120 // what is expected. 121 func AssertExistsIf(t assert.TestingT, expected bool, bean any, conditions ...any) { 122 exists, err := LoadBeanIfExists(bean, conditions...) 123 assert.NoError(t, err) 124 assert.Equal(t, expected, exists) 125 } 126 127 // AssertSuccessfulInsert assert that beans is successfully inserted 128 func AssertSuccessfulInsert(t assert.TestingT, beans ...any) { 129 err := db.Insert(db.DefaultContext, beans...) 130 assert.NoError(t, err) 131 } 132 133 // AssertCount assert the count of a bean 134 func AssertCount(t assert.TestingT, bean, expected any) bool { 135 return assert.EqualValues(t, expected, GetCount(t, bean)) 136 } 137 138 // AssertInt64InRange assert value is in range [low, high] 139 func AssertInt64InRange(t assert.TestingT, low, high, value int64) { 140 assert.True(t, value >= low && value <= high, 141 "Expected value in range [%d, %d], found %d", low, high, value) 142 } 143 144 // GetCountByCond get the count of database entries matching bean 145 func GetCountByCond(t assert.TestingT, tableName string, cond builder.Cond) int64 { 146 e := db.GetEngine(db.DefaultContext) 147 count, err := e.Table(tableName).Where(cond).Count() 148 assert.NoError(t, err) 149 return count 150 } 151 152 // AssertCountByCond test the count of database entries matching bean 153 func AssertCountByCond(t assert.TestingT, tableName string, cond builder.Cond, expected int) bool { 154 return assert.EqualValues(t, expected, GetCountByCond(t, tableName, cond), 155 "Failed consistency test, the counted bean (of table %s) was %+v", tableName, cond) 156 }