-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathpostgres_test.go
More file actions
149 lines (146 loc) · 4.01 KB
/
Copy pathpostgres_test.go
File metadata and controls
149 lines (146 loc) · 4.01 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package postgres
import (
"testing"
"gorm.io/gorm/schema"
)
func Test_DataTypeOf(t *testing.T) {
type fields struct {
Config *Config
}
type args struct {
field *schema.Field
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "it should return boolean",
args: args{field: &schema.Field{DataType: schema.Bool}},
want: "boolean",
},
{
name: "it should return text -1",
args: args{field: &schema.Field{DataType: schema.String, Size: -1}},
want: "text",
},
{
name: "it should return text > 10485760",
args: args{field: &schema.Field{DataType: schema.String, Size: 12345678}},
want: "text",
},
{
name: "it should return varchar(100)",
args: args{field: &schema.Field{DataType: schema.String, Size: 100}},
want: "varchar(100)",
},
{
name: "it should return varchar(10485760)",
args: args{field: &schema.Field{DataType: schema.String, Size: 10485760}},
want: "varchar(10485760)",
},
{
// https://github.com/go-gorm/gorm/issues/7191
name: "generated:identity renders an identity column (BY DEFAULT)",
args: args{field: &schema.Field{
DataType: schema.Uint,
GORMDataType: schema.Uint,
Size: 64,
PrimaryKey: true,
AutoIncrement: true,
TagSettings: map[string]string{"GENERATED": "identity"},
}},
want: "bigint GENERATED BY DEFAULT AS IDENTITY",
},
{
name: "generated:identity always renders an ALWAYS identity column",
args: args{field: &schema.Field{
DataType: schema.Uint,
GORMDataType: schema.Uint,
Size: 64,
PrimaryKey: true,
AutoIncrement: true,
TagSettings: map[string]string{"GENERATED": "identity always"},
}},
want: "bigint GENERATED ALWAYS AS IDENTITY",
},
{
name: "generated:always identity is order independent",
args: args{field: &schema.Field{
DataType: schema.Int,
GORMDataType: schema.Int,
Size: 32,
TagSettings: map[string]string{"GENERATED": "always identity"},
}},
want: "integer GENERATED ALWAYS AS IDENTITY",
},
{
name: "generated:<expr> renders a STORED computed column",
args: args{field: &schema.Field{
DataType: "numeric",
TagSettings: map[string]string{"GENERATED": "price * quantity"},
}},
want: "numeric GENERATED ALWAYS AS (price * quantity) STORED",
},
{
name: "generated:<expr> keeps commas inside the expression",
args: args{field: &schema.Field{
DataType: schema.String,
Size: -1,
TagSettings: map[string]string{"GENERATED": "coalesce(first_name, last_name)"},
}},
want: "text GENERATED ALWAYS AS (coalesce(first_name, last_name)) STORED",
},
{
name: "a bare generated tag is ignored",
args: args{field: &schema.Field{
DataType: schema.Uint,
GORMDataType: schema.Uint,
Size: 64,
AutoIncrement: true,
TagSettings: map[string]string{"GENERATED": "GENERATED"},
}},
want: "bigserial",
},
{
name: "a lowercase generated expression is not mistaken for a bare tag",
args: args{field: &schema.Field{
DataType: "numeric",
TagSettings: map[string]string{"GENERATED": "generated"},
}},
want: "numeric GENERATED ALWAYS AS (generated) STORED",
},
{
name: "it should still convert a plain custom type to bigserial for an auto increment field",
args: args{field: &schema.Field{
DataType: "bigint",
GORMDataType: schema.Uint,
AutoIncrement: true,
Size: 64,
}},
want: "bigserial",
},
{
name: "it should keep an explicit bigserial type for an auto increment field",
args: args{field: &schema.Field{
DataType: "bigserial",
GORMDataType: schema.Uint,
AutoIncrement: true,
Size: 64,
}},
want: "bigserial",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dialector := Dialector{
Config: tt.fields.Config,
}
if got := dialector.DataTypeOf(tt.args.field); got != tt.want {
t.Errorf("DataTypeOf() = %v, want %v", got, tt.want)
}
})
}
}