whi-cassie 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,38 +1,37 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe Cassie::Subscribers do
4
-
5
4
  it "should be able to add and remove a subscriber" do
6
5
  subscribers = Cassie::Subscribers.new
7
6
  expect(subscribers.empty?).to eq true
8
7
  data_1 = []
9
8
  data_2 = []
10
- subscriber_1 = lambda{|info| data_1 << info}
11
- subscriber_2 = lambda{|info| data_2 << info}
9
+ subscriber_1 = lambda { |info| data_1 << info }
10
+ subscriber_2 = lambda { |info| data_2 << info }
12
11
  subscribers.add(subscriber_1)
13
12
  subscribers << subscriber_2
14
13
  expect(subscribers.empty?).to eq false
15
14
  expect(subscribers.size).to eq 2
16
15
  expect(subscribers.include?(subscriber_1)).to eq true
17
16
  expect(subscribers.include?(subscriber_2)).to eq true
18
-
19
- subscribers.each{|s| s.call(:payload)}
17
+
18
+ subscribers.each { |s| s.call(:payload) }
20
19
  expect(data_1).to eq [:payload]
21
20
  expect(data_2).to eq [:payload]
22
-
21
+
23
22
  subscribers.remove(subscriber_2)
24
23
  expect(subscribers.size).to eq 1
25
24
  expect(subscribers.include?(subscriber_1)).to eq true
26
25
  expect(subscribers.include?(subscriber_2)).to eq false
27
-
28
- subscribers.each{|s| s.call(:more)}
26
+
27
+ subscribers.each { |s| s.call(:more) }
29
28
  expect(data_1).to eq [:payload, :more]
30
29
  expect(data_2).to eq [:payload]
31
-
30
+
32
31
  subscribers.delete(subscriber_1)
33
32
  expect(subscribers.size).to eq 0
34
33
  end
35
-
34
+
36
35
  it "should have a hierarchy of subscribers" do
37
36
  subscribers_1 = Cassie::Subscribers.new
38
37
  subscribers_2 = Cassie::Subscribers.new(subscribers_1)
@@ -40,24 +39,23 @@ describe Cassie::Subscribers do
40
39
  data_1 = []
41
40
  data_2 = []
42
41
  data_3 = []
43
- subscribers_1 << lambda{|info| data_1 << info}
44
- subscribers_2 << lambda{|info| data_2 << info}
45
-
42
+ subscribers_1 << lambda { |info| data_1 << info }
43
+ subscribers_2 << lambda { |info| data_2 << info }
44
+
46
45
  expect(subscribers_1.size).to eq 1
47
46
  expect(subscribers_2.size).to eq 2
48
47
  expect(subscribers_3.size).to eq 1
49
-
50
- subscribers_1.each{|subscriber| subscriber.call(:payload_1)}
51
- subscribers_2.each{|subscriber| subscriber.call(:payload_2)}
52
- subscribers_3.each{|subscriber| subscriber.call(:payload_3)}
53
-
48
+
49
+ subscribers_1.each { |subscriber| subscriber.call(:payload_1) }
50
+ subscribers_2.each { |subscriber| subscriber.call(:payload_2) }
51
+ subscribers_3.each { |subscriber| subscriber.call(:payload_3) }
52
+
54
53
  expect(data_1).to eq [:payload_1, :payload_2, :payload_3]
55
54
  expect(data_2).to eq [:payload_2]
56
-
55
+
57
56
  subscribers_2.clear
58
57
  expect(subscribers_2.size).to eq 1
59
58
  subscribers_1.clear
60
59
  expect(subscribers_2.size).to eq 0
61
60
  end
62
-
63
61
  end
@@ -1,208 +1,207 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe Cassie do
4
-
5
- let(:instance){ Cassie.instance }
6
- let(:table){ "cassie_specs.things" }
7
-
4
+ let(:instance) { Cassie.instance }
5
+ let(:table) { "cassie_specs.things" }
6
+
8
7
  describe "prepare" do
9
8
  it "should keep a cache of prepared statements" do
10
9
  statement_1 = instance.prepare("SELECT * FROM #{table} LIMIT ?")
11
10
  statement_2 = instance.prepare("SELECT * FROM #{table} LIMIT ?")
12
- statement_1.object_id.should == statement_2.object_id
11
+ expect(statement_1.object_id).to eq(statement_2.object_id)
13
12
  end
