cql-rb 1.2.2 → 2.0.0.pre0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +4 -0
  3. data/README.md +139 -17
  4. data/lib/cql/client.rb +237 -8
  5. data/lib/cql/client/asynchronous_client.rb +138 -54
  6. data/lib/cql/client/asynchronous_prepared_statement.rb +41 -6
  7. data/lib/cql/client/authenticators.rb +46 -0
  8. data/lib/cql/client/batch.rb +115 -0
  9. data/lib/cql/client/connector.rb +255 -0
  10. data/lib/cql/client/execute_options_decoder.rb +25 -9
  11. data/lib/cql/client/keyspace_changer.rb +5 -5
  12. data/lib/cql/client/peer_discovery.rb +33 -0
  13. data/lib/cql/client/query_result.rb +124 -1
  14. data/lib/cql/client/request_runner.rb +4 -2
  15. data/lib/cql/client/synchronous_client.rb +14 -2
  16. data/lib/cql/client/synchronous_prepared_statement.rb +19 -1
  17. data/lib/cql/future.rb +97 -50
  18. data/lib/cql/io/connection.rb +0 -1
  19. data/lib/cql/io/io_reactor.rb +1 -1
  20. data/lib/cql/protocol.rb +8 -1
  21. data/lib/cql/protocol/cql_protocol_handler.rb +2 -2
  22. data/lib/cql/protocol/decoding.rb +10 -15
  23. data/lib/cql/protocol/frame_decoder.rb +2 -1
  24. data/lib/cql/protocol/frame_encoder.rb +5 -4
  25. data/lib/cql/protocol/requests/auth_response_request.rb +31 -0
  26. data/lib/cql/protocol/requests/batch_request.rb +59 -0
  27. data/lib/cql/protocol/requests/credentials_request.rb +1 -1
  28. data/lib/cql/protocol/requests/execute_request.rb +45 -17
  29. data/lib/cql/protocol/requests/options_request.rb +1 -1
  30. data/lib/cql/protocol/requests/prepare_request.rb +1 -1
  31. data/lib/cql/protocol/requests/query_request.rb +97 -5
  32. data/lib/cql/protocol/requests/register_request.rb +1 -1
  33. data/lib/cql/protocol/requests/startup_request.rb +4 -4
  34. data/lib/cql/protocol/response.rb +2 -2
  35. data/lib/cql/protocol/responses/auth_challenge_response.rb +25 -0
  36. data/lib/cql/protocol/responses/auth_success_response.rb +25 -0
  37. data/lib/cql/protocol/responses/authenticate_response.rb +1 -1
  38. data/lib/cql/protocol/responses/detailed_error_response.rb +1 -1
  39. data/lib/cql/protocol/responses/error_response.rb +3 -2
  40. data/lib/cql/protocol/responses/event_response.rb +3 -2
  41. data/lib/cql/protocol/responses/prepared_result_response.rb +10 -6
  42. data/lib/cql/protocol/responses/raw_rows_result_response.rb +27 -0
  43. data/lib/cql/protocol/responses/ready_response.rb +1 -1
  44. data/lib/cql/protocol/responses/result_response.rb +2 -2
  45. data/lib/cql/protocol/responses/rows_result_response.rb +43 -23
  46. data/lib/cql/protocol/responses/schema_change_event_response.rb +1 -1
  47. data/lib/cql/protocol/responses/schema_change_result_response.rb +1 -1
  48. data/lib/cql/protocol/responses/set_keyspace_result_response.rb +1 -1
  49. data/lib/cql/protocol/responses/status_change_event_response.rb +1 -1
  50. data/lib/cql/protocol/responses/supported_response.rb +1 -1
  51. data/lib/cql/protocol/responses/void_result_response.rb +1 -1
  52. data/lib/cql/protocol/type_converter.rb +2 -2
  53. data/lib/cql/uuid.rb +2 -2
  54. data/lib/cql/version.rb +1 -1
  55. data/spec/cql/client/asynchronous_client_spec.rb +493 -50
  56. data/spec/cql/client/asynchronous_prepared_statement_spec.rb +193 -11
  57. data/spec/cql/client/authenticators_spec.rb +56 -0
  58. data/spec/cql/client/batch_spec.rb +277 -0
  59. data/spec/cql/client/connector_spec.rb +606 -0
  60. data/spec/cql/client/execute_options_decoder_spec.rb +95 -0
  61. data/spec/cql/client/keyspace_changer_spec.rb +8 -8
  62. data/spec/cql/client/peer_discovery_spec.rb +92 -0
  63. data/spec/cql/client/query_result_spec.rb +352 -0
  64. data/spec/cql/client/request_runner_spec.rb +31 -5
  65. data/spec/cql/client/synchronous_client_spec.rb +44 -1
  66. data/spec/cql/client/synchronous_prepared_statement_spec.rb +63 -1
  67. data/spec/cql/future_spec.rb +50 -2
  68. data/spec/cql/protocol/cql_protocol_handler_spec.rb +16 -5
  69. data/spec/cql/protocol/decoding_spec.rb +16 -6
  70. data/spec/cql/protocol/encoding_spec.rb +3 -1
  71. data/spec/cql/protocol/frame_encoder_spec.rb +99 -50
  72. data/spec/cql/protocol/requests/auth_response_request_spec.rb +62 -0
  73. data/spec/cql/protocol/requests/batch_request_spec.rb +155 -0
  74. data/spec/cql/protocol/requests/credentials_request_spec.rb +1 -1
  75. data/spec/cql/protocol/requests/execute_request_spec.rb +184 -71
  76. data/spec/cql/protocol/requests/options_request_spec.rb +1 -1
  77. data/spec/cql/protocol/requests/prepare_request_spec.rb +1 -1
  78. data/spec/cql/protocol/requests/query_request_spec.rb +255 -32
  79. data/spec/cql/protocol/requests/register_request_spec.rb +1 -1
  80. data/spec/cql/protocol/requests/startup_request_spec.rb +12 -6
  81. data/spec/cql/protocol/responses/auth_challenge_response_spec.rb +31 -0
  82. data/spec/cql/protocol/responses/auth_success_response_spec.rb +31 -0
  83. data/spec/cql/protocol/responses/authenticate_response_spec.rb +2 -1
  84. data/spec/cql/protocol/responses/detailed_error_response_spec.rb +14 -7
  85. data/spec/cql/protocol/responses/error_response_spec.rb +4 -2
  86. data/spec/cql/protocol/responses/event_response_spec.rb +7 -4
  87. data/spec/cql/protocol/responses/prepared_result_response_spec.rb +89 -34
  88. data/spec/cql/protocol/responses/raw_rows_result_response_spec.rb +66 -0
  89. data/spec/cql/protocol/responses/ready_response_spec.rb +1 -1
  90. data/spec/cql/protocol/responses/result_response_spec.rb +19 -7
  91. data/spec/cql/protocol/responses/rows_result_response_spec.rb +56 -11
  92. data/spec/cql/protocol/responses/schema_change_event_response_spec.rb +2 -1
  93. data/spec/cql/protocol/responses/schema_change_result_response_spec.rb +2 -1
  94. data/spec/cql/protocol/responses/set_keyspace_result_response_spec.rb +1 -1
  95. data/spec/cql/protocol/responses/status_change_event_response_spec.rb +2 -1
  96. data/spec/cql/protocol/responses/supported_response_spec.rb +2 -1
  97. data/spec/cql/protocol/responses/topology_change_event_response_spec.rb +2 -1
  98. data/spec/cql/protocol/responses/void_result_response_spec.rb +1 -1
  99. data/spec/cql/protocol/type_converter_spec.rb +21 -4
  100. data/spec/cql/uuid_spec.rb +10 -3
  101. data/spec/integration/client_spec.rb +251 -28
  102. data/spec/integration/protocol_spec.rb +213 -62
  103. data/spec/integration/regression_spec.rb +4 -1
  104. data/spec/integration/uuid_spec.rb +4 -1
  105. data/spec/support/fake_io_reactor.rb +5 -5
  106. metadata +36 -7
  107. data/lib/cql/client/connection_helper.rb +0 -181
  108. data/spec/cql/client/connection_helper_spec.rb +0 -429
