Unit tests for models/action (#619)

This commit is contained in:
Ethan Koenig 2017-01-08 22:08:36 -05:00 committed by Lunny Xiao
parent f4feeecc3a
commit 4b23e6a694
5 changed files with 398 additions and 20 deletions

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)
assert.NoError(t, err)
assert.True(t, has)
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, 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)
}