Add some tests for time

This commit is contained in:
Jinzhu 2013-10-27 01:41:29 +08:00
parent 96128cdec3
commit d18203ee62
2 changed files with 27 additions and 5 deletions

View File

@ -117,10 +117,15 @@ func TestWhere(t *testing.T) {
} }
func TestComplexWhere(t *testing.T) { func TestComplexWhere(t *testing.T) {
db.Save(&User{Name: "1", Age: 18}) var shortForm = "2006-01-02 15:04:05"
db.Save(&User{Name: "2", Age: 20}) t1, _ := time.Parse(shortForm, "2000-10-27 12:02:40")
db.Save(&User{Name: "3", Age: 22}) t2, _ := time.Parse(shortForm, "2002-01-01 00:00:00")
db.Save(&User{Name: "3", Age: 24}) t3, _ := time.Parse(shortForm, "2005-01-01 00:00:00")
t4, _ := time.Parse(shortForm, "2010-01-01 00:00:00")
db.Save(&User{Name: "1", Age: 18, Birthday: t1})
db.Save(&User{Name: "2", Age: 20, Birthday: t2})
db.Save(&User{Name: "3", Age: 22, Birthday: t3})
db.Save(&User{Name: "3", Age: 24, Birthday: t4})
var users []User var users []User
db.Where("age > ?", 20).Find(&users) db.Where("age > ?", 20).Find(&users)
@ -157,4 +162,22 @@ func TestComplexWhere(t *testing.T) {
if len(users) != 2 { if len(users) != 2 {
t.Errorf("Should only found 2 users that age >= 20 with name 3, but have %v", len(users)) t.Errorf("Should only found 2 users that age >= 20 with name 3, but have %v", len(users))
} }
users = []User{}
db.Where("birthday > ?", t2).Find(&users)
if len(users) != 2 {
t.Errorf("Should only found 2 users's birthday >= t2", len(users))
}
users = []User{}
db.Where("birthday >= ?", t1).Where("birthday < ?", t2).Find(&users)
if len(users) != 1 {
t.Errorf("Should only found 1 users's birthday <= t2, but have %v", len(users))
}
users = []User{}
db.Where("birthday >= ? and birthday <= ?", t1, t2).Find(&users)
if len(users) != 2 {
t.Errorf("Should only found 2 users's birthday <= t2, but have %v", len(users))
}
} }

1
sql.go
View File

@ -43,7 +43,6 @@ func (s *Orm) query(out interface{}) {
rows, err := s.db.Query(s.Sql, s.SqlVars...) rows, err := s.db.Query(s.Sql, s.SqlVars...)
defer rows.Close() defer rows.Close()
s.Error = err s.Error = err
if rows.Err() != nil { if rows.Err() != nil {
s.Error = rows.Err() s.Error = rows.Err()