@@ -14,7 +14,7 @@ module Cql
14
14
 
15
15
  describe '#write' do
16
16
  it 'encodes an OPTIONS request frame (i.e. an empty body)' do
17
- bytes = OptionsRequest.new.write('')
17
+ bytes = OptionsRequest.new.write(1, '')
18
18
  bytes.should be_empty
19
19
  end
20
20
  end
@@ -14,7 +14,7 @@ module Cql
14
14
 
15
15
  describe '#write' do
16
16
  it 'encodes a PREPARE request frame' do
17
- bytes = PrepareRequest.new('UPDATE users SET email = ? WHERE user_name = ?').write('')
17
+ bytes = PrepareRequest.new('UPDATE users SET email = ? WHERE user_name = ?').write(1, '')
18
18
  bytes.should == "\x00\x00\x00\x2eUPDATE users SET email = ? WHERE user_name = ?"
19
19
  end
20
20
  end
@@ -8,93 +8,316 @@ module Cql
8
8
  describe QueryRequest do
9
9
  describe '#initialize' do
10
10
  it 'raises an error when the CQL is nil' do
11
- expect { QueryRequest.new(nil, :one) }.to raise_error(ArgumentError)
11
+ expect { QueryRequest.new(nil, [], [], :one, nil, nil, nil, false) }.to raise_error(ArgumentError)
12
12
  end
