cassanity 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/.rspec +1 -0
- data/.travis.yml +10 -0
- data/Gemfile +11 -0
- data/Guardfile +17 -0
- data/LICENSE.txt +22 -0
- data/README.md +99 -0
- data/Rakefile +16 -0
- data/cassanity.gemspec +21 -0
- data/examples/_shared.rb +8 -0
- data/examples/batch.rb +40 -0
- data/examples/column_families.rb +68 -0
- data/examples/keyspaces.rb +57 -0
- data/lib/cassanity/argument_generators/batch.rb +52 -0
- data/lib/cassanity/argument_generators/column_family_alter.rb +47 -0
- data/lib/cassanity/argument_generators/column_family_create.rb +44 -0
- data/lib/cassanity/argument_generators/column_family_delete.rb +65 -0
- data/lib/cassanity/argument_generators/column_family_drop.rb +18 -0
- data/lib/cassanity/argument_generators/column_family_insert.rb +53 -0
- data/lib/cassanity/argument_generators/column_family_select.rb +34 -0
- data/lib/cassanity/argument_generators/column_family_truncate.rb +18 -0
- data/lib/cassanity/argument_generators/column_family_update.rb +69 -0
- data/lib/cassanity/argument_generators/index_create.rb +22 -0
- data/lib/cassanity/argument_generators/index_drop.rb +13 -0
- data/lib/cassanity/argument_generators/keyspace_create.rb +51 -0
- data/lib/cassanity/argument_generators/keyspace_drop.rb +13 -0
- data/lib/cassanity/argument_generators/keyspace_use.rb +13 -0
- data/lib/cassanity/argument_generators/keyspaces.rb +12 -0
- data/lib/cassanity/argument_generators/set_clause.rb +30 -0
- data/lib/cassanity/argument_generators/using_clause.rb +24 -0
- data/lib/cassanity/argument_generators/where_clause.rb +29 -0
- data/lib/cassanity/argument_generators/with_clause.rb +32 -0
- data/lib/cassanity/column_family.rb +233 -0
- data/lib/cassanity/connection.rb +88 -0
- data/lib/cassanity/error.rb +28 -0
- data/lib/cassanity/executors/cassandra_cql.rb +120 -0
- data/lib/cassanity/keyspace.rb +118 -0
- data/lib/cassanity/result_transformers/column_family_select.rb +15 -0
- data/lib/cassanity/result_transformers/mirror.rb +12 -0
- data/lib/cassanity/schema.rb +26 -0
- data/lib/cassanity/version.rb +3 -0
- data/lib/cassanity.rb +5 -0
- data/spec/helper.rb +27 -0
- data/spec/integration/cassanity/column_family_spec.rb +243 -0
- data/spec/integration/cassanity/connection_spec.rb +87 -0
- data/spec/integration/cassanity/keyspace_spec.rb +64 -0
- data/spec/support/cassanity_helpers.rb +35 -0
- data/spec/unit/cassanity/argument_generators/batch_spec.rb +36 -0
- data/spec/unit/cassanity/argument_generators/column_family_alter_spec.rb +85 -0
- data/spec/unit/cassanity/argument_generators/column_family_create_spec.rb +107 -0
- data/spec/unit/cassanity/argument_generators/column_family_delete_spec.rb +92 -0
- data/spec/unit/cassanity/argument_generators/column_family_drop_spec.rb +25 -0
- data/spec/unit/cassanity/argument_generators/column_family_insert_spec.rb +70 -0
- data/spec/unit/cassanity/argument_generators/column_family_select_spec.rb +113 -0
- data/spec/unit/cassanity/argument_generators/column_family_truncate_spec.rb +25 -0
- data/spec/unit/cassanity/argument_generators/column_family_update_spec.rb +109 -0
- data/spec/unit/cassanity/argument_generators/index_create_spec.rb +39 -0
- data/spec/unit/cassanity/argument_generators/index_drop_spec.rb +14 -0
- data/spec/unit/cassanity/argument_generators/keyspace_create_spec.rb +53 -0
- data/spec/unit/cassanity/argument_generators/keyspace_drop_spec.rb +14 -0
- data/spec/unit/cassanity/argument_generators/keyspace_use_spec.rb +14 -0
- data/spec/unit/cassanity/argument_generators/keyspaces_spec.rb +12 -0
- data/spec/unit/cassanity/argument_generators/set_clause_spec.rb +85 -0
- data/spec/unit/cassanity/argument_generators/using_clause_spec.rb +44 -0
- data/spec/unit/cassanity/argument_generators/where_clause_spec.rb +57 -0
- data/spec/unit/cassanity/argument_generators/with_clause_spec.rb +63 -0
- data/spec/unit/cassanity/column_family_spec.rb +250 -0
- data/spec/unit/cassanity/connection_spec.rb +75 -0
- data/spec/unit/cassanity/error_spec.rb +35 -0
- data/spec/unit/cassanity/executors/cassandra_cql_spec.rb +178 -0
- data/spec/unit/cassanity/keyspace_spec.rb +137 -0
- data/spec/unit/cassanity/result_transformers/column_family_select_spec.rb +0 -0
- data/spec/unit/cassanity/result_transformers/mirror_spec.rb +12 -0
- data/spec/unit/cassanity/schema_spec.rb +23 -0
- data/spec/unit/cassanity_spec.rb +5 -0
- metadata +172 -0
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'cassanity/keyspace'
|
3
|
+
require 'cassanity/executors/cassandra_cql'
|
4
|
+
|
5
|
+
describe Cassanity::Keyspace do
|
6
|
+
let(:keyspace_name) { 'cassanity_test' }
|
7
|
+
let(:self_created_keyspace_name) { 'self_created' }
|
8
|
+
let(:column_family_name) { 'apps' }
|
9
|
+
|
10
|
+
let(:client) {
|
11
|
+
CassandraCQL::Database.new('127.0.0.1:9160', {
|
12
|
+
cql_version: '3.0.0',
|
13
|
+
})
|
14
|
+
}
|
15
|
+
|
16
|
+
let(:executor) {
|
17
|
+
Cassanity::Executors::CassandraCql.new({
|
18
|
+
client: client,
|
19
|
+
})
|
20
|
+
}
|
21
|
+
|
22
|
+
let(:required_arguments) {
|
23
|
+
{
|
24
|
+
name: keyspace_name,
|
25
|
+
executor: executor,
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
subject {
|
30
|
+
described_class.new(required_arguments)
|
31
|
+
}
|
32
|
+
|
33
|
+
before do
|
34
|
+
client_drop_keyspace(client, keyspace_name)
|
35
|
+
client_create_keyspace(client, keyspace_name)
|
36
|
+
end
|
37
|
+
|
38
|
+
after do
|
39
|
+
client_drop_keyspace(client, keyspace_name)
|
40
|
+
client_drop_keyspace(client, self_created_keyspace_name)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "can create" do
|
44
|
+
client_keyspace?(client, self_created_keyspace_name).should be_false
|
45
|
+
instance = described_class.new(required_arguments.merge({
|
46
|
+
name: self_created_keyspace_name,
|
47
|
+
}))
|
48
|
+
instance.create
|
49
|
+
client_keyspace?(client, self_created_keyspace_name).should be_true
|
50
|
+
end
|
51
|
+
|
52
|
+
it "can use" do
|
53
|
+
client.execute("USE system")
|
54
|
+
client.keyspace.should_not eq(keyspace_name)
|
55
|
+
subject.use
|
56
|
+
client.keyspace.should eq(keyspace_name)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "can drop" do
|
60
|
+
client_keyspace?(client, keyspace_name).should be_true
|
61
|
+
subject.drop
|
62
|
+
client_keyspace?(client, keyspace_name).should be_false
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module CassanityHelpers
|
2
|
+
def client_keyspace?(client, name)
|
3
|
+
client.keyspaces.map(&:name).include?(name.to_s)
|
4
|
+
end
|
5
|
+
|
6
|
+
def client_create_keyspace(client, name)
|
7
|
+
unless client_keyspace?(client, name)
|
8
|
+
client.execute("CREATE KEYSPACE #{name} WITH strategy_class = 'SimpleStrategy' AND strategy_options:replication_factor = 1")
|
9
|
+
end
|
10
|
+
client.execute("USE #{name}")
|
11
|
+
end
|
12
|
+
|
13
|
+
def client_drop_keyspace(client, name)
|
14
|
+
if client_keyspace?(client, name)
|
15
|
+
client.execute("DROP KEYSPACE #{name}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def client_column_family?(client, name)
|
20
|
+
client.schema.column_family_names.include?(name.to_s)
|
21
|
+
end
|
22
|
+
|
23
|
+
def client_create_column_family(client, name, columns = nil)
|
24
|
+
columns ||= "id text PRIMARY KEY, name text"
|
25
|
+
unless client_column_family?(client, name)
|
26
|
+
client.execute("CREATE COLUMNFAMILY #{name} (#{columns})")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def client_drop_column_family(client, name)
|
31
|
+
if client_column_family?(client, name)
|
32
|
+
client.execute("DROP COLUMNFAMILY #{name}")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'cassanity/argument_generators/batch'
|
3
|
+
|
4
|
+
describe Cassanity::ArgumentGenerators::Batch do
|
5
|
+
describe "#call" do
|
6
|
+
it "returns array of arguments" do
|
7
|
+
cql = "BEGIN BATCH INSERT INTO users (id) VALUES (?) UPDATE users SET name = ? WHERE id = ? DELETE FROM users WHERE id = ? APPLY BATCH"
|
8
|
+
subject.call({
|
9
|
+
modifications: [
|
10
|
+
[:insert, name: :users, data: {id: '1'}],
|
11
|
+
[:update, name: :users, set: {name: 'GitHub'}, where: {id: '1'}],
|
12
|
+
[:delete, name: :users, where: {id: '1'}],
|
13
|
+
],
|
14
|
+
}).should eq([cql, '1', 'GitHub', '1', '1'])
|
15
|
+
end
|
16
|
+
|
17
|
+
context "with :using key" do
|
18
|
+
subject {
|
19
|
+
described_class.new({
|
20
|
+
using_clause: lambda { |args|
|
21
|
+
[" USING TIMESTAMP = #{args.fetch(:using).fetch(:timestamp)}"]
|
22
|
+
}
|
23
|
+
})
|
24
|
+
}
|
25
|
+
|
26
|
+
it "uses using clause to get additional cql and bound variables" do
|
27
|
+
cql = "BEGIN BATCH USING TIMESTAMP = 500 APPLY BATCH"
|
28
|
+
subject.call({
|
29
|
+
using: {
|
30
|
+
timestamp: 500,
|
31
|
+
},
|
32
|
+
}).should eq([cql])
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'cassanity/argument_generators/column_family_alter'
|
3
|
+
|
4
|
+
describe Cassanity::ArgumentGenerators::ColumnFamilyAlter do
|
5
|
+
describe "#call" do
|
6
|
+
it "returns array of arguments" do
|
7
|
+
cql = "ALTER COLUMNFAMILY apps"
|
8
|
+
expected = [cql]
|
9
|
+
subject.call({
|
10
|
+
name: :apps,
|
11
|
+
}).should eq(expected)
|
12
|
+
end
|
13
|
+
|
14
|
+
context "with :keyspace_name" do
|
15
|
+
it "returns array of arguments including keyspace name" do
|
16
|
+
cql = "ALTER COLUMNFAMILY foo.apps"
|
17
|
+
expected = [cql]
|
18
|
+
subject.call({
|
19
|
+
name: :apps,
|
20
|
+
keyspace_name: 'foo',
|
21
|
+
}).should eq(expected)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "altering a column type" do
|
26
|
+
it "returns array of arguments" do
|
27
|
+
cql = "ALTER COLUMNFAMILY apps ALTER created_at TYPE timestamp"
|
28
|
+
expected = [cql]
|
29
|
+
subject.call({
|
30
|
+
name: :apps,
|
31
|
+
alter: {
|
32
|
+
created_at: :timestamp,
|
33
|
+
},
|
34
|
+
}).should eq(expected)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "adding a column" do
|
39
|
+
it "returns array of arguments" do
|
40
|
+
cql = "ALTER COLUMNFAMILY apps ADD created_at timestamp"
|
41
|
+
expected = [cql]
|
42
|
+
subject.call({
|
43
|
+
name: :apps,
|
44
|
+
add: {
|
45
|
+
created_at: :timestamp,
|
46
|
+
},
|
47
|
+
}).should eq(expected)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "dropping a column" do
|
52
|
+
it "returns array of arguments" do
|
53
|
+
cql = "ALTER COLUMNFAMILY apps DROP created_at"
|
54
|
+
expected = [cql]
|
55
|
+
subject.call({
|
56
|
+
name: :apps,
|
57
|
+
drop: :created_at,
|
58
|
+
}).should eq(expected)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "altering column family WITH options" do
|
63
|
+
let(:with_clause) {
|
64
|
+
lambda { |args| [" WITH comment = ?", 'Testing']}
|
65
|
+
}
|
66
|
+
|
67
|
+
subject {
|
68
|
+
described_class.new({
|
69
|
+
with_clause: with_clause,
|
70
|
+
})
|
71
|
+
}
|
72
|
+
|
73
|
+
it "returns array of arguments with help from the with_clause" do
|
74
|
+
cql = "ALTER COLUMNFAMILY apps WITH comment = ?"
|
75
|
+
expected = [cql, 'Testing']
|
76
|
+
subject.call({
|
77
|
+
name: :apps,
|
78
|
+
with: {
|
79
|
+
comment: 'Testing',
|
80
|
+
}
|
81
|
+
}).should eq(expected)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'cassanity/argument_generators/column_family_create'
|
3
|
+
|
4
|
+
describe Cassanity::ArgumentGenerators::ColumnFamilyCreate do
|
5
|
+
let(:column_family_name) { 'tracks' }
|
6
|
+
|
7
|
+
let(:schema) {
|
8
|
+
Cassanity::Schema.new({
|
9
|
+
primary_key: :id,
|
10
|
+
columns: {
|
11
|
+
id: :text,
|
12
|
+
name: :text
|
13
|
+
},
|
14
|
+
})
|
15
|
+
}
|
16
|
+
|
17
|
+
describe "#call" do
|
18
|
+
context "when missing required argument key" do
|
19
|
+
let(:required_arguments) {
|
20
|
+
{
|
21
|
+
name: 'tracks',
|
22
|
+
schema: schema,
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
[:name, :schema].each do |key|
|
27
|
+
it "raises error if missing :#{key} key" do
|
28
|
+
args = required_arguments.reject { |k, v| k == key }
|
29
|
+
expect { subject.call(args) }.to raise_error(KeyError)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "when using single primary key" do
|
35
|
+
it "returns array of arguments" do
|
36
|
+
cql = "CREATE COLUMNFAMILY #{column_family_name} (id text, name text, PRIMARY KEY (id))"
|
37
|
+
expected = [cql]
|
38
|
+
subject.call({
|
39
|
+
name: column_family_name,
|
40
|
+
schema: schema,
|
41
|
+
}).should eq(expected)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "when including :keyspace_name" do
|
46
|
+
it "returns array of arguments" do
|
47
|
+
cql = "CREATE COLUMNFAMILY foo.#{column_family_name} (id text, name text, PRIMARY KEY (id))"
|
48
|
+
expected = [cql]
|
49
|
+
subject.call({
|
50
|
+
keyspace_name: :foo,
|
51
|
+
name: column_family_name,
|
52
|
+
schema: schema,
|
53
|
+
}).should eq(expected)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "when using composite primary key" do
|
58
|
+
it "returns array of arguments" do
|
59
|
+
schema = Cassanity::Schema.new({
|
60
|
+
primary_key: [:segment, :track_id],
|
61
|
+
columns: {
|
62
|
+
segment: :text,
|
63
|
+
track_id: :timeuuid,
|
64
|
+
page: :text,
|
65
|
+
},
|
66
|
+
})
|
67
|
+
cql = "CREATE COLUMNFAMILY #{column_family_name} (segment text, track_id timeuuid, page text, PRIMARY KEY (segment, track_id))"
|
68
|
+
expected = [cql]
|
69
|
+
subject.call({
|
70
|
+
name: column_family_name,
|
71
|
+
schema: schema,
|
72
|
+
}).should eq(expected)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context "when using WITH options" do
|
77
|
+
let(:with_clause) {
|
78
|
+
lambda { |args| [" WITH comment = ?", 'Testing']}
|
79
|
+
}
|
80
|
+
|
81
|
+
subject {
|
82
|
+
described_class.new({
|
83
|
+
with_clause: with_clause,
|
84
|
+
})
|
85
|
+
}
|
86
|
+
|
87
|
+
it "returns array of arguments with help from the with_clause" do
|
88
|
+
schema = Cassanity::Schema.new({
|
89
|
+
primary_key: :id,
|
90
|
+
columns: {
|
91
|
+
id: :text,
|
92
|
+
name: :text,
|
93
|
+
},
|
94
|
+
with: {
|
95
|
+
comment: "Testing",
|
96
|
+
}
|
97
|
+
})
|
98
|
+
cql = "CREATE COLUMNFAMILY apps (id text, name text, PRIMARY KEY (id)) WITH comment = ?"
|
99
|
+
expected = [cql, 'Testing']
|
100
|
+
subject.call({
|
101
|
+
name: :apps,
|
102
|
+
schema: schema,
|
103
|
+
}).should eq(expected)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'cassanity/argument_generators/column_family_delete'
|
3
|
+
|
4
|
+
describe Cassanity::ArgumentGenerators::ColumnFamilyDelete do
|
5
|
+
let(:column_family_name) { 'apps' }
|
6
|
+
|
7
|
+
describe "#call" do
|
8
|
+
it "returns array of arguments" do
|
9
|
+
cql = "DELETE FROM #{column_family_name} WHERE id = ?"
|
10
|
+
expected = [cql, '1']
|
11
|
+
subject.call({
|
12
|
+
name: column_family_name,
|
13
|
+
where: {
|
14
|
+
id: '1',
|
15
|
+
}
|
16
|
+
}).should eq(expected)
|
17
|
+
end
|
18
|
+
|
19
|
+
context "with :keyspace_name" do
|
20
|
+
it "returns array of arguments" do
|
21
|
+
cql = "DELETE FROM foo.#{column_family_name} WHERE id = ?"
|
22
|
+
expected = [cql, '1']
|
23
|
+
subject.call({
|
24
|
+
keyspace_name: :foo,
|
25
|
+
name: column_family_name,
|
26
|
+
where: {
|
27
|
+
id: '1',
|
28
|
+
}
|
29
|
+
}).should eq(expected)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "with specific columns" do
|
34
|
+
it "returns array of arguments only deleting specific columns" do
|
35
|
+
cql = "DELETE foo, bar FROM #{column_family_name} WHERE id = ?"
|
36
|
+
expected = [cql, '1']
|
37
|
+
subject.call({
|
38
|
+
name: column_family_name,
|
39
|
+
columns: [:foo, :bar],
|
40
|
+
where: {
|
41
|
+
id: '1',
|
42
|
+
}
|
43
|
+
}).should eq(expected)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "with :where key" do
|
48
|
+
subject {
|
49
|
+
described_class.new({
|
50
|
+
where_clause: lambda { |args|
|
51
|
+
[" WHERE id = ?", args.fetch(:where).fetch(:id)]
|
52
|
+
}
|
53
|
+
})
|
54
|
+
}
|
55
|
+
|
56
|
+
it "uses where clause to get additional cql and bound variables" do
|
57
|
+
cql = "DELETE FROM #{column_family_name} WHERE id = ?"
|
58
|
+
expected = [cql, '4']
|
59
|
+
subject.call({
|
60
|
+
name: column_family_name,
|
61
|
+
where: {
|
62
|
+
id: '4',
|
63
|
+
}
|
64
|
+
}).should eq(expected)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "with :using key" do
|
69
|
+
subject {
|
70
|
+
described_class.new({
|
71
|
+
using_clause: lambda { |args|
|
72
|
+
[" USING TTL = #{args.fetch(:using).fetch(:ttl)}"]
|
73
|
+
}
|
74
|
+
})
|
75
|
+
}
|
76
|
+
|
77
|
+
it "uses using clause to get additional cql and bound variables" do
|
78
|
+
cql = "DELETE FROM #{column_family_name} USING TTL = 500 WHERE id = ?"
|
79
|
+
expected = [cql, '4']
|
80
|
+
subject.call({
|
81
|
+
name: column_family_name,
|
82
|
+
using: {
|
83
|
+
ttl: 500,
|
84
|
+
},
|
85
|
+
where: {
|
86
|
+
id: '4',
|
87
|
+
}
|
88
|
+
}).should eq(expected)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'cassanity/argument_generators/column_family_drop'
|
3
|
+
|
4
|
+
describe Cassanity::ArgumentGenerators::ColumnFamilyDrop do
|
5
|
+
let(:column_family_name) { 'apps' }
|
6
|
+
|
7
|
+
describe "#call" do
|
8
|
+
it "returns array of arguments" do
|
9
|
+
cql = "DROP COLUMNFAMILY #{column_family_name}"
|
10
|
+
expected = [cql]
|
11
|
+
subject.call(name: column_family_name).should eq(expected)
|
12
|
+
end
|
13
|
+
|
14
|
+
context "with :keyspace_name" do
|
15
|
+
it "returns array of arguments" do
|
16
|
+
cql = "DROP COLUMNFAMILY foo.#{column_family_name}"
|
17
|
+
expected = [cql]
|
18
|
+
subject.call({
|
19
|
+
keyspace_name: :foo,
|
20
|
+
name: column_family_name
|
21
|
+
}).should eq(expected)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'cassanity/argument_generators/column_family_insert'
|
3
|
+
|
4
|
+
describe Cassanity::ArgumentGenerators::ColumnFamilyInsert do
|
5
|
+
let(:column_family_name) { 'apps' }
|
6
|
+
|
7
|
+
describe "#call" do
|
8
|
+
[:name, :data].each do |key|
|
9
|
+
it "raises error if missing :#{key} key" do
|
10
|
+
required_arguments = {
|
11
|
+
name: column_family_name,
|
12
|
+
data: {
|
13
|
+
id: '1',
|
14
|
+
name: 'GitHub',
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
args = required_arguments.reject { |k, v| k == key }
|
19
|
+
expect { subject.call(args) }.to raise_error(KeyError)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "returns array of arguments" do
|
24
|
+
cql = "INSERT INTO #{column_family_name} (id, name) VALUES (?, ?)"
|
25
|
+
expected = [cql, '1', 'GitHub']
|
26
|
+
subject.call({
|
27
|
+
name: column_family_name,
|
28
|
+
data: {
|
29
|
+
id: '1',
|
30
|
+
name: 'GitHub',
|
31
|
+
}
|
32
|
+
}).should eq(expected)
|
33
|
+
end
|
34
|
+
|
35
|
+
context "with :keyspace_name" do
|
36
|
+
it "returns array of arguments" do
|
37
|
+
cql = "INSERT INTO foo.#{column_family_name} (id, name) VALUES (?, ?)"
|
38
|
+
expected = [cql, '1', 'GitHub']
|
39
|
+
subject.call({
|
40
|
+
keyspace_name: :foo,
|
41
|
+
name: column_family_name,
|
42
|
+
data: {
|
43
|
+
id: '1',
|
44
|
+
name: 'GitHub',
|
45
|
+
}
|
46
|
+
}).should eq(expected)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "with :using key" do
|
51
|
+
it "returns array of arguments including using in cql string" do
|
52
|
+
millis = (Time.mktime(2012, 11, 1, 14, 9, 9).to_f * 1000).to_i
|
53
|
+
cql = "INSERT INTO #{column_family_name} (id, name) VALUES (?, ?) USING TTL 86400 AND TIMESTAMP #{millis} AND CONSISTENCY quorum"
|
54
|
+
expected = [cql, '1', 'GitHub']
|
55
|
+
subject.call({
|
56
|
+
name: column_family_name,
|
57
|
+
data: {
|
58
|
+
id: '1',
|
59
|
+
name: 'GitHub',
|
60
|
+
},
|
61
|
+
using: {
|
62
|
+
ttl: 86400,
|
63
|
+
timestamp: millis,
|
64
|
+
consistency: 'quorum',
|
65
|
+
}
|
66
|
+
}).should eq(expected)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'cassanity/argument_generators/column_family_select'
|
3
|
+
|
4
|
+
describe Cassanity::ArgumentGenerators::ColumnFamilySelect do
|
5
|
+
let(:column_family_name) { 'apps' }
|
6
|
+
|
7
|
+
describe "#call" do
|
8
|
+
it "returns array of arguments" do
|
9
|
+
cql = "SELECT * FROM #{column_family_name}"
|
10
|
+
expected = [cql]
|
11
|
+
subject.call(name: column_family_name).should eq(expected)
|
12
|
+
end
|
13
|
+
|
14
|
+
context "with :keyspace_name" do
|
15
|
+
it "returns array of arguments" do
|
16
|
+
cql = "SELECT * FROM foo.#{column_family_name}"
|
17
|
+
expected = [cql]
|
18
|
+
subject.call({
|
19
|
+
keyspace_name: :foo,
|
20
|
+
name: column_family_name
|
21
|
+
}).should eq(expected)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "with single column" do
|
26
|
+
it "returns array of arguments querying only one column" do
|
27
|
+
cql = "SELECT name FROM #{column_family_name}"
|
28
|
+
expected = [cql]
|
29
|
+
subject.call({
|
30
|
+
name: column_family_name,
|
31
|
+
select: :name,
|
32
|
+
}).should eq(expected)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "with multiple columns" do
|
37
|
+
it "returns array of arguments querying multiple columns" do
|
38
|
+
cql = "SELECT id, name, created_at FROM #{column_family_name}"
|
39
|
+
expected = [cql]
|
40
|
+
subject.call({
|
41
|
+
name: column_family_name,
|
42
|
+
select: [:id, :name, :created_at],
|
43
|
+
}).should eq(expected)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "with count *" do
|
48
|
+
it "returns array of arguments querying multiple columns" do
|
49
|
+
cql = "SELECT COUNT(*) FROM #{column_family_name}"
|
50
|
+
expected = [cql]
|
51
|
+
subject.call({
|
52
|
+
name: column_family_name,
|
53
|
+
select: 'COUNT(*)',
|
54
|
+
}).should eq(expected)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "with count 1" do
|
59
|
+
it "returns array of arguments querying multiple columns" do
|
60
|
+
cql = "SELECT COUNT(1) FROM #{column_family_name}"
|
61
|
+
expected = [cql]
|
62
|
+
subject.call({
|
63
|
+
name: column_family_name,
|
64
|
+
select: 'COUNT(1)',
|
65
|
+
}).should eq(expected)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context "with WRITETIME" do
|
70
|
+
it "returns array of arguments querying multiple columns" do
|
71
|
+
cql = "SELECT WRITETIME(name) FROM #{column_family_name}"
|
72
|
+
expected = [cql]
|
73
|
+
subject.call({
|
74
|
+
name: column_family_name,
|
75
|
+
select: 'WRITETIME(name)',
|
76
|
+
}).should eq(expected)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context "with TTL" do
|
81
|
+
it "returns array of arguments querying multiple columns" do
|
82
|
+
cql = "SELECT TTL(name) FROM #{column_family_name}"
|
83
|
+
expected = [cql]
|
84
|
+
subject.call({
|
85
|
+
name: column_family_name,
|
86
|
+
select: 'TTL(name)',
|
87
|
+
}).should eq(expected)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context "with where option" do
|
92
|
+
let(:where_clause) {
|
93
|
+
lambda { |args| [" WHERE foo = ?", args.fetch(:where).fetch(:foo)]}
|
94
|
+
}
|
95
|
+
|
96
|
+
subject {
|
97
|
+
described_class.new({
|
98
|
+
where_clause: where_clause,
|
99
|
+
})
|
100
|
+
}
|
101
|
+
|
102
|
+
it "returns array of arguments with help from where clause" do
|
103
|
+
where = {foo: 'bar'}
|
104
|
+
cql = "SELECT * FROM #{column_family_name} WHERE foo = ?"
|
105
|
+
expected = [cql, 'bar']
|
106
|
+
subject.call({
|
107
|
+
name: column_family_name,
|
108
|
+
where: where,
|
109
|
+
}).should eq(expected)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'cassanity/argument_generators/column_family_truncate'
|
3
|
+
|
4
|
+
describe Cassanity::ArgumentGenerators::ColumnFamilyTruncate do
|
5
|
+
let(:column_family_name) { 'apps' }
|
6
|
+
|
7
|
+
describe "#call" do
|
8
|
+
it "returns array of arguments" do
|
9
|
+
cql = "TRUNCATE #{column_family_name}"
|
10
|
+
expected = [cql]
|
11
|
+
subject.call(name: column_family_name).should eq(expected)
|
12
|
+
end
|
13
|
+
|
14
|
+
context "with :keyspace_name" do
|
15
|
+
it "returns array of arguments" do
|
16
|
+
cql = "TRUNCATE foo.#{column_family_name}"
|
17
|
+
expected = [cql]
|
18
|
+
subject.call({
|
19
|
+
keyspace_name: :foo,
|
20
|
+
name: column_family_name
|
21
|
+
}).should eq(expected)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|