diff options
author | Ethan Koenig <etk39@cornell.edu> | 2017-06-14 23:09:03 -0400 |
---|---|---|
committer | Kim "BKC" Carlbäcker <kim.carlbacker@gmail.com> | 2017-06-15 05:09:03 +0200 |
commit | 8fcda0442e60209d7e6783c95d96f20ff04dbdd6 (patch) | |
tree | 8b47fcaab6003453ec319fc4960d959a2d06e6e9 /models/unit_tests.go | |
parent | bf48c8ebdd3975d1090115a9106069796c67a6da (diff) | |
download | gitea-8fcda0442e60209d7e6783c95d96f20ff04dbdd6.tar.gz gitea-8fcda0442e60209d7e6783c95d96f20ff04dbdd6.zip |
Fix search by issue type (#1914)
* Fix search by issue type
Diffstat (limited to 'models/unit_tests.go')
-rw-r--r-- | models/unit_tests.go | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/models/unit_tests.go b/models/unit_tests.go index a3459e49f7..16eb09968c 100644 --- a/models/unit_tests.go +++ b/models/unit_tests.go @@ -37,13 +37,31 @@ func PrepareTestDatabase() error { return LoadFixtures() } +type testCond struct { + query interface{} + args []interface{} +} + +// Cond create a condition with arguments for a test +func Cond(query interface{}, args ...interface{}) interface{} { + return &testCond{query: query, args: args} +} + +func whereConditions(sess *xorm.Session, conditions []interface{}) { + for _, condition := range conditions { + switch cond := condition.(type) { + case *testCond: + sess.Where(cond.query, cond.args...) + default: + sess.Where(cond) + } + } +} + func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) { sess := x.NewSession() defer sess.Close() - - for _, cond := range conditions { - sess = sess.Where(cond) - } + whereConditions(sess, conditions) return sess.Get(bean) } @@ -65,6 +83,16 @@ func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...inter return bean } +// GetCount get the count of a bean +func GetCount(t *testing.T, bean interface{}, conditions ...interface{}) int { + sess := x.NewSession() + defer sess.Close() + whereConditions(sess, conditions) + count, err := sess.Count(bean) + assert.NoError(t, err) + return int(count) +} + // AssertNotExistsBean assert that a bean does not exist in the test database func AssertNotExistsBean(t *testing.T, bean interface{}, conditions ...interface{}) { exists, err := loadBeanIfExists(bean, conditions...) @@ -80,7 +108,5 @@ func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) { // AssertCount assert the count of a bean func AssertCount(t *testing.T, bean interface{}, expected interface{}) { - actual, err := x.Count(bean) - assert.NoError(t, err) - assert.EqualValues(t, expected, actual) + assert.EqualValues(t, expected, GetCount(t, bean)) } |