13
13
 
14
14
  it 'raises an error when the consistency is nil' do
15
- expect { QueryRequest.new('USE system', nil) }.to raise_error(ArgumentError)
15
+ expect { QueryRequest.new('USE system', [], [], nil, nil, nil, nil, false) }.to raise_error(ArgumentError)
16
16
  end
17
17
 
18
18
  it 'raises an error when the consistency is invalid' do
19
- expect { QueryRequest.new('USE system', :hello) }.to raise_error(ArgumentError)
19
+ expect { QueryRequest.new('USE system', [], [], :hello, nil, nil, nil, false) }.to raise_error(ArgumentError)
20
+ end
21
+
22
+ it 'raises an error when the serial consistency is invalid' do
23
+ expect { QueryRequest.new('USE system', [], [], :quorum, :fantastic_serial, nil, nil, false) }.to raise_error(ArgumentError)
24
+ end
25
+
26
+ it 'raises an error when there are not the same number of type hints as bound values' do
27
+ expect { QueryRequest.new('SELECT * FROM foo WHERE a = ? AND b = ?', ['x', 'y'], [:string], :quorum, nil, nil, nil, false) }.to raise_error(ArgumentError)
28
+ end
29
+
30
+ it 'raises an error when a paging state is given but no page size' do
31
+ expect { QueryRequest.new('USE system', [], [], :quorum, nil, nil, 'foo', false) }.to raise_error(ArgumentError)
20
32
  end
21
33
  end
22
34
 
23
35
  describe '#write' do
24
- it 'encodes a QUERY request frame' do
25
- bytes = QueryRequest.new('USE system', :all).write('')
26
- bytes.should == "\x00\x00\x00\x0aUSE system\x00\x05"
36
+ context 'when the protocol version is 1' do
37
+ let :frame_bytes do
38
+ QueryRequest.new('USE system', [], [], :all, nil, nil, nil, false).write(1, '')
39
+ end
40
+
41
+ it 'encodes the CQL' do
42
+ frame_bytes.to_s[0, 14].should == "\x00\x00\x00\x0aUSE system"
43
+ end
44
+
45
+ it 'encodes the consistency' do
46
+ frame_bytes.to_s[14, 999].should == "\x00\x05"
47
+ end
27
48
  end
28
49
 
