(t *testing.T)
| 7 | ) |
| 8 | |
| 9 | func TestHasOneAssociation(t *testing.T) { |
| 10 | user := *GetUser("hasone", Config{Account: true}) |
| 11 | |
| 12 | if err := DB.Create(&user).Error; err != nil { |
| 13 | t.Fatalf("errors happened when create: %v", err) |
| 14 | } |
| 15 | |
| 16 | CheckUser(t, user, user) |
| 17 | |
| 18 | // Find |
| 19 | var user2 User |
| 20 | DB.Find(&user2, "id = ?", user.ID) |
| 21 | DB.Model(&user2).Association("Account").Find(&user2.Account) |
| 22 | CheckUser(t, user2, user) |
| 23 | |
| 24 | // Count |
| 25 | AssertAssociationCount(t, user, "Account", 1, "") |
| 26 | |
| 27 | // Append |
| 28 | account := Account{Number: "account-has-one-append"} |
| 29 | |
| 30 | if err := DB.Model(&user2).Association("Account").Append(&account); err != nil { |
| 31 | t.Fatalf("Error happened when append account, got %v", err) |
| 32 | } |
| 33 | |
| 34 | if account.ID == 0 { |
| 35 | t.Fatalf("Account's ID should be created") |
| 36 | } |
| 37 | |
| 38 | user.Account = account |
| 39 | CheckUser(t, user2, user) |
| 40 | |
| 41 | AssertAssociationCount(t, user, "Account", 1, "AfterAppend") |
| 42 | |
| 43 | // Replace |
| 44 | account2 := Account{Number: "account-has-one-replace"} |
| 45 | |
| 46 | if err := DB.Model(&user2).Association("Account").Replace(&account2); err != nil { |
| 47 | t.Fatalf("Error happened when append Account, got %v", err) |
| 48 | } |
| 49 | |
| 50 | if account2.ID == 0 { |
| 51 | t.Fatalf("account2's ID should be created") |
| 52 | } |
| 53 | |
| 54 | user.Account = account2 |
| 55 | CheckUser(t, user2, user) |
| 56 | |
| 57 | AssertAssociationCount(t, user2, "Account", 1, "AfterReplace") |
| 58 | |
| 59 | // Delete |
| 60 | if err := DB.Model(&user2).Association("Account").Delete(&Account{}); err != nil { |
| 61 | t.Fatalf("Error happened when delete account, got %v", err) |
| 62 | } |
| 63 | AssertAssociationCount(t, user2, "Account", 1, "after delete non-existing data") |
| 64 | |
| 65 | if err := DB.Model(&user2).Association("Account").Delete(&account2); err != nil { |
| 66 | t.Fatalf("Error happened when delete Account, got %v", err) |
nothing calls this directly
no test coverage detected