cassanity 0.5.1 → 0.6.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +1 -1
  3. data/README.md +3 -16
  4. data/cassanity.gemspec +3 -1
  5. data/examples/keyspaces.rb +2 -2
  6. data/lib/cassanity/argument_generators/column_families.rb +1 -1
  7. data/lib/cassanity/argument_generators/columns.rb +2 -2
  8. data/lib/cassanity/argument_generators/keyspace_create.rb +4 -21
  9. data/lib/cassanity/argument_generators/with_clause.rb +2 -9
  10. data/lib/cassanity/client.rb +16 -13
  11. data/lib/cassanity/executors/{cassandra_cql.rb → cql_rb.rb} +28 -11
  12. data/lib/cassanity/keyspace.rb +7 -22
  13. data/lib/cassanity/migrator.rb +2 -2
  14. data/lib/cassanity/result_transformers/column_families.rb +1 -1
  15. data/lib/cassanity/result_transformers/columns.rb +2 -2
  16. data/lib/cassanity/result_transformers/keyspaces.rb +1 -1
  17. data/lib/cassanity/result_transformers/result_to_array.rb +1 -5
  18. data/lib/cassanity/retry_strategies/retry_strategy.rb +3 -3
  19. data/lib/cassanity/statement.rb +58 -0
  20. data/lib/cassanity/version.rb +1 -1
  21. data/spec/helper.rb +2 -4
  22. data/spec/integration/cassanity/column_family_spec.rb +62 -64
  23. data/spec/integration/cassanity/connection_spec.rb +2 -8
  24. data/spec/integration/cassanity/instrumentation/log_subscriber_spec.rb +4 -1
  25. data/spec/integration/cassanity/instrumentation/metriks_subscriber_spec.rb +2 -1
  26. data/spec/integration/cassanity/instrumentation/statsd_subscriber_spec.rb +2 -1
  27. data/spec/integration/cassanity/keyspace_spec.rb +1 -1
  28. data/spec/integration/cassanity/migration_spec.rb +5 -5
  29. data/spec/integration/cassanity/migrator_spec.rb +4 -4
  30. data/spec/support/cassanity_helpers.rb +7 -5
  31. data/spec/unit/cassanity/argument_generators/column_families_spec.rb +1 -1
  32. data/spec/unit/cassanity/argument_generators/columns_spec.rb +3 -3
  33. data/spec/unit/cassanity/argument_generators/keyspace_create_spec.rb +12 -16
  34. data/spec/unit/cassanity/argument_generators/with_clause_spec.rb +5 -6
  35. data/spec/unit/cassanity/client_spec.rb +15 -53
  36. data/spec/unit/cassanity/connection_spec.rb +6 -6
  37. data/spec/unit/cassanity/keyspace_spec.rb +12 -14
  38. data/spec/unit/cassanity/result_transformers/result_to_array_spec.rb +3 -16
  39. data/spec/unit/cassanity/statement_spec.rb +100 -0
  40. metadata +35 -24
  41. data/spec/unit/cassanity/executors/cassandra_cql_spec.rb +0 -286
