-
Notifications
You must be signed in to change notification settings - Fork 1
/
readable_dictionary.rb
191 lines (165 loc) · 6.32 KB
/
readable_dictionary.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
shared_examples 'readable dictionary' do
let(:uuid1) { uuid }
let(:uuid2) { uuid }
let(:uuid3) { uuid }
let(:cf) { dictionary.class.column_family.column_family }
context 'without row in memory' do
describe '#each_pair' do
it 'should load columns in batches and yield them' do
connection.should_receive(:execute).
with("SELECT FIRST 2 * FROM #{cf} WHERE ? = ? LIMIT 1", :blog_id, 1).
and_return result_stub('blog_id' => 1, uuid1 => 1, uuid2 => 2)
connection.should_receive(:execute).
with("SELECT FIRST 2 ?..? FROM #{cf} WHERE ? = ? LIMIT 1", uuid2, '', :blog_id, 1).
and_return result_stub('blog_id' => 1, uuid2 => 2, uuid3 => 3)
connection.should_receive(:execute).
with("SELECT FIRST 2 ?..? FROM #{cf} WHERE ? = ? LIMIT 1", uuid3, '', :blog_id, 1).
and_return result_stub({'blog_id' => 1})
hash = {}
dictionary.each_pair do |key, value|
hash[key] = value
end
hash.should == {uuid1 => 1, uuid2 => 2, uuid3 => 3}
end
end
describe '#[]' do
it 'should load column from cassandra' do
connection.stub(:execute).
with("SELECT ? FROM #{cf} WHERE ? = ? LIMIT 1", [uuid1], :blog_id, 1).
and_return result_stub(uuid1 => 1)
dictionary[uuid1].should == 1
end
end
describe '#slice' do
it 'should load columns from data store' do
connection.stub(:execute).
with("SELECT ? FROM #{cf} WHERE ? = ? LIMIT 1", [uuid1,uuid2], :blog_id, 1).
and_return result_stub(uuid1 => 1, uuid2 => 2)
dictionary.slice(uuid1, uuid2).should == {uuid1 => 1, uuid2 => 2}
end
end
describe '#keys' do
it 'should load keys from data store' do
connection.should_receive(:execute).
with("SELECT FIRST 2 * FROM #{cf} WHERE ? = ? LIMIT 1", :blog_id, 1).
and_return result_stub('blog_id' => 1, uuid1 => 1, uuid2 => 2)
connection.should_receive(:execute).
with("SELECT FIRST 2 ?..? FROM #{cf} WHERE ? = ? LIMIT 1", uuid2, '', :blog_id, 1).
and_return result_stub('blog_id' => 1, uuid2 => 2, uuid3 => 3)
connection.should_receive(:execute).
with("SELECT FIRST 2 ?..? FROM #{cf} WHERE ? = ? LIMIT 1", uuid3, '', :blog_id, 1).
and_return result_stub({'blog_id' => 1})
dictionary.keys.should == [uuid1, uuid2, uuid3]
end
end
describe '#values' do
it 'should load values from data store' do
connection.should_receive(:execute).
with("SELECT FIRST 2 * FROM #{cf} WHERE ? = ? LIMIT 1", :blog_id, 1).
and_return result_stub('blog_id' => 1, uuid1 => 1, uuid2 => 2)
connection.should_receive(:execute).
with("SELECT FIRST 2 ?..? FROM #{cf} WHERE ? = ? LIMIT 1", uuid2, '', :blog_id, 1).
and_return result_stub('blog_id' => 1, uuid2 => 2, uuid3 => 3)
connection.should_receive(:execute).
with("SELECT FIRST 2 ?..? FROM #{cf} WHERE ? = ? LIMIT 1", uuid3, '', :blog_id, 1).
and_return result_stub({'blog_id' => 1})
dictionary.values.should == [1, 2, 3]
end
end
describe '#first' do
it 'should load value from data store' do
connection.should_receive(:execute).
with("SELECT FIRST 1 * FROM #{cf} WHERE ? = ? LIMIT 1", :blog_id, 1).
and_return result_stub('blog_id' => 1, uuid1 => 1)
dictionary.first.should == [uuid1, 1]
end
end
describe '#last' do
it 'should load value from data store' do
connection.should_receive(:execute).
with("SELECT FIRST 1 REVERSED * FROM #{cf} WHERE ? = ? LIMIT 1", :blog_id, 1).
and_return result_stub('blog_id' => 1, uuid3 => 3)
dictionary.last.should == [uuid3, 3]
end
end
end
context 'with data loaded in memory' do
before do
connection.stub(:execute).
with("SELECT FIRST 2 * FROM #{cf} WHERE ? = ? LIMIT 1", :blog_id, 1).
and_return result_stub('blog_id' => 1, uuid1 => 1, uuid2 => 2)
connection.stub(:execute).
with("SELECT FIRST 2 ?..? FROM #{cf} WHERE ? = ? LIMIT 1", uuid2, '', :blog_id, 1).
and_return result_stub('blog_id' => 1, uuid2 => 2, uuid3 => 3)
connection.stub(:execute).
with("SELECT FIRST 2 ?..? FROM #{cf} WHERE ? = ? LIMIT 1", uuid3, '', :blog_id, 1).
and_return result_stub({'blog_id' => 1})
dictionary.load
connection.should_not_receive(:execute)
end
describe '#each_pair' do
it 'should yield data from memory' do
hash = {}
dictionary.each_pair do |key, value|
hash[key] = value
end
hash.should == {uuid1 => 1, uuid2 => 2, uuid3 => 3}
end
end
describe '#[]' do
it 'should return value from memory' do
dictionary[uuid1].should == 1
end
end
describe '#slice' do
it 'should return slice of data in memory' do
dictionary.slice(uuid1, uuid2).should == {uuid1 => 1, uuid2 => 2}
end
end
describe '#first' do
it 'should return first element in memory' do
dictionary.first.should == [uuid1, 1]
end
end
describe '#last' do
it 'should return first element in memory' do
dictionary.last.should == [uuid3, 3]
end
end
describe '#keys' do
it 'should return keys from memory' do
dictionary.keys.should == [uuid1, uuid2, uuid3]
end
end
describe '#values' do
it 'should return values from memory' do
dictionary.values.should == [1, 2, 3]
end
end
end
describe '::load' do
let :comments do
[
{'user' => 'Cequel User', 'comment' => 'How do I load multiple rows?'},
{'user' => 'Mat Brown', 'comment' => 'Just use the ::load class method'}
]
end
it 'should load all rows in one query' do
connection.stub(:execute).
with(
'SELECT * FROM post_comments WHERE ? IN (?)',
'post_id', [1, 2]
).and_return result_stub(
*comments.each_with_index.
map { |comment, i| {'post_id' => i+1, i+4 => comment.to_json} }
)
rows = PostComments.load(1, 2)
rows.map { |row| row.post_id }.should == [1, 2]
rows.map { |row| row.values.first }.should == comments
end
end
private
def uuid
SimpleUUID::UUID.new.to_guid
end
end