Skip to content

Commit

Permalink
Test Pluck with customized type
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jul 25, 2023
1 parent c10f807 commit a7f01bd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
18 changes: 16 additions & 2 deletions tests/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module gorm.io/gorm/tests

go 1.16
go 1.18

require (
github.com/google/uuid v1.3.0
Expand All @@ -10,7 +10,21 @@ require (
gorm.io/driver/postgres v1.5.3-0.20230607070428-18bc84b75196
gorm.io/driver/sqlite v1.5.2
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 => ../
24 changes: 24 additions & 0 deletions tests/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests_test

import (
"database/sql"
"database/sql/driver"
"fmt"
"reflect"
"regexp"
Expand Down Expand Up @@ -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) {
users := []*User{
GetUser("pluck-user1", Config{}),
Expand Down Expand Up @@ -685,6 +698,11 @@ func TestPluck(t *testing.T) {
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 {
if name != users[idx].Name {
t.Errorf("Unexpected result on pluck name, got %+v", names)
Expand All @@ -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
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)
Expand Down

0 comments on commit a7f01bd

Please sign in to comment.