29
- it 'encodes a QUERY request frame with tracing' do
30
- bytes = QueryRequest.new('USE system', :all, true).write('')
31
- bytes.should == "\x00\x00\x00\x0aUSE system\x00\x05"
50
+ context 'when the protocol version is 2' do
51
+ context 'and there are no bound values' do
52
+ let :frame_bytes do
53
+ QueryRequest.new('USE system', [], [], :all, nil, nil, nil, false).write(2, '')
54
+ end
55
+
56
+ it 'encodes the CQL' do
57
+ frame_bytes.to_s[0, 14].should == "\x00\x00\x00\x0aUSE system"
58
+ end
59
+
60
+ it 'encodes the consistency' do
61
+ frame_bytes.to_s[14, 2].should == "\x00\x05"
62
+ end
63
+
64
+ it 'encodes an empty flag field' do
65
+ frame_bytes.to_s[16, 999].should == "\x00"
66
+ end
67
+
68
+ it 'accepts that the bound values list is nil' do
69
+ frame_bytes = QueryRequest.new('USE system', nil, [], :all, nil, nil, nil, false).write(2, '')
70
+ frame_bytes.to_s[16, 999].should == "\x00"
71
+ end
72
+
73
+ it 'accepts that the type hints list is nil' do
74
+ expect { QueryRequest.new('SELECT * FROM foo WHERE a = ? AND b = ?', ['x', 'y'], nil, :all, nil, nil, nil, false).write(2, '') }.to_not raise_error
75
+ end
76
+ end
77
+
78
+ context 'and there are bound values' do
79
+ let :cql do
80
+ 'SELECT * FROM something WHERE id = ?'
81
+ end
82
+
83
+ let :frame_bytes do
84
+ QueryRequest.new(cql, ['foobar'], nil, :all, nil, nil, nil, false).write(2, '')
85
+ end
86
+
87
+ it 'encodes the CQL' do
88
+ frame_bytes.to_s[0, 40].should == "\x00\x00\x00\x24SELECT * FROM something WHERE id = ?"
89
+ end
90
+
91
+ it 'encodes the consistency' do
92
+ frame_bytes.to_s[40, 2].should == "\x00\x05"
93
+ end
94
+
95
+ it 'encodes a flags field with the values flag set' do
96
+ frame_bytes.to_s[42, 1].should == "\x01"
97
+ end
98
+
99
+ it 'encodes the number of bound values' do
100
+ frame_bytes.to_s[43, 2].should == "\x00\x01"
101
+ end
102
+
103
+ it 'encodes the bound values' do
104
+ frame_bytes.to_s[45, 999].should == "\x00\x00\x00\x06foobar"
105
+ end
106
+
107
+ [
108
+ ['foobar', 'VARCHAR', "\x00\x00\x00\x06foobar"],
109
+ [765438000, 'BIGINT', "\x00\x00\x00\x08\x00\x00\x00\x00\x2d\x9f\xa8\x30"],
110
+ [Math::PI, 'DOUBLE', "\x00\x00\x00\x08\x40\x09\x21\xfb\x54\x44\x2d\x18"],
111
+ [67890656781923123918798273492834712837198237, 'VARINT', "\x00\x00\x00\x13\x03\x0b\x58\xb5\x74\x0a\xce\x65\x95\xb4\x03\x26\x7b\x6b\x6a\x6e\x08\x91\x9d"],
112
+ [BigDecimal.new('1313123123.234234234234234234123'), 'DECIMAL', "\x00\x00\x00\x11\x00\x00\x00\x15\x10\x92\xed\xfd\x4b\x93\x4b\xd7\xa2\xc1\x0c\x65\x0b"],
113
+ [true, 'BOOLEAN', "\x00\x00\x00\x01\x01"],
114
+ [nil, 'null', "\xff\xff\xff\xff"],
115
+ [Uuid.new('00b69180-d0e1-11e2-8b8b-0800200c9a66'), 'UUID', "\x00\x00\x00\x10\x00\xb6\x91\x80\xd0\xe1\x11\xe2\x8b\x8b\x08\x00\x20\x0c\x9a\x66"],
116
+ [IPAddr.new('200.199.198.197'), 'INET', "\x00\x00\x00\x04\xc8\xc7\xc6\xc5"],
117
+ [IPAddr.new('2001:0db8:85a3:0000:0000:8a2e:0370:7334'), 'INET', "\x00\x00\x00\x10\x20\x01\x0d\xb8\x85\xa3\x00\x00\x00\x00\x8a\x2e\x03\x70\x73\x34"],
118
+ [Time.utc(2013, 12, 11, 10, 9, 8), 'TIMESTAMP', "\x00\x00\x00\x08\x00\x00\x01\x42\xe1\x21\xa5\xa0"],
119
+ [{'foo' => true}, 'MAP<STRING,BOOLEAN>', "\x00\x00\x00\x0a\x00\x01\x00\x03foo\x00\x01\x01"],
120
+ [[1, 2], 'LIST<BIGINT>', "\x00\x00\x00\x16\x00\x02\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00\x08\x00\x00\x00\x00\x00\x00\x00\x02"],
121
+ [[Math::PI, Math::PI/2].to_set, 'SET<DOUBLE>', "\x00\x00\x00\x16\x00\x02\x00\x08\x40\x09\x21\xfb\x54\x44\x2d\x18\x00\x08\x3f\xf9\x21\xfb\x54\x44\x2d\x18"],
122
+ ].each do |value, cql_type, expected_bytes|
123
+ it "encodes bound #{value.class}s as #{cql_type}" do
124
+ frame_bytes = QueryRequest.new(cql, [value], nil, :all, nil, nil, nil, false).write(2, '')
125
+ frame_bytes.to_s[45, 999].should == expected_bytes
126
+ end
127
+ end
128
+
129
+ it 'complains if it cannot guess the type to encode a value as' do
130
+ expect { QueryRequest.new(cql, [self], nil, :all, nil, nil, nil, false).write(2, '') }.to raise_error(EncodingError)
131
+ end
132
+
133
+ it 'uses the type hints to encode values' do
134
+ frame_bytes = QueryRequest.new(cql, [4, 3.14], [:int, :float], :all, nil, nil, nil, false).write(2, '')
135
+ frame_bytes.to_s[45, 8].should == "\x00\x00\x00\x04\x00\x00\x00\x04"
136
+ frame_bytes.to_s[45 + 8, 8].should == "\x00\x00\x00\x04\x40\x48\xf5\xc3"
137
+ end
138
+
139
+ it 'accepts that some hints are nil and defaults to guessing' do
140
+ frame_bytes = QueryRequest.new(cql, [4, 4], [:int, nil], :all, nil, nil, nil, false).write(2, '')
141
+ frame_bytes.to_s[45, 8].should == "\x00\x00\x00\x04\x00\x00\x00\x04"
142
+ frame_bytes.to_s[45 + 8, 12].should == "\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x04"
143
+ end
144
+ end
145
+
146
+ context 'and the serial consistency is LOCAL_SERIAL' do
147
+ it 'sets the serial flag' do
148
+ frame_bytes = QueryRequest.new('UPDATE x SET y = 3 WHERE z = 4 IF w = 6', nil, nil, :two, :local_serial, nil, nil, false).write(2, '')
149
+ frame_bytes.to_s[43, 2].should == "\x00\x02"
150
+ frame_bytes.to_s[45, 1].should == "\x10"
151
+ frame_bytes.to_s[46, 2].should == "\x00\x09"
152
+ end
153
+ end
154
+
155
+ context 'and page size and/or page state is set' do
156
+ it 'sets the page size flag and includes the page size' do
157
+ frame_bytes = QueryRequest.new('SELECT * FROM users', nil, nil, :one, nil, 10, nil, false).write(2, '')
158
+ frame_bytes.to_s[25, 1].should == "\x04"
159
+ frame_bytes.to_s[26, 4].should == "\x00\x00\x00\x0a"
160
+ end
161
+
162
+ it 'sets both the page size and paging state flags and includes both the page size and the paging state' do
163
+ frame_bytes = QueryRequest.new('SELECT * FROM users', nil, nil, :one, nil, 10, 'foo', false).write(2, '')
164
+ frame_bytes.to_s[25, 1].should == "\x0c"
165
+ frame_bytes.to_s[26, 4].should == "\x00\x00\x00\x0a"
166
+ frame_bytes.to_s[30, 7].should == "\x00\x00\x00\x03foo"
167
+ end
168
+ end
32
169
  end
