MCPcopy
hub / github.com/go-gorm/gorm / TestScannerValuer

Function TestScannerValuer

tests/scanner_valuer_test.go:21–66  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

19)
20
21func TestScannerValuer(t *testing.T) {
22 DB.Migrator().DropTable(&ScannerValuerStruct{})
23 if err := DB.Migrator().AutoMigrate(&ScannerValuerStruct{}); err != nil {
24 t.Fatalf("no error should happen when migrate scanner, valuer struct, got error %v", err)
25 }
26
27 data := ScannerValuerStruct{
28 Name: sql.NullString{String: "name", Valid: true},
29 Gender: &sql.NullString{String: "M", Valid: true},
30 Age: sql.NullInt64{Int64: 18, Valid: true},
31 Male: sql.NullBool{Bool: true, Valid: true},
32 Height: sql.NullFloat64{Float64: 1.8888, Valid: true},
33 Birthday: sql.NullTime{Time: time.Now(), Valid: true},
34 Allergen: NullString{sql.NullString{String: "Allergen", Valid: true}},
35 Password: EncryptedData("pass1"),
36 Bytes: []byte("byte"),
37 Num: 18,
38 Strings: StringsSlice{"a", "b", "c"},
39 Structs: StructsSlice{
40 {"name1", "value1"},
41 {"name2", "value2"},
42 },
43 Role: Role{Name: "admin"},
44 ExampleStruct: ExampleStruct{"name", "value1"},
45 ExampleStructPtr: &ExampleStruct{"name", "value2"},
46 }
47
48 if err := DB.Create(&data).Error; err != nil {
49 t.Fatalf("No error should happened when create scanner valuer struct, but got %v", err)
50 }
51
52 var result ScannerValuerStruct
53
54 if err := DB.Find(&result, "id = ?", data.ID).Error; err != nil {
55 t.Fatalf("no error should happen when query scanner, valuer struct, but got %v", err)
56 }
57
58 if result.ExampleStructPtr.Val != "value2" {
59 t.Errorf(`ExampleStructPtr.Val should equal to "value2", but got %v`, result.ExampleStructPtr.Val)
60 }
61
62 if result.ExampleStruct.Val != "value1" {
63 t.Errorf(`ExampleStruct.Val should equal to "value1", but got %#v`, result.ExampleStruct)
64 }
65 AssertObjEqual(t, data, result, "Name", "Gender", "Age", "Male", "Height", "Birthday", "Password", "Bytes", "Num", "Strings", "Structs")
66}
67
68func TestScannerValuerWithFirstOrCreate(t *testing.T) {
69 DB.Migrator().DropTable(&ScannerValuerStruct{})

Callers

nothing calls this directly

Calls 7

EncryptedDataTypeAlias · 0.85
AssertObjEqualFunction · 0.85
DropTableMethod · 0.65
MigratorMethod · 0.65
AutoMigrateMethod · 0.65
CreateMethod · 0.65
FindMethod · 0.65

Tested by

no test coverage detected