forked from mirror/gorm
Refact tests
This commit is contained in:
parent
aab4d773e4
commit
9d2f7650be
|
@ -3,6 +3,7 @@
|
|||
Yet Another ORM library for Go, aims for developer friendly
|
||||
|
||||
## TODO
|
||||
* Pluck
|
||||
* Order
|
||||
* Limit
|
||||
* Select
|
||||
|
|
4
main.go
4
main.go
|
@ -52,9 +52,9 @@ func (s *DB) Offset(value interface{}) (orm *Orm) {
|
|||
return
|
||||
}
|
||||
|
||||
func (s *DB) Order(value interface{}) (orm *Orm) {
|
||||
func (s *DB) Order(value string, reorder ...bool) (orm *Orm) {
|
||||
orm = s.buildORM()
|
||||
orm.Order(value)
|
||||
orm.Order(value, reorder...)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
9
orm.go
9
orm.go
|
@ -62,12 +62,11 @@ func (s *Orm) Offset(value interface{}) *Orm {
|
|||
return s
|
||||
}
|
||||
|
||||
func (s *Orm) Order(value interface{}) *Orm {
|
||||
switch value := value.(type) {
|
||||
case string:
|
||||
func (s *Orm) Order(value string, reorder ...bool) *Orm {
|
||||
if len(reorder) > 0 && reorder[0] {
|
||||
s.orderStr = value
|
||||
default:
|
||||
s.Error = errors.New("Can' understand the value of Order, Should be string")
|
||||
} else {
|
||||
s.orderStr = s.orderStr + value
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
|
48
orm_test.go
48
orm_test.go
|
@ -12,18 +12,31 @@ type User struct {
|
|||
Name string
|
||||
}
|
||||
|
||||
var db DB
|
||||
var (
|
||||
db DB
|
||||
t1, t2, t3, t4, t5 time.Time
|
||||
)
|
||||
|
||||
func init() {
|
||||
db, _ = Open("postgres", "user=gorm dbname=gorm sslmode=disable")
|
||||
db.Exec("drop table users;")
|
||||
}
|
||||
|
||||
func TestCreateTable(t *testing.T) {
|
||||
orm := db.CreateTable(&User{})
|
||||
if orm.Error != nil {
|
||||
t.Errorf("No error should raise when create table, but got %+v", orm.Error)
|
||||
panic("No error should raise when create table")
|
||||
}
|
||||
|
||||
var shortForm = "2006-01-02 15:04:05"
|
||||
t1, _ = time.Parse(shortForm, "2000-10-27 12:02:40")
|
||||
t2, _ = time.Parse(shortForm, "2002-01-01 00:00:00")
|
||||
t3, _ = time.Parse(shortForm, "2005-01-01 00:00:00")
|
||||
t4, _ = time.Parse(shortForm, "2010-01-01 00:00:00")
|
||||
t5, _ = time.Parse(shortForm, "2020-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})
|
||||
db.Save(&User{Name: "5", Age: 26, Birthday: t4})
|
||||
}
|
||||
|
||||
func TestSaveAndFind(t *testing.T) {
|
||||
|
@ -117,26 +130,16 @@ func TestWhere(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestComplexWhere(t *testing.T) {
|
||||
var shortForm = "2006-01-02 15:04:05"
|
||||
t1, _ := time.Parse(shortForm, "2000-10-27 12:02:40")
|
||||
t2, _ := time.Parse(shortForm, "2002-01-01 00:00:00")
|
||||
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
|
||||
db.Where("age > ?", 20).Find(&users)
|
||||
if len(users) != 2 {
|
||||
t.Errorf("Should only found 2 users that age > 20, but have %v", len(users))
|
||||
if len(users) != 3 {
|
||||
t.Errorf("Should only found 3 users that age > 20, but have %v", len(users))
|
||||
}
|
||||
|
||||
users = []User{}
|
||||
db.Where("age >= ?", 20).Find(&users)
|
||||
if len(users) != 3 {
|
||||
t.Errorf("Should only found 2 users that age >= 20, but have %v", len(users))
|
||||
if len(users) != 4 {
|
||||
t.Errorf("Should only found 4 users that age >= 20, but have %v", len(users))
|
||||
}
|
||||
|
||||
users = []User{}
|
||||
|
@ -165,8 +168,8 @@ func TestComplexWhere(t *testing.T) {
|
|||
|
||||
users = []User{}
|
||||
db.Where("birthday > ?", t2).Find(&users)
|
||||
if len(users) != 2 {
|
||||
t.Errorf("Should only found 2 users's birthday >= t2", len(users))
|
||||
if len(users) != 3 {
|
||||
t.Errorf("Should only found 3 users's birthday >= t2", len(users))
|
||||
}
|
||||
|
||||
users = []User{}
|
||||
|
@ -221,3 +224,8 @@ func TestComplexWhere(t *testing.T) {
|
|||
t.Errorf("Should only found 1 users's name in (1, 2) - search by the first id, but have %v", len(users))
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrder(t *testing.T) {
|
||||
var users []User
|
||||
db.Order("age desc").Find(&users)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue