-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathclient_spec.rb
More file actions
273 lines (229 loc) · 9.22 KB
/
client_spec.rb
File metadata and controls
273 lines (229 loc) · 9.22 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# frozen_string_literal: true
#
# Licensed to Crate.IO GmbH ("Crate") under one or more contributor
# license agreements. See the NOTICE file distributed with this work for
# additional information regarding copyright ownership. Crate licenses
# this file to you under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. You may
# obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# However, if you have executed another commercial license agreement
# with Crate these terms will supersede the license and you may use the
# software solely pursuant to the terms of the relevant commercial agreement.
require_relative '../spec_helper'
require 'securerandom'
require 'digest'
describe CrateRuby::Client do
let(:client) { described_class.new(['localhost:44200']) }
let(:client_w_schema) { described_class.new(['localhost:44200'], schema: 'custom') }
describe '#create_table' do
describe 'blob management' do
let(:file) { 'logo-crate.png' }
let(:path) { File.join(File.dirname(__FILE__), '../uploads/') }
let(:file_path) { File.join(path, file) }
let(:digest) { Digest::SHA1.file(file_path).hexdigest }
let(:store_location) { File.join(path, "get_#{file}") }
let(:blob_table) { 'my_blobs' }
before do
client.execute("create blob table #{blob_table} clustered into 1 shards with (number_of_replicas=0)")
end
after do
client.execute("drop blob table #{blob_table}")
end
describe '#blob_put' do
context 'when uploading a file' do
it 'is saved to a blob table' do
f = File.read(file_path)
expect(client.blob_put(blob_table, digest, f)).to be_truthy
end
end
describe '#string' do
let(:string) { 'my crazy' }
let(:digest) { Digest::SHA1.hexdigest(string) }
it 'uploads a string to a blob table' do
expect(client.blob_put(blob_table, digest, string)).to be_truthy
end
end
end
describe '#blob_get' do
before do
f = File.read(file_path)
client.blob_put(blob_table, digest, f)
end
it 'downloads a blob' do
data = client.blob_get(blob_table, digest)
expect(data).to be_truthy
File.binwrite(store_location, data)
end
end
describe '#blob_delete' do
before do
f = File.read(file_path)
client.blob_put(blob_table, digest, f)
end
it 'deletes a blob' do
response = client.blob_delete(blob_table, digest)
expect(response).to be_falsy
end
end
end
describe '#execute' do
let(:table_name) { 't_test' }
before do
client.execute("create table #{table_name} " \
'(id integer primary key, name string, address object, tags array(string)) ')
end
after do
client.execute("drop table #{table_name}")
end
it 'allows parameters' do
expect(client.execute("insert into #{table_name} (id, name, address, tags) VALUES (?, ?, ?, ?)",
[1, 'Post 1', { street: '1010 W 2nd Ave', city: 'Vancouver' },
%w[awesome freaky]])).to be_truthy
client.refresh_table table_name
expect(client.execute("select * from #{table_name}").rowcount).to eq(1)
end
it 'allows bulk parameters' do
bulk_args = [
[1, 'Post 1', { street: '1010 W 2nd Ave', city: 'New York' }, %w[foo bar]],
[2, 'Post 2', { street: '1010 W 2nd Ave', city: 'San Fran' }, []]
]
expect(client.execute("insert into #{table_name} (id, name, address, tags) VALUES (?, ?, ?, ?)",
nil, bulk_args)).to be_truthy
client.refresh_table table_name
expect(client.execute("select * from #{table_name}").rowcount).to eq(2)
expect(client.execute("select count(*) from #{table_name}")[0][0]).to eq(2)
end
it 'accepts http options' do
expect do
client.execute("select * from #{table_name}", nil, nil,
'read_timeout' => 0)
end.to raise_error Net::ReadTimeout
end
context 'with schema' do
before do
client_w_schema.execute("create table #{table_name} \n
(id integer primary key, name string, address object, tags array(string)) ")
end
after { client_w_schema.execute("drop table #{table_name}") }
it 'allows parameters' do
expect(client_w_schema.execute("insert into #{table_name} (id, name, address, tags) VALUES (?, ?, ?, ?)",
[1, 'Post 1', { street: '1010 W 2nd Ave', city: 'Vancouver' },
%w[awesome freaky]])).to be_truthy
client_w_schema.refresh_table table_name
expect(client.execute("select * from #{table_name}").rowcount).not_to eq(1)
expect(client_w_schema.execute("select * from #{table_name}").rowcount).to eq(1)
end
end
end
describe '#initialize' do
it 'uses host and ports parameters' do
logger = double
client = described_class.new ['10.0.0.1:4200'], logger: logger
expect(client.instance_variable_get(:@servers)).to eq(['10.0.0.1:4200'])
end
it 'uses default request parameters' do
client = described_class.new
expect(client.instance_variable_get(:@http_options)).to eq(read_timeout: 3600)
end
it 'uses request parameters' do
client = described_class.new ['10.0.0.1:4200'],
http_options: { read_timeout: 60 }
expect(client.instance_variable_get(:@http_options)).to eq(read_timeout: 60)
end
end
describe '#tables' do
before do
client.create_table 'posts', id: :integer
client.create_table 'comments', id: :integer
end
after do
client.tables.each do |table|
client.drop_table table
end
end
it 'returns all user tables as an array of string values' do
expect(client.tables).to eq %w[comments posts]
end
end
describe '#blob_tables' do
before do
client.create_blob_table 'pix'
end
after do
client.drop_table 'pix', true
end
it 'returns all user tables as an array of string values' do
expect(client.blob_tables).to eq %w[pix]
end
end
describe '#insert' do
before do
client.create_table('posts', id: [:string, 'primary key'],
title: :string, views: :integer)
end
after do
client.drop_table 'posts'
end
it 'inserts the record' do
expect do
id = SecureRandom.uuid
client.insert('posts', id: id, title: 'Test')
client.refresh_table('posts')
result_set = client.execute("Select * from posts where id = '#{id}'")
expect(result_set.rowcount).to eq 1
end.not_to raise_exception
end
end
describe '#refresh table' do
it 'issues the proper refresh statment' do
expect(client).to receive(:execute).with('refresh table posts')
client.refresh_table('posts')
end
end
end
describe 'authentication' do
let(:username) { 'matz' }
let(:password) { 'ruby' }
let(:encrypted_credentials) { Base64.strict_encode64 "#{username}:#{password}" }
describe 'with password' do
let(:auth_client) { described_class.new(['localhost:44200'], username: username, password: password) }
it 'sets the basic auth header' do
headers = auth_client.send(:headers)
expect(headers['Authorization']).to eq "Basic #{encrypted_credentials}"
end
end
describe 'without password' do
let(:auth_client) { described_class.new(['localhost:44200'], username: username) }
let(:enc_creds_wo_pwd) { Base64.strict_encode64 "#{username}:" }
it 'sets and encodes auth header even without password' do
headers = auth_client.send(:headers)
expect(headers['Authorization']).to eq "Basic #{enc_creds_wo_pwd}"
end
end
describe 'with a long password' do
let(:password) { 'this password is long and would cause Base64 wrapping' }
let(:auth_client) { described_class.new(['localhost:44200'], username: username, password: password) }
it 'encodes the basic auth header correctly' do
headers = auth_client.send(:headers)
expect(headers['Authorization']).to eq "Basic #{encrypted_credentials}"
expect(encrypted_credentials).not_to include("\n")
end
end
describe 'X-User header' do
let(:auth_client) { described_class.new(['localhost:44200'], username: username) }
it 'sets the X-User header' do
headers = auth_client.send(:headers)
expect(headers['X-User']).to eq username
end
end
end
end