forked from latolukasz/beeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflusher_test.go
1408 lines (1320 loc) · 52 KB
/
flusher_test.go
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package beeorm
import (
"context"
"encoding/json"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type obj struct {
ID uint64
StorageKey string
Data interface{}
}
type flushStruct struct {
Name2 string
Age int
Sub flushSubStruct
TestTime *time.Time `orm:"time=true"`
}
type flushSubStruct struct {
Name3 string
Age3 int
}
type flushStructAnonymous struct {
SubName string
SubAge float32 `orm:"decimal=9,5;unsigned=false"`
}
var TestSet = struct {
D string
E string
F string
}{
D: "d",
E: "e",
F: "f",
}
type benchmarkIsDirtyEntity struct {
ORM
ID uint32 `orm:"unique=ID"`
Token string `orm:"unique=Token"`
Name string `orm:"length=60;index=unique_name"`
CreatedAt time.Time `orm:"time=true;index=index_created_at"`
LastActiveAt time.Time `orm:"time=true;index=index_last_active_at"`
Guild *flushEntity ``
GuildRank string `orm:"enum=beeorm.TestSet"`
Lang string `orm:"length=2"`
CountryCode string `orm:"length=8"`
Level uint8 `orm:"index=index_level"`
Exp uint32 ``
Gold uint64 `orm:"index=index_gold"`
Premium uint64 `orm:"index=index_premium"`
Energy uint16 ``
Heropower uint32 `orm:"index=index_heropower"`
HeropowerMax uint32 ``
IsMale bool ``
PremiumPurchased uint32 `orm:"index=index_premium_purchased"`
LastPurchaseAt *time.Time `orm:"time=true"`
MaxInventorySize uint16 ``
NextDailybonus *time.Time `orm:"time=true"`
DailybonusTaken uint16 ``
NextDailyquestsReset *time.Time `orm:"time=true"`
DailytasksPoints uint16 ``
DailytasksRewardTaken uint16 ``
BankGoldTime *time.Time `orm:"time=true"`
BankWatchadsUsed uint8 ``
SexChanges uint8 ``
NickChanges uint8 ``
FlashNotPayLongDays uint8 ``
SpinsPremiumBuyDone uint8 ``
FreeNormalSpins uint8 ``
SpinWatchadsUsed uint8 ``
EventSpinDone1 uint16 ``
EventSpinDone2 uint16 ``
EventSpinDone3 uint16 ``
EventSpinDone4 uint16 ``
EventSpinDone5 uint16 ``
EventSpinDone6 uint16 ``
SpinEventSlotMul1 uint8 ``
SpinEventSlotMul2 uint8 ``
SpinEventSlotMul3 uint8 ``
SpinEventSlotMul4 uint8 ``
SpinEventSlotMul5 uint8 ``
SpinEventSlotMul6 uint8 ``
ShopReloadTime *time.Time `orm:"time=true"`
ShopReloadsDone uint8 ``
ShopHasEventContent bool ``
ShopItem1ID *flushEntity `orm:"skip_FK"`
ShopItem2ID *flushEntity `orm:"skip_FK"`
ShopItem3ID *flushEntity `orm:"skip_FK"`
ShopItem4ID *flushEntity `orm:"skip_FK"`
ShopItem5ID *flushEntity `orm:"skip_FK"`
ShopItem6ID *flushEntity `orm:"skip_FK"`
ShopItem7ID *flushEntity `orm:"skip_FK"`
ShopItem8ID *flushEntity `orm:"skip_FK"`
ShopItem1Amount uint8 ``
ShopItem2Amount uint8 ``
ShopItem3Amount uint8 ``
ShopItem4Amount uint8 ``
ShopItem5Amount uint8 ``
ShopItem6Amount uint8 ``
ShopItem7Amount uint8 ``
ShopItem8Amount uint8 ``
BodyHairID *flushEntity `orm:"skip_FK"`
BodyEyesID *flushEntity `orm:"skip_FK"`
BodyEyebrowsID *flushEntity `orm:"skip_FK"`
BodyMouthID *flushEntity `orm:"skip_FK"`
BodyFacespecialID *flushEntity `orm:"skip_FK"`
BodyFacespecial2ID *flushEntity `orm:"skip_FK"`
BodyFacialhairID *flushEntity `orm:"skip_FK"`
BodyEarsID *flushEntity `orm:"skip_FK"`
BodyNoseID *flushEntity `orm:"skip_FK"`
BodySkinColor uint32 ``
BodyHairColor uint32 ``
NextChestBronze *time.Time `orm:"time=true"`
BronzeChestLimit uint8 ``
NextChestSilver *time.Time `orm:"time=true"`
NextChestGold *time.Time `orm:"time=true"`
NextChestGoldLegendary uint8 ``
NextChestEvent1 *time.Time `orm:"time=true"`
NextChestEvent2 *time.Time `orm:"time=true"`
NextChestEvent3 *time.Time `orm:"time=true"`
NextChestEvent4 *time.Time `orm:"time=true"`
NextChestEvent5 *time.Time `orm:"time=true"`
NextChestEvent6 *time.Time `orm:"time=true"`
NextChestEvent7 *time.Time `orm:"time=true"`
NextChestEvent8 *time.Time `orm:"time=true"`
NextChestEvent9 *time.Time `orm:"time=true"`
NextChestEvent10 *time.Time `orm:"time=true"`
NextChestEvent11 *time.Time `orm:"time=true"`
NextChestEvent12 *time.Time `orm:"time=true"`
NextChestEvent13 *time.Time `orm:"time=true"`
NextChestEvent14 *time.Time `orm:"time=true"`
NextChestEvent15 *time.Time `orm:"time=true"`
NextChestEventLegendary1 uint8 ``
NextChestEventLegendary2 uint8 ``
NextChestEventLegendary3 uint8 ``
NextChestEventLegendary4 uint8 ``
NextChestEventLegendary5 uint8 ``
NextChestEventLegendary6 uint8 ``
NextChestEventLegendary7 uint8 ``
NextChestEventLegendary8 uint8 ``
NextChestEventLegendary9 uint8 ``
NextChestEventLegendary10 uint8 ``
NextChestEventLegendary11 uint8 ``
NextChestEventLegendary12 uint8 ``
NextChestEventLegendary13 uint8 ``
NextChestEventLegendary14 uint8 ``
NextChestEventLegendary15 uint8 ``
JourneyJourney1ID *flushEntity `orm:"skip_FK"`
JourneyJourney2ID *flushEntity `orm:"skip_FK"`
JourneyJourney3ID *flushEntity `orm:"skip_FK"`
JourneyJourney4ID *flushEntity `orm:"skip_FK"`
JourneyJourney5ID *flushEntity `orm:"skip_FK"`
JourneyNameSeed uint8
JourneyCurrentID *flushEntity `orm:"skip_FK"`
JourneyEnd *time.Time `orm:"time=true"`
JourneyReloadTime *time.Time `orm:"time=true"`
JourneyReloadsDone uint16 ``
JourneyWatchadsUsed uint8 ``
EnergyRenewUsed uint16 ``
EnergyBuyUsed uint8 ``
EnergyWatchadsUsed uint8 ``
EnergyLastCalculated time.Time `orm:"time=true"`
LastExtractRecipeID uint32 ``
Collection1UnlockedSlots uint8 ``
Collection2UnlockedSlots uint8 ``
Collection1Slot1Upg uint32 ``
Collection1Slot2Upg uint32 ``
Collection1Slot3Upg uint32 ``
Collection1Slot4Upg uint32 ``
Collection1Slot5Upg uint32 ``
Collection1Slot6Upg uint32 ``
Collection1Slot7Upg uint32 ``
Collection1Slot8Upg uint32 ``
Collection1Slot9Upg uint32 ``
Collection1Slot10Upg uint32 ``
Collection2Slot1Upg uint32 ``
Collection2Slot2Upg uint32 ``
Collection2Slot3Upg uint32 ``
Collection2Slot4Upg uint32 ``
Collection2Slot5Upg uint32 ``
Collection2Slot6Upg uint32 ``
Collection2Slot7Upg uint32 ``
Collection2Slot8Upg uint32 ``
Collection2Slot9Upg uint32 ``
Collection2Slot10Upg uint32 ``
NextStageAutobattle *time.Time `orm:"time=true"`
FirstChestFree uint16 ``
FirstChestPaid uint16 ``
NoobEventProgress uint8 ``
NoobRewardsTaken uint8 ``
UnreadMessages uint16 ``
MessagesIDAutoincrement uint32 ``
GlobalMessageIDx uint32 ``
LastQuestsFullcheckTs uint32 ``
TavernEnergyFlags uint8 ``
LastTavernEnergyUse *time.Time `orm:"time=true"`
TutorialStep uint32 ``
UtcOffset int32 ``
CacheDamageTotal uint32 ``
CacheDefenceTotal uint32 ``
CacheHpTotal uint32 ``
CacheMagicTotal uint32 ``
CacheCritvalTotal float64 ``
CacheCritchanceTotal uint16 ``
CacheDodgeTotal uint16 ``
SecretCode string `orm:"length=9"`
SecretCodesByothers uint32 `orm:"index=index_secret_codes_byothers"`
SecretCodesByme uint32 ``
SecretCodesByothersRewardsTaken uint32 ``
SecretCodesBymeRewardsTaken uint32 ``
WatchAdsViewed uint8 ``
ReforgeSeed uint32 ``
OfferBaseNo uint8 ``
OfferCycleNo uint8 ``
OfferCycleExpire *time.Time `orm:"time=true"`
OfferProduct1ID *flushEntity `orm:"skip_FK"`
OfferProduct2ID *flushEntity `orm:"skip_FK"`
OfferProduct3ID *flushEntity `orm:"skip_FK"`
OfferProduct4ID *flushEntity `orm:"skip_FK"`
OfferProduct5ID *flushEntity `orm:"skip_FK"`
OfferProduct6ID *flushEntity `orm:"skip_FK"`
OfferProduct7ID *flushEntity `orm:"skip_FK"`
OfferProduct8ID *flushEntity `orm:"skip_FK"`
OfferProduct1Expire *time.Time `orm:"time=true"`
OfferProduct2Expire *time.Time `orm:"time=true"`
OfferProduct3Expire *time.Time `orm:"time=true"`
OfferProduct4Expire *time.Time `orm:"time=true"`
OfferProduct5Expire *time.Time `orm:"time=true"`
OfferProduct6Expire *time.Time `orm:"time=true"`
OfferProduct7Expire *time.Time `orm:"time=true"`
OfferProduct8Expire *time.Time `orm:"time=true"`
OfferBase2BuyDailyLimit uint8 ``
ChBossID uint32 ``
ChBossEnergy uint32 ``
ChBossMaxTurnsToday uint32 ``
ChBossInflictedDamageMax uint32 `orm:"index=index_ch_boss_inflicted_damage_max"`
ChBossInflictedDamageToday uint32 ``
ChBossLeagueIndex uint32 ``
CurrentEventID uint32 ``
BattleEventID uint32 ``
Options uint32 ``
ItemIndexPoints uint32 ``
GuildJoinedAt *time.Time `orm:"time=true"`
GuildTreasureDonateBits uint8 ``
GuildFamePoints uint32 ``
GuildFameLevel uint8 ``
GuildStatsWeekNo uint8 ``
GuildShortName string `orm:"length=9"`
BattleEventMilestoneTaken uint64 ``
BattleEventGuildMilestoneTaken uint64 ``
Ars1Expire *time.Time `orm:"time=true"`
Ars1NextBonus *time.Time `orm:"time=true"`
ArsPlatform uint8 ``
GuildshopDailyCount uint8 ``
AutoBattleDailyUsed uint32 ``
BattleEventBossWarriorTicketsLastCalculated *time.Time `orm:"time=true"`
BattleEventBossMageTicketsLastCalculated *time.Time `orm:"time=true"`
BattleEventBossGuildTicketsLastCalculated *time.Time `orm:"time=true"`
BattleEventBossMultiTicketsLastCalculated *time.Time `orm:"time=true"`
LastIDleClaim *time.Time `orm:"time=true"`
VipNewRewardsSent bool ``
FlagPack uint32 ``
ItemExtractRenewTime *time.Time `orm:"time=true"`
TowerCurrentStage uint32 ``
Banned bool ``
BoughtsCount uint32 ``
BoughtsSum float64 ``
LastGuildCreation *time.Time `orm:"time=true"`
SessionStart time.Time `orm:"time=true"`
CachedIndexID *CachedQuery `queryOne:":ID= ?" json:"-"`
CachedIndexToken *CachedQuery `queryOne:":Token = ?" json:"-"`
ABTestsIDsCache []uint16 `orm:"ignore"`
CacheOnly flushSubStruct `orm:"ignore"`
AdminLabel string `orm:"ignore"`
AdminIcon string `orm:"ignore"`
}
type attributesValues map[uint64][]interface{}
func (av attributesValues) UnmarshalJSON(data []byte) error {
temp := map[uint64][]interface{}{}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
for attributeID, values := range temp {
valuesNew := make([]interface{}, len(values))
for i, value := range values {
if _, ok := value.(string); ok {
valuesNew[i] = value
} else {
valuesNew[i] = uint64(value.(float64))
}
}
av[attributeID] = valuesNew
}
return nil
}
type flushEntity struct {
ORM `orm:"localCache;redisCache;dirty=entity_changed"`
ID uint
City string `orm:"unique=city"`
Name string `orm:"unique=name;required"`
NameTranslated map[string]string
Age int
Uint uint
UintNullable *uint
IntNullable *int
Year uint16 `orm:"year"`
YearNullable *uint16 `orm:"year"`
BoolNullable *bool
FloatNullable *float64 `orm:"precision=10"`
Float32Nullable *float32 `orm:"precision=4"`
ReferenceOne *flushEntityReference `orm:"unique=ReferenceOne"`
ReferenceTwo *flushEntityReference `orm:"unique=ReferenceTwo"`
ReferenceMany []*flushEntityReference
ReferenceManyRequired []*flushEntityReference `orm:"required"`
StringSlice []string
StringSliceNotNull []string `orm:"required"`
SetNullable []string `orm:"set=beeorm.TestSet"`
SetNotNull []string `orm:"set=beeorm.TestSet;required"`
EnumNullable string `orm:"enum=beeorm.TestEnum"`
EnumNotNull string `orm:"enum=beeorm.TestEnum;required"`
Ignored []string `orm:"ignore"`
Blob []uint8
Bool bool
FakeDelete bool
Float64 float64 `orm:"precision=10"`
Decimal float64 `orm:"decimal=5,2"`
DecimalNullable *float64 `orm:"decimal=5,2"`
Float64Default float64 `orm:"unsigned"`
Float64Signed float64
CachedQuery *CachedQuery
Time time.Time
TimeWithTime time.Time `orm:"time"`
TimeNullable *time.Time
TimeWithTimeNullable *time.Time `orm:"time"`
Interface interface{}
FlushStruct flushStruct
FlushStructPtr *flushStruct
Int8Nullable *int8
Int16Nullable *int16
Int32Nullable *int32
Int64Nullable *int64
Uint8Nullable *uint8
Uint16Nullable *uint16
Uint32Nullable *uint32
Uint64Nullable *uint64
Images []obj
AttributesValues attributesValues
flushStructAnonymous
}
type flushEntityReference struct {
ORM `orm:"localCache;redisCache"`
ID uint
Name string
Age int
}
type flushEntityBenchmark struct {
ORM `orm:"localCache;redisCache"`
ID uint
Name string
Age int
}
func TestFlushLocalRedis(t *testing.T) {
testFlush(t, true, true)
}
func TestFlushLocal(t *testing.T) {
testFlush(t, true, false)
}
func TestFlushNoCache(t *testing.T) {
testFlush(t, false, false)
}
func TestFlushRedis(t *testing.T) {
testFlush(t, false, true)
}
func testFlush(t *testing.T, local bool, redis bool) {
var entity *flushEntity
var reference *flushEntityReference
registry := &Registry{}
registry.RegisterRedisStream("entity_changed", "default", []string{"test-group-1"})
registry.RegisterEnumStruct("beeorm.TestEnum", TestEnum)
registry.RegisterEnumStruct("beeorm.TestSet", TestSet)
engine, def := prepareTables(t, registry, 5, "", "2.0", entity, reference)
defer def()
schema := engine.registry.GetTableSchemaForEntity(entity).(*tableSchema)
schema2 := engine.registry.GetTableSchemaForEntity(reference).(*tableSchema)
if !local {
schema.hasLocalCache = false
schema.localCacheName = ""
schema2.hasLocalCache = false
schema2.localCacheName = ""
}
if !redis {
schema.hasRedisCache = false
schema.redisCacheName = ""
schema2.hasRedisCache = false
schema2.redisCacheName = ""
}
now := time.Now()
entity = &flushEntity{Name: "Tom", Age: 12, Uint: 7, Year: 1982}
entity.NameTranslated = map[string]string{"pl": "kot", "en": "cat"}
entity.ReferenceOne = &flushEntityReference{Name: "John", Age: 30}
entity.ReferenceMany = []*flushEntityReference{{Name: "Adam", Age: 18}}
entity.StringSlice = []string{"a", "b"}
entity.StringSliceNotNull = []string{"c", "d"}
entity.SetNotNull = []string{"d", "e"}
entity.FlushStructPtr = &flushStruct{"A", 12, flushSubStruct{Age3: 11, Name3: "G"}, nil}
entity.EnumNotNull = "a"
entity.FlushStruct.Name2 = "Ita"
entity.FlushStruct.Sub.Age3 = 13
entity.FlushStruct.Sub.Name3 = "Nanami"
entity.FlushStruct.TestTime = &now
entity.TimeWithTime = now
entity.Float64 = 2.12
entity.Decimal = 6.15
entity.TimeWithTimeNullable = &now
entity.Images = []obj{{ID: 1, StorageKey: "aaa", Data: map[string]string{"sss": "vv", "bb": "cc"}}}
entity.flushStructAnonymous = flushStructAnonymous{"Adam", 39.123}
entity.AttributesValues = attributesValues{12: []interface{}{"a", "b"}}
assert.True(t, entity.IsDirty())
assert.True(t, entity.ReferenceOne.IsDirty())
flusher := engine.NewFlusher().Track(entity)
flusher.Track(entity)
flusher.Flush()
flusher.Flush()
assert.True(t, entity.IsLoaded())
assert.True(t, entity.ReferenceOne.IsLoaded())
assert.False(t, entity.IsDirty())
assert.False(t, entity.ReferenceOne.IsDirty())
assert.Equal(t, uint(1), entity.ID)
assert.NotEqual(t, uint(0), entity.ReferenceOne.ID)
assert.True(t, entity.IsLoaded())
assert.True(t, entity.ReferenceOne.IsLoaded())
assert.NotEqual(t, uint(0), entity.ReferenceMany[0].ID)
assert.True(t, entity.ReferenceMany[0].IsLoaded())
refOneID := entity.ReferenceOne.ID
refManyID := entity.ReferenceMany[0].ID
entity = &flushEntity{}
found := engine.LoadByID(2, entity)
assert.False(t, found)
found = engine.LoadByID(1, entity)
assert.True(t, found)
assert.Equal(t, "Tom", entity.Name)
assert.Equal(t, 12, entity.Age)
assert.Equal(t, uint(7), entity.Uint)
assert.Equal(t, uint16(1982), entity.Year)
assert.Equal(t, map[string]string{"pl": "kot", "en": "cat"}, entity.NameTranslated)
assert.Equal(t, attributesValues{12: []interface{}{"a", "b"}}, entity.AttributesValues)
assert.Equal(t, []string{"a", "b"}, entity.StringSlice)
assert.Equal(t, []string{"c", "d"}, entity.StringSliceNotNull)
assert.Equal(t, "", entity.EnumNullable)
assert.Equal(t, "a", entity.EnumNotNull)
assert.Equal(t, now.Format(timeFormat), entity.TimeWithTime.Format(timeFormat))
assert.Equal(t, now.Unix(), entity.TimeWithTime.Unix())
assert.Equal(t, now.Format(timeFormat), entity.TimeWithTimeNullable.Format(timeFormat))
assert.Equal(t, now.Unix(), entity.TimeWithTimeNullable.Unix())
assert.Nil(t, entity.SetNullable)
assert.Equal(t, "", entity.City)
assert.NotNil(t, entity.FlushStructPtr)
assert.Equal(t, "A", entity.FlushStructPtr.Name2)
assert.Equal(t, 12, entity.FlushStructPtr.Age)
assert.Equal(t, "G", entity.FlushStructPtr.Sub.Name3)
assert.Equal(t, 11, entity.FlushStructPtr.Sub.Age3)
assert.Equal(t, now.Format(timeFormat), entity.FlushStruct.TestTime.Format(timeFormat))
assert.Nil(t, entity.UintNullable)
assert.Nil(t, entity.IntNullable)
assert.Nil(t, entity.YearNullable)
assert.Nil(t, entity.BoolNullable)
assert.Nil(t, entity.FloatNullable)
assert.Nil(t, entity.Float32Nullable)
assert.False(t, entity.IsDirty())
assert.True(t, entity.IsLoaded())
assert.False(t, entity.ReferenceOne.IsLoaded())
assert.Equal(t, refOneID, entity.ReferenceOne.ID)
assert.Nil(t, entity.Blob)
assert.Nil(t, entity.Int8Nullable)
assert.Nil(t, entity.Int16Nullable)
assert.Nil(t, entity.Int32Nullable)
assert.Nil(t, entity.Int64Nullable)
assert.Nil(t, entity.Uint8Nullable)
assert.Nil(t, entity.Uint16Nullable)
assert.Nil(t, entity.Uint32Nullable)
assert.Nil(t, entity.Uint64Nullable)
assert.Equal(t, "Adam", entity.SubName)
assert.Equal(t, float32(39.123), entity.SubAge)
assert.NotNil(t, entity.ReferenceMany)
assert.Len(t, entity.ReferenceMany, 1)
assert.Equal(t, refManyID, entity.ReferenceMany[0].ID)
assert.Equal(t, "Ita", entity.FlushStruct.Name2)
assert.Equal(t, 13, entity.FlushStruct.Sub.Age3)
assert.Equal(t, "Nanami", entity.FlushStruct.Sub.Name3)
entity.FlushStructPtr = nil
engine.Flush(entity)
entity = &flushEntity{}
engine.LoadByID(1, entity)
assert.Nil(t, entity.FlushStructPtr)
entity.ReferenceOne.Name = "John 2"
assert.PanicsWithError(t, fmt.Sprintf("entity is not loaded and can't be updated: beeorm.flushEntityReference [%d]", refOneID), func() {
engine.Flush(entity.ReferenceOne)
})
i := 42
i2 := uint(42)
i3 := uint16(1982)
i4 := false
i5 := 134.345
i6 := true
i7 := int8(4)
i8 := int16(4)
i9 := int32(4)
i10 := int64(4)
i11 := uint8(4)
i12 := uint16(4)
i13 := uint32(4)
i14 := uint64(4)
i15 := float32(134.345)
entity.IntNullable = &i
entity.UintNullable = &i2
entity.Int8Nullable = &i7
entity.Int16Nullable = &i8
entity.Int32Nullable = &i9
entity.Int64Nullable = &i10
entity.Uint8Nullable = &i11
entity.Uint16Nullable = &i12
entity.Uint32Nullable = &i13
entity.Uint64Nullable = &i14
entity.YearNullable = &i3
entity.BoolNullable = &i4
entity.FloatNullable = &i5
entity.Float32Nullable = &i15
entity.City = "New York"
entity.Blob = []uint8("Tom has a house")
entity.Bool = true
entity.BoolNullable = &i6
entity.Float64 = 134.345
entity.Decimal = 134.345
entity.StringSlice = []string{"a"}
entity.DecimalNullable = &entity.Decimal
entity.Interface = map[string]int{"test": 12}
entity.ReferenceMany = nil
engine.Flush(entity)
reference = &flushEntityReference{}
found = engine.LoadByID(uint64(refOneID), reference)
assert.True(t, found)
assert.Equal(t, "John", reference.Name)
assert.Equal(t, 30, reference.Age)
entity = &flushEntity{}
engine.LoadByID(1, entity)
assert.Equal(t, 42, *entity.IntNullable)
assert.Equal(t, int8(4), *entity.Int8Nullable)
assert.Equal(t, int16(4), *entity.Int16Nullable)
assert.Equal(t, int32(4), *entity.Int32Nullable)
assert.Equal(t, int64(4), *entity.Int64Nullable)
assert.Equal(t, uint8(4), *entity.Uint8Nullable)
assert.Equal(t, uint16(4), *entity.Uint16Nullable)
assert.Equal(t, uint32(4), *entity.Uint32Nullable)
assert.Equal(t, uint64(4), *entity.Uint64Nullable)
assert.Equal(t, uint(42), *entity.UintNullable)
assert.Equal(t, uint16(1982), *entity.YearNullable)
assert.True(t, *entity.BoolNullable)
assert.True(t, entity.Bool)
assert.Equal(t, 134.345, *entity.FloatNullable)
assert.Equal(t, float32(134.345), *entity.Float32Nullable)
assert.Equal(t, []string{"a"}, entity.StringSlice)
assert.Equal(t, "New York", entity.City)
assert.Equal(t, []uint8("Tom has a house"), entity.Blob)
assert.Equal(t, 134.345, entity.Float64)
assert.Equal(t, 134.35, entity.Decimal)
assert.Equal(t, 134.35, *entity.DecimalNullable)
assert.Equal(t, float32(39.123), entity.SubAge)
assert.Nil(t, entity.ReferenceMany)
assert.False(t, entity.IsDirty())
assert.False(t, reference.IsDirty())
assert.True(t, reference.IsLoaded())
entity.ReferenceMany = []*flushEntityReference{}
assert.False(t, entity.IsDirty())
engine.Flush(entity)
entity = &flushEntity{}
engine.LoadByID(1, entity)
assert.Nil(t, entity.ReferenceMany)
entity2 := &flushEntity{Name: "Tom", Age: 12, EnumNotNull: "a"}
assert.PanicsWithError(t, "Duplicate entry 'Tom' for key 'name'", func() {
engine.Flush(entity2)
})
entity2.Name = "Lucas"
entity2.ReferenceOne = &flushEntityReference{ID: 3}
assert.PanicsWithError(t, "foreign key error in key `test:flushEntity:ReferenceOne`", func() {
engine.Flush(entity2)
})
entity2.ReferenceOne = nil
entity2.Name = "Tom"
var uIntNullable *uint
entity2.SetOnDuplicateKeyUpdate(Bind{"Age": 40, "Year": 2020, "City": "Moscow", "UintNullable": uIntNullable,
"BoolNullable": nil, "TimeWithTime": now, "Time": now})
engine.Flush(entity2)
assert.Equal(t, uint(1), entity2.ID)
assert.Equal(t, 40, entity2.Age)
entity = &flushEntity{}
engine.LoadByID(1, entity)
assert.Equal(t, "Tom", entity.Name)
assert.Equal(t, "Moscow", entity.City)
assert.Nil(t, entity.UintNullable)
assert.Equal(t, 40, entity.Age)
assert.Equal(t, uint(1), entity.ID)
assert.Equal(t, now.Unix(), entity.TimeWithTime.Unix())
assert.Equal(t, entity.Time.Format(dateformat), now.Format(dateformat))
entity2 = &flushEntity{Name: "Tom", Age: 12, EnumNotNull: "a"}
entity2.SetOnDuplicateKeyUpdate(Bind{})
engine.Flush(entity2)
assert.Equal(t, uint(1), entity2.ID)
entity = &flushEntity{}
engine.LoadByID(1, entity)
assert.Equal(t, uint(1), entity.ID)
entity2 = &flushEntity{Name: "Arthur", Age: 18, EnumNotNull: "a"}
entity2.ReferenceTwo = reference
entity2.SetOnDuplicateKeyUpdate(Bind{})
engine.Flush(entity2)
assert.Equal(t, uint(6), entity2.ID)
entity = &flushEntity{}
engine.LoadByID(6, entity)
assert.Equal(t, uint(6), entity.ID)
engine.LoadByID(1, entity)
entity.Bool = false
now = now.Add(time.Hour * 40)
entity.TimeWithTime = now
entity.Name = ""
entity.IntNullable = nil
entity.EnumNullable = "b"
entity.Blob = nil
engine.Flush(entity)
entity = &flushEntity{}
engine.LoadByID(1, entity)
assert.Equal(t, false, entity.Bool)
assert.Equal(t, now.Format(timeFormat), entity.TimeWithTime.Format(timeFormat))
assert.Equal(t, "", entity.Name)
assert.Equal(t, "b", entity.EnumNullable)
assert.Nil(t, entity.IntNullable)
assert.Nil(t, entity.Blob)
entity.EnumNullable = ""
entity.Blob = []uint8("Tom has a house")
engine.Flush(entity)
entity = &flushEntity{}
engine.LoadByID(1, entity)
assert.Equal(t, "", entity.EnumNullable)
assert.PanicsWithError(t, "empty enum value for EnumNotNull", func() {
entity.EnumNotNull = ""
engine.Flush(entity)
})
entity = &flushEntity{Name: "Cat"}
engine.Flush(entity)
entity = &flushEntity{}
engine.LoadByID(1, entity)
assert.Equal(t, "a", entity.EnumNotNull)
entity2 = &flushEntity{Name: "Adam", Age: 20, ID: 10, EnumNotNull: "a"}
engine.Flush(entity2)
found = engine.LoadByID(10, entity2)
assert.True(t, found)
entity2.Age = 21
entity2.UintNullable = &i2
entity2.BoolNullable = &i4
entity2.FloatNullable = &i5
entity2.City = "War'saw '; New"
assert.True(t, entity2.IsDirty())
engine.Flush(entity2)
assert.False(t, entity2.IsDirty())
engine.LoadByID(10, entity2)
assert.Equal(t, 21, entity2.Age)
entity2.City = "War\\'saw"
engine.Flush(entity2)
engine.LoadByID(10, entity2)
assert.Equal(t, "War\\'saw", entity2.City)
entity2.Time = time.Now()
n := time.Now()
entity2.TimeNullable = &n
engine.Flush(entity2)
engine.LoadByID(10, entity2)
assert.NotNil(t, entity2.Time)
assert.NotNil(t, entity2.TimeNullable)
entity2.UintNullable = nil
entity2.BoolNullable = nil
entity2.FloatNullable = nil
entity2.City = ""
assert.True(t, entity2.IsDirty())
engine.Flush(entity2)
assert.False(t, entity2.IsDirty())
entity2 = &flushEntity{}
engine.LoadByID(10, entity2)
assert.Nil(t, entity2.UintNullable)
assert.Nil(t, entity2.BoolNullable)
assert.Nil(t, entity2.FloatNullable)
assert.Equal(t, "", entity2.City)
entity2.markToDelete()
assert.True(t, entity2.IsDirty())
engine.Delete(entity2)
found = engine.LoadByID(10, entity2)
assert.True(t, found)
assert.True(t, entity2.FakeDelete)
engine.Flush(&flushEntity{Name: "Tom", Age: 12, Uint: 7, Year: 1982, EnumNotNull: "a"})
entity3 := &flushEntity{}
found = engine.LoadByID(11, entity3)
assert.True(t, found)
assert.Nil(t, entity3.NameTranslated)
engine.Flush(&flushEntity{Name: "Eva", SetNullable: []string{}, EnumNotNull: "a"})
entity4 := &flushEntity{}
found = engine.LoadByID(12, entity4)
assert.True(t, found)
assert.Nil(t, entity4.SetNotNull)
assert.Nil(t, entity4.SetNullable)
entity4.SetNullable = []string{"d", "e"}
engine.Flush(entity4)
entity4 = &flushEntity{}
engine.LoadByID(12, entity4)
assert.Equal(t, []string{"d", "e"}, entity4.SetNullable)
engine.GetMysql().Begin()
entity5 := &flushEntity{Name: "test_transaction", EnumNotNull: "a"}
engine.Flush(entity5)
entity5.Age = 38
engine.Flush(entity5)
engine.GetMysql().Commit()
entity5 = &flushEntity{}
found = engine.LoadByID(13, entity5)
assert.True(t, found)
assert.Equal(t, "test_transaction", entity5.Name)
assert.Equal(t, 38, entity5.Age)
entity6 := &flushEntity{Name: "test_transaction_2", EnumNotNull: "a"}
flusher.Clear()
flusher.Flush()
flusher.Track(entity6)
flusher.Flush()
entity6 = &flushEntity{}
found = engine.LoadByID(14, entity6)
assert.True(t, found)
assert.Equal(t, "test_transaction_2", entity6.Name)
entity7 := &flushEntity{Name: "test_check", EnumNotNull: "a"}
flusher.Track(entity7)
err := flusher.FlushWithCheck()
assert.NoError(t, err)
entity7 = &flushEntity{}
found = engine.LoadByID(15, entity7)
assert.True(t, found)
assert.Equal(t, "test_check", entity7.Name)
entity7 = &flushEntity{Name: "test_check", EnumNotNull: "a"}
flusher.Track(entity7)
err = flusher.FlushWithCheck()
assert.EqualError(t, err, "Duplicate entry 'test_check' for key 'name'")
entity7 = &flushEntity{Name: "test_check_2", EnumNotNull: "a", ReferenceOne: &flushEntityReference{ID: 100}}
err = engine.FlushWithCheck(entity7)
assert.EqualError(t, err, "foreign key error in key `test:flushEntity:ReferenceOne`")
entity7 = &flushEntity{Name: "test_check_3", EnumNotNull: "Y"}
flusher.Track(entity7)
err = flusher.FlushWithFullCheck()
assert.EqualError(t, err, "unknown enum value for EnumNotNull - Y")
flusher.Track(entity7)
assert.Panics(t, func() {
_ = flusher.FlushWithCheck()
})
entity8 := &flushEntity{Name: "test_check", EnumNotNull: "a"}
flusher.Track(entity8)
err = flusher.FlushWithCheck()
assert.EqualError(t, err, "Duplicate entry 'test_check' for key 'name'")
assert.PanicsWithError(t, "track limit 10000 exceeded", func() {
for i := 1; i <= 10001; i++ {
flusher.Track(&flushEntity{})
}
})
flusher.Clear()
entity2 = &flushEntity{ID: 100, Name: "Eva", Age: 1, EnumNotNull: "a"}
entity2.SetOnDuplicateKeyUpdate(Bind{"Age": 2})
engine.Flush(entity2)
assert.Equal(t, uint(12), entity2.ID)
assert.Equal(t, 2, entity2.Age)
entity2 = &flushEntity{}
found = engine.LoadByID(100, entity2)
assert.False(t, found)
entity2 = &flushEntity{Name: "Frank", ID: 100, Age: 1, EnumNotNull: "a"}
entity2.SetOnDuplicateKeyUpdate(Bind{"Age": 2})
engine.Flush(entity2)
found = engine.LoadByID(100, entity2)
assert.True(t, found)
assert.Equal(t, 1, entity2.Age)
entity2 = &flushEntity{ID: 100, Age: 1, EnumNotNull: "a"}
entity2.SetOnDuplicateKeyUpdate(Bind{"Age": 2})
engine.Flush(entity2)
assert.Equal(t, uint(100), entity2.ID)
assert.Equal(t, 2, entity2.Age)
entity2 = &flushEntity{}
found = engine.LoadByID(100, entity2)
assert.True(t, found)
assert.Equal(t, 2, entity2.Age)
receiver := NewBackgroundConsumer(engine)
receiver.DisableLoop()
receiver.blockTime = time.Millisecond
testLogger := &testLogHandler{}
engine.RegisterQueryLogger(testLogger, true, false, false)
flusher = engine.NewFlusher()
entity1 := &flushEntity{}
engine.LoadByID(10, entity1)
entity2 = &flushEntity{}
engine.LoadByID(11, entity2)
entity3 = &flushEntity{}
engine.LoadByID(12, entity3)
entity1.Age = 99
entity2.Uint = 99
entity3.Name = "sss"
flusher.Track(entity1, entity2, entity3)
flusher.Flush()
receiver.Digest(context.Background())
if local {
assert.Len(t, testLogger.Logs, 3)
assert.Equal(t, "START TRANSACTION", testLogger.Logs[0]["query"])
assert.Equal(t, "UPDATE `flushEntity` SET `Age`=99 WHERE `ID` = 10;UPDATE `flushEntity` SET `Uint`=99 "+
"WHERE `ID` = 11;UPDATE `flushEntity` SET `Name`='sss' WHERE `ID` = 12;", testLogger.Logs[1]["query"])
assert.Equal(t, "COMMIT", testLogger.Logs[2]["query"])
}
entity = &flushEntity{Name: "Monica", EnumNotNull: "a", ReferenceMany: []*flushEntityReference{{Name: "Adam Junior"}}}
engine.Flush(entity)
assert.Equal(t, uint(101), entity.ID)
assert.Equal(t, uint(3), entity.ReferenceMany[0].ID)
entity = &flushEntity{Name: "John", EnumNotNull: "a", ReferenceMany: []*flushEntityReference{}}
engine.Flush(entity)
entity = &flushEntity{}
engine.LoadByID(102, entity)
assert.Nil(t, entity.ReferenceMany)
entity1 = &flushEntity{}
engine.LoadByID(13, entity1)
entity2 = &flushEntity{}
engine.LoadByID(14, entity2)
entity3 = &flushEntity{}
engine.LoadByID(15, entity3)
engine.LoadByID(1, entity)
engine.ForceDelete(entity)
flusher = engine.NewFlusher()
entity1.ReferenceOne = &flushEntityReference{ID: 1}
entity1.Name = "A"
entity2.ReferenceOne = &flushEntityReference{ID: 2}
entity2.Name = "B"
entity3.ReferenceOne = &flushEntityReference{ID: 3}
entity3.Name = "C"
flusher.Track(entity1, entity2, entity3)
flusher.Flush()
entities := make([]*flushEntity, 0)
engine.LoadByIDs([]uint64{13, 14, 15}, &entities, "ReferenceOne")
flusher = engine.NewFlusher()
for _, e := range entities {
newRef := &flushEntityReference{}
newRef.Name = fmt.Sprintf("%d33", e.ID)
oldRef := e.ReferenceOne
oldRef.Name = fmt.Sprintf("%d34", e.ID)
flusher.Track(oldRef)
e.Name = fmt.Sprintf("%d35", e.ID)
e.ReferenceOne = newRef
flusher.Track(e)
}
flusher.Flush()
entities = make([]*flushEntity, 0)
engine.LoadByIDs([]uint64{13, 14, 15}, &entities, "ReferenceOne")
assert.Equal(t, "1335", entities[0].Name)
assert.Equal(t, "1435", entities[1].Name)
assert.Equal(t, "1535", entities[2].Name)
assert.Equal(t, "1333", entities[0].ReferenceOne.Name)
assert.Equal(t, "1433", entities[1].ReferenceOne.Name)
assert.Equal(t, "1533", entities[2].ReferenceOne.Name)
entitiesRefs := make([]*flushEntityReference, 0)
engine.LoadByIDs([]uint64{1, 2, 3}, &entitiesRefs)
assert.Equal(t, "1334", entitiesRefs[0].Name)
assert.Equal(t, "1434", entitiesRefs[1].Name)
assert.Equal(t, "1534", entitiesRefs[2].Name)
if redis && !local {
testLogger2 := &testLogHandler{}
engine.RegisterQueryLogger(testLogger2, true, true, false)
testLogger.clear()
engine.GetMysql().Begin()
entity4.ReferenceOne = &flushEntityReference{}
engine.Flush(entity4)
engine.GetMysql().Commit()
assert.Len(t, testLogger2.Logs, 5)
assert.Equal(t, "BEGIN", testLogger2.Logs[0]["operation"])
assert.Equal(t, "EXEC", testLogger2.Logs[1]["operation"])
assert.Equal(t, "EXEC", testLogger2.Logs[2]["operation"])
assert.Equal(t, "COMMIT", testLogger2.Logs[3]["operation"])
assert.Equal(t, "PIPELINE EXEC", testLogger2.Logs[4]["operation"])
}
entity = &flushEntity{}
found = engine.LoadByID(6, entity)
entity.FlushStructPtr = &flushStruct{Name2: `okddlk"nokddlkno'kddlkn f;mf jg`}
engine.Flush(entity)
flusher.Clear()
flusher.ForceDelete(entity)
entity = &flushEntity{}
engine.LoadByID(7, entity)
flusher.ForceDelete(entity)
flusher.Flush()
found = engine.LoadByID(6, entity)
assert.False(t, found)
found = engine.LoadByID(7, entity)
assert.False(t, found)
entity = &flushEntity{}
engine.LoadByID(102, entity)
entity.Float64Default = 0.3
assert.True(t, entity.IsDirty())
engine.Flush(entity)
entity = &flushEntity{}
engine.LoadByID(102, entity)
assert.Equal(t, 0.3, entity.Float64Default)
entity.Float64Default = 0.4
entity.Float64Signed = -0.4
assert.True(t, entity.IsDirty())
engine.Flush(entity)
entity = &flushEntity{}
engine.LoadByID(102, entity)
assert.Equal(t, 0.4, entity.Float64Default)
assert.Equal(t, -0.4, entity.Float64Signed)
entity.SetNullable = []string{}
engine.Flush(entity)
entity = &flushEntity{}
engine.LoadByID(102, entity)
assert.Nil(t, nil, entity.SetNullable)
entity.SetNullable = []string{"d"}
engine.Flush(entity)
entity = &flushEntity{}
engine.LoadByID(102, entity)
assert.Equal(t, []string{"d"}, entity.SetNullable)
entity.SetNullable = []string{"f"}
engine.Flush(entity)
entity = &flushEntity{}
engine.LoadByID(102, entity)
assert.Equal(t, []string{"f"}, entity.SetNullable)
entity.SetNullable = []string{"f", "d"}