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

Function TestAssociationEmptyQueryClause

tests/associations_test.go:319–354  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

317}
318
319func TestAssociationEmptyQueryClause(t *testing.T) {
320 type Organization struct {
321 gorm.Model
322 Name string
323 }
324 type Region struct {
325 gorm.Model
326 Name string
327 Organizations []Organization `gorm:"many2many:region_orgs;"`
328 }
329 type RegionOrg struct {
330 RegionId uint
331 OrganizationId uint
332 Empty myType
333 }
334 if err := DB.SetupJoinTable(&Region{}, "Organizations", &RegionOrg{}); err != nil {
335 t.Fatalf("Failed to set up join table, got error: %s", err)
336 }
337 if err := DB.Migrator().DropTable(&Organization{}, &Region{}); err != nil {
338 t.Fatalf("Failed to migrate, got error: %s", err)
339 }
340 if err := DB.AutoMigrate(&Organization{}, &Region{}); err != nil {
341 t.Fatalf("Failed to migrate, got error: %v", err)
342 }
343 region := &Region{Name: "Region1"}
344 if err := DB.Create(region).Error; err != nil {
345 t.Fatalf("fail to create region %v", err)
346 }
347 var orgs []Organization
348
349 if err := DB.Model(&Region{}).Association("Organizations").Find(&orgs); err != nil {
350 t.Fatalf("fail to find region organizations %v", err)
351 } else {
352 AssertEqual(t, len(orgs), 0)
353 }
354}
355
356type AssociationEmptyUser struct {
357 ID uint

Callers

nothing calls this directly

Calls 9

SetupJoinTableMethod · 0.80
ModelMethod · 0.80
DropTableMethod · 0.65
MigratorMethod · 0.65
AutoMigrateMethod · 0.65
CreateMethod · 0.65
FindMethod · 0.65
AssertEqualFunction · 0.50
AssociationMethod · 0.45

Tested by

no test coverage detected