33
170
 
34
- it 'correctly encodes queries with multibyte characters' do
35
- bytes = QueryRequest.new("INSERT INTO users (user_id, first, last, age) VALUES ('test', 'ümlaut', 'test', 1)", :all).write('')
36
- bytes.should eql_bytes("\x00\x00\x00SINSERT INTO users (user_id, first, last, age) VALUES ('test', '\xC3\xBCmlaut', 'test', 1)\x00\x05")
171
+ context 'with multibyte characters' do
172
+ it 'correctly encodes the frame' do
173
+ bytes = QueryRequest.new("INSERT INTO users (user_id, first, last, age) VALUES ('test', 'ümlaut', 'test', 1)", nil, nil, :all, nil, nil, nil, false).write(1, '')
174
+ bytes.should eql_bytes("\x00\x00\x00SINSERT INTO users (user_id, first, last, age) VALUES ('test', '\xC3\xBCmlaut', 'test', 1)\x00\x05")
175
+ end
37
176
  end
38
177
  end
39
178
 
40
179
  describe '#to_s' do
41
180
  it 'returns a pretty string' do
42
- request = QueryRequest.new('SELECT * FROM system.peers', :local_quorum)
181
+ request = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :local_quorum, nil, nil, nil, false)
43
182
  request.to_s.should == 'QUERY "SELECT * FROM system.peers" LOCAL_QUORUM'