14
-
13
+
15
14
  it "should clear the prepared statement cache when reconnecting" do
16
15
  statement_1 = instance.prepare("SELECT * FROM #{table} LIMIT ?")
17
16
  instance.disconnect
18
17
  instance.connect
19
18
  statement_2 = instance.prepare("SELECT * FROM #{table} LIMIT ?")
20
- statement_1.object_id.should_not == statement_2.object_id
19
+ expect(statement_1.object_id).not_to eq(statement_2.object_id)
21
20
  end
22
21
  end
23
-
22
+
24
23
  describe "find" do
25
24
  before :each do
26
- instance.insert(table, :owner => 1, :id => 2, :val => 'foo')
27
- instance.insert(table, :owner => 10, :id => 2, :val => 'bar')
25
+ instance.insert(table, owner: 1, id: 2, val: "foo")
26
+ instance.insert(table, owner: 10, id: 2, val: "bar")
28
27
  end
29
-
28
+
30
29
  it "should construct a CQL query from the options" do
31
30
  results = instance.find("SELECT owner, id, val FROM #{table} WHERE owner = 1")
32
- results.rows.collect{|r| r["val"]}.should == ['foo']
31
+ expect(results.rows.collect { |r| r["val"] }).to eq(["foo"])
33
32
  end
34
-
33
+
35
34
  it "should construct a CQL query from a statement with variables" do
36
35
  results = instance.find("SELECT owner, id, val FROM #{table} WHERE owner = ? LIMIT ?", [10, 1])
37
- results.rows.collect{|r| r["val"]}.should == ['bar']
36
+ expect(results.rows.collect { |r| r["val"] }).to eq(["bar"])
38
37
  end
39
-
38
+
40
39
  it "should not batch find statements" do
41
40
  instance.batch do
42
41
  results = instance.find("SELECT owner, id, val FROM #{table} WHERE owner = ? LIMIT ?", [10, 1])
43
- results.rows.collect{|r| r["val"]}.should == ['bar']
42
+ expect(results.rows.collect { |r| r["val"] }).to eq(["bar"])
44
43
  end
45
44
  end
46
45
  end
47
-
46
+
48
47
  describe "insert" do
49
48
  it "should insert a row from a hash of values" do
50
49
  results = instance.find("SELECT owner, id, val FROM #{table} WHERE owner = ?", 1)
51
- results.size.should == 0
52
- instance.insert(table, :owner => 1, :id => 2, :val => 'foo')
50
+ expect(results.size).to eq(0)
51
+ instance.insert(table, owner: 1, id: 2, val: "foo")
53
52
  results = instance.find("SELECT owner, id, val FROM #{table} WHERE owner = ?", 1)
54
- results.rows.collect{|r| r['val']}.should == ['foo']
53
+ expect(results.rows.collect { |r| r["val"] }).to eq(["foo"])
55
54
  end
56
-
55
+
57
56
  it "should add statements to the current batch" do
58
57
  instance.batch do
59
- instance.insert(table, :owner => 1, :id => 2, :val => 'foo')
58
+ instance.insert(table, owner: 1, id: 2, val: "foo")
60
59
  results = instance.find("SELECT owner, id, val FROM #{table} WHERE owner = ?", 1)
61
- results.size.should == 0
60
+ expect(results.size).to eq(0)
62
61
  end
63
62
  results = instance.find("SELECT owner, id, val FROM #{table} WHERE owner = ?", 1)
64
- results.size.should == 1
63
+ expect(results.size).to eq(1)
65
64
  end
66
65
  end
67
-
66
+
68
67
  describe "update" do
69
68
  it "should update a row from a hash of values and a primary key" do
70
- instance.insert(table, :owner => 1, :id => 2, :val => 'foo')
71
- instance.update(table, {:val => 'bar'}, :owner => 1, :id => 2)
69
+ instance.insert(table, owner: 1, id: 2, val: "foo")
70
+ instance.update(table, {val: "bar"}, owner: 1, id: 2)
72
71
  results = instance.find("SELECT owner, id, val FROM #{table} WHERE owner = ?", 1)
73
- results.rows.collect{|r| r["val"]}.should == ['bar']
72
+ expect(results.rows.collect { |r| r["val"] }).to eq(["bar"])
74
73
  end
75
74
 
76
75
  it "should add statements to the current batch" do
77
- instance.insert(table, :owner => 1, :id => 2, :val => 'foo')
76
+ instance.insert(table, owner: 1, id: 2, val: "foo")
78
77
  instance.batch do
