gorm/orm_test.go

41 lines
699 B
Go
Raw Normal View History

2013-10-25 18:31:56 +04:00
package gorm
import (
"fmt"
2013-10-26 05:49:40 +04:00
"time"
2013-10-25 18:31:56 +04:00
"testing"
)
2013-10-26 03:14:57 +04:00
type User struct {
Name string
}
2013-10-26 05:49:40 +04:00
func getDB() DB {
2013-10-26 06:06:57 +04:00
db, _ := Open("postgres", "user=gorm dbname=gorm sslmode=disable")
2013-10-26 05:49:40 +04:00
return db
}
func TestSaveAndFirst(t *testing.T) {
db := getDB()
u := &User{Name: "jinzhu"}
2013-10-26 06:06:57 +04:00
fmt.Println("*******")
2013-10-26 05:49:40 +04:00
fmt.Println(db.Save(u).Sql)
2013-10-26 06:06:57 +04:00
fmt.Println(db.Save(u).Error)
2013-10-26 05:49:40 +04:00
fmt.Println(time.Now().String())
user := &User{}
db.First(&user)
if user.Name != "jinzhu" {
t.Errorf("User should be saved and fetched correctly")
2013-10-25 18:31:56 +04:00
}
2013-10-26 05:49:40 +04:00
}
2013-10-26 03:14:57 +04:00
2013-10-26 05:49:40 +04:00
func TestWhere(t *testing.T) {
db := getDB()
orm := db.Where("id = $1", 1, 3, 4, []int64{1, 2, 3}).Where("name = $1", "jinzhu")
2013-10-26 03:14:57 +04:00
user := &User{}
orm.First(user)
fmt.Println(user)
2013-10-25 18:31:56 +04:00
}