44
183
  end
45
184
  end
46
185
 
47
186
  describe '#eql?' do
48
187
  it 'returns true when the CQL and consistency are the same' do
49
- q1 = QueryRequest.new('SELECT * FROM system.peers', :two)
50
- q2 = QueryRequest.new('SELECT * FROM system.peers', :two)
188
+ q1 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, nil, nil, nil, false)
189
+ q2 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, nil, nil, nil, false)
51
190
  q2.should eql(q2)
52
191
  end
53
192
 
193
+ it 'returns false when the CQL is different' do
194
+ q1 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, nil, nil, nil, false)
195
+ q2 = QueryRequest.new('SELECT * FROM peers', nil, nil, :two, nil, nil, nil, false)
196
+ q1.should_not eql(q2)
197
+ end
198
+
199
+ it 'returns false when the values are different' do
200
+ q1 = QueryRequest.new('SELECT * FROM peers WHERE data_center = ?', ['dc1'], nil, :two, nil, nil, nil, false)
201
+ q2 = QueryRequest.new('SELECT * FROM peers WHERE data_center = ?', ['dc2'], nil, :two, nil, nil, nil, false)
202
+ q1.should_not eql(q2)
203
+ end
204
+
205
+ it 'returns false when the type hints are different' do
206
+ q1 = QueryRequest.new('SELECT * FROM peers WHERE data_center = ?', ['dc1'], [:text], :two, nil, nil, nil, false)
207
+ q2 = QueryRequest.new('SELECT * FROM peers WHERE data_center = ?', ['dc1'], [:varchar], :two, nil, nil, nil, false)
208
+ q1.should_not eql(q2)
209
+ end
210
+
54
211
  it 'returns false when the consistency is different' do
55
- q1 = QueryRequest.new('SELECT * FROM system.peers', :two)
56
- q2 = QueryRequest.new('SELECT * FROM system.peers', :three)
212
+ q1 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, nil, nil, nil, false)
213
+ q2 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :three, nil, nil, nil, false)
57
214
  q1.should_not eql(q2)
58
215
  end
59
216
 
60
- it 'returns false when the CQL is different' do
61
- q1 = QueryRequest.new('SELECT * FROM system.peers', :two)
62
- q2 = QueryRequest.new('SELECT * FROM peers', :two)
217
+ it 'returns false when the serial consistency is different' do
218
+ q1 = QueryRequest.new('SELECT * FROM peers', nil, nil, :two, :local_serial, nil, nil, false)
219
+ q2 = QueryRequest.new('SELECT * FROM peers', nil, nil, :two, :serial, nil, nil, false)
220
+ q1.should_not eql(q2)
221
+ end
222
+
223
+ it 'returns false when the page size is different' do
224
+ q1 = QueryRequest.new('SELECT * FROM peers', nil, nil, :two, nil, 10, nil, false)
225
+ q2 = QueryRequest.new('SELECT * FROM peers', nil, nil, :two, nil, 20, nil, false)
226
+ q1.should_not eql(q2)
227
+ end
228
+
229
+ it 'returns false when the paging state is different' do
230
+ q1 = QueryRequest.new('SELECT * FROM peers', nil, nil, :two, nil, 10, 'foo', false)
231
+ q2 = QueryRequest.new('SELECT * FROM peers', nil, nil, :two, nil, 10, 'bar', false)
63
232
  q1.should_not eql(q2)
64
233
  end
65
234
 
66
235
  it 'does not know about CQL syntax' do
67
- q1 = QueryRequest.new('SELECT * FROM system.peers', :two)
68
- q2 = QueryRequest.new('SELECT * FROM system.peers', :two)
236
+ q1 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, nil, nil, nil, false)
237
+ q2 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, nil, nil, nil, false)
69
238
  q1.should_not eql(q2)
70
239
  end
71
240
 
