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

Function TestConcurrentMany2ManyAssociation

tests/associations_many2many_test.go:360–395  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

358}
359
360func TestConcurrentMany2ManyAssociation(t *testing.T) {
361 db, err := OpenTestConnection(&gorm.Config{})
362 if err != nil {
363 t.Fatalf("open test connection failed, err: %+v", err)
364 }
365
366 count := 3
367
368 var languages []Language
369 for i := 0; i < count; i++ {
370 language := Language{Code: fmt.Sprintf("consurrent %d", i)}
371 db.Create(&language)
372 languages = append(languages, language)
373 }
374
375 user := User{}
376 db.Create(&user)
377 db.Preload("Languages").FirstOrCreate(&user)
378
379 var wg sync.WaitGroup
380 for i := 0; i < count; i++ {
381 wg.Add(1)
382 go func(user User, language Language) {
383 err := db.Model(&user).Association("Languages").Append(&language)
384 AssertEqual(t, err, nil)
385
386 wg.Done()
387 }(user, languages[i])
388 }
389 wg.Wait()
390
391 var find User
392 err = db.Preload(clause.Associations).Where("id = ?", user.ID).First(&find).Error
393 AssertEqual(t, err, nil)
394 AssertAssociationCount(t, find, "Languages", int64(count), "after concurrent append")
395}
396
397func TestMany2ManyDuplicateBelongsToAssociation(t *testing.T) {
398 user1 := User{Name: "TestMany2ManyDuplicateBelongsToAssociation-1", Friends: []*User{

Callers

nothing calls this directly

Calls 12

OpenTestConnectionFunction · 0.85
AssertAssociationCountFunction · 0.85
FirstOrCreateMethod · 0.80
AddMethod · 0.80
AppendMethod · 0.80
ModelMethod · 0.80
CreateMethod · 0.65
PreloadMethod · 0.65
FirstMethod · 0.65
WhereMethod · 0.65
AssertEqualFunction · 0.50
AssociationMethod · 0.45

Tested by

no test coverage detected