@@ -97,3 +97,117 @@ func TestNonStdPrimaryKeyAndDefaultValues(t *testing.T) {
97
97
t .Errorf ("Update a filed to blank with a default value should occur. But got %v\n " , animal .Name )
98
98
}
99
99
}
100
+
101
+ func TestPrimaryKeyAutoIncrement (t * testing.T ) {
102
+ DB .Migrator ().DropTable (& Animal {})
103
+ if err := DB .AutoMigrate (& Animal {}); err != nil {
104
+ t .Fatalf ("migration failed: %v" , err )
105
+ }
106
+
107
+ a1 := Animal {Name : "A1" }
108
+ a2 := Animal {Name : "A2" }
109
+ DB .Create (& a1 )
110
+ DB .Create (& a2 )
111
+
112
+ if a1 .Counter == 0 || a2 .Counter == 0 {
113
+ t .Errorf ("primary key Counter should be set, got %d and %d" , a1 .Counter , a2 .Counter )
114
+ }
115
+ if a2 .Counter <= a1 .Counter {
116
+ t .Errorf ("Counter should auto-increment, got %d then %d" , a1 .Counter , a2 .Counter )
117
+ }
118
+ }
119
+
120
+ func TestReservedKeywordColumn (t * testing.T ) {
121
+ DB .Migrator ().DropTable (& Animal {})
122
+ DB .AutoMigrate (& Animal {})
123
+
124
+ animal := Animal {From : "a nice place" }
125
+ DB .Create (& animal )
126
+
127
+ var fetched Animal
128
+ if err := DB .Where ("\" from\" = ?" , "a nice place" ).First (& fetched ).Error ; err != nil {
129
+ t .Errorf ("query with reserved keyword failed: %v" , err )
130
+ }
131
+ if fetched .From != "a nice place" {
132
+ t .Errorf ("expected From='a nice place', got %v" , fetched .From )
133
+ }
134
+
135
+ var badFetched Animal
136
+ err := DB .Where ("from = ?" , "a nice place" ).First (& badFetched ).Error
137
+ if err == nil {
138
+ t .Errorf ("expected error when querying without quotes on reserved keyword, but got none" )
139
+ }
140
+ }
141
+
142
+ func timePrecisionCheck (t1 , t2 time.Time , tolerance time.Duration ) bool {
143
+ return t1 .Sub (t2 ) < tolerance && t2 .Sub (t1 ) < tolerance
144
+ }
145
+
146
+ func TestPointerFieldNullability (t * testing.T ) {
147
+ DB .Migrator ().DropTable (& Animal {})
148
+ DB .AutoMigrate (& Animal {})
149
+
150
+ animal1 := Animal {Name : "NoAge" }
151
+ DB .Create (& animal1 )
152
+
153
+ var fetched1 Animal
154
+ DB .First (& fetched1 , animal1 .Counter )
155
+ if fetched1 .Age != nil {
156
+ t .Errorf ("expected Age=nil, got %v" , fetched1 .Age )
157
+ }
158
+
159
+ now := time .Now ()
160
+ animal2 := Animal {Name : "WithAge" , Age : & now }
161
+ DB .Create (& animal2 )
162
+
163
+ var fetched2 Animal
164
+ DB .First (& fetched2 , animal2 .Counter )
165
+ if fetched2 .Age == nil {
166
+ t .Errorf ("expected Age to be set, got nil" )
167
+ } else if ! timePrecisionCheck (* fetched2 .Age , now , time .Microsecond ) {
168
+ t .Errorf ("expected Age≈%v, got %v" , now , * fetched2 .Age )
169
+ }
170
+ }
171
+
172
+ func TestUnexportedFieldNotMigrated (t * testing.T ) {
173
+ DB .Migrator ().DropTable (& Animal {})
174
+ DB .AutoMigrate (& Animal {})
175
+
176
+ cols , _ := DB .Migrator ().ColumnTypes (& Animal {})
177
+ for _ , c := range cols {
178
+ if c .Name () == "unexported" {
179
+ t .Errorf ("unexported field should not be a DB column" )
180
+ }
181
+ }
182
+ }
183
+
184
+ func TestBatchInsertDefaults (t * testing.T ) {
185
+ DB .Migrator ().DropTable (& Animal {})
186
+ DB .AutoMigrate (& Animal {})
187
+
188
+ animals := []Animal {{From : "x" }, {From : "y" }}
189
+ DB .Create (& animals )
190
+
191
+ for _ , a := range animals {
192
+ if a .Counter == 0 {
193
+ t .Errorf ("Counter should be set for batch insert, got 0" )
194
+ }
195
+ if a .Name != "galeone" {
196
+ t .Errorf ("Name should default to 'galeone', got %v" , a .Name )
197
+ }
198
+ }
199
+ }
200
+
201
+ func TestUpdatedAtChangesOnUpdate (t * testing.T ) {
202
+ DB .Migrator ().DropTable (& Animal {})
203
+ DB .AutoMigrate (& Animal {})
204
+
205
+ animal := Animal {Name : "Ferdinand" }
206
+ DB .Create (& animal )
207
+ updatedAt1 := animal .UpdatedAt
208
+
209
+ DB .Model (& animal ).Update ("name" , "Francis" )
210
+ if updatedAt1 .Format (time .RFC3339Nano ) == animal .UpdatedAt .Format (time .RFC3339Nano ) {
211
+ t .Errorf ("UpdatedAt should be updated" )
212
+ }
213
+ }
0 commit comments