(t *testing.T)
| 853 | } |
| 854 | |
| 855 | func TestGenericsUpsert(t *testing.T) { |
| 856 | ctx := context.Background() |
| 857 | lang := Language{Code: "upsert", Name: "Upsert"} |
| 858 | |
| 859 | if err := gorm.G[Language](DB, clause.OnConflict{DoNothing: true}).Create(ctx, &lang); err != nil { |
| 860 | t.Fatalf("failed to upsert, got %v", err) |
| 861 | } |
| 862 | |
| 863 | lang2 := Language{Code: "upsert", Name: "Upsert"} |
| 864 | if err := gorm.G[Language](DB, clause.OnConflict{DoNothing: true}).Create(ctx, &lang2); err != nil { |
| 865 | t.Fatalf("failed to upsert, got %v", err) |
| 866 | } |
| 867 | |
| 868 | langs, err := gorm.G[Language](DB).Where("code = ?", lang.Code).Find(ctx) |
| 869 | if err != nil { |
| 870 | t.Errorf("no error should happen when find languages with code, but got %v", err) |
| 871 | } else if len(langs) != 1 { |
| 872 | t.Errorf("should only find only 1 languages, but got %+v", langs) |
| 873 | } |
| 874 | |
| 875 | lang3 := Language{Code: "upsert", Name: "Upsert"} |
| 876 | if err := gorm.G[Language](DB, clause.OnConflict{ |
| 877 | Columns: []clause.Column{{Name: "code"}}, |
| 878 | DoUpdates: clause.Assignments(map[string]interface{}{"name": "upsert-new"}), |
| 879 | }).Create(ctx, &lang3); err != nil { |
| 880 | t.Fatalf("failed to upsert, got %v", err) |
| 881 | } |
| 882 | |
| 883 | if langs, err := gorm.G[Language](DB).Where("code = ?", lang.Code).Find(ctx); err != nil { |
| 884 | t.Errorf("no error should happen when find languages with code, but got %v", err) |
| 885 | } else if len(langs) != 1 { |
| 886 | t.Errorf("should only find only 1 languages, but got %+v", langs) |
| 887 | } else if langs[0].Name != "upsert-new" { |
| 888 | t.Errorf("should update name on conflict, but got name %+v", langs[0].Name) |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | func TestGenericsWithResult(t *testing.T) { |
| 893 | ctx := context.Background() |
nothing calls this directly
no test coverage detected