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

Function TestUpdateColumnsSkipsAssociations

tests/update_test.go:520–545  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

518}
519
520func TestUpdateColumnsSkipsAssociations(t *testing.T) {
521 user := *GetUser("update_column_skips_association", Config{})
522 DB.Create(&user)
523
524 // Update a single field of the user and verify that the changed address is not stored.
525 newAge := uint(100)
526 user.Account.Number = "new_account_number"
527 db := DB.Model(&user).UpdateColumns(User{Age: newAge})
528
529 if db.RowsAffected != 1 {
530 t.Errorf("Expected RowsAffected=1 but instead RowsAffected=%v", db.RowsAffected)
531 }
532
533 // Verify that Age now=`newAge`.
534 result := &User{}
535 result.ID = user.ID
536 DB.Preload("Account").First(result)
537
538 if result.Age != newAge {
539 t.Errorf("Expected freshly queried user to have Age=%v but instead found Age=%v", newAge, result.Age)
540 }
541
542 if result.Account.Number != user.Account.Number {
543 t.Errorf("account number should not been changed, expects: %v, got %v", user.Account.Number, result.Account.Number)
544 }
545}
546
547func TestUpdatesWithBlankValues(t *testing.T) {
548 user := *GetUser("updates_with_blank_value", Config{})

Callers

nothing calls this directly

Calls 6

GetUserFunction · 0.85
UpdateColumnsMethod · 0.80
ModelMethod · 0.80
CreateMethod · 0.65
FirstMethod · 0.65
PreloadMethod · 0.65

Tested by

no test coverage detected