Skip to content

Commit 2667f8d

Browse files
Merge pull request #44 from oracle-samples/scanner-tests
Scanner tests
2 parents 4645aca + 885e33a commit 2667f8d

File tree

1 file changed

+65
-19
lines changed

1 file changed

+65
-19
lines changed

tests/scanner_valuer_test.go

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import (
5151
"testing"
5252
"time"
5353

54+
"github.com/godror/godror"
5455
. "github.com/oracle-samples/gorm-oracle/tests/utils"
5556

5657
"gorm.io/gorm"
@@ -59,7 +60,6 @@ import (
5960
)
6061

6162
func TestScannerValuer(t *testing.T) {
62-
t.Skip()
6363
DB.Migrator().DropTable(&ScannerValuerStruct{})
6464
if err := DB.Migrator().AutoMigrate(&ScannerValuerStruct{}); err != nil {
6565
t.Fatalf("no error should happen when migrate scanner, valuer struct, got error %v", err)
@@ -92,7 +92,7 @@ func TestScannerValuer(t *testing.T) {
9292

9393
var result ScannerValuerStruct
9494

95-
if err := DB.Find(&result, "id = ?", data.ID).Error; err != nil {
95+
if err := DB.Find(&result, "\"id\" = ?", data.ID).Error; err != nil {
9696
t.Fatalf("no error should happen when query scanner, valuer struct, but got %v", err)
9797
}
9898

@@ -107,22 +107,24 @@ func TestScannerValuer(t *testing.T) {
107107
}
108108

109109
func TestScannerValuerWithFirstOrCreate(t *testing.T) {
110-
t.Skip()
111110
DB.Migrator().DropTable(&ScannerValuerStruct{})
112111
if err := DB.Migrator().AutoMigrate(&ScannerValuerStruct{}); err != nil {
113112
t.Errorf("no error should happen when migrate scanner, valuer struct")
114113
}
115114

116-
data := ScannerValuerStruct{
117-
Name: sql.NullString{String: "name", Valid: true},
118-
Gender: &sql.NullString{String: "M", Valid: true},
119-
Age: sql.NullInt64{Int64: 18, Valid: true},
115+
cond := ScannerValuerStruct{
116+
Name: sql.NullString{String: "name", Valid: true},
117+
Gender: &sql.NullString{String: "M", Valid: true},
118+
Age: sql.NullInt64{Int64: 18, Valid: true},
119+
}
120+
121+
attrs := ScannerValuerStruct{
120122
ExampleStruct: ExampleStruct{"name", "value1"},
121123
ExampleStructPtr: &ExampleStruct{"name", "value2"},
122124
}
123125

124126
var result ScannerValuerStruct
125-
tx := DB.Where(data).FirstOrCreate(&result)
127+
tx := DB.Where(cond).Attrs(attrs).FirstOrCreate(&result)
126128

127129
if tx.RowsAffected != 1 {
128130
t.Errorf("RowsAffected should be 1 after create some record")
@@ -132,9 +134,9 @@ func TestScannerValuerWithFirstOrCreate(t *testing.T) {
132134
t.Errorf("Should not raise any error, but got %v", tx.Error)
133135
}
134136

135-
tests.AssertObjEqual(t, result, data, "Name", "Gender", "Age")
137+
tests.AssertObjEqual(t, result, cond, "Name", "Gender", "Age")
136138

137-
if err := DB.Where(data).Assign(ScannerValuerStruct{Age: sql.NullInt64{Int64: 18, Valid: true}}).FirstOrCreate(&result).Error; err != nil {
139+
if err := DB.Where(cond).Assign(ScannerValuerStruct{Age: sql.NullInt64{Int64: 18, Valid: true}}).FirstOrCreate(&result).Error; err != nil {
138140
t.Errorf("Should not raise any error, but got %v", err)
139141
}
140142

@@ -151,7 +153,6 @@ func TestScannerValuerWithFirstOrCreate(t *testing.T) {
151153
}
152154

153155
func TestInvalidValuer(t *testing.T) {
154-
t.Skip()
155156
DB.Migrator().DropTable(&ScannerValuerStruct{})
156157
if err := DB.Migrator().AutoMigrate(&ScannerValuerStruct{}); err != nil {
157158
t.Errorf("no error should happen when migrate scanner, valuer struct")
@@ -205,6 +206,24 @@ type ScannerValuerStruct struct {
205206
ExampleStructPtr *ExampleStruct
206207
}
207208

209+
func (StringsSlice) GormDataType() string { return "CLOB" }
210+
func (l StringsSlice) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
211+
v, err := l.Value()
212+
if err != nil {
213+
return gorm.Expr("?", err)
214+
}
215+
return gorm.Expr("?", v)
216+
}
217+
218+
func (StructsSlice) GormDataType() string { return "CLOB" }
219+
func (l StructsSlice) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
220+
v, err := l.Value()
221+
if err != nil {
222+
return gorm.Expr("?", err)
223+
}
224+
return gorm.Expr("?", v)
225+
}
226+
208227
type EncryptedData []byte
209228

210229
func (data *EncryptedData) Scan(value interface{}) error {
@@ -235,17 +254,44 @@ func (data EncryptedData) Value() (driver.Value, error) {
235254

236255
type Num int64
237256

238-
func (i *Num) Scan(src interface{}) error {
239-
switch s := src.(type) {
240-
case []byte:
241-
n, _ := strconv.Atoi(string(s))
242-
*i = Num(n)
257+
func (n *Num) Scan(val interface{}) error {
258+
if val == nil {
259+
*n = 0
260+
return nil
261+
}
262+
263+
switch x := val.(type) {
243264
case int64:
244-
*i = Num(s)
265+
*n = Num(x)
266+
return nil
267+
268+
case godror.Number:
269+
i, err := strconv.ParseInt(string(x), 10, 64)
270+
if err != nil {
271+
return fmt.Errorf("Num.Scan: cannot parse godror.Number %q: %w", string(x), err)
272+
}
273+
*n = Num(i)
274+
return nil
275+
276+
case string:
277+
i, err := strconv.ParseInt(x, 10, 64)
278+
if err != nil {
279+
return fmt.Errorf("Num.Scan: cannot parse string %q: %w", x, err)
280+
}
281+
*n = Num(i)
282+
return nil
283+
284+
case []byte:
285+
i, err := strconv.ParseInt(string(x), 10, 64)
286+
if err != nil {
287+
return fmt.Errorf("Num.Scan: cannot parse []byte %q: %w", string(x), err)
288+
}
289+
*n = Num(i)
290+
return nil
291+
245292
default:
246-
return errors.New("Cannot scan NamedInt from " + reflect.ValueOf(src).String())
293+
return fmt.Errorf("Num.Scan: unsupported type %T", val)
247294
}
248-
return nil
249295
}
250296

251297
type StringsSlice []string

0 commit comments

Comments
 (0)