gorm/orm_test.go

161 lines
3.7 KiB
Go
Raw Normal View History

2013-10-25 18:31:56 +04:00
package gorm
2013-10-26 20:36:56 +04:00
import (
"testing"
"time"
)
2013-10-25 18:31:56 +04:00
2013-10-26 03:14:57 +04:00
type User struct {
2013-10-26 20:36:56 +04:00
Id int64
Age int64
Birthday time.Time
Name string
2013-10-26 17:05:54 +04:00
}
var db DB
func init() {
db, _ = Open("postgres", "user=gorm dbname=gorm sslmode=disable")
db.Exec("drop table users;")
2013-10-26 03:14:57 +04:00
}
2013-10-26 17:05:54 +04:00
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)
}
2013-10-26 05:49:40 +04:00
}
2013-10-26 17:05:54 +04:00
func TestSaveAndFind(t *testing.T) {
name := "save_and_find"
u := &User{Name: name}
2013-10-26 06:15:09 +04:00
db.Save(u)
2013-10-26 16:20:49 +04:00
if u.Id == 0 {
t.Errorf("Should have ID after create record")
}
2013-10-26 05:49:40 +04:00
user := &User{}
2013-10-26 07:48:07 +04:00
db.First(user)
2013-10-26 17:05:54 +04:00
if user.Name != name {
2013-10-26 05:49:40 +04:00
t.Errorf("User should be saved and fetched correctly")
2013-10-25 18:31:56 +04:00
}
2013-10-26 08:33:05 +04:00
users := []User{}
db.Find(&users)
2013-10-26 05:49:40 +04:00
}
2013-10-26 03:14:57 +04:00
2013-10-26 17:37:42 +04:00
func TestUpdate(t *testing.T) {
2013-10-26 18:00:38 +04:00
name, name2, new_name := "update", "update2", "new_update"
2013-10-26 17:37:42 +04:00
user := User{Name: name}
db.Save(&user)
2013-10-26 18:00:38 +04:00
db.Save(&User{Name: name2})
2013-10-26 17:37:42 +04:00
2013-10-26 18:00:38 +04:00
if user.Id == 0 {
2013-10-26 17:37:42 +04:00
t.Errorf("User Id should exist after create")
}
2013-10-26 18:00:38 +04:00
user.Name = new_name
2013-10-26 17:37:42 +04:00
db.Save(&user)
2013-10-26 18:00:38 +04:00
orm := db.Where("name = ?", name).First(&User{})
2013-10-26 17:37:42 +04:00
if orm.Error == nil {
t.Errorf("Should raise error when looking for a existing user with an outdated name")
}
2013-10-26 18:00:38 +04:00
orm = db.Where("name = ?", new_name).First(&User{})
2013-10-26 17:37:42 +04:00
if orm.Error != nil {
t.Errorf("Shouldn't raise error when looking for a existing user with the new name")
}
2013-10-26 18:00:38 +04:00
orm = db.Where("name = ?", name2).First(&User{})
if orm.Error != nil {
t.Errorf("Shouldn't update other users")
}
2013-10-26 17:37:42 +04:00
}
2013-10-26 17:51:44 +04:00
func TestDelete(t *testing.T) {
name, name2 := "delete", "delete2"
user := User{Name: name}
db.Save(&user)
db.Save(&User{Name: name2})
orm := db.Delete(&user)
orm = db.Where("name = ?", name).First(&User{})
if orm.Error == nil {
t.Errorf("User should be deleted successfully")
}
orm = db.Where("name = ?", name2).First(&User{})
if orm.Error != nil {
t.Errorf("User2 should not be deleted")
}
}
2013-10-26 05:49:40 +04:00
func TestWhere(t *testing.T) {
2013-10-26 17:05:54 +04:00
name := "where"
db.Save(&User{Name: name})
2013-10-26 10:10:47 +04:00
2013-10-26 03:14:57 +04:00
user := &User{}
2013-10-26 17:05:54 +04:00
db.Where("Name = ?", name).First(user)
if user.Name != name {
t.Errorf("Should found out user with name '%v'", name)
}
2013-10-26 10:10:47 +04:00
user = &User{}
2013-10-26 17:05:54 +04:00
orm := db.Where("Name = ?", "noexisting-user").First(user)
if orm.Error == nil {
2013-10-26 17:05:54 +04:00
t.Errorf("Should return error when looking for none existing record, %+v", user)
}
2013-10-26 17:05:54 +04:00
users := []User{}
orm = db.Where("Name = ?", "none-noexisting").Find(&users)
if orm.Error != nil {
2013-10-26 17:05:54 +04:00
t.Errorf("Shouldn't return error when looking for none existing records, %+v", users)
}
2013-10-26 17:05:54 +04:00
if len(users) != 0 {
t.Errorf("Shouldn't find anything when looking for none existing records, %+v", users)
2013-10-26 13:28:52 +04:00
}
}
2013-10-26 20:36:56 +04:00
func TestComplexWhere(t *testing.T) {
db.Save(&User{Name: "1", Age: 18})
db.Save(&User{Name: "2", Age: 20})
db.Save(&User{Name: "3", Age: 22})
db.Save(&User{Name: "3", Age: 24})
var users []User
db.Where("age > ?", 20).Find(&users)
if len(users) != 2 {
2013-10-26 21:02:57 +04:00
t.Errorf("Should only found 2 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))
}
users = []User{}
db.Where("age = ?", 20).Find(&users)
if len(users) != 1 {
t.Errorf("Should only found 1 users age == 20, but have %v", len(users))
}
users = []User{}
db.Where("age <> ?", 20).Find(&users)
if len(users) < 3 {
t.Errorf("Should have more than 3 users age != 20, but have %v", len(users))
}
users = []User{}
db.Where("name = ? and age >= ?", "3", 20).Find(&users)
if len(users) != 2 {
t.Errorf("Should only found 2 users that age >= 20 with name 3, but have %v", len(users))
}
users = []User{}
db.Where("name = ?", "3").Where("age >= ?", 20).Find(&users)
if len(users) != 2 {
t.Errorf("Should only found 2 users that age >= 20 with name 3, but have %v", len(users))
2013-10-26 20:36:56 +04:00
}
}