spec/support/sequel_test_support.rb in terrestrial-0.1.1 vs spec/support/sequel_test_support.rb in terrestrial-0.3.0

- old
+ new

@@ -1,45 +1,69 @@ require "sequel" module Terrestrial module SequelTestSupport - def create_database + 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 + + # The query_counter will let us make assertions about how efficiently + # the database is being used + reset_query_counter + db.loggers << query_counter + } + end + + module_function def query_counter + @@query_counter ||= QueryCounter.new + end + + module_function def before_suite(schema) + drop_tables + create_tables(schema.fetch(:tables)) + add_foreign_keys(schema.fetch(:foreign_keys)) + end + + module_function def excluded_adapters + "memory" + end + + module_function def reset_query_counter + @@query_counter = nil + end + + module_function def create_database `psql postgres --command "CREATE DATABASE $PGDATABASE;"` end - module_function :create_database - def drop_database + module_function def drop_database `psql postgres --command "DROP DATABASE $PGDATABASE;"` end - module_function :drop_database - def drop_tables(tables = db_connection.tables) + module_function def drop_tables(tables = db_connection.tables) tables.each do |table_name| db_connection.drop_table(table_name, cascade: true) end end - module_function :drop_tables - def truncate_tables(tables = db_connection.tables) + module_function def truncate_tables(tables = db_connection.tables) tables.each do |table_name| db_connection[table_name].truncate(cascade: true) end end - module_function :truncate_tables - def db_connection - Sequel.default_timezone = :utc - @db_connection ||= Sequel.postgres( + 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 } end - module_function :db_connection - def create_tables(schema) - schema.fetch(:tables).each do |table_name, fields| + module_function def create_tables(tables) + tables.each do |table_name, fields| db_connection.create_table(table_name) do fields.each do |field| type = field.fetch(:type) name = field.fetch(:name) options = field.fetch(:options, {}) @@ -47,26 +71,21 @@ column(name, type, options) end end end - schema.fetch(:foreign_keys).each do |(table, fk_col, foreign_table, key_col)| + tables.keys + end + + module_function def add_foreign_keys(foreign_keys) + foreign_keys.each do |(table, fk_col, foreign_table, key_col)| db_connection.alter_table(table) do add_foreign_key([fk_col], foreign_table, key: key_col, deferrable: false, on_delete: :set_null) end end - - schema.fetch(:tables).keys end - module_function :create_tables - def insert_records(datastore, records) - records.each { |(namespace, record)| - datastore[namespace].insert(record) - } - end - class QueryCounter def initialize reset end @@ -86,17 +105,31 @@ @info.count { |query| /\A\([0-9\.]+s\) SELECT/i === query } end + def write_count + insert_count + update_count + end + def update_count updates.count end + def insert_count + inserts.count + end + def updates @info .map { |query| query.gsub(/\A\([0-9\.]+s\) /, "") } .select { |query| query.start_with?("UPDATE") } + end + + def inserts + @info + .map { |query| query.gsub(/\A\([0-9\.]+s\) /, "") } + .select { |query| query.start_with?("INSERT") } end def show_queries puts @info.join("\n") end