44package  unittest
55
66import  (
7+ 	"fmt" 
78	"math" 
9+ 	"os" 
10+ 	"strings" 
811
912	"code.gitea.io/gitea/models/db" 
1013
1114	"github.com/stretchr/testify/assert" 
1215	"github.com/stretchr/testify/require" 
1316	"xorm.io/builder" 
17+ 	"xorm.io/xorm" 
1418)
1519
1620// 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 {
5155	return  e .OrderBy (orderBy )
5256}
5357
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 ) {
5659	e  :=  db .GetEngine (db .DefaultContext )
5760	return  whereOrderConditions (e , conditions ).Get (bean )
5861}
5962
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 
6570}
6671
6772// AssertExistsAndLoadBean assert that a bean exists and load it from the test database 
6873func  AssertExistsAndLoadBean [T  any ](t  require.TestingT , bean  T , conditions  ... any ) T  {
69- 	exists , err  :=  LoadBeanIfExists (bean , conditions ... )
74+ 	exists , err  :=  getBeanIfExists (bean , conditions ... )
7075	require .NoError (t , err )
7176	require .True (t , exists ,
7277		"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 {
112117
113118// AssertNotExistsBean assert that a bean does not exist in the test database 
114119func  AssertNotExistsBean (t  assert.TestingT , bean  any , conditions  ... any ) {
115- 	exists , err  :=  LoadBeanIfExists (bean , conditions ... )
120+ 	exists , err  :=  getBeanIfExists (bean , conditions ... )
116121	assert .NoError (t , err )
117122	assert .False (t , exists )
118123}
119124
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- 
134125// AssertCount assert the count of a bean 
135126func  AssertCount (t  assert.TestingT , bean , expected  any ) bool  {
136127	return  assert .EqualValues (t , expected , GetCount (t , bean ))
@@ -155,3 +146,38 @@ func AssertCountByCond(t assert.TestingT, tableName string, cond builder.Cond, e
155146	return  assert .EqualValues (t , expected , GetCountByCond (t , tableName , cond ),
156147		"Failed consistency test, the counted bean (of table %s) was %+v" , tableName , cond )
157148}
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+ 	columns , err  :=  rows .Columns ()
163+ 	require .NoError (t , err )
164+ 
165+ 	_ , _  =  fmt .Fprintf (os .Stdout , "====== DumpQueryResult: %s ======\n " , sql )
166+ 	idx  :=  0 
167+ 	for  rows .Next () {
168+ 		row  :=  make ([]any , len (columns ))
169+ 		rowPointers  :=  make ([]any , len (columns ))
170+ 		for  i  :=  range  row  {
171+ 			rowPointers [i ] =  & row [i ]
172+ 		}
173+ 		require .NoError (t , rows .Scan (rowPointers ... ))
174+ 		_ , _  =  fmt .Fprintf (os .Stdout , "- # row[%d]\n " , idx )
175+ 		for  i , col  :=  range  columns  {
176+ 			_ , _  =  fmt .Fprintf (os .Stdout , "  %s: %v\n " , col , row [i ])
177+ 		}
178+ 		idx ++ 
179+ 	}
180+ 	if  idx  ==  0  {
181+ 		_ , _  =  fmt .Fprintf (os .Stdout , "(no result)\n " )
182+ 	}
183+ }
0 commit comments