Test Pluck with customized type

This commit is contained in:
Jinzhu 2023-07-25 10:47:19 +08:00
parent c10f807d3c
commit a7f01bd1b2
2 changed files with 40 additions and 2 deletions

View File

@ -1,6 +1,6 @@
module gorm.io/gorm/tests module gorm.io/gorm/tests
go 1.16 go 1.18
require ( require (
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0
@ -10,7 +10,21 @@ require (
gorm.io/driver/postgres v1.5.3-0.20230607070428-18bc84b75196 gorm.io/driver/postgres v1.5.3-0.20230607070428-18bc84b75196
gorm.io/driver/sqlite v1.5.2 gorm.io/driver/sqlite v1.5.2
gorm.io/driver/sqlserver v1.5.2-0.20230613072041-6e2cde390b0a gorm.io/driver/sqlserver v1.5.2-0.20230613072041-6e2cde390b0a
gorm.io/gorm v1.25.2-0.20230610234218-206613868439 gorm.io/gorm v1.25.2
)
require (
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/microsoft/go-mssqldb v1.4.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/text v0.11.0 // indirect
) )
replace gorm.io/gorm => ../ replace gorm.io/gorm => ../

View File

@ -2,6 +2,7 @@ package tests_test
import ( import (
"database/sql" "database/sql"
"database/sql/driver"
"fmt" "fmt"
"reflect" "reflect"
"regexp" "regexp"
@ -658,6 +659,18 @@ func TestOrWithAllFields(t *testing.T) {
} }
} }
type Int64 int64
func (v Int64) Value() (driver.Value, error) {
return v - 1, nil
}
func (f *Int64) Scan(v interface{}) error {
y := v.(int64)
*f = Int64(y + 1)
return nil
}
func TestPluck(t *testing.T) { func TestPluck(t *testing.T) {
users := []*User{ users := []*User{
GetUser("pluck-user1", Config{}), GetUser("pluck-user1", Config{}),
@ -685,6 +698,11 @@ func TestPluck(t *testing.T) {
t.Errorf("got error when pluck id: %v", err) t.Errorf("got error when pluck id: %v", err)
} }
var ids2 []Int64
if err := DB.Model(User{}).Where("name like ?", "pluck-user%").Pluck("id", &ids2).Error; err != nil {
t.Errorf("got error when pluck id: %v", err)
}
for idx, name := range names { for idx, name := range names {
if name != users[idx].Name { if name != users[idx].Name {
t.Errorf("Unexpected result on pluck name, got %+v", names) t.Errorf("Unexpected result on pluck name, got %+v", names)
@ -697,6 +715,12 @@ func TestPluck(t *testing.T) {
} }
} }
for idx, id := range ids2 {
if int(id) != int(users[idx].ID+1) {
t.Errorf("Unexpected result on pluck id, got %+v", ids)
}
}
var times []time.Time var times []time.Time
if err := DB.Model(User{}).Where("name like ?", "pluck-user%").Pluck("created_at", &times).Error; err != nil { if err := DB.Model(User{}).Where("name like ?", "pluck-user%").Pluck("created_at", &times).Error; err != nil {
t.Errorf("got error when pluck time: %v", err) t.Errorf("got error when pluck time: %v", err)