Remove raw sqls and use orm instead
This commit is contained in:
parent
09318cf444
commit
3d8e6f2835
42
main_test.go
42
main_test.go
@ -1061,32 +1061,32 @@ func TestBlockGlobalUpdate(t *testing.T) {
|
|||||||
|
|
||||||
func TestDB_DataSource(t *testing.T) {
|
func TestDB_DataSource(t *testing.T) {
|
||||||
source := "user=gorm password=gorm DB.name=gorm port=9920 sslmode=disable"
|
source := "user=gorm password=gorm DB.name=gorm port=9920 sslmode=disable"
|
||||||
db, er := gorm.Open("postgres", source)
|
if DB.DataSource() != source {
|
||||||
if er != nil {
|
t.Fatal(fmt.Sprintf("want '%s', but got '%s'", source, DB.DataSource()))
|
||||||
panic(fmt.Sprintf("No error should happen when connecting to test database, but got err=%+v", er))
|
|
||||||
}
|
|
||||||
if db.DataSource() != source {
|
|
||||||
t.Fatal(fmt.Sprintf("want '%s', but got '%s'", source, db.DataSource()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Example struct{
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
}
|
||||||
|
func (e Example) TableName()string{
|
||||||
|
return "example"
|
||||||
|
}
|
||||||
func TestDB_CopyIn(t *testing.T) {
|
func TestDB_CopyIn(t *testing.T) {
|
||||||
source := "user=gorm password=gorm DB.name=gorm port=9920 sslmode=disable"
|
if !DB.HasTable(&Example{}) {
|
||||||
db, er := gorm.Open("postgres", source)
|
if e:=DB.Create(&Example{}).Error;e!=nil {
|
||||||
if er != nil {
|
|
||||||
panic(fmt.Sprintf("No error should happen when connecting to test database, but got err=%+v", er))
|
|
||||||
}
|
|
||||||
e := db.Exec("create table if not exists example(name varchar, age integer)").Error
|
|
||||||
defer func() {
|
|
||||||
er := db.Exec("drop table if exists example").Error
|
|
||||||
if er != nil {
|
|
||||||
t.Fatal(e.Error())
|
t.Fatal(e.Error())
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
if e != nil {
|
|
||||||
t.Fatal(e.Error())
|
|
||||||
}
|
}
|
||||||
defer db.Exec("drop table")
|
defer func() {
|
||||||
|
if DB.HasTable(&Example{}) {
|
||||||
|
if e:=DB.DropTable(&Example{}).Error;e!=nil{
|
||||||
|
t.Fatal(e.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
var args = make([][]interface{}, 0)
|
var args = make([][]interface{}, 0)
|
||||||
args = append(args, []interface{}{
|
args = append(args, []interface{}{
|
||||||
"tom", 9,
|
"tom", 9,
|
||||||
@ -1095,7 +1095,7 @@ func TestDB_CopyIn(t *testing.T) {
|
|||||||
}, []interface{}{
|
}, []interface{}{
|
||||||
"jim", 11,
|
"jim", 11,
|
||||||
})
|
})
|
||||||
e = db.CopyIn(true, "example", args, "name", "age")
|
e := DB.CopyIn(true, "example", args, "name", "age")
|
||||||
if e != nil {
|
if e != nil {
|
||||||
t.Fatal(e.Error())
|
t.Fatal(e.Error())
|
||||||
}
|
}
|
||||||
@ -1104,7 +1104,7 @@ func TestDB_CopyIn(t *testing.T) {
|
|||||||
Age int
|
Age int
|
||||||
}
|
}
|
||||||
var examples = make([]Example, 0)
|
var examples = make([]Example, 0)
|
||||||
e = db.Raw("select * from example").Find(&examples).Error
|
e = DB.Model(&Example{}).Find(&examples).Error
|
||||||
if e != nil {
|
if e != nil {
|
||||||
t.Fatal(e.Error())
|
t.Fatal(e.Error())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user