241
+ it 'does not make a difference between an empty values array and nil' do
242
+ q1 = QueryRequest.new('SELECT * FROM peers', [], nil, :two, nil, nil, nil, false)
243
+ q2 = QueryRequest.new('SELECT * FROM peers', nil, nil, :two, nil, nil, nil, false)
244
+ q1.should eql(q2)
245
+ end
246
+
247
+ it 'does not make a difference between an empty type hints array and nil' do
248
+ q1 = QueryRequest.new('SELECT * FROM peers', nil, [], :two, nil, nil, nil, false)
249
+ q2 = QueryRequest.new('SELECT * FROM peers', nil, nil, :two, nil, nil, nil, false)
250
+ q1.should eql(q2)
251
+ end
252
+
72
253
  it 'is aliased as ==' do
73
- q1 = QueryRequest.new('SELECT * FROM system.peers', :two)
74
- q2 = QueryRequest.new('SELECT * FROM system.peers', :two)
254
+ q1 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, nil, nil, nil, false)
255
+ q2 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, nil, nil, nil, false)
75
256
  q1.should == q2
76
257
  end
77
258
  end
78
259
 
79
260
  describe '#hash' do
80
261
  it 'has the same hash code as another identical object' do
81
- q1 = QueryRequest.new('SELECT * FROM system.peers', :two)
82
- q2 = QueryRequest.new('SELECT * FROM system.peers', :two)
262
+ q1 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, nil, nil, nil, false)
263
+ q2 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, nil, nil, nil, false)
83
264
  q1.hash.should == q2.hash
84
265
  end
85
266
 
267
+ it 'does not have the same hash code when the CQL is different' do
268
+ q1 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, nil, nil, nil, false)
269
+ q2 = QueryRequest.new('SELECT * FROM peers', nil, nil, :two, nil, nil, nil, false)
270
+ q1.hash.should_not == q2.hash
271
+ end
272
+
273
+ it 'does not have the same hash code when the values are different' do
274
+ q1 = QueryRequest.new('SELECT * FROM peers WHERE data_center = ?', ['dc1'], nil, :two, nil, nil, nil, false)
275
+ q2 = QueryRequest.new('SELECT * FROM peers WHERE data_center = ?', ['dc2'], nil, :two, nil, nil, nil, false)
276
+ q1.hash.should_not == q2.hash
277
+ end
278
+
279
+ it 'does not have the same hash code when the type hints are different' do
280
+ q1 = QueryRequest.new('SELECT * FROM peers WHERE data_center = ?', ['dc1'], [:text], :two, nil, nil, nil, false)
281
+ q2 = QueryRequest.new('SELECT * FROM peers WHERE data_center = ?', ['dc1'], [:varchar], :two, nil, nil, nil, false)
282
+ q1.hash.should_not == q2.hash
283
+ end
284
+
86
285
  it 'does not have the same hash code when the consistency is different' do
87
- q1 = QueryRequest.new('SELECT * FROM system.peers', :two)
88
- q2 = QueryRequest.new('SELECT * FROM system.peers', :three)
286
+ q1 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, nil, nil, nil, false)
287
+ q2 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :three, nil, nil, nil, false)
89
288
  q1.hash.should_not == q2.hash
90
289
  end
91
290
 
92
- it 'does not have the same hash code when the CQL is different' do
93
- q1 = QueryRequest.new('SELECT * FROM system.peers', :two)
94
- q2 = QueryRequest.new('SELECT * FROM peers', :two)
291
+ it 'does not have the same hash code when the serial consistency is different' do
292
+ q1 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, :local_serial, nil, nil, false)
293
+ q2 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, :serial, nil, nil, false)
95
294
  q1.hash.should_not == q2.hash
96
295
  end
296
+
297
+ it 'does not have the same hash code when the page size is different' do
298
+ q1 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, nil, 10, nil, false)
299
+ q2 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, nil, 20, nil, false)
300
+ q1.hash.should_not == q2.hash
301
+ end
302
+
303
+ it 'does not have the same hash code when the paging state is different' do
304
+ q1 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, nil, 10, 'foo', false)
305
+ q2 = QueryRequest.new('SELECT * FROM system.peers', nil, nil, :two, nil, 10, 'bar', false)
306
+ q1.hash.should_not == q2.hash
307
+ end
308
+
309
+ it 'does not make a difference between an empty values array and nil' do
310
+ q1 = QueryRequest.new('SELECT * FROM peers', [], nil, :two, nil, nil, nil, false)
311
+ q2 = QueryRequest.new('SELECT * FROM peers', nil, nil, :two, nil, nil, nil, false)
312
+ q1.hash.should == q2.hash
313
+ end
314
+
315
+ it 'does not make a difference between an empty type hints array and nil' do
316
+ q1 = QueryRequest.new('SELECT * FROM peers', nil, [], :two, nil, nil, nil, false)
317
+ q2 = QueryRequest.new('SELECT * FROM peers', nil, nil, :two, nil, nil, nil, false)
318
+ q1.hash.should == q2.hash
319
+ end
97
320
  end
