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

Function TestScannerValuerWithFirstOrCreate

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

Source from the content-addressed store, hash-verified

66}
67
68func TestScannerValuerWithFirstOrCreate(t *testing.T) {
69 DB.Migrator().DropTable(&ScannerValuerStruct{})
70 if err := DB.Migrator().AutoMigrate(&ScannerValuerStruct{}); err != nil {
71 t.Errorf("no error should happen when migrate scanner, valuer struct")
72 }
73
74 data := ScannerValuerStruct{
75 Name: sql.NullString{String: "name", Valid: true},
76 Gender: &sql.NullString{String: "M", Valid: true},
77 Age: sql.NullInt64{Int64: 18, Valid: true},
78 ExampleStruct: ExampleStruct{"name", "value1"},
79 ExampleStructPtr: &ExampleStruct{"name", "value2"},
80 }
81
82 var result ScannerValuerStruct
83 tx := DB.Where(data).FirstOrCreate(&result)
84
85 if tx.RowsAffected != 1 {
86 t.Errorf("RowsAffected should be 1 after create some record")
87 }
88
89 if tx.Error != nil {
90 t.Errorf("Should not raise any error, but got %v", tx.Error)
91 }
92
93 AssertObjEqual(t, result, data, "Name", "Gender", "Age")
94
95 if err := DB.Where(data).Assign(ScannerValuerStruct{Age: sql.NullInt64{Int64: 18, Valid: true}}).FirstOrCreate(&result).Error; err != nil {
96 t.Errorf("Should not raise any error, but got %v", err)
97 }
98
99 if result.Age.Int64 != 18 {
100 t.Errorf("should update age to 18")
101 }
102
103 var result2 ScannerValuerStruct
104 if err := DB.First(&result2, result.ID).Error; err != nil {
105 t.Errorf("got error %v when query with %v", err, result.ID)
106 }
107
108 AssertObjEqual(t, result2, result, "ID", "CreatedAt", "UpdatedAt", "Name", "Gender", "Age")
109}
110
111func TestInvalidValuer(t *testing.T) {
112 DB.Migrator().DropTable(&ScannerValuerStruct{})

Callers

nothing calls this directly

Calls 8

AssertObjEqualFunction · 0.85
FirstOrCreateMethod · 0.80
AssignMethod · 0.80
DropTableMethod · 0.65
MigratorMethod · 0.65
AutoMigrateMethod · 0.65
WhereMethod · 0.65
FirstMethod · 0.65

Tested by

no test coverage detected