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

Function TestGORMValuer

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

Source from the content-addressed store, hash-verified

326}
327
328func TestGORMValuer(t *testing.T) {
329 type UserWithPoint struct {
330 Name string
331 Point Point
332 }
333
334 dryRunDB := DB.Session(&gorm.Session{DryRun: true})
335
336 stmt := dryRunDB.Create(&UserWithPoint{
337 Name: "jinzhu",
338 Point: Point{X: 100, Y: 100},
339 }).Statement
340
341 if stmt.SQL.String() == "" || len(stmt.Vars) != 2 {
342 t.Errorf("Failed to generate sql, got %v", stmt.SQL.String())
343 }
344
345 if !regexp.MustCompile(`INSERT INTO .user_with_points. \(.name.,.point.\) VALUES \(.+,ST_PointFromText\(.+\)\)`).MatchString(stmt.SQL.String()) {
346 t.Errorf("insert with sql.Expr, but got %v", stmt.SQL.String())
347 }
348
349 if !reflect.DeepEqual([]interface{}{"jinzhu", "POINT(100 100)"}, stmt.Vars) {
350 t.Errorf("generated vars is not equal, got %v", stmt.Vars)
351 }
352
353 stmt = dryRunDB.Model(UserWithPoint{}).Create(map[string]interface{}{
354 "Name": "jinzhu",
355 "Point": clause.Expr{SQL: "ST_PointFromText(?)", Vars: []interface{}{"POINT(100 100)"}},
356 }).Statement
357
358 if !regexp.MustCompile(`INSERT INTO .user_with_points. \(.name.,.point.\) VALUES \(.+,ST_PointFromText\(.+\)\)`).MatchString(stmt.SQL.String()) {
359 t.Errorf("insert with sql.Expr, but got %v", stmt.SQL.String())
360 }
361
362 if !reflect.DeepEqual([]interface{}{"jinzhu", "POINT(100 100)"}, stmt.Vars) {
363 t.Errorf("generated vars is not equal, got %v", stmt.Vars)
364 }
365
366 stmt = dryRunDB.Table("user_with_points").Create(&map[string]interface{}{
367 "Name": "jinzhu",
368 "Point": clause.Expr{SQL: "ST_PointFromText(?)", Vars: []interface{}{"POINT(100 100)"}},
369 }).Statement
370
371 if !regexp.MustCompile(`INSERT INTO .user_with_points. \(.Name.,.Point.\) VALUES \(.+,ST_PointFromText\(.+\)\)`).MatchString(stmt.SQL.String()) {
372 t.Errorf("insert with sql.Expr, but got %v", stmt.SQL.String())
373 }
374
375 if !reflect.DeepEqual([]interface{}{"jinzhu", "POINT(100 100)"}, stmt.Vars) {
376 t.Errorf("generated vars is not equal, got %v", stmt.Vars)
377 }
378
379 stmt = dryRunDB.Session(&gorm.Session{
380 AllowGlobalUpdate: true,
381 }).Model(&UserWithPoint{}).Updates(UserWithPoint{
382 Name: "jinzhu",
383 Point: Point{X: 100, Y: 100},
384 }).Statement
385

Callers

nothing calls this directly

Calls 6

SessionMethod · 0.80
ModelMethod · 0.80
CreateMethod · 0.65
TableMethod · 0.65
UpdatesMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected