@@ -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,13 +122,16 @@ 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 )
129131 }
130132
131133 createdAt := time .Date (2020 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )
134+ fmt .Printf ("======= createdAt1 = %v\n " , createdAt .Unix ())
132135 updatedAt := createdAt .Unix ()
133136
134137 data := SerializerStruct {
@@ -168,8 +171,82 @@ func TestSerializer(t *testing.T) {
168171 }
169172}
170173
174+ // Issue 48: https://github.com/oracle-samples/gorm-oracle/issues/48
175+ func TestSerializerBulkInsert (t * testing.T ) {
176+ if _ , ok := schema .GetSerializer ("custom" ); ! ok {
177+ schema .RegisterSerializer ("custom" , NewCustomSerializer ("hello" ))
178+ }
179+ DB .Migrator ().DropTable (adaptorSerializerModel (& SerializerStruct {}))
180+ if err := DB .Migrator ().AutoMigrate (adaptorSerializerModel (& SerializerStruct {})); err != nil {
181+ t .Fatalf ("no error should happen when migrate scanner, valuer struct, got error %v" , err )
182+ }
183+
184+ createdAt := time .Date (2020 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )
185+ updatedAt := createdAt .Unix ()
186+
187+ data := []SerializerStruct {
188+ {
189+ Name : []byte ("jinzhu" ),
190+ Roles : []string {"r1" , "r2" },
191+ Roles3 : & Roles {},
192+ Contracts : map [string ]interface {}{"name" : "jinzhu" , "age" : 10 },
193+ EncryptedString : EncryptedString ("pass" ),
194+ CreatedTime : createdAt .Unix (),
195+ UpdatedTime : & updatedAt ,
196+ JobInfo : Job {
197+ Title : "programmer" ,
198+ Number : 9920 ,
199+ Location : "Kenmawr" ,
200+ IsIntern : false ,
201+ },
202+ CustomSerializerString : "world" ,
203+ },
204+ {
205+ Name : []byte ("john" ),
206+ Roles : []string {"l1" , "l2" },
207+ Roles3 : & Roles {},
208+ Contracts : map [string ]interface {}{"name" : "john" , "age" : 20 },
209+ EncryptedString : EncryptedString ("pass" ),
210+ CreatedTime : createdAt .Unix (),
211+ UpdatedTime : & updatedAt ,
212+ JobInfo : Job {
213+ Title : "manager" ,
214+ Number : 7710 ,
215+ Location : "Redwood City" ,
216+ IsIntern : false ,
217+ },
218+ CustomSerializerString : "foo" ,
219+ },
220+ }
221+
222+ if err := DB .Create (& data ).Error ; err != nil {
223+ t .Fatalf ("failed to create data, got error %v" , err )
224+ }
225+
226+ var result []SerializerStruct
227+ if err := DB .Find (& result ).Error ; err != nil {
228+ t .Fatalf ("failed to query data, got error %v" , err )
229+ }
230+
231+ tests .AssertEqual (t , result , data )
232+
233+ // Update all the "roles" columns to "n1"
234+ if err := DB .Model (& SerializerStruct {}).Where ("\" roles\" IS NOT NULL" ).Update ("roles" , []string {"n1" }).Error ; err != nil {
235+ t .Fatalf ("failed to update data's roles, got error %v" , err )
236+ }
237+
238+ var count int64
239+ if err := DB .Model (& SerializerStruct {}).Where ("\" roles\" = ?" , "n1" ).Count (& count ).Error ; err != nil {
240+ t .Fatalf ("failed to query data, got error %v" , err )
241+ }
242+
243+ tests .AssertEqual (t , count , 2 )
244+ }
245+
171246func TestSerializerZeroValue (t * testing.T ) {
172- schema .RegisterSerializer ("custom" , NewCustomSerializer ("hello" ))
247+ if _ , ok := schema .GetSerializer ("custom" ); ! ok {
248+ schema .RegisterSerializer ("custom" , NewCustomSerializer ("hello" ))
249+ }
173250 DB .Migrator ().DropTable (adaptorSerializerModel (& SerializerStruct {}))
174251 if err := DB .Migrator ().AutoMigrate (adaptorSerializerModel (& SerializerStruct {})); err != nil {
175252 t .Fatalf ("no error should happen when migrate scanner, valuer struct, got error %v" , err )
@@ -200,7 +277,9 @@ func TestSerializerZeroValue(t *testing.T) {
200277}
201278
202279func TestSerializerAssignFirstOrCreate (t * testing.T ) {
203- schema .RegisterSerializer ("custom" , NewCustomSerializer ("hello" ))
280+ if _ , ok := schema .GetSerializer ("custom" ); ! ok {
281+ schema .RegisterSerializer ("custom" , NewCustomSerializer ("hello" ))
282+ }
204283 DB .Migrator ().DropTable (adaptorSerializerModel (& SerializerStruct {}))
205284 if err := DB .Migrator ().AutoMigrate (adaptorSerializerModel (& SerializerStruct {})); err != nil {
206285 t .Fatalf ("no error should happen when migrate scanner, valuer struct, got error %v" , err )
0 commit comments