spec/support/sequel_test_support.rb in terrestrial-0.3.0 vs spec/support/sequel_test_support.rb in terrestrial-0.5.0
- old
+ new
@@ -2,12 +2,11 @@
module Terrestrial
module SequelTestSupport
module_function def build_datastore(_schema)
db_connection.tap { |db|
- # This test is using the database so we better clean it out first
- truncate_tables
+ clean_database
# The query_counter will let us make assertions about how efficiently
# the database is being used
reset_query_counter
db.loggers << query_counter
@@ -19,10 +18,11 @@
end
module_function def before_suite(schema)
drop_tables
create_tables(schema.fetch(:tables))
+ add_unique_indexes(schema.fetch(:unique_indexes))
add_foreign_keys(schema.fetch(:foreign_keys))
end
module_function def excluded_adapters
"memory"
@@ -44,22 +44,35 @@
tables.each do |table_name|
db_connection.drop_table(table_name, cascade: true)
end
end
- module_function def truncate_tables(tables = db_connection.tables)
- tables.each do |table_name|
- db_connection[table_name].truncate(cascade: true)
+ module_function def clean_database(tables = db_connection.tables)
+ stardard_test_tables = BLOG_SCHEMA.fetch(:tables).keys
+ test_tables_in_deletable_order = stardard_test_tables.reverse
+
+ clean_tables(test_tables_in_deletable_order)
+ end
+
+ module_function def clean_tables(names)
+ names.each do |name|
+ clean_table(name)
end
end
+ module_function def clean_table(name)
+ db_connection[name].delete
+ end
+
module_function def db_connection
- @@db_connection ||= Sequel.postgres(
- host: ENV.fetch("PGHOST"),
- user: ENV.fetch("PGUSER"),
- database: ENV.fetch("PGDATABASE"),
- ).tap { Sequel.default_timezone = :utc }
+ @db_connection ||= begin
+ Sequel.postgres(
+ host: ENV.fetch("PGHOST"),
+ user: ENV.fetch("PGUSER"),
+ database: ENV.fetch("PGDATABASE"),
+ ).tap { Sequel.default_timezone = :utc }
+ end
end
module_function def create_tables(tables)
tables.each do |table_name, fields|
db_connection.create_table(table_name) do
@@ -74,14 +87,28 @@
end
tables.keys
end
+ module_function def add_unique_indexes(unique_indexes)
+ unique_indexes.each do |(table, *cols)|
+ db_connection.alter_table(table) do
+ add_unique_constraint(cols)
+ end
+ end
+ end
+
module_function def add_foreign_keys(foreign_keys)
- foreign_keys.each do |(table, fk_col, foreign_table, key_col)|
+ default_options = { deferrable: false, on_delete: :set_null }
+
+ foreign_keys.each do |(table, fk_col, foreign_table, key_col, options)|
+ options_with_defaults = default_options
+ .merge(options || {})
+ .merge(key: key_col)
+
db_connection.alter_table(table) do
- add_foreign_key([fk_col], foreign_table, key: key_col, deferrable: false, on_delete: :set_null)
+ add_foreign_key([fk_col], foreign_table, options_with_defaults)
end
end
end
class QueryCounter
@@ -106,18 +133,24 @@
/\A\([0-9\.]+s\) SELECT/i === query
}
end
def write_count
- insert_count + update_count
+ upserts.count
end
def update_count
updates.count
end
def insert_count
inserts.count
+ end
+
+ def upserts
+ @info
+ .map { |query| query.gsub(/\A\([0-9\.]+s\) /, "") }
+ .select { |query| query.start_with?("INSERT") && query.include?("ON CONFLICT") }
end
def updates
@info
.map { |query| query.gsub(/\A\([0-9\.]+s\) /, "") }