redis-objects-legacy 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,198 @@
1
+
2
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
+
4
+ require 'redis/objects'
5
+
6
+ class CustomSerializer
7
+ class << self
8
+ attr_accessor :dump_called, :load_called, :dump_args, :load_args
9
+ end
10
+
11
+ def self.dump(value, *args, **kargs)
12
+ @dump_called = true
13
+ @dump_args = [args, kargs]
14
+ Marshal.dump(value)
15
+ end
16
+
17
+ def self.load(value, *args, **kargs)
18
+ @load_called = true
19
+ @load_args = [args, kargs]
20
+ Marshal.load(value)
21
+ end
22
+
23
+ def self.reset!
24
+ @dump_called = nil
25
+ @load_called = nil
26
+ end
27
+ end
28
+
29
+ describe 'with custom serialization' do
30
+ before do
31
+ CustomSerializer.reset!
32
+ end
33
+
34
+ describe Redis::Value do
35
+ before do
36
+ @value = Redis::Value.new(
37
+ 'spec/value_custom_serializer',
38
+ marshal: true,
39
+ serializer: CustomSerializer
40
+ )
41
+ @value.clear
42
+ end
43
+
44
+ it 'uses custom serializer' do
45
+ @value.value = { json: 'data' }
46
+ CustomSerializer.dump_called.should == true
47
+ CustomSerializer.load_called.should == nil
48
+ @value.value.should == { json: 'data' }
49
+ CustomSerializer.dump_called.should == true
50
+ CustomSerializer.load_called.should == true
51
+ end
52
+
53
+ it 'passes extra arguments to dump' do
54
+ @value.options[:marshal_dump_args] = ['some', { extra: 'arguments' }]
55
+ @value.value = 1
56
+ CustomSerializer.dump_args.should == [['some'], { extra: 'arguments' }]
57
+ end
58
+
59
+ it 'passes extra arguments to load' do
60
+ @value.options[:marshal_load_args] = ['some', { extra: 'arguments' }]
61
+ @value.value = 1
62
+ @value.value.should == 1
63
+ CustomSerializer.load_args.should == [['some'], { extra: 'arguments' }]
64
+ end
65
+ end
66
+
67
+ describe Redis::List do
68
+ before do
69
+ @list = Redis::List.new(
70
+ 'spec/list_custom_serializer',
71
+ marshal: true,
72
+ serializer: CustomSerializer
73
+ )
74
+ @list.clear
75
+ end
76
+
77
+ it 'uses custom serializer' do
78
+ @list << { json: 'data' }
79
+ CustomSerializer.dump_called.should == true
80
+ CustomSerializer.load_called.should == nil
81
+ @list.should == [{ json: 'data' }]
82
+ CustomSerializer.dump_called.should == true
83
+ CustomSerializer.load_called.should == true
84
+ end
85
+
86
+ it 'passes extra arguments to dump' do
87
+ @list.options[:marshal_dump_args] = ['some', { extra: 'arguments' }]
88
+ @list << 1
89
+ CustomSerializer.dump_args.should == [['some'], { extra: 'arguments' }]
90
+ end
91
+
92
+ it 'passes extra arguments to load' do
93
+ @list.options[:marshal_load_args] = ['some', { extra: 'arguments' }]
94
+ @list << 1
95
+ @list.values.should == [1]
96
+ CustomSerializer.load_args.should == [['some'], { extra: 'arguments' }]
97
+ end
98
+ end
99
+
100
+ describe Redis::HashKey do
101
+ before do
102
+ @hash = Redis::HashKey.new(
103
+ 'spec/hash_custom_serializer',
104
+ marshal: true,
105
+ serializer: CustomSerializer
106
+ )
107
+ @hash.clear
108
+ end
109
+
110
+ it 'uses custom serializer' do
111
+ @hash['a'] = 1
112
+ CustomSerializer.dump_called.should == true
113
+ CustomSerializer.load_called.should == nil
114
+ @hash.value.should == { 'a' => 1 }
115
+ CustomSerializer.dump_called.should == true
116
+ CustomSerializer.load_called.should == true
117
+ end
118
+
119
+ it 'passes extra arguments to dump' do
120
+ @hash.options[:marshal_dump_args] = ['some', { extra: 'arguments' }]
121
+ @hash['a'] = 1
122
+ CustomSerializer.dump_args.should == [['some'], { extra: 'arguments' }]
123
+ end
124
+
125
+ it 'passes extra arguments to load' do
126
+ @hash.options[:marshal_load_args] = ['some', { extra: 'arguments' }]
127
+ @hash['a'] = 1
128
+ @hash.value.should == { 'a' => 1 }
129
+ CustomSerializer.load_args.should == [['some'], { extra: 'arguments' }]
130
+ end
131
+ end
132
+
133
+ describe Redis::Set do
134
+ before do
135
+ @set = Redis::Set.new(
136
+ 'spec/set_custom_serializer',
137
+ marshal: true,
138
+ serializer: CustomSerializer
139
+ )
140
+ @set.clear
141
+ end
142
+
143
+ it 'uses custom serializer' do
144
+ @set << 'a'
145
+ CustomSerializer.dump_called.should == true
146
+ CustomSerializer.load_called.should == nil
147
+ @set.members.should == ['a']
148
+ CustomSerializer.dump_called.should == true
149
+ CustomSerializer.load_called.should == true
150
+ end
151
+
152
+ it 'passes extra arguments to dump' do
153
+ @set.options[:marshal_dump_args] = ['some', { extra: 'arguments' }]
154
+ @set << 'a'
155
+ CustomSerializer.dump_args.should == [['some'], { extra: 'arguments' }]
156
+ end
157
+
158
+ it 'passes extra arguments to load' do
159
+ @set.options[:marshal_load_args] = ['some', { extra: 'arguments' }]
160
+ @set << 'a'
161
+ @set.members.should == ['a']
162
+ CustomSerializer.load_args.should == [['some'], { extra: 'arguments' }]
163
+ end
164
+ end
165
+
166
+ describe Redis::SortedSet do
167
+ before do
168
+ @set = Redis::SortedSet.new(
169
+ 'spec/zset_custom_serializer',
170
+ marshal: true,
171
+ serializer: CustomSerializer
172
+ )
173
+ @set.clear
174
+ end
175
+
176
+ it 'uses custom serializer' do
177
+ @set['a'] = 1
178
+ CustomSerializer.dump_called.should == true
179
+ CustomSerializer.load_called.should == nil
180
+ @set.members.should == ['a']
181
+ CustomSerializer.dump_called.should == true
182
+ CustomSerializer.load_called.should == true
183
+ end
184
+
185
+ it 'passes extra arguments to dump' do
186
+ @set.options[:marshal_dump_args] = ['some', { extra: 'arguments' }]
187
+ @set['a'] = 1
188
+ CustomSerializer.dump_args.should == [['some'], { extra: 'arguments' }]
189
+ end
190
+
191
+ it 'passes extra arguments to load' do
192
+ @set.options[:marshal_load_args] = ['some', { extra: 'arguments' }]
193
+ @set['a'] = 1
194
+ @set.members.should == ['a']
195
+ CustomSerializer.load_args.should == [['some'], { extra: 'arguments' }]
196
+ end
197
+ end
198
+ end