Skip to content

Commit

Permalink
Cleanup mixer specs
Browse files Browse the repository at this point in the history
  • Loading branch information
gussan committed May 22, 2017
1 parent e2a0ca8 commit b6f3f05
Showing 1 changed file with 42 additions and 74 deletions.
116 changes: 42 additions & 74 deletions spec/active_record/turntable/mixer_spec.rb
Expand Up @@ -4,109 +4,77 @@
before do
@cluster = ActiveRecord::Turntable::Cluster.new(ActiveRecord::Base.turntable_config[:clusters][:user_cluster])
@connection_proxy = ActiveRecord::Turntable::ConnectionProxy.new(User, @cluster)
@mixer = ActiveRecord::Turntable::Mixer.new(@connection_proxy)
end

context "When initialized" do
before do
@mixer = ActiveRecord::Turntable::Mixer.new(@connection_proxy)
context "#divide_insert_values" do
subject { @mixer.send(:divide_insert_values, sql_tree, "id") }

context "with a single insert sql expression" do
let(:sql_tree) { SQLTree["INSERT INTO `users` (id, hp, mp) VALUES (1, 10, 10)"] }

it { is_expected.to be_instance_of(Hash) }
it { is_expected.to have_key(1) }
it { is_expected.to have(1).item }
end

context "For Insert SQL" do
context "When call divide_insert_values with Single INSERT and shard_key 'id'" do
subject {
tree = SQLTree["INSERT INTO `users` (id, hp, mp) VALUES (1, 10, 10)"]
@mixer.send(:divide_insert_values, tree, "id")
}
context "with a bulk insert sql expression" do
let(:sql_tree) { SQLTree["INSERT INTO `users` (id, hp, mp) VALUES (1, 10, 10), (2,10,10), (3,10,10)"] }

it { is_expected.to be_instance_of(Hash) }
it { is_expected.to have_key(1) }
it { is_expected.to have(1).item }
end
it { is_expected.to be_instance_of(Hash) }
it { is_expected.to have_key(3) }
it { expect(subject.values).to all(have(1).item) }
end
end

context "When call divide_insert_values with Bulk INSERT and shard_key 'id'" do
subject {
tree = SQLTree["INSERT INTO `users` (id, hp, mp) VALUES (1, 10, 10), (2,10,10), (3,10,10)"]
@mixer.send(:divide_insert_values, tree, "id")
}
context "#find_shard_keys" do
subject { @mixer.find_shard_keys(sql_tree.where, "users", "id") }

it { is_expected.to be_instance_of(Hash) }
it { is_expected.to have_key(3) }
it { expect(subject.values).to all(have(1).item) }
end
context "with an update sql expression includes an equals shard_key condition" do
let(:sql_tree) { SQLTree["UPDATE `users` SET `users`.`hp` = 20 WHERE `users`.`id` = 1"] }

it { is_expected.to be_instance_of Array }
it { is_expected.to eq([1]) }
end

context "For Update SQL" do
context "When call find_shard_keys with eql shardkey condition" do
subject {
tree = SQLTree["UPDATE `users` SET `users`.`hp` = 20 WHERE `users`.`id` = 1"]
@mixer.find_shard_keys(tree.where, "users", "id")
}
context "with a delete sql expression includes an equals shard_key condition" do
let(:sql_tree) { SQLTree["DELETE FROM `users` WHERE `users`.`id` = 1"] }

it { is_expected.to be_instance_of Array }
it { is_expected.to eq([1]) }
end
it { is_expected.to be_instance_of Array }
it { is_expected.to eq([1]) }
end

context "For Delete SQL" do
context "When call find_shard_keys with eql shardkey condition" do
subject {
tree = SQLTree["DELETE FROM `users` WHERE `users`.`id` = 1"]
@mixer.find_shard_keys(tree.where, "users", "id")
}
context "with a select sql expression" do
context "includes a single equals condition" do
let(:sql_tree) { SQLTree["SELECT * FROM `users` WHERE `users`.`id` = 1"] }

it { is_expected.to be_instance_of Array }
it { is_expected.to eq([1]) }
end
end

context "For Select SQL" do
context "When call find_shard_keys with eql shardkey condition" do
context "with single equal expression" do
subject {
tree = SQLTree["SELECT * FROM `users` WHERE `users`.`id` = 1"]
@mixer.find_shard_keys(tree.where, "users", "id")
}

it { is_expected.to be_instance_of Array }
it { is_expected.to eq([1]) }
end

context "with duplicated equal expressions" do
subject {
tree = SQLTree["SELECT * FROM `users` WHERE `users`.`id` = 1 AND `users`.`id` = 1"]
@mixer.find_shard_keys(tree.where, "users", "id")
}

it { is_expected.to be_instance_of Array }
it { is_expected.to eq([1]) }
end
context "includes duplicated equals conditions" do
let(:sql_tree) { SQLTree["SELECT * FROM `users` WHERE `users`.`id` = 1 AND `users`.`id` = 1"] }

it { is_expected.to be_instance_of Array }
it { is_expected.to eq([1]) }
end

context "When call find_shard_keys with shardkey collection condition" do
subject {
tree = SQLTree["SELECT * FROM `users` WHERE `users`.`id` IN (1,2,3,4,5)"]
@mixer.find_shard_keys(tree.where, "users", "id")
}
context "includes `IN` shard_key condition" do
let(:sql_tree) { SQLTree["SELECT * FROM `users` WHERE `users`.`id` IN (1,2,3,4,5)"] }

it { is_expected.to be_instance_of Array }
it { is_expected.to eq([1, 2, 3, 4, 5]) }
end

context "When call find_shard_keys with not determine shardkey condition" do
subject {
tree = SQLTree["SELECT * FROM `users` WHERE `users`.`id` = 1 OR 1"]
@mixer.find_shard_keys(tree.where, "users", "id")
}
context "couldn't determine shard_key by their conditions" do
let(:sql_tree) { SQLTree["SELECT * FROM `users` WHERE `users`.`id` = 1 OR 1"] }

it { is_expected.to be_instance_of Array }
it { is_expected.to eq([]) }
end

context "When call find_shard_keys with except table definition SQL" do
subject {
tree = SQLTree["SELECT * FROM `users` WHERE id = 10"]
@mixer.find_shard_keys(tree.where, "users", "id")
}
context "includes shard_key conditions without table prefix" do
let(:sql_tree) { SQLTree["SELECT * FROM `users` WHERE id = 10"] }

it { is_expected.to be_instance_of Array }
it { is_expected.to eq([]) }
Expand Down

0 comments on commit b6f3f05

Please sign in to comment.