98
321
  end
99
322
  end
100
- end
323
+ end
@@ -8,7 +8,7 @@ module Cql
8
8
  describe RegisterRequest do
9
9
  describe '#write' do
10
10
  it 'encodes a REGISTER request frame' do
11
- bytes = RegisterRequest.new('TOPOLOGY_CHANGE', 'STATUS_CHANGE').write('')
11
+ bytes = RegisterRequest.new('TOPOLOGY_CHANGE', 'STATUS_CHANGE').write(1, '')
12
12
  bytes.should == "\x00\x02\x00\x0fTOPOLOGY_CHANGE\x00\x0dSTATUS_CHANGE"
13
13
  end
14
14
  end
@@ -6,27 +6,33 @@ require 'spec_helper'
6
6
  module Cql
7
7
  module Protocol
8
8
  describe StartupRequest do
9
+ describe '#initialize' do
10
+ it 'raises an error if the CQL version is not specified' do
11
+ expect { described_class.new(nil) }.to raise_error(ArgumentError)
12
+ end
13
+ end
14
+
9
15
  describe '#compressable?' do
10
16
  it 'is not compressable' do
11
- described_class.new.should_not be_compressable
17
+ described_class.new('3.1.1').should_not be_compressable
12
18
  end
13
19
  end
14
20
 
15
21
  describe '#write' do
16
22
  it 'encodes a STARTUP request frame' do
17
- bytes = StartupRequest.new('3.0.0', 'snappy').write('')
23
+ bytes = StartupRequest.new('3.0.0', 'snappy').write(1, '')
18
24
  bytes.should == "\x00\x02\x00\x0bCQL_VERSION\x00\x053.0.0\x00\x0bCOMPRESSION\x00\x06snappy"
19
25
  end
20
26
 
21
- it 'defaults to CQL 3.0.0 and no compression' do
22
- bytes = StartupRequest.new.write('')
23
- bytes.should == "\x00\x01\x00\x0bCQL_VERSION\x00\x053.0.0"
27
+ it 'defaults to no compression' do
28
+ bytes = StartupRequest.new('3.1.1').write(1, '')
29
+ bytes.should == "\x00\x01\x00\x0bCQL_VERSION\x00\x053.1.1"
24
30
  end
25
31
  end
26
32
 
27
33
  describe '#to_s' do
28
34
  it 'returns a pretty string' do
29
- request = StartupRequest.new
35
+ request = StartupRequest.new('3.0.0')
30
36
  request.to_s.should == 'STARTUP {"CQL_VERSION"=>"3.0.0"}'
31
37
  end
32
38
  end
@@ -0,0 +1,31 @@
1
+ # encoding: ascii-8bit
2
+
3
+ require 'spec_helper'
4
+
5
+
6
+ module Cql
7
+ module Protocol
8
+ describe AuthChallengeResponse do
9
+ describe '.decode!' do
10
+ it 'decodes the token' do
11
+ buffer = ByteBuffer.new("\x00\x00\x00\x0cbingbongpong")
12
+ response = described_class.decode!(1, buffer, buffer.length)
13
+ response.token.should == 'bingbongpong'
14
+ end
15
+
16
+ it 'decodes a nil token' do
17
+ buffer = ByteBuffer.new("\xff\xff\xff\xff")
18
+ response = described_class.decode!(1, buffer, buffer.length)
19
+ response.token.should be_nil
20
+ end
21
+ end
22
+
23
+ describe '#to_s' do
24
+ it 'returns a string with number of bytes in the token' do
25
+ response = described_class.new('bingbongpong')
26
+ response.to_s.should == 'AUTH_CHALLENGE 12'
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end