mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
Merge pull request #247 from chrismo/fix_sqlite_adapter_truncation
Add truncate_tables method to SQLiteAdapter.
This commit is contained in:
commit
9be69424dc
5 changed files with 93 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -8,4 +8,5 @@ vendor/
|
|||
examples/db/*.db
|
||||
examples/config/database.yml
|
||||
db/config.yml
|
||||
db/test.sqlite3
|
||||
.vagrant
|
||||
|
|
|
@ -24,3 +24,9 @@ postgres:
|
|||
host: 127.0.0.1
|
||||
encoding: unicode
|
||||
template: template0
|
||||
|
||||
sqlite3:
|
||||
adapter: sqlite3
|
||||
database: db/test.sqlite3
|
||||
pool: 5
|
||||
timeout: 5000
|
||||
|
|
|
@ -100,6 +100,10 @@ module DatabaseCleaner
|
|||
end
|
||||
alias truncate_table delete_table
|
||||
|
||||
def truncate_tables(tables)
|
||||
tables.each { |t| truncate_table(t) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Returns a boolean indicating if the SQLite database is using the sqlite_sequence table.
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
require 'spec_helper'
|
||||
require 'active_record'
|
||||
require 'support/active_record/sqlite3_setup'
|
||||
require 'database_cleaner/active_record/truncation'
|
||||
|
||||
module ActiveRecord
|
||||
module ConnectionAdapters
|
||||
describe do
|
||||
before(:all) { active_record_sqlite3_setup }
|
||||
|
||||
let(:adapter) { SQLite3Adapter }
|
||||
|
||||
let(:connection) do
|
||||
active_record_sqlite3_connection
|
||||
end
|
||||
|
||||
before(:each) do
|
||||
connection.truncate_tables connection.tables
|
||||
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.delete_all
|
||||
|
||||
connection.truncate_table('users')
|
||||
|
||||
User.create.id.should eq 1
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
40
spec/support/active_record/sqlite3_setup.rb
Normal file
40
spec/support/active_record/sqlite3_setup.rb
Normal file
|
@ -0,0 +1,40 @@
|
|||
require 'support/active_record/database_setup'
|
||||
require 'support/active_record/schema_setup'
|
||||
|
||||
module SQLite3Helper
|
||||
puts "Active Record #{ActiveRecord::VERSION::STRING}, sqlite3"
|
||||
|
||||
# ActiveRecord::Base.logger = Logger.new(STDERR)
|
||||
|
||||
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
|
||||
ActiveRecord::Base.establish_connection(config)
|
||||
end
|
||||
|
||||
def active_record_sqlite3_setup
|
||||
create_db
|
||||
establish_connection
|
||||
load_schema
|
||||
end
|
||||
|
||||
def active_record_sqlite3_connection
|
||||
ActiveRecord::Base.connection
|
||||
end
|
||||
end
|
||||
|
||||
RSpec.configure do |c|
|
||||
c.include SQLite3Helper
|
||||
end
|
Loading…
Reference in a new issue