-
Notifications
You must be signed in to change notification settings - Fork 66
/
ormmysql_test_m.lua
1529 lines (1294 loc) · 49.9 KB
/
ormmysql_test_m.lua
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
local skynet = require "skynet"
local ormtable = require "skynet-fly.db.orm.ormtable"
local ormadapter_mysql = require "skynet-fly.db.ormadapter.ormadapter_mysql"
local math_util = require "skynet-fly.utils.math_util"
local string_util = require "skynet-fly.utils.string_util"
local table_util = require "skynet-fly.utils.table_util"
local mysqlf = require "skynet-fly.db.mysqlf"
local log = require "skynet-fly.log"
local assert = assert
local CMD = {}
local function delete_table()
mysqlf:instance("admin"):query("drop table if exists t_player")
end
--测试创建表
local function test_create_table(is_del)
local adapter = ormadapter_mysql:new("admin")
local orm_obj = ormtable:new("t_player")
:int64("player_id")
:int64("role_id")
:int8("sex")
:string32("nickname")
:string64("email")
:uint8("sex1")
:int16("sex2")
:uint16("sex3")
:int32("sex4")
:uint32("sex5")
:int64("sex6")
:string128("sex7")
:string256("sex8")
:string512("sex9")
:string1024("sex10")
:string2048("sex11")
:string4096("sex12")
:string8192("sex13")
:text("sex14")
:blob("sex15")
:set_keys("player_id","role_id","sex")
:builder(adapter)
local sqlret = mysqlf:instance("admin"):query("DESCRIBE t_player")
for _, info in pairs(sqlret) do
info.Type = info.Type:gsub("(%a*)int%(%d+%)", "%1int")
end
assert(not sqlret.err,sqlret.err)
assert(sqlret[1].Field == 'player_id' and sqlret[1].Type == 'bigint' and sqlret[1].Key == 'PRI')
assert(sqlret[2].Field == 'role_id' and sqlret[2].Type == 'bigint' and sqlret[2].Key == 'PRI')
assert(sqlret[3].Field == 'sex' and sqlret[3].Type == 'tinyint' and sqlret[3].Key == 'PRI')
assert(sqlret[4].Field == 'nickname' and sqlret[4].Type == 'varchar(32)')
assert(sqlret[5].Field == 'email' and sqlret[5].Type == 'varchar(64)')
assert(sqlret[6].Field == 'sex1' and sqlret[6].Type == 'tinyint unsigned')
assert(sqlret[7].Field == 'sex2' and sqlret[7].Type == 'smallint')
assert(sqlret[8].Field == 'sex3' and sqlret[8].Type == 'smallint unsigned')
assert(sqlret[9].Field == 'sex4' and sqlret[9].Type == 'int')
assert(sqlret[10].Field == 'sex5' and sqlret[10].Type == 'int unsigned')
assert(sqlret[11].Field == 'sex6' and sqlret[11].Type == 'bigint')
assert(sqlret[12].Field == 'sex7' and sqlret[12].Type == 'varchar(128)')
assert(sqlret[13].Field == 'sex8' and sqlret[13].Type == 'varchar(256)')
assert(sqlret[14].Field == 'sex9' and sqlret[14].Type == 'varchar(512)')
assert(sqlret[15].Field == 'sex10' and sqlret[15].Type == 'varchar(1024)')
assert(sqlret[16].Field == 'sex11' and sqlret[16].Type == 'varchar(2048)')
assert(sqlret[17].Field == 'sex12' and sqlret[17].Type == 'varchar(4096)')
assert(sqlret[18].Field == 'sex13' and sqlret[18].Type == 'varchar(8192)')
assert(sqlret[19].Field == 'sex14' and sqlret[19].Type == 'text')
assert(sqlret[20].Field == 'sex15' and sqlret[20].Type == 'blob')
if is_del then
delete_table()
end
return orm_obj
end
--测试修改表
local function test_alter_table()
test_create_table()
local adapter = ormadapter_mysql:new("admin")
local orm_obj = ormtable:new("t_player")
:int64("player_id")
:int64("role_id")
:int8("sex")
:int8("nickname1")
:set_keys("player_id","role_id","sex")
:builder(adapter)
local sqlret = mysqlf:instance("admin"):query("DESCRIBE t_player")
for _, info in pairs(sqlret) do
info.Type = info.Type:gsub("(%a*)int%(%d+%)", "%1int")
end
assert(not sqlret.err,sqlret.err)
assert(sqlret[1].Field == 'player_id' and sqlret[1].Type == 'bigint' and sqlret[1].Key == 'PRI')
assert(sqlret[2].Field == 'role_id' and sqlret[2].Type == 'bigint' and sqlret[2].Key == 'PRI')
assert(sqlret[3].Field == 'sex' and sqlret[3].Type == 'tinyint' and sqlret[3].Key == 'PRI')
assert(sqlret[4].Field == 'nickname' and sqlret[4].Type == 'varchar(32)')
assert(sqlret[5].Field == 'email' and sqlret[5].Type == 'varchar(64)')
assert(sqlret[6].Field == 'sex1' and sqlret[6].Type == 'tinyint unsigned')
assert(sqlret[7].Field == 'sex2' and sqlret[7].Type == 'smallint')
assert(sqlret[8].Field == 'sex3' and sqlret[8].Type == 'smallint unsigned')
assert(sqlret[9].Field == 'sex4' and sqlret[9].Type == 'int')
assert(sqlret[10].Field == 'sex5' and sqlret[10].Type == 'int unsigned')
assert(sqlret[11].Field == 'sex6' and sqlret[11].Type == 'bigint')
assert(sqlret[12].Field == 'sex7' and sqlret[12].Type == 'varchar(128)')
assert(sqlret[13].Field == 'sex8' and sqlret[13].Type == 'varchar(256)')
assert(sqlret[14].Field == 'sex9' and sqlret[14].Type == 'varchar(512)')
assert(sqlret[15].Field == 'sex10' and sqlret[15].Type == 'varchar(1024)')
assert(sqlret[16].Field == 'sex11' and sqlret[16].Type == 'varchar(2048)')
assert(sqlret[17].Field == 'sex12' and sqlret[17].Type == 'varchar(4096)')
assert(sqlret[18].Field == 'sex13' and sqlret[18].Type == 'varchar(8192)')
assert(sqlret[19].Field == 'sex14' and sqlret[19].Type == 'text')
assert(sqlret[20].Field == 'sex15' and sqlret[20].Type == 'blob')
assert(sqlret[21].Field == 'nickname1' and sqlret[21].Type == 'tinyint')
delete_table()
end
--测试新增数据
local function test_create_entry()
local orm_obj = test_create_table()
--新建单条数据
local new_data = {player_id = 10001,role_id = 1, sex = 1}
local entry = orm_obj:create_one_entry(new_data)
assert(entry)
assert(entry:get('player_id') == 10001)
assert(entry:get('role_id') == 1)
assert(entry:get('sex') == 1)
assert(entry:get('nickname') == "") --没有设置的string 会默认给个空 string
assert(entry:get('sex1') == 0) --没有设置的number 会默认给个 0
--主键冲突
local new_data = {player_id = 10001,role_id = 1, sex = 1}
local isok, res = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
--缺少主键数据
local new_data = {player_id = 10001,role_id = 2}
local isok,res = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok) --会崩溃报错
--新建多条数据
local new_data_list = {
{player_id = 10002,role_id = 1, sex = 1},
{player_id = 10002,role_id = 2, sex = 1},
{player_id = 10002,role_id = 3, sex = 1}
}
local res = orm_obj:create_entry(new_data_list)
assert(#res == 3)
for i,v in pairs(res) do
assert(v)
end
-- 新增数据值范围要合理
local new_data = {player_id = 100055,role_id = 1, sex = -128}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local new_data = {player_id = 10005,role_id = 1, sex = -129}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local new_data = {player_id = 10006,role_id = 1, sex = 128}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local new_data = {player_id = 100066,role_id = 1, sex = 127}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local new_data = {player_id = 10007,role_id = 1, sex = 1, sex1 = 256}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local new_data = {player_id = 100077,role_id = 1, sex = 1, sex1 = 255}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local new_data = {player_id = 10008,role_id = 1, sex = 1, sex1 = -1}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local new_data = {player_id = 100088,role_id = 1, sex = 1, sex1 = 0}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local new_data = {player_id = 11007,role_id = 1, sex = 1, sex2 = 32768}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local new_data = {player_id = 110077,role_id = 1, sex = 1, sex2 = 32767}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local new_data = {player_id = 11008,role_id = 1, sex = 1, sex2 = -32769}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local new_data = {player_id = 110088,role_id = 1, sex = 1, sex2 = -32768}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local new_data = {player_id = 12007,role_id = 1, sex = 1, sex3 = 65536}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local new_data = {player_id = 120077,role_id = 1, sex = 1, sex3 = 65535}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local new_data = {player_id = 12008,role_id = 1, sex = 1, sex3 = -1}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local new_data = {player_id = 120088,role_id = 1, sex = 1, sex3 = 0}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local new_data = {player_id = 13007,role_id = 1, sex = 1, sex4 = 2147483648}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local new_data = {player_id = 130077,role_id = 1, sex = 1, sex4 = 2147483647}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local new_data = {player_id = 13008,role_id = 1, sex = 1, sex4 = -2147483649}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local new_data = {player_id = 130088,role_id = 1, sex = 1, sex4 = -2147483648}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local new_data = {player_id = 14007,role_id = 1, sex = 1, sex5 = 4294967296}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local new_data = {player_id = 140077,role_id = 1, sex = 1, sex5 = 4294967295}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local new_data = {player_id = 14008,role_id = 1, sex = 1, sex5 = -1}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local new_data = {player_id = 140088,role_id = 1, sex = 1, sex5 = 0}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local new_data = {player_id = 15007,role_id = 1, sex = 1, sex6 = 9223372036854775808}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local new_data = {player_id = 150077,role_id = 1, sex = 1, sex6 = 9223372036854775807}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local new_data = {player_id = 15008,role_id = 1, sex = 1, sex6 = -9223372036854775809}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok and type(err) == 'table') --插入成功 数据会被修正为-9223372036854775808
local new_data = {player_id = 150088,role_id = 1, sex = 1, sex6 = -9223372036854775808}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok and type(err) == 'table') --插入成功
local test_str = ""
for i = 1,33 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 10009,role_id = 1, sex = 1, nickname = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local test_str = ""
for i = 1,32 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 100099,role_id = 1, sex = 1, nickname = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local test_str = ""
for i = 1,65 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 10010,role_id = 1, sex = 1, email = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local test_str = ""
for i = 1,64 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 100100,role_id = 1, sex = 1, email = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local test_str = ""
for i = 1,129 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 10011,role_id = 1, sex = 1, sex7 = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local test_str = ""
for i = 1,128 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 100111,role_id = 1, sex = 1, sex7 = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local test_str = ""
for i = 1,257 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 10012,role_id = 1, sex = 1, sex8 = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local test_str = ""
for i = 1,256 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 100122,role_id = 1, sex = 1, sex8 = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local test_str = ""
for i = 1,513 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 10013,role_id = 1, sex = 1, sex9 = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local test_str = ""
for i = 1,512 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 100133,role_id = 1, sex = 1, sex9 = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local test_str = ""
for i = 1,1025 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 10014,role_id = 1, sex = 1, sex10 = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local test_str = ""
for i = 1,1024 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 100144,role_id = 1, sex = 1, sex10 = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local test_str = ""
for i = 1,2049 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 10015,role_id = 1, sex = 1, sex11 = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local test_str = ""
for i = 1,2048 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 100155,role_id = 1, sex = 1, sex11 = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local test_str = ""
for i = 1,4097 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 10016,role_id = 1, sex = 1, sex12 = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local test_str = ""
for i = 1,4096 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 100166,role_id = 1, sex = 1, sex12 = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
local test_str = ""
for i = 1,8193 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 10017,role_id = 1, sex = 1, sex13 = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok)
local test_str = ""
for i = 1,8192 do
test_str = test_str .. 'i'
end
local new_data = {player_id = 100177,role_id = 1, sex = 1, sex13 = test_str}
local isok,err = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(isok)
delete_table()
end
--测试查询数据
local function test_select_entry()
local orm_obj = test_create_table()
--新建多条数据
local new_data_list = {
{player_id = 10002,role_id = 1, sex = 1},
{player_id = 10002,role_id = 2, sex = 1},
{player_id = 10002,role_id = 2, sex = 2},
{player_id = 10002,role_id = 3, sex = 2},
{player_id = 10003,role_id = 1, sex = 1},
{player_id = 10003,role_id = 2, sex = 1},
{player_id = 10003,role_id = 2, sex = 2},
{player_id = 10003,role_id = 3, sex = 2},
}
local res = orm_obj:create_entry(new_data_list)
assert(#res == 8)
for i,v in pairs(res) do
assert(v)
end
--通过player_id查询
local entry_list = orm_obj:get_entry(10002)
assert(#entry_list == 4) --有四条数据
for i,entry in ipairs(entry_list) do
assert(entry:get('player_id') == new_data_list[i].player_id)
assert(entry:get('role_id') == new_data_list[i].role_id)
assert(entry:get('sex') == new_data_list[i].sex)
end
--通过player_id 加 role_id 查询
local entry_list = orm_obj:get_entry(10002,2)
assert(#entry_list == 2) --有2条数据
--通过player_id 加 role_id 加 sex 查询
local entry_list = orm_obj:get_entry(10002,2,1)
assert(#entry_list == 1) --有1条数据
--查询不存在的数据
local entry_list = orm_obj:get_entry(10004)
assert(#entry_list == 0)
local entry_list = orm_obj:get_entry(10004,1)
assert(#entry_list == 0)
local entry_list = orm_obj:get_entry(10004,1,1)
assert(#entry_list == 0)
--查询版本一用,版本二不用,版本三再用,字段取出不能为nil
--版本二只有3个主键
local adapter = ormadapter_mysql:new("admin")
local orm_obj = ormtable:new("t_player")
:int64("player_id")
:int64("role_id")
:int8("sex")
:set_keys("player_id","role_id","sex")
:builder(adapter)
local res = orm_obj:create_one_entry({player_id = 10004,role_id = 1, sex = 1})
assert(res)
--版本三又用回来
orm_obj = test_create_table()
local entry_list = orm_obj:get_entry(10004)
local entry = assert(entry_list[1])
assert(entry:get('nickname') == "")
assert(entry:get('sex4') == 0)
delete_table()
end
--测试变更保存数据
local function test_save_entry()
delete_table()
local orm_obj = test_create_table()
local entry = orm_obj:create_one_entry({player_id = 10004,role_id = 1, sex = 1, nickname = "ddasda", sex1 = 222})
--主键值不能修改
local isok = pcall(entry.set,entry,'player_id',1000)
assert(not isok)
local isok = pcall(entry.set,entry,'role_id',1000)
assert(not isok)
local isok = pcall(entry.set,entry,'sex',2)
assert(not isok)
--修改数据范围要合理
local isok = pcall(entry.set,entry,'sex1',256)
assert(not isok)
local isok = pcall(entry.set,entry,'sex1',-1)
assert(not isok)
local isok = pcall(entry.set,entry,'nickname',"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")
assert(not isok)
--修改单条数据
entry:set('nickname',"abcde")
entry:set('sex1',111)
local res = orm_obj:save_one_entry(entry)
assert(res)
local entry_list = orm_obj:get_entry(10004)
local entry = assert(entry_list[1])
assert(entry:get('nickname') == "abcde")
assert(entry:get('sex1') == 111)
--修改多条数据
local res = orm_obj:create_entry({
{player_id = 11001,role_id = 1, sex = 1, nickname = "abcd", sex1 = 1},
{player_id = 11002,role_id = 1, sex = 1, nickname = "efgh", sex1 = 2}
})
local entry1 = assert(res[1])
entry1:set('nickname',"abcde")
entry1:set('sex1',111)
local entry2 = assert(res[2])
entry2:set('nickname',"efghg")
entry2:set('sex1',222)
local res = orm_obj:save_entry({entry1, entry2})
assert(res[1])
assert(res[2])
local res = orm_obj:get_entry(11001)
entry1 = assert(res[1])
local res = orm_obj:get_entry(11002)
entry2 = assert(res[1])
assert(entry1:get('nickname') == "abcde")
assert(entry1:get('sex1') == 111)
assert(entry2:get('nickname') == "efghg")
assert(entry2:get('sex1') == 222)
delete_table()
end
--测试删除数据
local function test_delete_entry()
local orm_obj = test_create_table()
local res = orm_obj:create_entry({
{player_id = 10004,role_id = 1, sex = 1, nickname = "ddasda", sex1 = 222},
{player_id = 10004,role_id = 1, sex = 2, nickname = "ddasda", sex1 = 223},
{player_id = 10004,role_id = 1, sex = 3, nickname = "ddasda", sex1 = 224},
{player_id = 10004,role_id = 2, sex = 1, nickname = "ddasda", sex1 = 222},
{player_id = 10004,role_id = 2, sex = 2, nickname = "ddasda", sex1 = 223},
{player_id = 10004,role_id = 2, sex = 3, nickname = "ddasda", sex1 = 224},
{player_id = 10004,role_id = 3, sex = 3, nickname = "ddasda", sex1 = 224},
{player_id = 10005,role_id = 2, sex = 1, nickname = "ddasda", sex1 = 224}
})
--删除一条数据 (使用三个关联唯一key)
local res = orm_obj:delete_entry(10004, 1, 1)
assert(res)
local entry_list = orm_obj:get_entry(10004, 1, 1)
assert(not next(entry_list))
--应该还有两条数据
local entry_list = orm_obj:get_entry(10004, 1)
assert(#entry_list == 2)
--删除数据 (使用 2个 关联 key)
local res = orm_obj:delete_entry(10004, 1)
assert(res)
--应该还有四条数据
local entry_list = orm_obj:get_entry(10004)
assert(#entry_list == 4)
-- 删除数据 (使用 1个 关联 key)
local res = orm_obj:delete_entry(10004)
assert(res)
--应该没有条数据
local entry_list = orm_obj:get_entry(10004)
assert(#entry_list == 0)
--10005应该还存在
local entry_list = orm_obj:get_entry(10005)
assert(#entry_list == 1)
delete_table()
end
--测试本地缓存
local function test_cache_entry()
local adapter = ormadapter_mysql:new("admin")
local orm_obj = ormtable:new("t_player")
:int64("player_id")
:int64("role_id")
:int8("sex")
:string32("nickname")
:string64("email")
:uint8("sex1")
:set_keys("player_id","role_id","sex")
:set_cache(500, 100) --缓存5秒
:builder(adapter)
--创建entry 建立缓存
local entry = orm_obj:create_one_entry({player_id = 10001, role_id = 1, sex = 1})
local get_entry_list = orm_obj:get_entry(10001)
local get_entry = assert(get_entry_list[1])
assert(entry == get_entry) --是同一个表
skynet.sleep(600)
--缓存过期
local gg_entry_list,is_cache = orm_obj:get_entry(10001)
local gg_entry = assert(gg_entry_list[1])
assert(not is_cache)
assert(entry ~= gg_entry) --不是同一个表
--查询建立了缓存,再次查询应命中缓存
local reget_entry_list,is_cache = orm_obj:get_entry(10001)
local reget_entry = assert(reget_entry_list[1])
assert(gg_entry == reget_entry)
assert(is_cache)
skynet.sleep(300)
--查询延迟缓存时间 重置为5秒
local rr_list = orm_obj:get_entry(10001)
skynet.sleep(300)
local rrr_list,is_cache = orm_obj:get_entry(10001)
local rrr_entry = assert(rrr_list[1])
assert(rrr_entry == reget_entry)
assert(is_cache)
-- 多条数据命中缓存
local entry_list = orm_obj:create_entry({
{player_id = 10002, role_id = 1, sex = 1},
{player_id = 10002, role_id = 2, sex = 1},
{player_id = 10002, role_id = 3, sex = 1},
{player_id = 10002, role_id = 1, sex = 2},
{player_id = 10002, role_id = 2, sex = 2},
{player_id = 10002, role_id = 3, sex = 2},
{player_id = 10003, role_id = 1, sex = 1},
{player_id = 10003, role_id = 2, sex = 1},
{player_id = 10003, role_id = 3, sex = 1},
{player_id = 10003, role_id = 1, sex = 2},
{player_id = 10003, role_id = 2, sex = 2},
{player_id = 10003, role_id = 3, sex = 2}
})
--两个关联key
local get_entry_list = orm_obj:get_entry(10002, 1)
assert(entry_list[1] == get_entry_list[1])
assert(entry_list[4] == get_entry_list[2])
--1个关联key
local gg_entry_list = orm_obj:get_entry(10002)
assert(entry_list[1] == gg_entry_list[1])
-- 多条数据中 有数据缓存过期查询应重拉数据
skynet.sleep(600)
--两个关联key
local ggg_entry_list = orm_obj:get_entry(10002, 1)
skynet.sleep(300)
orm_obj:get_entry(10002, 1, 1) --保活一条数据
skynet.sleep(300) --3秒后过期1条数据
local gggg_entry_list,is_cache = orm_obj:get_entry(10002, 1) --拉取新的
--1因为缓存还在 2缓存不在
assert(ggg_entry_list[1] == gggg_entry_list[1])
assert(ggg_entry_list[2] ~= gggg_entry_list[2])
assert(not is_cache)
skynet.sleep(600)
--1个关联key
local ggg_entry_list = orm_obj:get_entry(10002)
skynet.sleep(300)
orm_obj:get_entry(10002, 1, 1) --保活一条数据
skynet.sleep(300) --3秒后过期5条数据
local gggg_entry_list,is_cache = orm_obj:get_entry(10002) --拉取新的
assert(ggg_entry_list[1] == gggg_entry_list[1])
assert(ggg_entry_list[2] ~= gggg_entry_list[2])
assert(not is_cache)
--删除数据后拿取关联多条,应命中缓存
local entry_list = orm_obj:get_entry(10003)
orm_obj:delete_entry(10003, 3, 2)
local gg_entry_list,is_cache = orm_obj:get_entry(10003)
assert(is_cache)
delete_table()
end
--测试定期自动保存数据
local function test_inval_save()
delete_table()
local adapter = ormadapter_mysql:new("admin")
local orm_obj = ormtable:new("t_player")
:int64("player_id")
:int64("role_id")
:int8("sex")
:string32("nickname")
:string64("email")
:uint8("sex1")
:set_keys("player_id","role_id","sex")
:set_cache(500,100) --1保存一次
:builder(adapter)
-- 自动保存数据
local entry_list = orm_obj:create_entry({
{player_id = 10002, role_id = 1, sex = 1},
{player_id = 10002, role_id = 2, sex = 1},
{player_id = 10002, role_id = 3, sex = 1},
{player_id = 10002, role_id = 1, sex = 2},
{player_id = 10002, role_id = 2, sex = 2},
{player_id = 10002, role_id = 3, sex = 2},
{player_id = 10003, role_id = 1, sex = 1},
{player_id = 10003, role_id = 2, sex = 1},
{player_id = 10003, role_id = 3, sex = 1},
{player_id = 10003, role_id = 1, sex = 2},
{player_id = 10003, role_id = 2, sex = 2},
{player_id = 10003, role_id = 3, sex = 2}
})
for i,entry in ipairs(entry_list) do
entry:set("email", "emailssss")
end
skynet.sleep(1000)
local entry_list = orm_obj:get_entry(10002)
for i,entry in ipairs(entry_list) do
local email = entry:get("email")
assert(email == "emailssss")
end
delete_table()
end
--测试定期保存数据,数据库挂了之后再启动,数据应该还能落地
local function test_sql_over()
delete_table()
local adapter = ormadapter_mysql:new("admin")
local orm_obj = ormtable:new("t_player")
:int64("player_id")
:int64("role_id")
:int8("sex")
:string32("nickname")
:string64("email")
:uint8("sex1")
:set_keys("player_id","role_id","sex")
:set_cache(500,500) --5秒保存一次
:builder(adapter)
-- 自动保存数据
local entry_list = orm_obj:create_entry({
{player_id = 10002, role_id = 1, sex = 1},
{player_id = 10002, role_id = 2, sex = 1},
{player_id = 10002, role_id = 3, sex = 1},
{player_id = 10002, role_id = 1, sex = 2},
{player_id = 10002, role_id = 2, sex = 2},
{player_id = 10002, role_id = 3, sex = 2},
{player_id = 10003, role_id = 1, sex = 1},
{player_id = 10003, role_id = 2, sex = 1},
{player_id = 10003, role_id = 3, sex = 1},
{player_id = 10003, role_id = 1, sex = 2},
{player_id = 10003, role_id = 2, sex = 2},
{player_id = 10003, role_id = 3, sex = 2}
})
for i,entry in ipairs(entry_list) do
entry:set("email", "emailssss")
end
--杀掉数据库
os.execute("pkill mysql")
log.info("杀掉数据库》》》》》》》》》》》》》")
skynet.sleep(6000)
os.execute("systemctl start mysql")
log.info("启动数据库》》》》》》》》》》》》》")
--等待缓存过期
log.info("等待缓存过期》》》》》》》》》》》》》")
skynet.sleep(1000)
log.info("缓存过期》》》》》》》》》》》》》")
local entry_list = orm_obj:get_entry(10002)
for i,entry in ipairs(entry_list) do
local email = entry:get("email")
assert(email == "emailssss")
end
log.info("保存成功》》》》》》》》》》》》》")
delete_table()
end
--测试对象没用后的定时器清除
local function test_over_clear_time()
delete_table()
local adapter = ormadapter_mysql:new("admin")
local orm_obj = ormtable:new("t_player")
:int64("player_id")
:int64("role_id")
:int8("sex")
:string32("nickname")
:string64("email")
:uint8("sex1")
:set_keys("player_id","role_id","sex")
:set_cache(500,500) --5秒保存一次
:builder(adapter)
local time_obj = orm_obj._time_obj
orm_obj = nil
collectgarbage("collect")
collectgarbage("collect")
assert(time_obj.is_cancel == true)
delete_table()
end
--测试缓存不过期
local function test_permanent()
delete_table()
local adapter = ormadapter_mysql:new("admin")
local orm_obj = ormtable:new("t_player")
:int64("player_id")
:int64("role_id")
:int8("sex")
:string32("nickname")
:string64("email")
:uint8("sex1")
:set_keys("player_id","role_id","sex")
:set_cache(0,500) --5秒保存一次
:builder(adapter)
local entry = orm_obj:create_one_entry({
player_id = 10001,
role_id = 1,
sex = 1
})
skynet.sleep(1)
local get_entry_List,is_cache = orm_obj:get_entry(10001)
assert(is_cache)
local g_entry = get_entry_List[1]
assert(entry == g_entry)
delete_table()
end
--测试查询所有数据
local function test_get_all()
delete_table()
local adapter = ormadapter_mysql:new("admin")
local orm_obj = ormtable:new("t_player")
:int64("player_id")
:int64("role_id")
:int8("sex")
:string32("nickname")
:string64("email")
:uint8("sex1")
:set_keys("player_id","role_id","sex")
:set_cache(500,500) --5秒保存一次
:builder(adapter)
local entry_list = orm_obj:create_entry({
{player_id = 10002, role_id = 1, sex = 1},
{player_id = 10002, role_id = 2, sex = 1},
{player_id = 10002, role_id = 3, sex = 1},
{player_id = 10002, role_id = 1, sex = 2},
{player_id = 10002, role_id = 2, sex = 2},
{player_id = 10002, role_id = 3, sex = 2},
{player_id = 10003, role_id = 1, sex = 1},
{player_id = 10003, role_id = 2, sex = 1},
{player_id = 10003, role_id = 3, sex = 1},
{player_id = 10003, role_id = 1, sex = 2},
{player_id = 10003, role_id = 2, sex = 2},
{player_id = 10003, role_id = 3, sex = 2}
})
local get_entry_list,is_cache = orm_obj:get_all_entry()
assert(not is_cache and #get_entry_list == #entry_list)--首次查询,不确定缓存数量与实际数量是否一致
local get_entry_list,is_cache = orm_obj:get_all_entry()
assert(is_cache and #get_entry_list == #entry_list)--首次查询,第二次查询应命中缓存
skynet.sleep(300)
local get_entry_list,is_cache = orm_obj:get_entry(10002) --保活
assert(not is_cache)
skynet.sleep(300)
assert(orm_obj._key_cache_count == 6 and orm_obj._key_cache_total_count == nil)
local get_entry_list,is_cache = orm_obj:get_all_entry()
assert(not is_cache)
assert(orm_obj._key_cache_count == 12 and orm_obj._key_cache_total_count == 12)
orm_obj:delete_entry(10002, 1, 1)
assert(orm_obj._key_cache_count == 11 and orm_obj._key_cache_total_count == 11)
orm_obj:create_one_entry({player_id = 10004, role_id = 3, sex = 2})
assert(orm_obj._key_cache_count == 12 and orm_obj._key_cache_total_count == 12)
delete_table()
end
--测试删除所有数据
local function test_delete_all()
delete_table()
local adapter = ormadapter_mysql:new("admin")
local orm_obj = ormtable:new("t_player")
:int64("player_id")
:int64("role_id")
:int8("sex")
:string32("nickname")
:string64("email")
:uint8("sex1")
:set_keys("player_id","role_id","sex")
:set_cache(500,500) --5秒保存一次
:builder(adapter)
local entry_list = orm_obj:create_entry({
{player_id = 10002, role_id = 1, sex = 1},
{player_id = 10002, role_id = 2, sex = 1},
{player_id = 10002, role_id = 3, sex = 1},
{player_id = 10002, role_id = 1, sex = 2},
{player_id = 10002, role_id = 2, sex = 2},
{player_id = 10002, role_id = 3, sex = 2},
{player_id = 10003, role_id = 1, sex = 1},
{player_id = 10003, role_id = 2, sex = 1},
{player_id = 10003, role_id = 3, sex = 1},
{player_id = 10003, role_id = 1, sex = 2},
{player_id = 10003, role_id = 2, sex = 2},
{player_id = 10003, role_id = 3, sex = 2}
})
orm_obj:delete_all_entry()
assert(orm_obj._key_cache_count == 0 and orm_obj._key_cache_total_count == 0)
local entry_list = orm_obj:get_all_entry()
assert(#entry_list == 0)
local entry = orm_obj:create_one_entry({player_id = 10002, role_id = 1, sex = 1})
assert(orm_obj._key_cache_count == 1 and orm_obj._key_cache_total_count == 1)
local entry_list = orm_obj:get_all_entry()
assert(#entry_list == 1)
delete_table()
end
-- 测试创建单条数据
local function test_craete_one()
delete_table()
local orm_obj = test_create_table()
--新建单条数据
local new_data = {player_id = 10001,role_id = 1, sex = 1}
local entry = orm_obj:create_one_entry(new_data)
assert(entry)
assert(entry:get('player_id') == 10001)
assert(entry:get('role_id') == 1)
assert(entry:get('sex') == 1)
assert(entry:get('nickname') == "") --没有设置的string 会默认给个空 string
assert(entry:get('sex1') == 0) --没有设置的number 会默认给个 0
--主键冲突
local new_data = {player_id = 10001,role_id = 1, sex = 1}
local isok, err = pcall(orm_obj.create_one_entry, orm_obj, new_data)
assert(not isok)
--缺少主键数据
local new_data = {player_id = 10001,role_id = 2}
local isok,res = pcall(orm_obj.create_one_entry,orm_obj,new_data)
assert(not isok) --会崩溃报错
delete_table()
end