79
- instance.update(table, {:val => 'bar'}, :owner => 1, :id => 2)
78
+ instance.update(table, {val: "bar"}, owner: 1, id: 2)
80
79
  results = instance.find("SELECT owner, id, val FROM #{table} WHERE owner = ?", 1)
81
- results.rows.collect{|r| r['val']}.should == ['foo']
80
+ expect(results.rows.collect { |r| r["val"] }).to eq(["foo"])
82
81
  end
83
82
  results = instance.find("SELECT owner, id, val FROM #{table} WHERE owner = ?", 1)
84
- results.rows.collect{|r| r['val']}.should == ['bar']
83
+ expect(results.rows.collect { |r| r["val"] }).to eq(["bar"])
85
84
  end
86
85
  end
87
-
86
+
88
87
  describe "delete" do
89
88
  it "should update a row from a primary key hash" do
90
- instance.insert(table, :owner => 1, :id => 2, :val => 'foo')
91
- instance.find("SELECT owner, id, val FROM #{table} WHERE owner = ?", 1).size.should == 1
92
- instance.delete(table, :owner => 1)
93
- instance.find("SELECT owner, id, val FROM #{table} WHERE owner = ?", 1).size.should == 0
89
+ instance.insert(table, owner: 1, id: 2, val: "foo")
90
+ expect(instance.find("SELECT owner, id, val FROM #{table} WHERE owner = ?", 1).size).to eq(1)
91
+ instance.delete(table, owner: 1)
92
+ expect(instance.find("SELECT owner, id, val FROM #{table} WHERE owner = ?", 1).size).to eq(0)
94
93
  end
95
94
 
96
95
  it "should add statements to the current batch" do
97
- instance.insert(table, :owner => 1, :id => 2, :val => 'foo')
96
+ instance.insert(table, owner: 1, id: 2, val: "foo")
98
97
  instance.batch do
99
- instance.delete(table, :owner => 1)
100
- instance.find("SELECT owner, id, val FROM #{table} WHERE owner = ?", 1).size.should == 1
98
+ instance.delete(table, owner: 1)
99
+ expect(instance.find("SELECT owner, id, val FROM #{table} WHERE owner = ?", 1).size).to eq(1)
101
100
  end
102
- instance.find("SELECT owner, id, val FROM #{table} WHERE owner = ?", 1).size.should == 0
101
+ expect(instance.find("SELECT owner, id, val FROM #{table} WHERE owner = ?", 1).size).to eq(0)
103
102
  end
104
103
  end
105
-
104
+
106
105
  describe "execute" do
107
106
  before :each do
108
- instance.insert(table, :owner => 1, :id => 2, :val => 'foo')
107
+ instance.insert(table, owner: 1, id: 2, val: "foo")
109
108
  end
110
-
109
+
111
110
  it "should execute a plain CQL statement" do
112
- instance.execute("SELECT owner, id, val FROM #{table} WHERE owner = 1").size.should == 1
111
+ expect(instance.execute("SELECT owner, id, val FROM #{table} WHERE owner = 1").size).to eq(1)
113
112
  end
114
-
113
+
115
114
  it "should execute a prepared statement" do
116
115
  statement = instance.prepare("SELECT owner, id, val FROM #{table} WHERE owner = 1")
117
- instance.execute(statement).size.should == 1
116
+ expect(instance.execute(statement).size).to eq(1)
118
117
  end
119
-
118
+
120
119
  it "should prepare and execute a CQL statement when values are provided" do
121
- instance.execute("SELECT owner, id, val FROM #{table} WHERE owner = ?", [1]).size.should == 1
120
+ expect(instance.execute("SELECT owner, id, val FROM #{table} WHERE owner = ?", [1]).size).to eq(1)
122
121
  end
123
-
122
+
124
123
  it "should call subscribers with details about the call" do
125
124
  instance.subscribers.clear
126
125
  begin
127
126
  messages = []
128
- instance.subscribers << lambda{|details| messages << details}
127
+ instance.subscribers << lambda { |details| messages << details }
129
128
 
130
129
  instance.execute("SELECT owner, id, val FROM #{table} WHERE owner = ?", [1])
131
- messages.size.should == 1
130
+ expect(messages.size).to eq(1)
132
131
  message = messages.shift
