mirror of
https://github.com/astaxie/beego.git
synced 2025-07-03 17:10:18 +00:00
This commit is contained in:
177
orm/orm_test.go
177
orm/orm_test.go
@ -1322,58 +1322,6 @@ func TestRawQueryRow(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
type Tmp struct {
|
||||
Skip0 string
|
||||
Id int
|
||||
Char *string
|
||||
Skip1 int `orm:"-"`
|
||||
Date time.Time
|
||||
DateTime time.Time
|
||||
}
|
||||
|
||||
Boolean = false
|
||||
Text = ""
|
||||
Int64 = 0
|
||||
Uint = 0
|
||||
|
||||
tmp := new(Tmp)
|
||||
|
||||
cols = []string{
|
||||
"int", "char", "date", "datetime", "boolean", "text", "int64", "uint",
|
||||
}
|
||||
query = fmt.Sprintf("SELECT NULL, %s%s%s FROM data WHERE id = ?", Q, strings.Join(cols, sep), Q)
|
||||
values = []interface{}{
|
||||
tmp, &Boolean, &Text, &Int64, &Uint,
|
||||
}
|
||||
err = dORM.Raw(query, 1).QueryRow(values...)
|
||||
throwFailNow(t, err)
|
||||
|
||||
for _, col := range cols {
|
||||
switch col {
|
||||
case "id":
|
||||
throwFail(t, AssertIs(tmp.Id, data_values[col]))
|
||||
case "char":
|
||||
c := tmp.Char
|
||||
throwFail(t, AssertIs(*c, data_values[col]))
|
||||
case "date":
|
||||
v := tmp.Date.In(DefaultTimeLoc)
|
||||
value := data_values[col].(time.Time).In(DefaultTimeLoc)
|
||||
throwFail(t, AssertIs(v, value, test_Date))
|
||||
case "datetime":
|
||||
v := tmp.DateTime.In(DefaultTimeLoc)
|
||||
value := data_values[col].(time.Time).In(DefaultTimeLoc)
|
||||
throwFail(t, AssertIs(v, value, test_DateTime))
|
||||
case "boolean":
|
||||
throwFail(t, AssertIs(Boolean, data_values[col]))
|
||||
case "text":
|
||||
throwFail(t, AssertIs(Text, data_values[col]))
|
||||
case "int64":
|
||||
throwFail(t, AssertIs(Int64, data_values[col]))
|
||||
case "uint":
|
||||
throwFail(t, AssertIs(Uint, data_values[col]))
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
uid int
|
||||
status *int
|
||||
@ -1394,22 +1342,13 @@ func TestRawQueryRow(t *testing.T) {
|
||||
func TestQueryRows(t *testing.T) {
|
||||
Q := dDbBaser.TableQuote()
|
||||
|
||||
cols := []string{
|
||||
"id", "boolean", "char", "text", "date", "datetime", "byte", "rune", "int", "int8", "int16", "int32",
|
||||
"int64", "uint", "uint8", "uint16", "uint32", "uint64", "float32", "float64", "decimal",
|
||||
}
|
||||
|
||||
var datas []*Data
|
||||
var dids []int
|
||||
|
||||
sep := fmt.Sprintf("%s, %s", Q, Q)
|
||||
query := fmt.Sprintf("SELECT %s%s%s, id FROM %sdata%s", Q, strings.Join(cols, sep), Q, Q, Q)
|
||||
num, err := dORM.Raw(query).QueryRows(&datas, &dids)
|
||||
query := fmt.Sprintf("SELECT * FROM %sdata%s", Q, Q)
|
||||
num, err := dORM.Raw(query).QueryRows(&datas)
|
||||
throwFailNow(t, err)
|
||||
throwFailNow(t, AssertIs(num, 1))
|
||||
throwFailNow(t, AssertIs(len(datas), 1))
|
||||
throwFailNow(t, AssertIs(len(dids), 1))
|
||||
throwFailNow(t, AssertIs(dids[0], 1))
|
||||
|
||||
ind := reflect.Indirect(reflect.ValueOf(datas[0]))
|
||||
|
||||
@ -1427,90 +1366,42 @@ func TestQueryRows(t *testing.T) {
|
||||
throwFail(t, AssertIs(vu == value, true), value, vu)
|
||||
}
|
||||
|
||||
type Tmp struct {
|
||||
Id int
|
||||
Name string
|
||||
Skiped0 string `orm:"-"`
|
||||
Pid *int
|
||||
Skiped1 Data
|
||||
Skiped2 *Data
|
||||
var datas2 []Data
|
||||
|
||||
query = fmt.Sprintf("SELECT * FROM %sdata%s", Q, Q)
|
||||
num, err = dORM.Raw(query).QueryRows(&datas2)
|
||||
throwFailNow(t, err)
|
||||
throwFailNow(t, AssertIs(num, 1))
|
||||
throwFailNow(t, AssertIs(len(datas2), 1))
|
||||
|
||||
ind = reflect.Indirect(reflect.ValueOf(datas2[0]))
|
||||
|
||||
for name, value := range Data_Values {
|
||||
e := ind.FieldByName(name)
|
||||
vu := e.Interface()
|
||||
switch name {
|
||||
case "Date":
|
||||
vu = vu.(time.Time).In(DefaultTimeLoc).Format(test_Date)
|
||||
value = value.(time.Time).In(DefaultTimeLoc).Format(test_Date)
|
||||
case "DateTime":
|
||||
vu = vu.(time.Time).In(DefaultTimeLoc).Format(test_DateTime)
|
||||
value = value.(time.Time).In(DefaultTimeLoc).Format(test_DateTime)
|
||||
}
|
||||
throwFail(t, AssertIs(vu == value, true), value, vu)
|
||||
}
|
||||
|
||||
var (
|
||||
ids []int
|
||||
userNames []string
|
||||
profileIds1 []int
|
||||
profileIds2 []*int
|
||||
createds []time.Time
|
||||
updateds []time.Time
|
||||
tmps1 []*Tmp
|
||||
tmps2 []Tmp
|
||||
)
|
||||
cols = []string{
|
||||
"id", "user_name", "profile_id", "profile_id", "id", "user_name", "profile_id", "id", "user_name", "profile_id", "created", "updated",
|
||||
}
|
||||
query = fmt.Sprintf("SELECT %s%s%s FROM %suser%s ORDER BY id", Q, strings.Join(cols, sep), Q, Q, Q)
|
||||
num, err = dORM.Raw(query).QueryRows(&ids, &userNames, &profileIds1, &profileIds2, &tmps1, &tmps2, &createds, &updateds)
|
||||
var ids []int
|
||||
var usernames []string
|
||||
num, err = dORM.Raw("SELECT id, user_name FROM user ORDER BY id asc").QueryRows(&ids, &usernames)
|
||||
throwFailNow(t, err)
|
||||
throwFailNow(t, AssertIs(num, 3))
|
||||
|
||||
var users []User
|
||||
dORM.QueryTable("user").OrderBy("Id").All(&users)
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
id := ids[i]
|
||||
name := userNames[i]
|
||||
pid1 := profileIds1[i]
|
||||
pid2 := profileIds2[i]
|
||||
created := createds[i]
|
||||
updated := updateds[i]
|
||||
|
||||
user := users[i]
|
||||
throwFailNow(t, AssertIs(id, user.Id))
|
||||
throwFailNow(t, AssertIs(name, user.UserName))
|
||||
if user.Profile != nil {
|
||||
throwFailNow(t, AssertIs(pid1, user.Profile.Id))
|
||||
throwFailNow(t, AssertIs(*pid2, user.Profile.Id))
|
||||
} else {
|
||||
throwFailNow(t, AssertIs(pid1, 0))
|
||||
throwFailNow(t, AssertIs(pid2, nil))
|
||||
}
|
||||
throwFailNow(t, AssertIs(created, user.Created, test_Date))
|
||||
throwFailNow(t, AssertIs(updated, user.Updated, test_DateTime))
|
||||
|
||||
tmp := tmps1[i]
|
||||
tmp1 := *tmp
|
||||
throwFailNow(t, AssertIs(tmp1.Id, user.Id))
|
||||
throwFailNow(t, AssertIs(tmp1.Name, user.UserName))
|
||||
if user.Profile != nil {
|
||||
pid := tmp1.Pid
|
||||
throwFailNow(t, AssertIs(*pid, user.Profile.Id))
|
||||
} else {
|
||||
throwFailNow(t, AssertIs(tmp1.Pid, nil))
|
||||
}
|
||||
|
||||
tmp2 := tmps2[i]
|
||||
throwFailNow(t, AssertIs(tmp2.Id, user.Id))
|
||||
throwFailNow(t, AssertIs(tmp2.Name, user.UserName))
|
||||
if user.Profile != nil {
|
||||
pid := tmp2.Pid
|
||||
throwFailNow(t, AssertIs(*pid, user.Profile.Id))
|
||||
} else {
|
||||
throwFailNow(t, AssertIs(tmp2.Pid, nil))
|
||||
}
|
||||
}
|
||||
|
||||
type Sec struct {
|
||||
Id int
|
||||
Name string
|
||||
}
|
||||
|
||||
var tmp []*Sec
|
||||
query = fmt.Sprintf("SELECT NULL, NULL FROM %suser%s LIMIT 1", Q, Q)
|
||||
num, err = dORM.Raw(query).QueryRows(&tmp)
|
||||
throwFail(t, err)
|
||||
throwFail(t, AssertIs(num, 1))
|
||||
throwFail(t, AssertIs(tmp[0], nil))
|
||||
throwFailNow(t, AssertIs(len(ids), 3))
|
||||
throwFailNow(t, AssertIs(ids[0], 2))
|
||||
throwFailNow(t, AssertIs(usernames[0], "slene"))
|
||||
throwFailNow(t, AssertIs(ids[1], 3))
|
||||
throwFailNow(t, AssertIs(usernames[1], "astaxie"))
|
||||
throwFailNow(t, AssertIs(ids[2], 4))
|
||||
throwFailNow(t, AssertIs(usernames[2], "nobody"))
|
||||
}
|
||||
|
||||
func TestRawValues(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user