forked from nateware/redis-objects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_objects_model_spec.rb
798 lines (701 loc) · 29.4 KB
/
redis_objects_model_spec.rb
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
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'redis/objects'
Redis::Objects.redis = $redis
class Roster
include Redis::Objects
counter :available_slots, :start => 10
counter :pitchers, :limit => :max_pitchers
counter :basic
hash_key :contact_information, :marshal_keys=>{'updated_at'=>true}
lock :resort, :timeout => 2
value :starting_pitcher, :marshal => true
list :player_stats, :marshal => true
set :outfielders, :marshal => true
sorted_set :rank
# global class counters
counter :total_players_online, :global => true
set :all_players_online, :global => true
value :last_player, :global => true
# custom keys
counter :player_totals, :key => 'players/#{username}/total'
list :all_player_stats, :key => 'players:all_stats', :global => true
set :total_wins, :key => 'players:#{id}:all_stats'
value :my_rank, :key => 'players:my_rank:#{username}'
value :weird_key, :key => 'players:weird_key:#{raise}', :global => true
#callable as key
counter :daily, :global => true, :key => Proc.new { |roster| "#{roster.name}:#{Time.now.strftime('%Y-%m-%dT%H')}:daily" }
def initialize(id=1) @id = id end
def id; @id; end
def username; "user#{id}"; end
def max_pitchers; 3; end
end
class VanillaRoster < Roster
# No explicit Redis::Objects
end
class CustomRoster < Roster
include Redis::Objects
counter :basic # Override
counter :special # New
end
class MethodRoster
def increment(attribute, by=1)
42
end
def initialize(id=1) @id = id end
def id; @id; end
end
class CustomMethodRoster < MethodRoster
include Redis::Objects
attr_accessor :counter
counter :basic
end
describe Redis::Objects do
before do
@roster = Roster.new
@roster2 = Roster.new
@roster_1 = Roster.new(1)
@roster_2 = Roster.new(2)
@roster_3 = Roster.new(3)
@vanilla_roster = VanillaRoster.new
@custom_roster = CustomRoster.new
@roster.available_slots.reset
@roster.pitchers.reset
@roster.basic.reset
@roster.resort_lock.clear
@roster.starting_pitcher.delete
@roster.player_stats.clear
@roster.outfielders.clear
@roster.contact_information.clear
@roster_1.outfielders.clear
@roster_2.outfielders.clear
@roster_3.outfielders.clear
@roster.redis.del(UNIONSTORE_KEY)
@roster.redis.del(INTERSTORE_KEY)
@roster.redis.del(DIFFSTORE_KEY)
Roster.total_players_online.reset
Roster.all_player_stats.clear
Roster.all_players_online.clear
Roster.last_player.delete
Roster.weird_key.clear
@roster.player_totals.clear
@roster.all_player_stats.clear
@roster.total_wins.clear
@roster.my_rank.clear
@roster.daily.clear
@custom_roster.basic.reset
@custom_roster.special.reset
end
it "should provide a connection method" do
Roster.redis.should == Redis::Objects.redis
# Roster.redis.should.be.kind_of(Redis)
end
it "should support interpolation of key names" do
@roster.player_totals.incr
@roster.redis.get('players/user1/total').should == '1'
@roster.redis.get('players/#{username}/total').should.be.nil
@roster.all_player_stats << 'a'
@roster.redis.lindex('players:all_stats', 0).should == 'a'
@roster.total_wins << 'a'
# test for interpolation of key names
@roster.redis.smembers('players:#{id}:all_stats').should == []
@roster.redis.smembers('players:1:all_stats').should == ['a']
@roster.my_rank = 'a'
@roster.redis.get('players:my_rank:user1').should == 'a'
Roster.weird_key = 'tuka'
Roster.redis.get('players:weird_key:#{raise}').should == 'tuka'
k = "Roster:#{Time.now.strftime('%Y-%m-%dT%H')}:daily"
@roster.daily.incr
@roster.redis.get(k).should == '1'
end
it "should be able to get/set contact info" do
@roster.contact_information['John_Phone'] = '123415352'
@roster.contact_information['John_Address'] = '123 LANE'
@roster.contact_information['John_Phone'].should == '123415352'
@roster.contact_information['John_Address'].should == '123 LANE'
@roster.contact_information['asdasd'].should.be.nil
@roster.contact_information.size.should == 2
end
it "should be marshalling hash keys" do
@roster.contact_information['updated_at'] = Time.now
@roster.contact_information['updated_at'].class.should == Time
end
it "should create counter accessors" do
[:available_slots, :pitchers, :basic].each do |m|
@roster.respond_to?(m).should == true
end
end
it "should support increment/decrement of counters" do
@roster.available_slots.key.should == 'roster:1:available_slots'
@roster.available_slots.should == 10
# math proxy ops
(@roster.available_slots == 10).should.be.true
(@roster.available_slots <= 10).should.be.true
(@roster.available_slots < 11).should.be.true
(@roster.available_slots > 9).should.be.true
(@roster.available_slots >= 10).should.be.true
"#{@roster.available_slots}".should == "10"
@roster.available_slots.increment.should == 11
@roster.available_slots.increment.should == 12
@roster2.available_slots.increment.should == 13
@roster2.available_slots.increment(2).should == 15
@roster.available_slots.decrement.should == 14
@roster2.available_slots.decrement.should == 13
@roster.available_slots.decrement.should == 12
@roster2.available_slots.decrement(4).should == 8
@roster.available_slots.should == 8
@roster.available_slots.reset.should.be.true
@roster.available_slots.should == 10
@roster.available_slots.reset(15).should.be.true
@roster.available_slots.should == 15
@roster.pitchers.increment.should == 1
@roster.basic.increment.should == 1
@roster2.basic.decrement.should == 0
@roster.basic.get.should == 0
end
it "should support class-level increment/decrement of counters" do
Roster.get_counter(:available_slots, @roster.id).should == 10
Roster.increment_counter(:available_slots, @roster.id).should == 11
Roster.increment_counter(:available_slots, @roster.id, 3).should == 14
Roster.decrement_counter(:available_slots, @roster.id, 2).should == 12
Roster.decrement_counter(:available_slots, @roster.id).should == 11
Roster.reset_counter(:available_slots, @roster.id).should == true
Roster.get_counter(:available_slots, @roster.id).should == 10
Roster.getset_counter(:available_slots, @roster.id, 555).should == 10
Roster.get_counter(:available_slots, @roster.id).should == 555
end
it "should support class-level increment/decrement of global counters" do
Roster.total_players_online.should == 0
Roster.total_players_online.increment.should == 1
Roster.total_players_online.decrement.should == 0
Roster.total_players_online.increment(3).should == 3
Roster.total_players_online.decrement(2).should == 1
Roster.total_players_online.reset.should.be.true
Roster.total_players_online.should == 0
Roster.get_counter(:total_players_online).should == 0
Roster.increment_counter(:total_players_online).should == 1
Roster.increment_counter(:total_players_online, nil, 3).should == 4
Roster.decrement_counter(:total_players_online, nil, 2).should == 2
Roster.decrement_counter(:total_players_online).should == 1
Roster.reset_counter(:total_players_online).should == true
Roster.get_counter(:total_players_online).should == 0
Roster.getset_counter(:total_players_online, nil, 111).should == 0
Roster.get_counter(:total_players_online).should == 111
end
it "should take an atomic block for increment/decrement" do
a = false
@roster.available_slots.should == 10
@roster.available_slots.decr do |cnt|
if cnt >= 0
a = true
end
end
@roster.available_slots.should == 9
a.should.be.true
@roster.available_slots.should == 9
@roster.available_slots.decr do |cnt|
@roster.available_slots.should == 8
false
end
@roster.available_slots.should == 8
@roster.available_slots.should == 8
@roster.available_slots.decr do |cnt|
@roster.available_slots.should == 7
nil # should rewind
end
@roster.available_slots.should == 8
@roster.available_slots.should == 8
@roster.available_slots.incr do |cnt|
if 1 == 2 # should rewind
true
end
end
@roster.available_slots.should == 8
@roster.available_slots.should == 8
@roster.available_slots.incr do |cnt|
@roster.available_slots.should == 9
[]
end
@roster.available_slots.should == 9
@roster.available_slots.should == 9
begin
@roster.available_slots.decr do |cnt|
@roster.available_slots.should == 8
raise 'oops'
end
rescue
end
@roster.available_slots.should == 9
# check return value from the block
value =
@roster.available_slots.decr do |cnt|
@roster.available_slots.should == 8
42
end
value.should == 42
@roster.available_slots.should == 8
end
it "should take an atomic block for increment/decrement class methods" do
a = false
Roster.get_counter(:available_slots, @roster.id).should == 10
Roster.decrement_counter(:available_slots, @roster.id) do |cnt|
if cnt >= 0
a = true
end
end
Roster.get_counter(:available_slots, @roster.id).should == 9
a.should.be.true
Roster.get_counter(:available_slots, @roster.id).should == 9
Roster.decrement_counter(:available_slots, @roster.id) do |cnt|
Roster.get_counter(:available_slots, @roster.id).should == 8
false
end
Roster.get_counter(:available_slots, @roster.id).should == 8
Roster.get_counter(:available_slots, @roster.id).should == 8
Roster.decrement_counter(:available_slots, @roster.id) do |cnt|
Roster.get_counter(:available_slots, @roster.id).should == 7
nil # should rewind
end
Roster.get_counter(:available_slots, @roster.id).should == 8
Roster.get_counter(:available_slots, @roster.id).should == 8
Roster.increment_counter(:available_slots, @roster.id) do |cnt|
if 1 == 2 # should rewind
true
end
end
Roster.get_counter(:available_slots, @roster.id).should == 8
Roster.get_counter(:available_slots, @roster.id).should == 8
Roster.increment_counter(:available_slots, @roster.id) do |cnt|
Roster.get_counter(:available_slots, @roster.id).should == 9
[]
end
Roster.get_counter(:available_slots, @roster.id).should == 9
Roster.get_counter(:available_slots, @roster.id).should == 9
begin
Roster.decrement_counter(:available_slots, @roster.id) do |cnt|
Roster.get_counter(:available_slots, @roster.id).should == 8
raise 'oops'
end
rescue
end
Roster.get_counter(:available_slots, @roster.id).should == 9
# check return value from the block
value =
Roster.decrement_counter(:available_slots, @roster.id) do |cnt|
Roster.get_counter(:available_slots, @roster.id).should == 8
42
end
value.should == 42
Roster.get_counter(:available_slots, @roster.id).should == 8
end
it "should properly throw errors on bad counters" do
error = nil
begin
Roster.increment_counter(:badness, 2)
rescue => error
end
error.should.be.kind_of(NoMethodError)
error = nil
begin
Roster.obtain_lock(:badness, 2){}
rescue => error
end
error.should.be.kind_of(Redis::Objects::UndefinedLock)
error = nil
begin
@roster.available_slots = 42
rescue => error
end
error.should.be.kind_of(NoMethodError)
error = nil
begin
@roster.available_slots += 69
rescue => error
end
error.should.be.kind_of(NoMethodError)
error = nil
begin
@roster.available_slots -= 15
rescue => error
end
error.should.be.kind_of(NoMethodError)
end
it "should support obtain_lock as a class method" do
error = nil
begin
Roster.obtain_lock(:resort, 2) do
Roster.redis.get("roster:2:resort_lock").should.not.be.nil
end
rescue => error
end
error.should.be.nil
Roster.redis.get("roster:2:resort_lock").should.be.nil
end
it "should handle simple values" do
@roster.starting_pitcher.should == nil
@roster.starting_pitcher = 'Trevor Hoffman'
@roster.starting_pitcher.should == 'Trevor Hoffman'
@roster.starting_pitcher.get.should == 'Trevor Hoffman'
@roster.starting_pitcher = 'Tom Selleck'
@roster.starting_pitcher.should == 'Tom Selleck'
@roster.starting_pitcher.del.should == 1
@roster.starting_pitcher.should.be.nil
end
it "should handle complex marshaled values" do
@roster.starting_pitcher.should == nil
@roster.starting_pitcher = {:json => 'data'}
@roster.starting_pitcher.should == {:json => 'data'}
@roster.starting_pitcher.get.should == {:json => 'data'}
@roster.starting_pitcher.del.should == 1
@roster.starting_pitcher.should.be.nil
end
it "should handle lists of simple values" do
@roster.player_stats.should.be.empty
@roster.player_stats << 'a'
@roster.player_stats.should == ['a']
@roster.player_stats.get.should == ['a']
@roster.player_stats.unshift 'b'
@roster.player_stats.to_s.should == 'b, a'
@roster.player_stats.should == ['b','a']
@roster.player_stats.get.should == ['b','a']
@roster.player_stats.push 'c'
@roster.player_stats.should == ['b','a','c']
@roster.player_stats.get.should == ['b','a','c']
@roster.player_stats.first.should == 'b'
@roster.player_stats.last.should == 'c'
@roster.player_stats << 'd'
@roster.player_stats.should == ['b','a','c','d']
@roster.player_stats[1].should == 'a'
@roster.player_stats[0].should == 'b'
@roster.player_stats[2].should == 'c'
@roster.player_stats[3].should == 'd'
@roster.player_stats.include?('c').should.be.true
@roster.player_stats.include?('no').should.be.false
@roster.player_stats.pop.should == 'd'
@roster.player_stats[0].should == @roster.player_stats.at(0)
@roster.player_stats[1].should == @roster.player_stats.at(1)
@roster.player_stats[2].should == @roster.player_stats.at(2)
@roster.player_stats.should == ['b','a','c']
@roster.player_stats.get.should == ['b','a','c']
@roster.player_stats.shift.should == 'b'
@roster.player_stats.should == ['a','c']
@roster.player_stats.get.should == ['a','c']
@roster.player_stats << 'e' << 'f' << 'e'
@roster.player_stats.should == ['a','c','e','f','e']
@roster.player_stats.get.should == ['a','c','e','f','e']
@roster.player_stats.delete('e').should == 2
@roster.player_stats.should == ['a','c','f']
@roster.player_stats.get.should == ['a','c','f']
@roster.player_stats << 'j'
@roster.player_stats.should == ['a','c','f','j']
@roster.player_stats[0..2].should == ['a','c','f']
@roster.player_stats[1, 3].should == ['c','f','j']
@roster.player_stats.length.should == 4
@roster.player_stats.size.should == 4
@roster.player_stats.should == ['a','c','f','j']
@roster.player_stats.get.should == ['a','c','f','j']
i = -1
@roster.player_stats.each do |st|
st.should == @roster.player_stats[i += 1]
end
@roster.player_stats.should == ['a','c','f','j']
@roster.player_stats.get.should == ['a','c','f','j']
@roster.player_stats.each_with_index do |st,i|
st.should == @roster.player_stats[i]
end
@roster.player_stats.should == ['a','c','f','j']
@roster.player_stats.get.should == ['a','c','f','j']
coll = @roster.player_stats.collect{|st| st}
coll.should == ['a','c','f','j']
@roster.player_stats.should == ['a','c','f','j']
@roster.player_stats.get.should == ['a','c','f','j']
@roster.player_stats << 'a'
coll = @roster.player_stats.select{|st| st == 'a'}
coll.should == ['a','a']
@roster.player_stats.should == ['a','c','f','j','a']
@roster.player_stats.get.should == ['a','c','f','j','a']
end
it "should handle sets of simple values" do
@roster.outfielders.should.be.empty
@roster.outfielders << 'a' << 'a' << 'a'
@roster.outfielders.should == ['a']
@roster.outfielders.get.should == ['a']
@roster.outfielders << 'b' << 'b'
@roster.outfielders.to_s.should == 'a, b'
@roster.outfielders.should == ['a','b']
@roster.outfielders.members.should == ['a','b']
@roster.outfielders.get.should == ['a','b']
@roster.outfielders << 'c'
@roster.outfielders.sort.should == ['a','b','c']
@roster.outfielders.get.sort.should == ['a','b','c']
@roster.outfielders.delete('c')
@roster.outfielders.should == ['a','b']
@roster.outfielders.get.sort.should == ['a','b']
@roster.outfielders.length.should == 2
@roster.outfielders.size.should == 2
i = 0
@roster.outfielders.each do |st|
i += 1
end
i.should == @roster.outfielders.length
coll = @roster.outfielders.collect{|st| st}
coll.should == ['a','b']
@roster.outfielders.should == ['a','b']
@roster.outfielders.get.should == ['a','b']
@roster.outfielders << 'c'
@roster.outfielders.member?('c').should.be.true
@roster.outfielders.include?('c').should.be.true
@roster.outfielders.member?('no').should.be.false
coll = @roster.outfielders.select{|st| st == 'c'}
coll.should == ['c']
@roster.outfielders.sort.should == ['a','b','c']
end
it "should handle set intersections and unions" do
@roster_1.outfielders << 'a' << 'b' << 'c' << 'd' << 'e'
@roster_2.outfielders << 'c' << 'd' << 'e' << 'f' << 'g'
@roster_3.outfielders << 'a' << 'd' << 'g' << 'l' << 'm'
@roster_1.outfielders.sort.should == %w(a b c d e)
@roster_2.outfielders.sort.should == %w(c d e f g)
@roster_3.outfielders.sort.should == %w(a d g l m)
(@roster_1.outfielders & @roster_2.outfielders).sort.should == ['c','d','e']
@roster_1.outfielders.intersection(@roster_2.outfielders).sort.should == ['c','d','e']
@roster_1.outfielders.intersection(@roster_2.outfielders, @roster_3.outfielders).sort.should == ['d']
@roster_1.outfielders.intersect(@roster_2.outfielders).sort.should == ['c','d','e']
@roster_1.outfielders.inter(@roster_2.outfielders, @roster_3.outfielders).sort.should == ['d']
@roster_1.outfielders.interstore(INTERSTORE_KEY, @roster_2.outfielders).should == 3
@roster_1.redis.smembers(INTERSTORE_KEY).sort.should == ['c','d','e']
@roster_1.outfielders.interstore(INTERSTORE_KEY, @roster_2.outfielders, @roster_3.outfielders).should == 1
@roster_1.redis.smembers(INTERSTORE_KEY).sort.should == ['d']
(@roster_1.outfielders | @roster_2.outfielders).sort.should == ['a','b','c','d','e','f','g']
(@roster_1.outfielders + @roster_2.outfielders).sort.should == ['a','b','c','d','e','f','g']
@roster_1.outfielders.union(@roster_2.outfielders).sort.should == ['a','b','c','d','e','f','g']
@roster_1.outfielders.union(@roster_2.outfielders, @roster_3.outfielders).sort.should == ['a','b','c','d','e','f','g','l','m']
@roster_1.outfielders.unionstore(UNIONSTORE_KEY, @roster_2.outfielders).should == 7
@roster_1.redis.smembers(UNIONSTORE_KEY).sort.should == ['a','b','c','d','e','f','g']
@roster_1.outfielders.unionstore(UNIONSTORE_KEY, @roster_2.outfielders, @roster_3.outfielders).should == 9
@roster_1.redis.smembers(UNIONSTORE_KEY).sort.should == ['a','b','c','d','e','f','g','l','m']
end
it "should handle class-level global lists of simple values" do
Roster.all_player_stats.should.be.empty
Roster.all_player_stats << 'a'
Roster.all_player_stats.should == ['a']
Roster.all_player_stats.get.should == ['a']
Roster.all_player_stats.unshift 'b'
Roster.all_player_stats.to_s.should == 'b, a'
Roster.all_player_stats.should == ['b','a']
Roster.all_player_stats.get.should == ['b','a']
Roster.all_player_stats.push 'c'
Roster.all_player_stats.should == ['b','a','c']
Roster.all_player_stats.get.should == ['b','a','c']
Roster.all_player_stats.first.should == 'b'
Roster.all_player_stats.last.should == 'c'
Roster.all_player_stats << 'd'
Roster.all_player_stats.should == ['b','a','c','d']
Roster.all_player_stats[1].should == 'a'
Roster.all_player_stats[0].should == 'b'
Roster.all_player_stats[2].should == 'c'
Roster.all_player_stats[3].should == 'd'
Roster.all_player_stats.include?('c').should.be.true
Roster.all_player_stats.include?('no').should.be.false
Roster.all_player_stats.pop.should == 'd'
Roster.all_player_stats[0].should == Roster.all_player_stats.at(0)
Roster.all_player_stats[1].should == Roster.all_player_stats.at(1)
Roster.all_player_stats[2].should == Roster.all_player_stats.at(2)
Roster.all_player_stats.should == ['b','a','c']
Roster.all_player_stats.get.should == ['b','a','c']
Roster.all_player_stats.shift.should == 'b'
Roster.all_player_stats.should == ['a','c']
Roster.all_player_stats.get.should == ['a','c']
Roster.all_player_stats << 'e' << 'f' << 'e'
Roster.all_player_stats.should == ['a','c','e','f','e']
Roster.all_player_stats.get.should == ['a','c','e','f','e']
Roster.all_player_stats.delete('e').should == 2
Roster.all_player_stats.should == ['a','c','f']
Roster.all_player_stats.get.should == ['a','c','f']
Roster.all_player_stats << 'j'
Roster.all_player_stats.should == ['a','c','f','j']
Roster.all_player_stats[0..2].should == ['a','c','f']
Roster.all_player_stats[1, 3].should == ['c','f','j']
Roster.all_player_stats.length.should == 4
Roster.all_player_stats.size.should == 4
Roster.all_player_stats.should == ['a','c','f','j']
Roster.all_player_stats.get.should == ['a','c','f','j']
i = -1
Roster.all_player_stats.each do |st|
st.should == Roster.all_player_stats[i += 1]
end
Roster.all_player_stats.should == ['a','c','f','j']
Roster.all_player_stats.get.should == ['a','c','f','j']
Roster.all_player_stats.each_with_index do |st,i|
st.should == Roster.all_player_stats[i]
end
Roster.all_player_stats.should == ['a','c','f','j']
Roster.all_player_stats.get.should == ['a','c','f','j']
coll = Roster.all_player_stats.collect{|st| st}
coll.should == ['a','c','f','j']
Roster.all_player_stats.should == ['a','c','f','j']
Roster.all_player_stats.get.should == ['a','c','f','j']
Roster.all_player_stats << 'a'
coll = Roster.all_player_stats.select{|st| st == 'a'}
coll.should == ['a','a']
Roster.all_player_stats.should == ['a','c','f','j','a']
Roster.all_player_stats.get.should == ['a','c','f','j','a']
end
it "should handle class-level global sets of simple values" do
Roster.all_players_online.should.be.empty
Roster.all_players_online << 'a' << 'a' << 'a'
Roster.all_players_online.should == ['a']
Roster.all_players_online.get.should == ['a']
Roster.all_players_online << 'b' << 'b'
Roster.all_players_online.to_s.should == 'a, b'
Roster.all_players_online.should == ['a','b']
Roster.all_players_online.members.should == ['a','b']
Roster.all_players_online.get.should == ['a','b']
Roster.all_players_online << 'c'
Roster.all_players_online.sort.should == ['a','b','c']
Roster.all_players_online.get.sort.should == ['a','b','c']
Roster.all_players_online.delete('c')
Roster.all_players_online.should == ['a','b']
Roster.all_players_online.get.sort.should == ['a','b']
Roster.all_players_online.length.should == 2
Roster.all_players_online.size.should == 2
i = 0
Roster.all_players_online.each do |st|
i += 1
end
i.should == Roster.all_players_online.length
coll = Roster.all_players_online.collect{|st| st}
coll.should == ['a','b']
Roster.all_players_online.should == ['a','b']
Roster.all_players_online.get.should == ['a','b']
Roster.all_players_online << 'c'
Roster.all_players_online.member?('c').should.be.true
Roster.all_players_online.include?('c').should.be.true
Roster.all_players_online.member?('no').should.be.false
coll = Roster.all_players_online.select{|st| st == 'c'}
coll.should == ['c']
Roster.all_players_online.sort.should == ['a','b','c']
end
it "should handle class-level global values" do
Roster.last_player.should == nil
Roster.last_player = 'Trevor Hoffman'
Roster.last_player.should == 'Trevor Hoffman'
Roster.last_player.get.should == 'Trevor Hoffman'
Roster.last_player = 'Tom Selleck'
Roster.last_player.should == 'Tom Selleck'
Roster.last_player.del.should == 1
Roster.last_player.should.be.nil
end
it "should easily enable @object.class.global_objects" do
@roster.class.all_players_online.should.be.empty
@roster.class.all_players_online << 'a' << 'a' << 'a'
@roster.class.all_players_online.should == ['a']
@roster2.class.all_players_online.should == ['a']
@roster.all_players_online.should == ['a']
@roster2.all_players_online.should == ['a']
@roster.class.all_player_stats.should.be.empty
@roster.class.all_player_stats << 'a'
@roster.class.all_player_stats.should == ['a']
@roster.class.all_player_stats.get.should == ['a']
@roster.class.all_player_stats.unshift 'b'
@roster.class.all_player_stats.to_s.should == 'b, a'
@roster.class.all_player_stats.should == ['b','a']
@roster2.class.all_player_stats.should == ['b','a']
@roster.all_player_stats.should == ['b','a']
@roster2.all_player_stats.should == ['b','a']
@roster2.all_player_stats << 'b'
@roster.all_player_stats.should == ['b','a','b']
@roster.last_player.should == nil
@roster.class.last_player = 'Trevor Hoffman'
@roster.last_player.should == 'Trevor Hoffman'
@roster.last_player.get.should == 'Trevor Hoffman'
@roster2.last_player.get.should == 'Trevor Hoffman'
@roster2.last_player = 'Tom Selleck'
@roster.last_player.should == 'Tom Selleck'
@roster.last_player.del.should == 1
@roster.last_player.should.be.nil
@roster2.last_player.should.be.nil
end
it "should handle lists of complex data types" do
@roster.player_stats << {:json => 'data'}
@roster.player_stats << {:json2 => 'data2'}
@roster.player_stats.first.should == {:json => 'data'}
@roster.player_stats.last.should == {:json2 => 'data2'}
@roster.player_stats << [1,2,3,[4,5]]
@roster.player_stats.last.should == [1,2,3,[4,5]]
@roster.player_stats.shift.should == {:json => 'data'}
end
it "should handle sets of complex data types" do
@roster.outfielders << {:a => 1} << {:b => 2}
@roster.outfielders.member?({:b => 2})
@roster.outfielders.members.should == [{:b => 2}, {:a => 1}]
@roster_1.outfielders << {:a => 1} << {:b => 2}
@roster_2.outfielders << {:b => 2} << {:c => 3}
(@roster_1.outfielders & @roster_2.outfielders).should == [{:b => 2}]
(@roster_1.outfielders | @roster_2.outfielders).should == [{:b=>2}, {:c=>3}, {:a=>1}]
end
it "should provide a lock method that accepts a block" do
@roster.resort_lock.key.should == 'roster:1:resort_lock'
a = false
@roster.resort_lock.lock do
a = true
end
a.should.be.true
end
it "should raise an exception if the timeout is exceeded" do
@roster.redis.set(@roster.resort_lock.key, 1)
error = nil
begin
@roster.resort_lock.lock {}
rescue => error
end
error.should.not.be.nil
error.should.be.kind_of(Redis::Lock::LockTimeout)
end
it "should pick up objects from superclass automatically" do
@vanilla_roster.available_slots.should.be.kind_of(Redis::Counter)
@vanilla_roster.pitchers.should.be.kind_of(Redis::Counter)
@vanilla_roster.basic.should.be.kind_of(Redis::Counter)
@vanilla_roster.resort_lock.should.be.kind_of(Redis::Lock)
@vanilla_roster.starting_pitcher.should.be.kind_of(Redis::Value)
@vanilla_roster.player_stats.should.be.kind_of(Redis::List)
@vanilla_roster.outfielders.should.be.kind_of(Redis::Set)
@vanilla_roster.rank.should.be.kind_of(Redis::SortedSet)
# custom keys
@vanilla_roster.player_totals.should.be.kind_of(Redis::Counter)
@vanilla_roster.all_player_stats.should.be.kind_of(Redis::List)
@vanilla_roster.total_wins.should.be.kind_of(Redis::Set)
@vanilla_roster.my_rank.should.be.kind_of(Redis::Value)
@vanilla_roster.weird_key.should.be.kind_of(Redis::Value)
# globals via class
@vanilla_roster.total_players_online.should.be.kind_of(Redis::Counter)
@vanilla_roster.all_player_stats.should.be.kind_of(Redis::List)
@vanilla_roster.all_players_online.should.be.kind_of(Redis::Set)
@vanilla_roster.last_player.should.be.kind_of(Redis::Value)
VanillaRoster.total_players_online.should.be.kind_of(Redis::Counter)
VanillaRoster.all_player_stats.should.be.kind_of(Redis::List)
VanillaRoster.all_players_online.should.be.kind_of(Redis::Set)
VanillaRoster.last_player.should.be.kind_of(Redis::Value)
end
it "should allow subclass overrides of the same redis object" do
@roster.basic.should == 0
@custom_roster.basic.increment.should == 1
@roster2.basic.should == 0
CustomRoster.new.basic.should == 1
@custom_roster.basic.decrement.should == 0
end
it "should handle new subclass objects" do
@custom_roster.special.increment.should == 1
end
it "should allow passing of increment/decrement to super class" do
@custom_method_roster = CustomMethodRoster.new
@custom_method_roster.counter.should.be.nil
@custom_method_roster.increment(:counter).should == 42
@custom_method_roster.increment(:basic).should == 1
@custom_method_roster.basic.increment.should == 2
@custom_method_roster.decrement(:basic).should == 1
@custom_method_roster.basic.decrement.should == 0
@custom_method_roster.basic.reset.should.be.true
@custom_method_roster.basic.should == 0
@custom_method_roster.basic.should.be.kind_of(Redis::Counter)
end
end