cequel 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cequel.rb +1 -0
- data/lib/cequel/data_set.rb +23 -2
- data/lib/cequel/keyspace.rb +103 -20
- data/lib/cequel/model.rb +9 -4
- data/lib/cequel/model/class_internals.rb +1 -1
- data/lib/cequel/model/counter.rb +35 -0
- data/lib/cequel/model/dictionary.rb +31 -133
- data/lib/cequel/model/railtie.rb +1 -1
- data/lib/cequel/model/readable_dictionary.rb +169 -0
- data/lib/cequel/model/scoped.rb +16 -4
- data/lib/cequel/version.rb +1 -1
- data/spec/examples/data_set_spec.rb +26 -0
- data/spec/examples/keyspace_spec.rb +1 -1
- data/spec/examples/model/counter_spec.rb +94 -0
- data/spec/examples/model/dictionary_spec.rb +2 -125
- data/spec/examples/spec_helper.rb +3 -0
- data/spec/models/comment_counts.rb +8 -0
- data/spec/shared/readable_dictionary.rb +174 -0
- metadata +26 -18
@@ -3,6 +3,9 @@ require File.expand_path('../../environment', __FILE__)
|
|
3
3
|
Dir.glob(File.expand_path('../../support/**/*.rb', __FILE__)).each do |file|
|
4
4
|
require file
|
5
5
|
end
|
6
|
+
Dir.glob(File.expand_path('../../shared/**/*.rb', __FILE__)).each do |file|
|
7
|
+
require file
|
8
|
+
end
|
6
9
|
|
7
10
|
RSpec.configure do |config|
|
8
11
|
config.include(Cequel::SpecSupport::Helpers)
|
@@ -0,0 +1,174 @@
|
|
1
|
+
shared_examples 'readable dictionary' do
|
2
|
+
let(:uuid1) { uuid }
|
3
|
+
let(:uuid2) { uuid }
|
4
|
+
let(:uuid3) { uuid }
|
5
|
+
let(:cf) { dictionary.class.column_family.column_family }
|
6
|
+
|
7
|
+
context 'without row in memory' do
|
8
|
+
|
9
|
+
describe '#each_pair' do
|
10
|
+
|
11
|
+
it 'should load columns in batches and yield them' do
|
12
|
+
connection.should_receive(:execute).
|
13
|
+
with("SELECT FIRST 2 * FROM #{cf} WHERE ? = ? LIMIT 1", :blog_id, 1).
|
14
|
+
and_return result_stub('blog_id' => 1, uuid1 => 1, uuid2 => 2)
|
15
|
+
connection.should_receive(:execute).
|
16
|
+
with("SELECT FIRST 2 ?..? FROM #{cf} WHERE ? = ? LIMIT 1", uuid2, '', :blog_id, 1).
|
17
|
+
and_return result_stub('blog_id' => 1, uuid2 => 2, uuid3 => 3)
|
18
|
+
connection.should_receive(:execute).
|
19
|
+
with("SELECT FIRST 2 ?..? FROM #{cf} WHERE ? = ? LIMIT 1", uuid3, '', :blog_id, 1).
|
20
|
+
and_return result_stub({'blog_id' => 1})
|
21
|
+
hash = {}
|
22
|
+
dictionary.each_pair do |key, value|
|
23
|
+
hash[key] = value
|
24
|
+
end
|
25
|
+
hash.should == {uuid1 => 1, uuid2 => 2, uuid3 => 3}
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#[]' do
|
31
|
+
|
32
|
+
it 'should load column from cassandra' do
|
33
|
+
connection.stub(:execute).
|
34
|
+
with("SELECT ? FROM #{cf} WHERE ? = ? LIMIT 1", [uuid1], :blog_id, 1).
|
35
|
+
and_return result_stub(uuid1 => 1)
|
36
|
+
dictionary[uuid1].should == 1
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#slice' do
|
42
|
+
it 'should load columns from data store' do
|
43
|
+
connection.stub(:execute).
|
44
|
+
with("SELECT ? FROM #{cf} WHERE ? = ? LIMIT 1", [uuid1,uuid2], :blog_id, 1).
|
45
|
+
and_return result_stub(uuid1 => 1, uuid2 => 2)
|
46
|
+
dictionary.slice(uuid1, uuid2).should == {uuid1 => 1, uuid2 => 2}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '#keys' do
|
51
|
+
it 'should load keys from data store' do
|
52
|
+
connection.should_receive(:execute).
|
53
|
+
with("SELECT FIRST 2 * FROM #{cf} WHERE ? = ? LIMIT 1", :blog_id, 1).
|
54
|
+
and_return result_stub('blog_id' => 1, uuid1 => 1, uuid2 => 2)
|
55
|
+
connection.should_receive(:execute).
|
56
|
+
with("SELECT FIRST 2 ?..? FROM #{cf} WHERE ? = ? LIMIT 1", uuid2, '', :blog_id, 1).
|
57
|
+
and_return result_stub('blog_id' => 1, uuid2 => 2, uuid3 => 3)
|
58
|
+
connection.should_receive(:execute).
|
59
|
+
with("SELECT FIRST 2 ?..? FROM #{cf} WHERE ? = ? LIMIT 1", uuid3, '', :blog_id, 1).
|
60
|
+
and_return result_stub({'blog_id' => 1})
|
61
|
+
dictionary.keys.should == [uuid1, uuid2, uuid3]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '#values' do
|
66
|
+
it 'should load values from data store' do
|
67
|
+
connection.should_receive(:execute).
|
68
|
+
with("SELECT FIRST 2 * FROM #{cf} WHERE ? = ? LIMIT 1", :blog_id, 1).
|
69
|
+
and_return result_stub('blog_id' => 1, uuid1 => 1, uuid2 => 2)
|
70
|
+
connection.should_receive(:execute).
|
71
|
+
with("SELECT FIRST 2 ?..? FROM #{cf} WHERE ? = ? LIMIT 1", uuid2, '', :blog_id, 1).
|
72
|
+
and_return result_stub('blog_id' => 1, uuid2 => 2, uuid3 => 3)
|
73
|
+
connection.should_receive(:execute).
|
74
|
+
with("SELECT FIRST 2 ?..? FROM #{cf} WHERE ? = ? LIMIT 1", uuid3, '', :blog_id, 1).
|
75
|
+
and_return result_stub({'blog_id' => 1})
|
76
|
+
dictionary.values.should == [1, 2, 3]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
context 'with data loaded in memory' do
|
83
|
+
before do
|
84
|
+
connection.stub(:execute).
|
85
|
+
with("SELECT FIRST 2 * FROM #{cf} WHERE ? = ? LIMIT 1", :blog_id, 1).
|
86
|
+
and_return result_stub('blog_id' => 1, uuid1 => 1, uuid2 => 2)
|
87
|
+
connection.stub(:execute).
|
88
|
+
with("SELECT FIRST 2 ?..? FROM #{cf} WHERE ? = ? LIMIT 1", uuid2, '', :blog_id, 1).
|
89
|
+
and_return result_stub('blog_id' => 1, uuid2 => 2, uuid3 => 3)
|
90
|
+
connection.stub(:execute).
|
91
|
+
with("SELECT FIRST 2 ?..? FROM #{cf} WHERE ? = ? LIMIT 1", uuid3, '', :blog_id, 1).
|
92
|
+
and_return result_stub({'blog_id' => 1})
|
93
|
+
dictionary.load
|
94
|
+
connection.should_not_receive(:execute)
|
95
|
+
end
|
96
|
+
|
97
|
+
describe '#each_pair' do
|
98
|
+
it 'should yield data from memory' do
|
99
|
+
hash = {}
|
100
|
+
dictionary.each_pair do |key, value|
|
101
|
+
hash[key] = value
|
102
|
+
end
|
103
|
+
hash.should == {uuid1 => 1, uuid2 => 2, uuid3 => 3}
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe '#[]' do
|
108
|
+
it 'should return value from memory' do
|
109
|
+
dictionary[uuid1].should == 1
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe '#slice' do
|
114
|
+
it 'should return slice of data in memory' do
|
115
|
+
dictionary.slice(uuid1, uuid2).should == {uuid1 => 1, uuid2 => 2}
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe '#keys' do
|
120
|
+
it 'should return keys from memory' do
|
121
|
+
dictionary.keys.should == [uuid1, uuid2, uuid3]
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe '#values' do
|
126
|
+
it 'should return values from memory' do
|
127
|
+
dictionary.values.should == [1, 2, 3]
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
describe '::load' do
|
133
|
+
let :comments do
|
134
|
+
[
|
135
|
+
{'user' => 'Cequel User', 'comment' => 'How do I load multiple rows?'},
|
136
|
+
{'user' => 'Mat Brown', 'comment' => 'Just use the ::load class method'}
|
137
|
+
]
|
138
|
+
end
|
139
|
+
|
140
|
+
it 'should load all rows in one query' do
|
141
|
+
connection.stub(:execute).
|
142
|
+
with(
|
143
|
+
'SELECT FIRST 1000 * FROM post_comments WHERE ? IN (?)',
|
144
|
+
'post_id', [1, 2]
|
145
|
+
).and_return result_stub(
|
146
|
+
*comments.each_with_index.
|
147
|
+
map { |comment, i| {'post_id' => i+1, i+4 => comment.to_json} }
|
148
|
+
)
|
149
|
+
rows = PostComments.load(1, 2)
|
150
|
+
rows.map { |row| row.post_id }.should == [1, 2]
|
151
|
+
rows.map { |row| row.values.first }.should == comments
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'should respect columns option' do
|
155
|
+
connection.stub(:execute).
|
156
|
+
with(
|
157
|
+
'SELECT FIRST 20 * FROM post_comments WHERE ? IN (?)',
|
158
|
+
'post_id', [1, 2]
|
159
|
+
).and_return result_stub(
|
160
|
+
*comments.each_with_index.
|
161
|
+
map { |comment, i| {'post_id' => i+1, i+4 => comment.to_json} }
|
162
|
+
)
|
163
|
+
rows = PostComments.load(1, 2, :columns => 20)
|
164
|
+
rows.map { |row| row.post_id }.should == [1, 2]
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
private
|
169
|
+
|
170
|
+
def uuid
|
171
|
+
SimpleUUID::UUID.new.to_guid
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
metadata
CHANGED
@@ -1,15 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cequel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Mat Brown
|
9
|
+
- Aubrey Holland
|
10
|
+
- Keenan Brock
|
9
11
|
autorequire:
|
10
12
|
bindir: bin
|
11
13
|
cert_chain: []
|
12
|
-
date: 2012-
|
14
|
+
date: 2012-10-29 00:00:00.000000000 Z
|
13
15
|
dependencies:
|
14
16
|
- !ruby/object:Gem::Dependency
|
15
17
|
name: activesupport
|
@@ -60,53 +62,53 @@ dependencies:
|
|
60
62
|
- !ruby/object:Gem::Version
|
61
63
|
version: '1.0'
|
62
64
|
- !ruby/object:Gem::Dependency
|
63
|
-
name:
|
65
|
+
name: connection_pool
|
64
66
|
requirement: !ruby/object:Gem::Requirement
|
65
67
|
none: false
|
66
68
|
requirements:
|
67
|
-
- -
|
69
|
+
- - ~>
|
68
70
|
- !ruby/object:Gem::Version
|
69
|
-
version:
|
71
|
+
version: 0.9.2
|
70
72
|
type: :runtime
|
71
73
|
prerelease: false
|
72
74
|
version_requirements: !ruby/object:Gem::Requirement
|
73
75
|
none: false
|
74
76
|
requirements:
|
75
|
-
- -
|
77
|
+
- - ~>
|
76
78
|
- !ruby/object:Gem::Version
|
77
|
-
version:
|
79
|
+
version: 0.9.2
|
78
80
|
- !ruby/object:Gem::Dependency
|
79
|
-
name:
|
81
|
+
name: i18n
|
80
82
|
requirement: !ruby/object:Gem::Requirement
|
81
83
|
none: false
|
82
84
|
requirements:
|
83
|
-
- -
|
85
|
+
- - ! '>='
|
84
86
|
- !ruby/object:Gem::Version
|
85
|
-
version: '
|
86
|
-
type: :
|
87
|
+
version: '0'
|
88
|
+
type: :runtime
|
87
89
|
prerelease: false
|
88
90
|
version_requirements: !ruby/object:Gem::Requirement
|
89
91
|
none: false
|
90
92
|
requirements:
|
91
|
-
- -
|
93
|
+
- - ! '>='
|
92
94
|
- !ruby/object:Gem::Version
|
93
|
-
version: '
|
95
|
+
version: '0'
|
94
96
|
- !ruby/object:Gem::Dependency
|
95
|
-
name:
|
97
|
+
name: rspec
|
96
98
|
requirement: !ruby/object:Gem::Requirement
|
97
99
|
none: false
|
98
100
|
requirements:
|
99
|
-
- -
|
101
|
+
- - ~>
|
100
102
|
- !ruby/object:Gem::Version
|
101
|
-
version: '0'
|
103
|
+
version: '2.0'
|
102
104
|
type: :development
|
103
105
|
prerelease: false
|
104
106
|
version_requirements: !ruby/object:Gem::Requirement
|
105
107
|
none: false
|
106
108
|
requirements:
|
107
|
-
- -
|
109
|
+
- - ~>
|
108
110
|
- !ruby/object:Gem::Version
|
109
|
-
version: '0'
|
111
|
+
version: '2.0'
|
110
112
|
- !ruby/object:Gem::Dependency
|
111
113
|
name: yard
|
112
114
|
requirement: !ruby/object:Gem::Requirement
|
@@ -189,7 +191,9 @@ files:
|
|
189
191
|
- lib/cequel/statement.rb
|
190
192
|
- lib/cequel/model/inheritable.rb
|
191
193
|
- lib/cequel/model/remote_association.rb
|
194
|
+
- lib/cequel/model/counter.rb
|
192
195
|
- lib/cequel/model/naming.rb
|
196
|
+
- lib/cequel/model/readable_dictionary.rb
|
193
197
|
- lib/cequel/model/associations.rb
|
194
198
|
- lib/cequel/model/mass_assignment_security.rb
|
195
199
|
- lib/cequel/model/dictionary.rb
|
@@ -234,6 +238,7 @@ files:
|
|
234
238
|
- spec/examples/model/timestamps_spec.rb
|
235
239
|
- spec/examples/model/persistence_spec.rb
|
236
240
|
- spec/examples/model/magic_spec.rb
|
241
|
+
- spec/examples/model/counter_spec.rb
|
237
242
|
- spec/examples/model/callbacks_spec.rb
|
238
243
|
- spec/examples/model/inheritable_spec.rb
|
239
244
|
- spec/examples/model/scope_spec.rb
|
@@ -245,6 +250,7 @@ files:
|
|
245
250
|
- spec/models/post_observer.rb
|
246
251
|
- spec/models/blog_posts.rb
|
247
252
|
- spec/models/blog.rb
|
253
|
+
- spec/models/comment_counts.rb
|
248
254
|
- spec/models/post.rb
|
249
255
|
- spec/models/photo.rb
|
250
256
|
- spec/models/asset.rb
|
@@ -254,6 +260,7 @@ files:
|
|
254
260
|
- spec/models/category.rb
|
255
261
|
- spec/support/helpers.rb
|
256
262
|
- spec/support/result_stub.rb
|
263
|
+
- spec/shared/readable_dictionary.rb
|
257
264
|
homepage:
|
258
265
|
licenses:
|
259
266
|
- MIT
|
@@ -294,6 +301,7 @@ test_files:
|
|
294
301
|
- spec/examples/model/timestamps_spec.rb
|
295
302
|
- spec/examples/model/persistence_spec.rb
|
296
303
|
- spec/examples/model/magic_spec.rb
|
304
|
+
- spec/examples/model/counter_spec.rb
|
297
305
|
- spec/examples/model/callbacks_spec.rb
|
298
306
|
- spec/examples/model/inheritable_spec.rb
|
299
307
|
- spec/examples/model/scope_spec.rb
|