mirror of https://github.com/go-gorm/gorm.git
Refact tests
This commit is contained in:
parent
2f4945dc35
commit
91ad8a0459
65
orm_test.go
65
orm_test.go
|
@ -3,19 +3,27 @@ package gorm
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
Name string
|
|
||||||
Id int64
|
Id int64
|
||||||
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDB() DB {
|
var db DB
|
||||||
db, _ := Open("postgres", "user=gorm dbname=gorm sslmode=disable")
|
|
||||||
return db
|
func init() {
|
||||||
|
db, _ = Open("postgres", "user=gorm dbname=gorm sslmode=disable")
|
||||||
|
db.Exec("drop table users;")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSaveAndFirst(t *testing.T) {
|
func TestCreateTable(t *testing.T) {
|
||||||
// create table "users" ("name" varchar(255));
|
orm := db.CreateTable(&User{})
|
||||||
db := getDB()
|
if orm.Error != nil {
|
||||||
u := &User{Name: "jinzhu"}
|
t.Errorf("No error should raise when create table, but got %+v", orm.Error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSaveAndFind(t *testing.T) {
|
||||||
|
name := "save_and_find"
|
||||||
|
u := &User{Name: name}
|
||||||
db.Save(u)
|
db.Save(u)
|
||||||
if u.Id == 0 {
|
if u.Id == 0 {
|
||||||
t.Errorf("Should have ID after create record")
|
t.Errorf("Should have ID after create record")
|
||||||
|
@ -23,49 +31,36 @@ func TestSaveAndFirst(t *testing.T) {
|
||||||
|
|
||||||
user := &User{}
|
user := &User{}
|
||||||
db.First(user)
|
db.First(user)
|
||||||
if user.Name != "jinzhu" {
|
if user.Name != name {
|
||||||
t.Errorf("User should be saved and fetched correctly")
|
t.Errorf("User should be saved and fetched correctly")
|
||||||
}
|
}
|
||||||
|
|
||||||
users := []User{}
|
users := []User{}
|
||||||
db.Find(&users)
|
db.Find(&users)
|
||||||
for _, user := range users {
|
|
||||||
if user.Name != "jinzhu" {
|
|
||||||
t.Errorf("User should be saved and fetched correctly")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWhere(t *testing.T) {
|
func TestWhere(t *testing.T) {
|
||||||
db := getDB()
|
name := "where"
|
||||||
u := &User{Name: "jinzhu"}
|
db.Save(&User{Name: name})
|
||||||
db.Save(u)
|
|
||||||
|
|
||||||
user := &User{}
|
user := &User{}
|
||||||
db.Where("Name = ?", "jinzhu").First(user)
|
db.Where("Name = ?", name).First(user)
|
||||||
if user.Name != "jinzhu" {
|
if user.Name != name {
|
||||||
t.Errorf("Should found out user with name 'jinzhu'")
|
t.Errorf("Should found out user with name '%v'", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
user = &User{}
|
user = &User{}
|
||||||
orm := db.Where("Name = ?", "jinzhu-noexisting").First(user)
|
orm := db.Where("Name = ?", "noexisting-user").First(user)
|
||||||
if orm.Error == nil {
|
if orm.Error == nil {
|
||||||
t.Errorf("Should return error when looking for unexist record, %+v", user)
|
t.Errorf("Should return error when looking for none existing record, %+v", user)
|
||||||
}
|
}
|
||||||
|
|
||||||
users := &[]User{}
|
users := []User{}
|
||||||
orm = db.Where("Name = ?", "jinzhu-noexisting").First(users)
|
orm = db.Where("Name = ?", "none-noexisting").Find(&users)
|
||||||
if orm.Error != nil {
|
if orm.Error != nil {
|
||||||
t.Errorf("Shouldn't return error when looking for unexist records, %+v", users)
|
t.Errorf("Shouldn't return error when looking for none existing records, %+v", users)
|
||||||
}
|
}
|
||||||
}
|
if len(users) != 0 {
|
||||||
|
t.Errorf("Shouldn't find anything when looking for none existing records, %+v", users)
|
||||||
func TestCreateTable(t *testing.T) {
|
|
||||||
db := getDB()
|
|
||||||
db.Exec("drop table users;")
|
|
||||||
|
|
||||||
orm := db.CreateTable(&User{})
|
|
||||||
if orm.Error != nil {
|
|
||||||
t.Errorf("No error should raise when create table, but got %+v", orm.Error)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue