forked from go-gorm/sqlserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_test.go
More file actions
85 lines (70 loc) · 2.14 KB
/
create_test.go
File metadata and controls
85 lines (70 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package sqlserver
import (
"strings"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type mergeSchemaTableModel struct {
ID uint `gorm:"primaryKey"`
Name string `gorm:"column:name"`
}
func (mergeSchemaTableModel) TableName() string {
return "testschema.users"
}
type identityInsertSchemaTableModel struct {
ID uint `gorm:"primaryKey;autoIncrement"`
Name string `gorm:"column:name"`
}
func (identityInsertSchemaTableModel) TableName() string {
return "testschema.identity_models"
}
func openDryRunDB(t *testing.T) *gorm.DB {
t.Helper()
sqlDB, _, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
t.Cleanup(func() {
_ = sqlDB.Close()
})
db, err := gorm.Open(New(Config{Conn: sqlDB}), &gorm.Config{
DryRun: true,
SkipDefaultTransaction: true,
})
if err != nil {
t.Fatalf("failed to open dry run db: %v", err)
}
return db
}
func TestCreateSQL_MergeUsesSchemaQualifiedTable(t *testing.T) {
db := openDryRunDB(t)
tx := db.Clauses(clause.OnConflict{
DoUpdates: clause.AssignmentColumns([]string{"name"}),
}).Create(&mergeSchemaTableModel{ID: 1, Name: "alice"})
if tx.Error != nil {
t.Fatalf("create failed: %v", tx.Error)
}
sql := tx.Statement.SQL.String()
if !strings.Contains(sql, `MERGE INTO "testschema"."users"`) {
t.Fatalf("expected schema-qualified MERGE target, got SQL: %s", sql)
}
if !strings.Contains(sql, `"testschema"."users"."id"`) {
t.Fatalf("expected schema-qualified target column in ON clause, got SQL: %s", sql)
}
}
func TestCreateSQL_IdentityInsertUsesSchemaQualifiedTable(t *testing.T) {
db := openDryRunDB(t)
tx := db.Create(&identityInsertSchemaTableModel{ID: 42, Name: "bob"})
if tx.Error != nil {
t.Fatalf("create failed: %v", tx.Error)
}
sql := tx.Statement.SQL.String()
if !strings.Contains(sql, `SET IDENTITY_INSERT "testschema"."identity_models" ON;`) {
t.Fatalf("expected schema-qualified IDENTITY_INSERT ON, got SQL: %s", sql)
}
if !strings.Contains(sql, `SET IDENTITY_INSERT "testschema"."identity_models" OFF;`) {
t.Fatalf("expected schema-qualified IDENTITY_INSERT OFF, got SQL: %s", sql)
}
}