@@ -1,286 +0,0 @@
1
- require 'helper'
2
- require 'cassanity/executors/cassandra_cql'
3
- require 'cassanity/instrumenters/memory'
4
-
5
- describe Cassanity::Executors::CassandraCql do
6
- let(:driver) { double('Client', :execute => nil) }
7
-
8
- let(:required_arguments) {
9
- {
10
- driver: driver,
11
- }
12
- }
13
-
14
- let(:argument_generators) {
15
- {
16
- :foo => lambda { |*args| ['mapped', *args] },
17
- }
18
- }
19
-
20
- let(:result_transformers) {
21
- {
22
- :foo => lambda { |*args| ['transformed', *args] }
23
- }
24
- }
25
-
26
- subject { described_class.new(required_arguments) }
27
-
28
- describe "#initialize" do
29
- [:driver].each do |key|
30
- it "raises error without :#{key} key" do
31
- args = required_arguments.reject { |k, v| k == key }
32
- expect { described_class.new(args) }.to raise_error(KeyError)
33
- end
34
- end
35
-
36
- it "sets driver" do
37
- subject.driver.should eq(driver)
38
- end
39
-
40
- it "sets a default retry strategy when none is passed" do
41
- subject.retry_strategy.should be_an_instance_of(Cassanity::RetryStrategies::RetryNTimes)
42
- end
43
-
44
- it "defaults :argument_generators" do
45
- subject.argument_generators.should eq(described_class::DefaultArgumentGenerators)
46
- end
47
-
48
- it "defaults :result_transformers" do
49
- subject.result_transformers.should eq(described_class::DefaultResultTransformers)
50
- end
51
-
52
- it "defaults :instrumenter" do
53
- subject.instrumenter.should eq(Cassanity::Instrumenters::Noop)
54
- end
55
-
56
- it "defaults instrumenter if nil is passed in" do
57
- instance = described_class.new(required_arguments.merge({
58
- instrumenter: nil,
59
- }))
60
- instance.instrumenter.should eq(Cassanity::Instrumenters::Noop)
61
- end
62
-
63
- it "allows overriding :argument_generators" do
64
- instance = described_class.new(required_arguments.merge({
65
- argument_generators: argument_generators
66
- }))
67
-
68
- instance.argument_generators.should eq(argument_generators)
69
- end
70
-
71
- it "allows overriding :result_transformers" do
72
- instance = described_class.new(required_arguments.merge({
73
- result_transformers: result_transformers
74
- }))
75
-
76
- instance.result_transformers.should eq(result_transformers)
77
- end
78
- end
79
-
80
- KnownCommands = [
81
- :keyspaces,
82
- :keyspace_create,
83
- :keyspace_drop,
84
- :keyspace_use,
85
- :column_families,
86
- :column_family_create,
87
- :column_family_drop,
88
- :column_family_truncate,
89
- :column_family_select,
90
- :column_family_insert,
91
- :column_family_update,
92
- :column_family_delete,
93
- :column_family_alter,
94
- :index_create,
95
- :index_drop,
96
- :batch,
97
- :columns,
98
- ]
99
-
100
- KnownCommands.each do |key|
101
- it "responds to #{key} command by default" do
102
- subject.argument_generators.should have_key(key)
103
- end
104
- end
105
-
106
- describe "#call" do
107
- subject {
108
- described_class.new(required_arguments.merge({
109
- argument_generators: argument_generators,
110
- }))
111
- }
112
-
113
- context "for known command" do
114
- it "generates arguments based on command to argument map and passes generated arguments driver execute method" do
115
- args = {
116
- command: :foo,
117
- arguments: {
118
- something: 'else',
119
- },
120
- }
121
-
122
- driver.should_receive(:execute).with('mapped', args[:arguments])
123
- subject.call(args)
124
- end
125
-
126
- context "with instrumenter" do
127
- let(:instrumenter) { Cassanity::Instrumenters::Memory.new }
128
-
129
- subject {
130
- described_class.new(required_arguments.merge({
131
- argument_generators: argument_generators,
132
- instrumenter: instrumenter,
133
- }))
134
- }
135
-
136
- it "instruments executed arguments" do
137
- args = {
138
- command: :foo,
139
- arguments: {
140
- something: 'else',
141
- },
142
- }
143
-
144
- subject.call(args)
145
-
146
- event = instrumenter.events.last
147
- event.should_not be_nil
148
- event.name.should eq('cql.cassanity')
149
- event.payload.should eq({
150
- command: :foo,
151
- result: nil,
152
- cql: 'mapped',
153
- cql_variables: [{something: 'else'}],
154
- attempts: 1,
155
- })
156
- end
157
-
158
- context "with retries" do
159
- let(:retry_strategy) { Cassanity::RetryStrategies::RetryStrategy.new }
160
- let(:driver) { double('Driver') }
161
-
162
- subject {
163
- described_class.new(required_arguments.merge({
164
- driver: driver,
165
- argument_generators: argument_generators,
166
- instrumenter: instrumenter,
167
- retry_strategy: retry_strategy,
168
- }))
169
- }
170
-
171
- it "adds retry count to the cql.cassanity payload" do
172
- retry_strategy.should_receive(:fail).once
173
-
174
- args = {
175
- command: :foo,
176
- arguments: {
177
- something: 'else',
178
- },
179
- }
180
-
181
- i = 0
182
- driver.stub(:execute) {
183
- i += 1
184
- if i > 1
185
- 'ok!'
186
- else
187
- raise cassandra_error('not ok!')
188
- end
189
- }
190
-
191
- subject.call(args)
192
-
193
- event = instrumenter.events.last
194
- event.should_not be_nil
195
- event.name.should eq('cql.cassanity')
196
- event.payload.should eq({
197
- command: :foo,
198
- result: 'ok!',
199
- cql: 'mapped',
200
- cql_variables: [{something: 'else'}],
201
- attempts: 2,
202
- })
203
- end
204
- end
205
- end
206
-
207
- context "with result transformer" do
208
- subject {
209
- described_class.new(required_arguments.merge({
210
- argument_generators: argument_generators,
211
- result_transformers: result_transformers,
212
- }))
213
- }
214
-
215
- it "returns result transformed" do
216
- result = double('Result')
217
- driver.stub(:execute => result)
218
- tranformer = result_transformers[:foo]
219
-
220
- args = {
221
- command: :foo,
222
- arguments: {
223
- something: 'else',
224
- },
225
- }
226
-
227
- subject.call(args).should eq(['transformed', result, nil])
228
- end
229
- end
230
-
231
- context "without result transformer" do
232
- subject {
233
- described_class.new(required_arguments.merge({
234
- argument_generators: argument_generators,
235
- result_transformers: {},
236
- }))
237
- }
238
-
239
- it "returns result transformed" do
240
- result = double('Result')
241
- driver.stub(:execute => result)
242
- tranformer = result_transformers[:foo]
243
-
244
- args = {
245
- command: :foo,
246
- arguments: {
247
- something: 'else',
248
- },
249
- }
250
-
251
- subject.call(args).should eq(result)
252
- end
253
- end
254
- end
255
-
256
- context "for unknown command" do
257
- it "generates arguments based on command to argument map and passes
258
- generated arguments driver execute method" do
259
- expect {
260
- subject.call({
261
- command: :surprise,
262
- })
263
- }.to raise_error(Cassanity::UnknownCommand, 'Original Exception: KeyError: key not found: :surprise')
264
- end
265
- end
266
-
267
- context "when driver raises exception" do
268
- it "raises Cassanity::Error" do
269
- driver.should_receive(:execute).and_raise(StandardError.new)
270
- expect {
271
- subject.call({
272
- command: :foo,
273
- })
274
- }.to raise_error(Cassanity::Error, /StandardError: StandardError/)
275
- end
276
- end
277
- end
278
-
279
- describe "#inspect" do
280
- it "return representation" do
281
- result = subject.inspect
282
- result.should match(/#{described_class}/)
283
- result.should match(/driver=/)
284
- end
285
- end
286
- end