@@ -60,8 +60,8 @@ type SerializerStruct struct {
6060 Roles3 * Roles `gorm:"serializer:json;not null"`
6161 Contracts map [string ]interface {} `gorm:"serializer:json"`
6262 JobInfo Job `gorm:"type:bytes;serializer:gob"`
63- CreatedTime int64 `gorm:"serializer:unixtime;type:timestamp"` // store time in db, use int as field type
64- UpdatedTime * int64 `gorm:"serializer:unixtime;type:timestamp"` // store time in db, use int as field type
63+ CreatedTime int64 `gorm:"serializer:unixtime;type:timestamp with time zone "` // store time in db, use int as field type
64+ UpdatedTime * int64 `gorm:"serializer:unixtime;type:timestamp with time zone "` // store time in db, use int as field type
6565 CustomSerializerString string `gorm:"serializer:custom"`
6666 EncryptedString EncryptedString
6767}
@@ -122,7 +122,9 @@ func (c *CustomSerializer) Value(ctx context.Context, field *schema.Field, dst r
122122}
123123
124124func TestSerializer (t * testing.T ) {
125- schema .RegisterSerializer ("custom" , NewCustomSerializer ("hello" ))
125+ if _ , ok := schema .GetSerializer ("custom" ); ! ok {
126+ schema .RegisterSerializer ("custom" , NewCustomSerializer ("hello" ))
127+ }
126128 DB .Migrator ().DropTable (adaptorSerializerModel (& SerializerStruct {}))
127129 if err := DB .Migrator ().AutoMigrate (adaptorSerializerModel (& SerializerStruct {})); err != nil {
128130 t .Fatalf ("no error should happen when migrate scanner, valuer struct, got error %v" , err )
@@ -168,8 +170,82 @@ func TestSerializer(t *testing.T) {
168170 }
169171}
170172
173+ // Issue 48: https://github.com/oracle-samples/gorm-oracle/issues/48
174+ func TestSerializerBulkInsert (t * testing.T ) {
175+ if _ , ok := schema .GetSerializer ("custom" ); ! ok {
176+ schema .RegisterSerializer ("custom" , NewCustomSerializer ("hello" ))
177+ }
178+ DB .Migrator ().DropTable (adaptorSerializerModel (& SerializerStruct {}))
179+ if err := DB .Migrator ().AutoMigrate (adaptorSerializerModel (& SerializerStruct {})); err != nil {
180+ t .Fatalf ("no error should happen when migrate scanner, valuer struct, got error %v" , err )
181+ }
182+
183+ createdAt := time .Date (2020 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )
184+ updatedAt := createdAt .Unix ()
185+
186+ data := []SerializerStruct {
187+ {
188+ Name : []byte ("jinzhu" ),
189+ Roles : []string {"r1" , "r2" },
190+ Roles3 : & Roles {},
191+ Contracts : map [string ]interface {}{"name" : "jinzhu" , "age" : 10 },
192+ EncryptedString : EncryptedString ("pass" ),
193+ CreatedTime : createdAt .Unix (),
194+ UpdatedTime : & updatedAt ,
195+ JobInfo : Job {
196+ Title : "programmer" ,
197+ Number : 9920 ,
198+ Location : "Kenmawr" ,
199+ IsIntern : false ,
200+ },
201+ CustomSerializerString : "world" ,
202+ },
203+ {
204+ Name : []byte ("john" ),
205+ Roles : []string {"l1" , "l2" },
206+ Roles3 : & Roles {},
207+ Contracts : map [string ]interface {}{"name" : "john" , "age" : 20 },
208+ EncryptedString : EncryptedString ("pass" ),
209+ CreatedTime : createdAt .Unix (),
210+ UpdatedTime : & updatedAt ,
211+ JobInfo : Job {
212+ Title : "manager" ,
213+ Number : 7710 ,
214+ Location : "Redwood City" ,
215+ IsIntern : false ,
216+ },
217+ CustomSerializerString : "foo" ,
218+ },
219+ }
220+
221+ if err := DB .Create (& data ).Error ; err != nil {
222+ t .Fatalf ("failed to create data, got error %v" , err )
223+ }
224+
225+ var result []SerializerStruct
226+ if err := DB .Find (& result ).Error ; err != nil {
227+ t .Fatalf ("failed to query data, got error %v" , err )
228+ }
229+
230+ tests .AssertEqual (t , result , data )
231+
232+ // Update all the "roles" columns to "n1"
233+ if err := DB .Model (& SerializerStruct {}).Where ("\" roles\" IS NOT NULL" ).Update ("roles" , []string {"n1" }).Error ; err != nil {
234+ t .Fatalf ("failed to update data's roles, got error %v" , err )
235+ }
236+
237+ var count int64
238+ if err := DB .Model (& SerializerStruct {}).Where ("\" roles\" = ?" , "n1" ).Count (& count ).Error ; err != nil {
239+ t .Fatalf ("failed to query data, got error %v" , err )
240+ }
241+
242+ tests .AssertEqual (t , count , 2 )
243+ }
244+
171245func TestSerializerZeroValue (t * testing.T ) {
172- schema .RegisterSerializer ("custom" , NewCustomSerializer ("hello" ))
246+ if _ , ok := schema .GetSerializer ("custom" ); ! ok {
247+ schema .RegisterSerializer ("custom" , NewCustomSerializer ("hello" ))
248+ }
173249 DB .Migrator ().DropTable (adaptorSerializerModel (& SerializerStruct {}))
174250 if err := DB .Migrator ().AutoMigrate (adaptorSerializerModel (& SerializerStruct {})); err != nil {
175251 t .Fatalf ("no error should happen when migrate scanner, valuer struct, got error %v" , err )
@@ -200,7 +276,9 @@ func TestSerializerZeroValue(t *testing.T) {
200276}
201277
202278func TestSerializerAssignFirstOrCreate (t * testing.T ) {
203- schema .RegisterSerializer ("custom" , NewCustomSerializer ("hello" ))
279+ if _ , ok := schema .GetSerializer ("custom" ); ! ok {
280+ schema .RegisterSerializer ("custom" , NewCustomSerializer ("hello" ))
281+ }
204282 DB .Migrator ().DropTable (adaptorSerializerModel (& SerializerStruct {}))
205283 if err := DB .Migrator ().AutoMigrate (adaptorSerializerModel (& SerializerStruct {})); err != nil {
206284 t .Fatalf ("no error should happen when migrate scanner, valuer struct, got error %v" , err )
0 commit comments