mirror of
https://github.com/discourse/discourse.git
synced 2024-12-22 20:03:54 +08:00
68 lines
2.0 KiB
Ruby
68 lines
2.0 KiB
Ruby
|
# frozen_string_literal: true
|
||
|
|
||
|
RSpec.describe ::Migrations::Database::OfflineConnection do
|
||
|
subject(:connection) { described_class.new }
|
||
|
|
||
|
let!(:sql) { "INSERT INTO foo (id, name) VALUES (?, ?)" }
|
||
|
|
||
|
it_behaves_like "a database connection"
|
||
|
|
||
|
describe "#close" do
|
||
|
it "removes the cached statements" do
|
||
|
connection.insert(sql, [1, "Alice"])
|
||
|
connection.insert(sql, [2, "Bob"])
|
||
|
|
||
|
expect(connection.parametrized_insert_statements).to_not be_empty
|
||
|
|
||
|
connection.close
|
||
|
expect(connection.parametrized_insert_statements).to be_nil
|
||
|
end
|
||
|
end
|
||
|
|
||
|
describe "#closed?" do
|
||
|
it "correctly reports if connection is closed" do
|
||
|
expect(connection.closed?).to be false
|
||
|
connection.close
|
||
|
expect(connection.closed?).to be true
|
||
|
end
|
||
|
end
|
||
|
|
||
|
describe "#insert" do
|
||
|
it "can be called without errors" do
|
||
|
expect { connection.insert(sql, [1, "Alice"]) }.not_to raise_error
|
||
|
end
|
||
|
end
|
||
|
|
||
|
describe "#parametrized_insert_statements" do
|
||
|
it "returns an empty array if nothing has been cached" do
|
||
|
expect(connection.parametrized_insert_statements).to eq([])
|
||
|
end
|
||
|
|
||
|
it "returns the cached INSERT statements and parameters in original order" do
|
||
|
connection.insert(sql, [1, "Alice"])
|
||
|
connection.insert(sql, [2, "Bob"])
|
||
|
connection.insert(sql, [3, "Carol"])
|
||
|
|
||
|
expected_data = [[sql, [1, "Alice"]], [sql, [2, "Bob"]], [sql, [3, "Carol"]]]
|
||
|
expect(connection.parametrized_insert_statements).to eq(expected_data)
|
||
|
|
||
|
# multiple calls return the same data
|
||
|
expect(connection.parametrized_insert_statements).to eq(expected_data)
|
||
|
expect(connection.parametrized_insert_statements).to eq(expected_data)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
describe "#clear!" do
|
||
|
it "clears all cached data" do
|
||
|
connection.insert(sql, [1, "Alice"])
|
||
|
connection.insert(sql, [2, "Bob"])
|
||
|
connection.insert(sql, [3, "Carol"])
|
||
|
|
||
|
expect(connection.parametrized_insert_statements).to_not be_empty
|
||
|
|
||
|
connection.clear!
|
||
|
expect(connection.parametrized_insert_statements).to eq([])
|
||
|
end
|
||
|
end
|
||
|
end
|