refactor datamapper sqlite3 test helper to inherit from DatabaseHelper.

This commit is contained in:
Micah Geisel 2018-05-05 12:44:02 -07:00
parent 673ef22ff2
commit ac54a9656f
4 changed files with 43 additions and 67 deletions

View file

@ -1,38 +1,30 @@
require 'dm-core'
require 'dm-sqlite-adapter'
require 'support/data_mapper/sqlite3_setup'
require 'support/data_mapper/sqlite3_helper'
require 'database_cleaner/data_mapper/truncation'
RSpec.describe DatabaseCleaner::DataMapper::Truncation do
let(:helper) { DataMapperSQLite3Helper.new }
let(:connection) do
helper.data_mapper_sqlite3_connection
end
let(:connection) { helper.connection }
around do |example|
helper.data_mapper_sqlite3_setup
helper.setup
example.run
helper.data_mapper_sqlite3_teardown
helper.teardown
end
describe "DM connection adapter monkeypatches" do
before do
2.times { DmUser.create }
end
describe "#truncate_table" do
it "truncates the table" do
2.times { DmUser.create }
connection.truncate_table(DmUser.storage_names[:default])
expect(DmUser.count).to eq 0
end
it "resets AUTO_INCREMENT index of table" do
2.times { DmUser.create }
DmUser.destroy
connection.truncate_table(DmUser.storage_names[:default])
expect(DmUser.create.id).to eq 1
end
end

View file

@ -1,15 +0,0 @@
def data_mapper_load_schema
require 'dm-migrations'
DataMapper.auto_migrate!
end
class ::DmUser
include DataMapper::Resource
self.storage_names[:default] = 'users'
property :id, Serial
property :name, String
end

View file

@ -0,0 +1,35 @@
require 'dm-core'
require 'dm-sqlite-adapter'
require 'support/database_helper'
class ::DmUser
include DataMapper::Resource
self.storage_names[:default] = 'users'
property :id, Serial
property :name, String
end
class DataMapperSQLite3Helper < DatabaseHelper
puts "DataMapper #{DataMapper::VERSION}, sqlite3"
def connection
DataMapper.repository.adapter
end
private
def default_config
db_config['sqlite3']
end
def create_db
# NO-OP
end
def establish_connection(config = default_config)
DataMapper.setup(:default, config)
end
end

View file

@ -1,36 +0,0 @@
require 'support/active_record/database_setup'
require 'support/data_mapper/schema_setup'
class DataMapperSQLite3Helper
puts "DataMapper #{DataMapper::VERSION}, sqlite3"
def data_mapper_sqlite3_setup
create_db
establish_connection
data_mapper_load_schema
end
def data_mapper_sqlite3_connection
DataMapper.repository.adapter
end
def data_mapper_sqlite3_teardown
DataMapper.repository.adapter.truncate_tables(DataMapper::Model.descendants.map { |d| d.storage_names[:default] || d.name.underscore })
end
private
def default_config
db_config['sqlite3']
end
def create_db
@encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8'
establish_connection(default_config.merge('database' => 'sqlite3', 'schema_search_path' => 'public'))
end
def establish_connection(config = default_config)
DataMapper.setup(:default, config)
end
end