Unit tests for models/action (#619)

This commit is contained in:
Ethan Koenig
2017-01-09 11:08:36 +08:00
committed by Lunny Xiao
parent f4feeecc3a
commit 4b23e6a694
5 changed files with 397 additions and 19 deletions
+16 -5
View File
@@ -47,16 +47,27 @@ func PrepareTestDatabase() error {
return fixtures.Load()
}
// LoadFixture load a test fixture from the test database, failing if fixture
// does not exist
func LoadTestFixture(t *testing.T, fixture interface{}, conditions... interface{}) {
func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {
sess := x.NewSession()
defer sess.Close()
for _, cond := range conditions {
sess = sess.Where(cond)
}
has, err := sess.Get(fixture)
return sess.Get(bean)
}
// AssertExistsAndLoadBean assert that a bean exists and load it from the test
// database
func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...interface{}) {
exists, err := loadBeanIfExists(bean, conditions...)
assert.NoError(t, err)
assert.True(t, has)
assert.True(t, exists)
}
// 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...)
assert.NoError(t, err)
assert.False(t, exists)
}