fix datamapper sqlite adapter truncation

This commit is contained in:
Josh Lane 2014-06-24 10:13:28 -07:00
parent 8d7f72ffc9
commit 0a66461050
4 changed files with 95 additions and 1 deletions

View file

@ -12,7 +12,7 @@ module DataMapper
def truncate_tables(table_names)
table_names.each do |table_name|
adapter.truncate_table table_name
truncate_table table_name
end
end

View file

@ -0,0 +1,43 @@
require 'spec_helper'
require 'dm-core'
require 'dm-sqlite-adapter'
require File.expand_path('../../../../support/data_mapper/sqlite3_setup', __FILE__)
require 'database_cleaner/data_mapper/truncation'
module DataMapper
module ConnectionAdapters
describe do
before(:all) { data_mapper_sqlite3_setup }
let(:adapter) { DataMapperSQLite3Adapter }
let(:connection) do
data_mapper_sqlite3_connection
end
before(:each) do
connection.truncate_tables ['users']
end
describe "#truncate_table" do
it "truncates the table" do
2.times { User.create }
connection.truncate_table('users')
User.count.should eq 0
end
it "resets AUTO_INCREMENT index of table" do
2.times { User.create }
User.destroy
connection.truncate_table('users')
User.create.id.should eq 1
end
end
end
end
end

View file

@ -0,0 +1,13 @@
def load_schema
require 'dm-migrations'
DataMapper.auto_migrate!
end
class ::User
include DataMapper::Resource
property :id, Serial
property :name, String
end

View file

@ -0,0 +1,38 @@
require 'support/active_record/database_setup'
require 'support/data_mapper/schema_setup'
module DataMapperSQLite3Helper
puts "DataMapper #{DataMapper::VERSION}, sqlite3"
def config
db_config['sqlite3']
end
def create_db
@encoding = config['encoding'] || ENV['CHARSET'] || 'utf8'
begin
establish_connection(config.merge('database' => 'sqlite3', 'schema_search_path' => 'public'))
rescue Exception => e
$stderr.puts e, *(e.backtrace)
$stderr.puts "Couldn't create database for #{config.inspect}"
end
end
def establish_connection(config = config)
DataMapper.setup(:default, config)
end
def data_mapper_sqlite3_setup
create_db
establish_connection
load_schema
end
def data_mapper_sqlite3_connection
DataMapper.repository.adapter
end
end
RSpec.configure do |c|
c.include(DataMapperSQLite3Helper)
end