Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/improve specs #342

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions spec/chrono_model/adapter/ddl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def ids(table)
end

it 'supports a sequence of INSERT commands' do
adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
INSERT INTO #{table} (test, foo) VALUES
('test1', 1),
('test2', 2);
Expand All @@ -38,7 +38,7 @@ def ids(table)
expect(count(history)).to eq 2

expect do
adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
INSERT INTO #{table} (test, foo) VALUES
('test3', 3),
(NULL, 0);
Expand All @@ -48,7 +48,7 @@ def ids(table)
expect(count(current)).to eq 2
expect(count(history)).to eq 2

adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
INSERT INTO #{table} (test, foo) VALUES
('test4', 3),
('test5', 4);
Expand All @@ -64,7 +64,7 @@ def ids(table)
before do
adapter.create_table table, temporal: true, &columns

adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
INSERT INTO #{table} DEFAULT VALUES
SQL
end
Expand All @@ -74,7 +74,7 @@ def ids(table)
end

let(:select) do
adapter.select_values <<-SQL.squish
adapter.select_values <<~SQL.squish
SELECT test FROM #{table}
SQL
end
Expand All @@ -86,7 +86,7 @@ def ids(table)
before do
adapter.create_table table, temporal: true, id: :string, &columns

adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
INSERT INTO #{table} (test, id) VALUES ('test1', 'hello');
SQL
end
Expand All @@ -103,15 +103,15 @@ def ids(table)
before do
adapter.create_table table, temporal: true, &columns

adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
INSERT INTO #{table} (test, foo) VALUES ('test1', 1);
SQL

adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
UPDATE #{table} SET test = 'test2';
SQL

adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
UPDATE #{table} SET test = 'test2';
SQL
end
Expand All @@ -131,20 +131,20 @@ def ids(table)
t.timestamps null: false
end

adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
INSERT INTO #{table} (test, created_at, updated_at) VALUES ('test', now(), now());
SQL

adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
UPDATE #{table} SET test = 'test2', updated_at = now();
SQL

2.times do
adapter.execute <<-SQL.squish # Redundant update with only updated_at change
adapter.execute <<~SQL.squish # Redundant update with only updated_at change
UPDATE #{table} SET test = 'test2', updated_at = now();
SQL

adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
UPDATE #{table} SET updated_at = now();
SQL
end
Expand All @@ -166,16 +166,16 @@ def ids(table)
t.string 'bar'
end

adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
INSERT INTO #{table} (foo, bar) VALUES ('test foo', 'test bar');
SQL

adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
UPDATE #{table} SET foo = 'test foo', bar = 'no history';
SQL

2.times do
adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
UPDATE #{table} SET bar = 'really no history';
SQL
end
Expand Down Expand Up @@ -205,14 +205,14 @@ def ids(table)
it 'preserves options upon column change' do
adapter.change_table table, temporal: true, journal: %w[foo bar]

adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
INSERT INTO #{table} (foo, bar) VALUES ('test foo', 'test bar');
SQL

expect(count(current)).to eq 1
expect(count(history)).to eq 1

adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
UPDATE #{table} SET foo = 'test foo', bar = 'chronomodel';
SQL

Expand All @@ -223,15 +223,15 @@ def ids(table)
it 'changes option upon table change' do
adapter.change_table table, temporal: true, journal: %w[bar]

adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
INSERT INTO #{table} (foo, bar) VALUES ('test foo', 'test bar');
UPDATE #{table} SET foo = 'test foo', bar = 'no history';
SQL

expect(count(current)).to eq 1
expect(count(history)).to eq 1

adapter.execute <<-SQL.squish
adapter.execute <<~SQL.squish
UPDATE #{table} SET foo = 'test foo again', bar = 'no history';
SQL

Expand Down
15 changes: 7 additions & 8 deletions spec/support/matchers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ module Matchers
class Base
include ActiveRecord::Sanitization::ClassMethods

attr_reader :table

def matches?(table)
table = table.table_name if table.respond_to?(:table_name)
@table = table
end
private :matches? # This is an abstract class

def failure_message_for_should_not
failure_message_for_should.gsub('to ', 'to not ')
end
Expand Down Expand Up @@ -55,6 +47,13 @@ def select_rows(sql, binds, name = nil)

private

attr_reader :table

def matches?(table)
table = table.table_name if table.respond_to?(:table_name)
@table = table
end

def exec_query(sql, binds, name)
sql = sanitize_sql_array([sql, *Array.wrap(binds)])
connection.exec_query(sql, name)
Expand Down
2 changes: 1 addition & 1 deletion spec/support/matchers/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def has_column?(name, type)
def column_type(name)
table = "#{@schema}.#{self.table}"

select_rows(<<-SQL.squish, [table, name], 'Check column').first
select_rows(<<~SQL.squish, [table, name], 'Check column').first
SELECT attname, FORMAT_TYPE(atttypid, atttypmod)
FROM pg_attribute
WHERE attrelid = ?::regclass::oid
Expand Down
2 changes: 1 addition & 1 deletion spec/support/matchers/function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def failure_message_when_negated
protected