133
- message.statement.should be_a(Cassandra::Statement)
134
- message.options.should == {:arguments => [1], :consistency => :local_one}
135
- message.elapsed_time.should be_a(Float)
132
+ expect(message.statement).to be_a(Cassandra::Statement)
133
+ expect(message.options).to eq({arguments: [1], consistency: :local_one})
134
+ expect(message.elapsed_time).to be_a(Float)
136
135
 
137
136
  instance.execute("SELECT owner, id, val FROM #{table} WHERE owner = 1")
138
- messages.size.should == 1
137
+ expect(messages.size).to eq(1)
139
138
  message = messages.shift
140
- message.statement.should be_a(Cassandra::Statement)
141
- message.options.should == {:consistency => :local_one}
142
- message.elapsed_time.should be_a(Float)
139
+ expect(message.statement).to be_a(Cassandra::Statement)
140
+ expect(message.options).to eq({consistency: :local_one})
141
+ expect(message.elapsed_time).to be_a(Float)
143
142
 
144
143
  instance.batch do
145
- instance.insert(table, :owner => 1, :id => 2, :val => 'foo')
146
- instance.delete(table, :owner => 1)
144
+ instance.insert(table, owner: 1, id: 2, val: "foo")
145
+ instance.delete(table, owner: 1)
147
146
  end
148
- messages.size.should == 1
147
+ expect(messages.size).to eq(1)
149
148
  message = messages.shift
150
- message.statement.should be_a(Cassandra::Statements::Batch)
151
- message.options.should == {:consistency => :local_one}
152
- message.elapsed_time.should be_a(Float)
149
+ expect(message.statement).to be_a(Cassandra::Statements::Batch)
150
+ expect(message.options).to eq({consistency: :local_one})
151
+ expect(message.elapsed_time).to be_a(Float)
153
152
  ensure
154
153
  instance.subscribers.clear
155
154
  end
156
155
  end
157
156
  end
158
-
157
+
159
158
  describe "consistency" do
160
- let(:session){ instance.send(:session) }
161
-
159
+ let(:session) { instance.send(:session) }
160
+
162
161
  it "should not specify query consistency by default" do
163
- expect(session).to receive(:execute).with(Cassandra::Statements::Simple.new("SELECT * FROM dual"), {:consistency => :local_one})
162
+ expect(session).to receive(:execute).with(Cassandra::Statements::Simple.new("SELECT * FROM dual"), {consistency: :local_one})
164
163
  instance.execute("SELECT * FROM dual")
165
164
  expect(instance.current_consistency).to eq :local_one
166
165
  end
167
-
166
+
168
167
  it "should allow specifying the consistency in a block" do
169
- expect(session).to receive(:execute).with(Cassandra::Statements::Simple.new("SELECT * FROM dual"), {:consistency => :one})
168
+ expect(session).to receive(:execute).with(Cassandra::Statements::Simple.new("SELECT * FROM dual"), {consistency: :one})
170
169
  Cassie.consistency(:one) do
171
170
  instance.execute("SELECT * FROM dual")
172
171
  expect(instance.current_consistency).to eq :one
173
172
  end
174
173
  end
175
-
174
+
176
175
  it "should allow specifying the consistency in a block if statement consistency is explicitly nil" do
177
- expect(session).to receive(:execute).with(Cassandra::Statements::Simple.new("SELECT * FROM dual"), {:consistency => :one})
176
+ expect(session).to receive(:execute).with(Cassandra::Statements::Simple.new("SELECT * FROM dual"), {consistency: :one})
178
177
  Cassie.consistency(:one) do
179
- instance.execute("SELECT * FROM dual", nil, :consistency => nil)
178
+ instance.execute("SELECT * FROM dual", nil, consistency: nil)
180
179
  expect(instance.current_consistency).to eq :one
181
180
  end
182
181
  end
183
-
182
+
184
183
  it "should use the consistency specified to execute if provided" do
185
- expect(session).to receive(:execute).with(Cassandra::Statements::Simple.new("SELECT * FROM dual"), {:consistency => :two})
184
+ expect(session).to receive(:execute).with(Cassandra::Statements::Simple.new("SELECT * FROM dual"), {consistency: :two})
186
185
  Cassie.consistency(:one) do
187
- instance.execute("SELECT * FROM dual", nil, :consistency => :two)
186
+ instance.execute("SELECT * FROM dual", nil, consistency: :two)
188
187
  expect(instance.current_consistency).to eq :one
189
188
  end
190
189
  end
