spec/multisert_spec.rb in multisert-0.0.3 vs spec/multisert_spec.rb in multisert-0.0.4
- old
+ new
@@ -1,12 +1,13 @@
require 'mysql2'
require './spec/spec_helper'
require './lib/multisert'
# TODO: allow overriding in yaml config
-TEST_DATABASE = 'multisert_test'
-TEST_TABLE = 'test_data'
+TEST_DATABASE = 'multisert_test'
+TEST_TABLE = 'test_data'
+TEST_INDEXED_TABLE = 'test_indexed_data'
# TODO: make into yaml config
$connection = Mysql2::Client.new(host: 'localhost', username: 'root')
$cleaner = MultisertSpec::MrClean.new(database: TEST_DATABASE, connection: $connection) do |mgr|
@@ -16,12 +17,17 @@
test_field_int_2 int default null,
test_field_int_3 int default null,
test_field_int_4 int default null,
test_field_varchar varchar(10) default null,
test_field_date DATE default null,
- test_field_datetime DATETIME default null
- )]
+ test_field_datetime DATETIME default null)]
+
+ mgr.create_table_schemas << %[
+ CREATE TABLE IF NOT EXISTS #{mgr.database}.#{TEST_INDEXED_TABLE} (
+ test_id int not null,
+ test_field varchar(15) default null,
+ primary key (test_id))]
end
describe Multisert do
describe "<<" do
let(:buffer) { described_class.new }
@@ -46,21 +52,20 @@
before do
$cleaner.ensure_clean_database! teardown_tables: (!!ENV['TEARDOWN'] || false)
end
it "does not fall over when there are no entries" do
- write_buffer_records = connection.query "DELETE FROM #{TEST_DATABASE}.#{TEST_TABLE}"
- expect(write_buffer_records.to_a).to eq []
+ connection.query "DELETE FROM #{TEST_DATABASE}.#{TEST_TABLE}"
buffer.write_buffer!
write_buffer_records = connection.query "SELECT * FROM #{TEST_DATABASE}.#{TEST_TABLE}"
expect(write_buffer_records.to_a).to eq []
expect(buffer.entries).to eq []
end
- it "multi-inserts all added entries" do
+ it "multi-inserts all added entries and clears #entries" do
pre_write_buffer_records = connection.query "SELECT * FROM #{TEST_DATABASE}.#{TEST_TABLE}"
expect(pre_write_buffer_records.to_a).to eq []
buffer.connection = connection
buffer.database = TEST_DATABASE
@@ -87,12 +92,11 @@
expect(post_write_buffer_records.to_a).to eq [
{'test_field_int_1' => 1, 'test_field_int_2' => 3, 'test_field_int_3' => 4, 'test_field_int_4' => 5},
{'test_field_int_1' => 6, 'test_field_int_2' => 7, 'test_field_int_3' => 8, 'test_field_int_4' => 9},
{'test_field_int_1' => 10, 'test_field_int_2' => 11, 'test_field_int_3' => 12, 'test_field_int_4' => 13},
- {'test_field_int_1' => 14, 'test_field_int_2' => 15, 'test_field_int_3' => 16, 'test_field_int_4' => 17}
- ]
+ {'test_field_int_1' => 14, 'test_field_int_2' => 15, 'test_field_int_3' => 16, 'test_field_int_4' => 17}]
expect(buffer.entries).to eq []
end
it "works with strings" do
@@ -114,12 +118,11 @@
post_write_buffer_records = connection.query %[SELECT test_field_varchar FROM #{TEST_DATABASE}.#{TEST_TABLE}]
expect(post_write_buffer_records.to_a).to eq [
{'test_field_varchar' => 'a'},
{'test_field_varchar' => 'b'},
{'test_field_varchar' => 'c'},
- {'test_field_varchar' => 'd'}
- ]
+ {'test_field_varchar' => 'd'}]
expect(buffer.entries).to eq []
end
it "works with strings that have illegal characters"
@@ -144,12 +147,11 @@
expect(post_write_buffer_records.to_a).to eq [
{'test_field_date' => Date.parse('2013-01-15')},
{'test_field_date' => Date.parse('2013-01-16')},
{'test_field_date' => Date.parse('2013-01-17')},
- {'test_field_date' => Date.parse('2013-01-18')}
- ]
+ {'test_field_date' => Date.parse('2013-01-18')}]
expect(buffer.entries).to eq []
end
it "works with times" do
@@ -191,8 +193,91 @@
describe "#write!" do
it "aliases #write_buffer!" do
instance = described_class.new
flush_method = instance.method(:write!)
expect(flush_method).to eq instance.method(:write_buffer!)
+ end
+ end
+
+ describe "#insert_strategy" do
+ let(:connection) { $connection }
+ let(:buffer) { described_class.new }
+
+ before do
+ $cleaner.ensure_clean_database! teardown_tables: (!!ENV['TEARDOWN'] || false)
+ end
+
+ context "set to replace" do
+ it "writes over an existing record with the same primary / unique key" do
+ connection.query %[INSERT INTO #{TEST_DATABASE}.#{TEST_INDEXED_TABLE} (test_id, test_field)
+ VALUES (1, 'ONE'), (2, 'TWO')]
+
+ buffer.connection = connection
+ buffer.database = TEST_DATABASE
+ buffer.table = TEST_INDEXED_TABLE
+ buffer.fields = ['test_id', 'test_field']
+ buffer.insert_strategy = :replace
+
+ buffer << [1, 'SOMETHING NEW']
+
+ buffer.write_buffer!
+
+ post_write_buffer_records = connection.query %[
+ SELECT
+ test_id
+ , test_field
+ FROM #{TEST_DATABASE}.#{TEST_INDEXED_TABLE}]
+
+ expect(post_write_buffer_records.to_a).to eq [
+ {'test_id' => 1, 'test_field' => 'SOMETHING NEW'},
+ {'test_id' => 2, 'test_field' => 'TWO'}]
+ end
+ end
+
+ context "set to ignore" do
+ before do
+ connection.query %[INSERT INTO #{TEST_DATABASE}.#{TEST_INDEXED_TABLE} (test_id, test_field)
+ VALUES (1, 'ONE'), (2, 'TWO')]
+
+ buffer.connection = connection
+ buffer.database = TEST_DATABASE
+ buffer.table = TEST_INDEXED_TABLE
+ buffer.fields = ['test_id', 'test_field']
+ buffer.insert_strategy = :ignore
+
+ buffer << [1, 'NEW']
+ end
+
+ it "does not raise an error" do
+ expect { buffer.write_buffer! }.to_not raise_error
+ end
+
+ it "writes over an existing record with the same primary / unique key" do
+ buffer.write_buffer!
+
+ post_write_buffer_records = connection.query %[
+ SELECT
+ test_id
+ , test_field
+ FROM #{TEST_DATABASE}.#{TEST_INDEXED_TABLE}]
+
+ expect(post_write_buffer_records.to_a).to eq [
+ {'test_id' => 1, 'test_field' => 'ONE'},
+ {'test_id' => 2, 'test_field' => 'TWO'}]
+ end
+ end
+
+ context "set to non-supported insert strategy" do
+ it "tells of an error" do
+ buffer.connection = connection
+ buffer.database = TEST_DATABASE
+ buffer.table = TEST_INDEXED_TABLE
+ buffer.fields = ['test_id', 'test_field']
+ buffer.insert_strategy = :some_bogus_operation
+
+ buffer << [1, 'NEW']
+
+ expect { buffer.write_buffer! }.to raise_error
+ end
end
end
end