4
4
package unittest
5
5
6
6
import (
7
+ "fmt"
7
8
"math"
9
+ "os"
10
+ "strings"
8
11
9
12
"code.gitea.io/gitea/models/db"
10
13
11
14
"github.com/stretchr/testify/assert"
12
15
"github.com/stretchr/testify/require"
13
16
"xorm.io/builder"
17
+ "xorm.io/xorm"
14
18
)
15
19
16
20
// Code in this file is mainly used by unittest.CheckConsistencyFor, which is not in the unit test for various reasons.
@@ -51,22 +55,23 @@ func whereOrderConditions(e db.Engine, conditions []any) db.Engine {
51
55
return e .OrderBy (orderBy )
52
56
}
53
57
54
- // LoadBeanIfExists loads beans from fixture database if exist
55
- func LoadBeanIfExists (bean any , conditions ... any ) (bool , error ) {
58
+ func getBeanIfExists (bean any , conditions ... any ) (bool , error ) {
56
59
e := db .GetEngine (db .DefaultContext )
57
60
return whereOrderConditions (e , conditions ).Get (bean )
58
61
}
59
62
60
- // BeanExists for testing, check if a bean exists
61
- func BeanExists (t assert.TestingT , bean any , conditions ... any ) bool {
62
- exists , err := LoadBeanIfExists (bean , conditions ... )
63
- assert .NoError (t , err )
64
- return exists
63
+ func GetBean [T any ](t require.TestingT , bean T , conditions ... any ) (ret T ) {
64
+ exists , err := getBeanIfExists (bean , conditions ... )
65
+ require .NoError (t , err )
66
+ if exists {
67
+ return bean
68
+ }
69
+ return ret
65
70
}
66
71
67
72
// AssertExistsAndLoadBean assert that a bean exists and load it from the test database
68
73
func AssertExistsAndLoadBean [T any ](t require.TestingT , bean T , conditions ... any ) T {
69
- exists , err := LoadBeanIfExists (bean , conditions ... )
74
+ exists , err := getBeanIfExists (bean , conditions ... )
70
75
require .NoError (t , err )
71
76
require .True (t , exists ,
72
77
"Expected to find %+v (of type %T, with conditions %+v), but did not" ,
@@ -112,25 +117,11 @@ func GetCount(t assert.TestingT, bean any, conditions ...any) int {
112
117
113
118
// AssertNotExistsBean assert that a bean does not exist in the test database
114
119
func AssertNotExistsBean (t assert.TestingT , bean any , conditions ... any ) {
115
- exists , err := LoadBeanIfExists (bean , conditions ... )
120
+ exists , err := getBeanIfExists (bean , conditions ... )
116
121
assert .NoError (t , err )
117
122
assert .False (t , exists )
118
123
}
119
124
120
- // AssertExistsIf asserts that a bean exists or does not exist, depending on
121
- // what is expected.
122
- func AssertExistsIf (t assert.TestingT , expected bool , bean any , conditions ... any ) {
123
- exists , err := LoadBeanIfExists (bean , conditions ... )
124
- assert .NoError (t , err )
125
- assert .Equal (t , expected , exists )
126
- }
127
-
128
- // AssertSuccessfulInsert assert that beans is successfully inserted
129
- func AssertSuccessfulInsert (t assert.TestingT , beans ... any ) {
130
- err := db .Insert (db .DefaultContext , beans ... )
131
- assert .NoError (t , err )
132
- }
133
-
134
125
// AssertCount assert the count of a bean
135
126
func AssertCount (t assert.TestingT , bean , expected any ) bool {
136
127
return assert .EqualValues (t , expected , GetCount (t , bean ))
@@ -155,3 +146,39 @@ func AssertCountByCond(t assert.TestingT, tableName string, cond builder.Cond, e
155
146
return assert .EqualValues (t , expected , GetCountByCond (t , tableName , cond ),
156
147
"Failed consistency test, the counted bean (of table %s) was %+v" , tableName , cond )
157
148
}
149
+
150
+ // DumpQueryResult dumps the result of a query for debugging purpose
151
+ func DumpQueryResult (t require.TestingT , sqlOrBean any , sqlArgs ... any ) {
152
+ x := db .GetEngine (db .DefaultContext ).(* xorm.Engine )
153
+ goDB := x .DB ().DB
154
+ sql , ok := sqlOrBean .(string )
155
+ if ! ok {
156
+ sql = fmt .Sprintf ("SELECT * FROM %s" , db .TableName (sqlOrBean ))
157
+ } else if ! strings .Contains (sql , " " ) {
158
+ sql = fmt .Sprintf ("SELECT * FROM %s" , sql )
159
+ }
160
+ rows , err := goDB .Query (sql , sqlArgs ... )
161
+ require .NoError (t , err )
162
+ defer rows .Close ()
163
+ columns , err := rows .Columns ()
164
+ require .NoError (t , err )
165
+
166
+ _ , _ = fmt .Fprintf (os .Stdout , "====== DumpQueryResult: %s ======\n " , sql )
167
+ idx := 0
168
+ for rows .Next () {
169
+ row := make ([]any , len (columns ))
170
+ rowPointers := make ([]any , len (columns ))
171
+ for i := range row {
172
+ rowPointers [i ] = & row [i ]
173
+ }
174
+ require .NoError (t , rows .Scan (rowPointers ... ))
175
+ _ , _ = fmt .Fprintf (os .Stdout , "- # row[%d]\n " , idx )
176
+ for i , col := range columns {
177
+ _ , _ = fmt .Fprintf (os .Stdout , " %s: %v\n " , col , row [i ])
178
+ }
179
+ idx ++
180
+ }
181
+ if idx == 0 {
182
+ _ , _ = fmt .Fprintf (os .Stdout , "(no result, columns: %s)\n " , strings .Join (columns , ", " ))
183
+ }
184
+ }
0 commit comments