2013-10-25 18:31:56 +04:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"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) {
|
2013-10-26 07:48:07 +04:00
|
|
|
// create table "users" ("name" varchar(255));
|
2013-10-26 05:49:40 +04:00
|
|
|
db := getDB()
|
|
|
|
u := &User{Name: "jinzhu"}
|
2013-10-26 06:15:09 +04:00
|
|
|
db.Save(u)
|
2013-10-26 05:49:40 +04:00
|
|
|
|
|
|
|
user := &User{}
|
2013-10-26 07:48:07 +04:00
|
|
|
db.First(user)
|
2013-10-26 05:49:40 +04:00
|
|
|
if user.Name != "jinzhu" {
|
|
|
|
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)
|
|
|
|
for _, user := range users {
|
|
|
|
if user.Name != "jinzhu" {
|
|
|
|
t.Errorf("User should be saved and fetched correctly")
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|