def has_function?(name)
select_value(<<-SQL.squish, [@schema, name], 'Check function') == true
select_value(<<~SQL.squish, [@schema, name], 'Check function') == true
SELECT EXISTS(
SELECT 1
FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n
Expand Down
26 changes: 13 additions & 13 deletions spec/support/matchers/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ def description
def matches?(table)
super

select_values(<<-SQL.squish, [table, name, schema], 'Check index') == columns
SELECT a.attname
FROM pg_class t
JOIN pg_index d ON t.oid = d.indrelid
JOIN pg_class i ON i.oid = d.indexrelid
JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(d.indkey)
WHERE i.relkind = 'i'
AND t.relname = ?
AND i.relname = ?
AND i.relnamespace = (
SELECT oid FROM pg_namespace WHERE nspname = ?
)
ORDER BY a.attname
select_values(<<~SQL.squish, [table, name, schema], 'Check index') == columns
SELECT a.attname
FROM pg_class t
JOIN pg_index d ON t.oid = d.indrelid
JOIN pg_class i ON i.oid = d.indexrelid
JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(d.indkey)
WHERE i.relkind = 'i'
AND t.relname = ?
AND i.relname = ?
AND i.relnamespace = (
SELECT oid FROM pg_namespace WHERE nspname = ?
)
ORDER BY a.attname
SQL
end

Expand Down
4 changes: 2 additions & 2 deletions spec/support/matchers/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def failure_message_when_negated
end

def matches?(*)
@current = select_value(<<-SQL.squish, [], 'Current schema')
SHOW search_path
@current = select_value(<<~SQL.squish, [], 'Current schema')
SHOW search_path
SQL

@current == @expected
Expand Down
6 changes: 3 additions & 3 deletions spec/support/matchers/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def description
def matches?(table)
super

source = select_value(<<-SQL.squish, [@function], "Get #{@function} source")
SELECT prosrc FROM pg_catalog.pg_proc WHERE proname = ?
source = select_value(<<~SQL.squish, [@function], "Get #{@function} source")
SELECT prosrc FROM pg_catalog.pg_proc WHERE proname = ?
SQL

!(source =~ @regexp).nil?
@regexp.match?(source)
end

def failure_message
Expand Down
13 changes: 6 additions & 7 deletions spec/support/matchers/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class Base < ChronoTest::Matchers::Base
def relation_exists?(options)
schema = options[:in]
kind = options[:kind] == :view ? 'v' : 'r'

select_value(<<-SQL.squish, [table, schema], 'Check table exists') == true
select_value(<<~SQL.squish, [table, schema], 'Check table exists') == true
SELECT EXISTS (
SELECT 1
FROM pg_class c
Expand Down Expand Up @@ -130,7 +129,7 @@ def table_exists?
def inherits_from_temporal?
binds = ["#{history_schema}.#{table}", "#{temporal_schema}.#{table}"]

@inheritance = select_value(<<-SQL.squish, binds, 'Check inheritance') == true
@inheritance = select_value(<<~SQL.squish, binds, 'Check inheritance') == true
SELECT EXISTS (
SELECT 1 FROM pg_catalog.pg_inherits
WHERE inhrelid = ?::regclass::oid
Expand All @@ -142,7 +141,7 @@ def inherits_from_temporal?
def has_history_indexes?
binds = [history_schema, table]

indexes = select_values(<<-SQL.squish, binds, 'Check history indexes')
indexes = select_values(<<~SQL.squish, binds, 'Check history indexes')
SELECT indexdef FROM pg_indexes
WHERE schemaname = ?
AND tablename = ?
Expand Down Expand Up @@ -173,7 +172,7 @@ def has_consistency_constraint?
attname: connection.primary_key(table)
}

@constraint = select_value(<<-SQL.squish, binds, 'Check Consistency Constraint') == true
@constraint = select_value(<<~SQL.squish, binds, 'Check Consistency Constraint') == true
SELECT EXISTS (
SELECT 1 FROM pg_catalog.pg_constraint
WHERE conname = :conname
Expand Down Expand Up @@ -244,14 +243,14 @@ def view_exists?
def is_updatable?
binds = [public_schema, table]

@updatable = select_value(<<-SQL.squish, binds, 'Check updatable') == 'YES'
@updatable = select_value(<<~SQL.squish, binds, 'Check updatable') == 'YES'
SELECT is_updatable FROM information_schema.views
WHERE table_schema = ? AND table_name = ?
SQL
end

def has_triggers?
triggers = select_values(<<-SQL.squish, [public_schema, table], 'Check triggers')
triggers = select_values(<<~SQL.squish, [public_schema, table], 'Check triggers')
SELECT t.tgname
FROM pg_catalog.pg_trigger t, pg_catalog.pg_class c, pg_catalog.pg_namespace n
WHERE n.oid = c.relnamespace
Expand Down
Loading