2021-11-16 16:53:21 +08:00
// Copyright 2016 The Gitea Authors. All rights reserved.
2022-11-27 13:20:29 -05:00
// SPDX-License-Identifier: MIT
2021-11-16 16:53:21 +08:00
package unittest
import (
"math"
2024-07-30 19:41:10 +00:00
"testing"
2021-11-16 16:53:21 +08:00
"code.gitea.io/gitea/models/db"
"github.com/stretchr/testify/assert"
2024-07-26 19:26:44 +02:00
"github.com/stretchr/testify/require"
2021-11-16 16:53:21 +08:00
"xorm.io/builder"
)
// Code in this file is mainly used by unittest.CheckConsistencyFor, which is not in the unit test for various reasons.
// In the future if we can decouple CheckConsistencyFor into separate unit test code, then this file can be moved into unittest package too.
// NonexistentID an ID that will never exist
const NonexistentID = int64 ( math . MaxInt64 )
type testCond struct {
2023-05-29 11:20:57 +08:00
query any
args [ ] any
2021-11-16 16:53:21 +08:00
}
2023-05-29 11:20:57 +08:00
type testOrderBy string
2021-11-16 16:53:21 +08:00
// Cond create a condition with arguments for a test
2023-05-29 11:20:57 +08:00
func Cond ( query any , args ... any ) any {
2021-11-16 16:53:21 +08:00
return & testCond { query : query , args : args }
}
2023-05-29 11:20:57 +08:00
// OrderBy creates "ORDER BY" a test query
func OrderBy ( orderBy string ) any {
return testOrderBy ( orderBy )
}
func whereOrderConditions ( e db . Engine , conditions [ ] any ) db . Engine {
orderBy := "id" // query must have the "ORDER BY", otherwise the result is not deterministic
2021-11-16 16:53:21 +08:00
for _ , condition := range conditions {
switch cond := condition . ( type ) {
case * testCond :
e = e . Where ( cond . query , cond . args ... )
2023-05-29 11:20:57 +08:00
case testOrderBy :
orderBy = string ( cond )
2021-11-16 16:53:21 +08:00
default :
e = e . Where ( cond )
}
}
2023-05-29 11:20:57 +08:00
return e . OrderBy ( orderBy )
2021-11-16 16:53:21 +08:00
}
// LoadBeanIfExists loads beans from fixture database if exist
2023-05-29 11:20:57 +08:00
func LoadBeanIfExists ( bean any , conditions ... any ) ( bool , error ) {
2021-11-16 16:53:21 +08:00
e := db . GetEngine ( db . DefaultContext )
2023-05-29 11:20:57 +08:00
return whereOrderConditions ( e , conditions ) . Get ( bean )
2021-11-16 16:53:21 +08:00
}
// BeanExists for testing, check if a bean exists
2024-07-30 19:41:10 +00:00
func BeanExists ( t testing . TB , bean any , conditions ... any ) bool {
2021-11-16 16:53:21 +08:00
exists , err := LoadBeanIfExists ( bean , conditions ... )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2021-11-16 16:53:21 +08:00
return exists
}
// AssertExistsAndLoadBean assert that a bean exists and load it from the test database
2024-07-30 19:41:10 +00:00
func AssertExistsAndLoadBean [ T any ] ( t testing . TB , bean T , conditions ... any ) T {
2021-11-16 16:53:21 +08:00
exists , err := LoadBeanIfExists ( bean , conditions ... )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2021-11-16 16:53:21 +08:00
assert . True ( t , exists ,
"Expected to find %+v (of type %T, with conditions %+v), but did not" ,
bean , bean , conditions )
return bean
}
// AssertExistsAndLoadMap assert that a row exists and load it from the test database
2024-07-30 19:41:10 +00:00
func AssertExistsAndLoadMap ( t testing . TB , table string , conditions ... any ) map [ string ] string {
2021-11-16 16:53:21 +08:00
e := db . GetEngine ( db . DefaultContext ) . Table ( table )
2023-05-29 11:20:57 +08:00
res , err := whereOrderConditions ( e , conditions ) . Query ( )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
assert . Len ( t , res , 1 ,
2021-11-16 16:53:21 +08:00
"Expected to find one row in %s (with conditions %+v), but found %d" ,
table , conditions , len ( res ) ,
)
if len ( res ) == 1 {
rec := map [ string ] string { }
for k , v := range res [ 0 ] {
rec [ k ] = string ( v )
}
return rec
}
return nil
}
// GetCount get the count of a bean
2024-07-30 19:41:10 +00:00
func GetCount ( t testing . TB , bean any , conditions ... any ) int {
2021-11-16 16:53:21 +08:00
e := db . GetEngine ( db . DefaultContext )
2023-10-19 18:25:57 +08:00
for _ , condition := range conditions {
switch cond := condition . ( type ) {
case * testCond :
e = e . Where ( cond . query , cond . args ... )
default :
e = e . Where ( cond )
}
}
count , err := e . Count ( bean )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2021-11-16 16:53:21 +08:00
return int ( count )
}
// AssertNotExistsBean assert that a bean does not exist in the test database
2024-07-30 19:41:10 +00:00
func AssertNotExistsBean ( t testing . TB , bean any , conditions ... any ) {
2021-11-16 16:53:21 +08:00
exists , err := LoadBeanIfExists ( bean , conditions ... )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2021-11-16 16:53:21 +08:00
assert . False ( t , exists )
}
// AssertExistsIf asserts that a bean exists or does not exist, depending on
// what is expected.
2024-07-30 19:41:10 +00:00
func AssertExistsIf ( t testing . TB , expected bool , bean any , conditions ... any ) {
2021-11-16 16:53:21 +08:00
exists , err := LoadBeanIfExists ( bean , conditions ... )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2021-11-16 16:53:21 +08:00
assert . Equal ( t , expected , exists )
}
// AssertSuccessfulInsert assert that beans is successfully inserted
2024-07-30 19:41:10 +00:00
func AssertSuccessfulInsert ( t testing . TB , beans ... any ) {
2021-11-16 16:53:21 +08:00
err := db . Insert ( db . DefaultContext , beans ... )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2021-11-16 16:53:21 +08:00
}
2024-07-26 19:26:44 +02:00
// AssertSuccessfulDelete assert that beans is successfully deleted
func AssertSuccessfulDelete ( t require . TestingT , beans ... any ) {
err := db . DeleteBeans ( db . DefaultContext , beans ... )
require . NoError ( t , err )
}
2021-11-16 16:53:21 +08:00
// AssertCount assert the count of a bean
2024-07-30 19:41:10 +00:00
func AssertCount ( t testing . TB , bean , expected any ) bool {
2024-02-19 14:42:18 +01:00
return assert . EqualValues ( t , expected , GetCount ( t , bean ) )
2021-11-16 16:53:21 +08:00
}
// AssertInt64InRange assert value is in range [low, high]
2024-07-30 19:41:10 +00:00
func AssertInt64InRange ( t testing . TB , low , high , value int64 ) {
2021-11-16 16:53:21 +08:00
assert . True ( t , value >= low && value <= high ,
"Expected value in range [%d, %d], found %d" , low , high , value )
}
// GetCountByCond get the count of database entries matching bean
2024-07-30 19:41:10 +00:00
func GetCountByCond ( t testing . TB , tableName string , cond builder . Cond ) int64 {
2021-11-16 16:53:21 +08:00
e := db . GetEngine ( db . DefaultContext )
count , err := e . Table ( tableName ) . Where ( cond ) . Count ( )
2024-07-30 19:41:10 +00:00
require . NoError ( t , err )
2021-11-16 16:53:21 +08:00
return count
}
// AssertCountByCond test the count of database entries matching bean
2024-07-30 19:41:10 +00:00
func AssertCountByCond ( t testing . TB , tableName string , cond builder . Cond , expected int ) bool {
2024-02-19 14:42:18 +01:00
return assert . EqualValues ( t , expected , GetCountByCond ( t , tableName , cond ) ,
2021-11-16 16:53:21 +08:00
"Failed consistency test, the counted bean (of table %s) was %+v" , tableName , cond )
}