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

Function AssertAssociationCount

tests/associations_test.go:12–29  ·  view source on GitHub ↗
(t *testing.T, data interface{}, name string, result int64, reason string)

Source from the content-addressed store, hash-verified

10)
11
12func AssertAssociationCount(t *testing.T, data interface{}, name string, result int64, reason string) {
13 if count := DB.Model(data).Association(name).Count(); count != result {
14 t.Fatalf("invalid %v count %v, expects: %v got %v", name, reason, result, count)
15 }
16
17 var newUser User
18 if user, ok := data.(User); ok {
19 DB.Find(&newUser, "id = ?", user.ID)
20 } else if user, ok := data.(*User); ok {
21 DB.Find(&newUser, "id = ?", user.ID)
22 }
23
24 if newUser.ID != 0 {
25 if count := DB.Model(&newUser).Association(name).Count(); count != result {
26 t.Fatalf("invalid %v count %v, expects: %v got %v", name, reason, result, count)
27 }
28 }
29}
30
31func TestInvalidAssociation(t *testing.T) {
32 user := *GetUser("invalid", Config{Company: true, Manager: true})

Calls 4

ModelMethod · 0.80
CountMethod · 0.65
FindMethod · 0.65
AssociationMethod · 0.45

Tested by

no test coverage detected