gorm/tests/scan_test.go

95 lines
2.7 KiB
Go
Raw Normal View History

2020-05-26 16:30:17 +03:00
package tests_test
import (
2020-05-26 18:13:05 +03:00
"reflect"
"sort"
"strings"
2020-05-26 16:30:17 +03:00
"testing"
"gorm.io/gorm"
2020-06-02 05:34:50 +03:00
. "gorm.io/gorm/utils/tests"
2020-05-26 16:30:17 +03:00
)
func TestScan(t *testing.T) {
user1 := User{Name: "ScanUser1", Age: 1}
user2 := User{Name: "ScanUser2", Age: 10}
user3 := User{Name: "ScanUser3", Age: 20}
DB.Save(&user1).Save(&user2).Save(&user3)
type result struct {
ID uint
2020-05-26 16:30:17 +03:00
Name string
Age int
}
var res result
DB.Table("users").Select("id, name, age").Where("id = ?", user3.ID).Scan(&res)
if res.ID != user3.ID || res.Name != user3.Name || res.Age != int(user3.Age) {
t.Fatalf("Scan into struct should work, got %#v, should %#v", res, user3)
}
DB.Table("users").Select("id, name, age").Where("id = ?", user2.ID).Scan(&res)
if res.ID != user2.ID || res.Name != user2.Name || res.Age != int(user2.Age) {
t.Fatalf("Scan into struct should work, got %#v, should %#v", res, user2)
}
DB.Model(&User{Model: gorm.Model{ID: user3.ID}}).Select("id, name, age").Scan(&res)
if res.ID != user3.ID || res.Name != user3.Name || res.Age != int(user3.Age) {
t.Fatalf("Scan into struct should work, got %#v, should %#v", res, user3)
2020-05-26 16:30:17 +03:00
}
var doubleAgeRes = &result{}
2020-05-26 18:13:05 +03:00
if err := DB.Table("users").Select("age + age as age").Where("id = ?", user3.ID).Scan(&doubleAgeRes).Error; err != nil {
2020-05-26 16:30:17 +03:00
t.Errorf("Scan to pointer of pointer")
}
if doubleAgeRes.Age != int(res.Age)*2 {
t.Errorf("Scan double age as age, expect: %v, got %v", res.Age*2, doubleAgeRes.Age)
}
2020-05-26 18:13:05 +03:00
var results []result
DB.Table("users").Select("name, age").Where("id in ?", []uint{user2.ID, user3.ID}).Scan(&results)
sort.Slice(results, func(i, j int) bool {
return strings.Compare(results[i].Name, results[j].Name) <= -1
2020-05-26 18:13:05 +03:00
})
if len(results) != 2 || results[0].Name != user2.Name || results[1].Name != user3.Name {
t.Errorf("Scan into struct map, got %#v", results)
2020-05-26 16:30:17 +03:00
}
}
2020-05-26 18:13:05 +03:00
func TestScanRows(t *testing.T) {
user1 := User{Name: "ScanRowsUser1", Age: 1}
user2 := User{Name: "ScanRowsUser2", Age: 10}
user3 := User{Name: "ScanRowsUser3", Age: 20}
DB.Save(&user1).Save(&user2).Save(&user3)
rows, err := DB.Table("users").Where("name = ? or name = ?", user2.Name, user3.Name).Select("name, age").Rows()
if err != nil {
t.Errorf("Not error should happen, got %v", err)
}
type Result struct {
Name string
Age int
}
var results []Result
for rows.Next() {
var result Result
if err := DB.ScanRows(rows, &result); err != nil {
t.Errorf("should get no error, but got %v", err)
}
results = append(results, result)
}
sort.Slice(results, func(i, j int) bool {
return strings.Compare(results[i].Name, results[j].Name) <= -1
})
2020-05-26 18:13:05 +03:00
if !reflect.DeepEqual(results, []Result{{Name: "ScanRowsUser2", Age: 10}, {Name: "ScanRowsUser3", Age: 20}}) {
t.Errorf("Should find expected results")
}
}