/
tyrant_spec.rb
218 lines (165 loc) · 4.18 KB
/
tyrant_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
#
# Specifying rufus-tokyo
#
# Sun Feb 8 13:13:41 JST 2009
#
require File.dirname(__FILE__) + '/spec_base'
require File.dirname(__FILE__) + '/shared_abstract_spec'
require File.join(File.dirname(__FILE__), 'shared_tyrant_spec')
require 'rufus/tokyo/tyrant'
describe 'a missing Rufus::Tokyo::Tyrant' do
it 'should raise an error' do
lambda {
Rufus::Tokyo::Tyrant.new('127.0.0.1', 1)
}.should.raise(Rufus::Tokyo::TokyoError).message.should.equal(
"couldn't connect to tyrant at 127.0.0.1:1")
end
end
describe Rufus::Tokyo::Tyrant do
it 'should open and close' do
should.not.raise {
t = Rufus::Tokyo::Tyrant.new('127.0.0.1', 45000)
t.close
}
end
it 'should refuse to connect to a TyrantTable' do
lambda {
t = Rufus::Tokyo::Tyrant.new('127.0.0.1', 45001)
}.should.raise(ArgumentError)
end
end
describe Rufus::Tokyo::Tyrant do
it 'should use open with a block will auto close the db correctly' do
res = Rufus::Tokyo::Tyrant.open('127.0.0.1', 45000) do |cab|
cab.clear
10.times { |i| cab["key #{i}"] = "val #{i}" }
cab.size.should.equal(10)
:result
end
res.should.equal(:result)
cab = Rufus::Tokyo::Tyrant.new('127.0.0.1', 45000)
10.times do |i|
cab["key #{i}"].should.equal("val #{i}")
end
cab.close
end
it 'should use open without a block just like calling new correctly' do
cab = Rufus::Tokyo::Tyrant.open('127.0.0.1', 45000)
cab.clear
10.times { |i| cab["key #{i}"] = "val #{i}" }
cab.size.should.equal(10)
cab.close
cab = Rufus::Tokyo::Tyrant.new('127.0.0.1', 45000)
10.times do |i|
cab["key #{i}"].should.equal("val #{i}")
end
cab.close
end
end
describe Rufus::Tokyo::Tyrant do
before do
@db = Rufus::Tokyo::Tyrant.new('127.0.0.1', 45000)
@db.clear
end
after do
@db.close
end
behaves_like 'an abstract structure'
behaves_like 'a Tyrant structure (no transactions)'
behaves_like 'a Tyrant structure (copy method)'
it 'should respond to stat' do
stat = @db.stat
stat['type'].should.equal('hash')
end
end
describe Rufus::Tokyo::Tyrant do
before do
@n = 50
@db = Rufus::Tokyo::Tyrant.new('127.0.0.1', 45000)
@db.clear
@n.times { |i| @db["person#{i}"] = 'whoever' }
@n.times { |i| @db["animal#{i}"] = 'whichever' }
@db["toto#{0.chr}5"] = 'toto'
end
after do
@db.close
end
behaves_like 'abstract structure #keys'
end
describe Rufus::Tokyo::Tyrant do
before do
@db = Rufus::Tokyo::Tyrant.new('127.0.0.1', 45000)
@db.clear
3.times { |i| @db[i.to_s] = "val#{i}" }
end
after do
@db.close
end
behaves_like 'abstract structure #lget/lput/ldelete'
end
describe Rufus::Tokyo::Tyrant do
before do
@db = Rufus::Tokyo::Tyrant.new('127.0.0.1', 45000)
@db.clear
end
after do
@db.close
end
behaves_like 'abstract structure #add{int|double}'
end
describe Rufus::Tokyo::Tyrant do
before do
@db = Rufus::Tokyo::Tyrant.new('127.0.0.1', 45000)
@db.clear
end
after do
@db.close
end
behaves_like 'abstract structure #putkeep'
behaves_like 'abstract structure #putcat'
end
describe 'Rufus::Tokyo::Tyrant (lua extensions)' do
before do
@db = Rufus::Tokyo::Tyrant.new('127.0.0.1', 45000)
@db.clear
end
after do
@db.close
end
behaves_like 'tyrant with embedded lua'
end
describe Rufus::Tokyo::Tyrant do
before do
@db = Rufus::Tokyo::Tyrant.new('127.0.0.1', 45000)
@db.clear
end
after do
@db.close
end
behaves_like 'an abstract structure flattening keys and values'
end
describe 'Rufus::Tokyo::Tyrant with a default value' do
before do
@db = Rufus::Tokyo::Tyrant.new('127.0.0.1', 45000, :default => 'Nemo')
@db.clear
@db['known'] = 'Ulysse'
end
after do
@db.close
end
behaves_like 'an abstract structure with a default value'
end
describe 'Rufus::Tokyo::Tyrant with a default_proc' do
before do
@db = Rufus::Tokyo::Tyrant.new(
'127.0.0.1',
45000,
:default_proc => lambda { |db, k| "default:#{k}" })
@db.clear
@db['known'] = 'Ulysse'
end
after do
@db.close
end
behaves_like 'an abstract structure with a default_proc'
end