191
-
190
+
192
191
  it "should use the consistency passed in the batch for all statements in the batch" do
193
- expect(session).to receive(:execute).with(an_instance_of(Cassandra::Statements::Batch::Logged), {:consistency => :two})
194
- Cassie.instance.batch(:consistency => :two) do
195
- instance.insert(table, :owner => 1, :id => 2, :val => 'foo')
192
+ expect(session).to receive(:execute).with(an_instance_of(Cassandra::Statements::Batch::Logged), {consistency: :two})
193
+ Cassie.instance.batch(consistency: :two) do
194
+ instance.insert(table, owner: 1, id: 2, val: "foo")
196
195
  end
197
196
  end
198
-
197
+
199
198
  it "should be able to specify a global consistancy" do
200
- expect(session).to receive(:execute).with(Cassandra::Statements::Simple.new("SELECT * FROM dual"), {:consistency => :one})
201
- expect(session).to receive(:execute).with(Cassandra::Statements::Simple.new("SELECT COUNT(*) FROM dual"), {:consistency => :local_quorum})
202
- expect(session).to receive(:execute).with(Cassandra::Statements::Simple.new("SELECT COUNT(1) FROM dual"), {:consistency => :local_one})
199
+ expect(session).to receive(:execute).with(Cassandra::Statements::Simple.new("SELECT * FROM dual"), {consistency: :one})
200
+ expect(session).to receive(:execute).with(Cassandra::Statements::Simple.new("SELECT COUNT(*) FROM dual"), {consistency: :local_quorum})
201
+ expect(session).to receive(:execute).with(Cassandra::Statements::Simple.new("SELECT COUNT(1) FROM dual"), {consistency: :local_one})
203
202
  instance.execute("SELECT COUNT(1) FROM dual", nil)
204
203
  expect(instance.current_consistency).to eq :local_one
205
-
204
+
206
205
  begin
207
206
  instance.consistency = :local_quorum
208
207
  expect(instance.current_consistency).to eq :local_quorum
@@ -1,36 +1,36 @@
1
1
  class Cassie::Thing
2
2
  include Cassie::Model
3
-
3
+
4
4
  self.table_name = "things"
5
5
  self.keyspace = "test"
6
6
  self.primary_key = [:owner, :id]
7
7
  self.read_consistency = :one
8
8
  self.write_consistency = :quorum
9
-
9
+
10
10
  column :owner, :int
11
- column :id, :int, :as => :identifier
12
- column :val, :varchar, :as => :value
13
-
11
+ column :id, :int, as: :identifier
12
+ column :val, :varchar, as: :value
13
+
14
14
  ordering_key :id, :desc
15
-
15
+
16
16
  validates_presence_of :owner, :id
17
-
17
+
18
18
  before_save do
19
19
  callbacks << :save
20
20
  end
21
-
21
+
22
22
  before_create do
23
23
  callbacks << :create
24
24
  end
25
-
25
+
26
26
  before_update do
27
27
  callbacks << :update
28
28
  end
29
-
29
+
30
30
  before_destroy do
31
31
  callbacks << :destroy
32
32
  end
33
-
33
+
34
34
  def callbacks
35
35
  @callbacks ||= []
36
36
  end
@@ -1,12 +1,12 @@
1
- require 'securerandom'
1
+ require "securerandom"
2
2
 
3
3
  class Cassie::TypeTester
4
4
  include Cassie::Model
5
-
5
+
6
6
  self.table_name = "type_testers"
7
7
  self.keyspace = "test"
8
8
  self.primary_key = [:id]
9
-
9
+
10
10
  column :id, :varchar
11
11
  column :int_value, :int
12
12
  column :varint_value, :varint
@@ -26,17 +26,17 @@ class Cassie::TypeTester
26
26
  column :list_value, :list
27
27
  column :set_value, :set
28
28
  column :map_value, :map
29
-
30
- before_create{ self.id = SecureRandom.uuid }
29
+
30
+ before_create { self.id = SecureRandom.uuid }
31
31
  end
32
32
 
33
33
  class Cassie::TypeTesterCounter
34
34
  include Cassie::Model
35
-
35
+
36
36
  self.table_name = "type_tester_counters"
37
37
  self.keyspace = "test"
38
38
  self.primary_key = [:id]
39
-
39
+
40
40
  column :id, :varchar
41
41
  column :counter_value, :counter, as: :counter_column
42
42
  end