Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request DatabaseCleaner#56 from gucki/master
sequel adapter
  • Loading branch information
bmabey committed May 11, 2011
2 parents e8607e5 + c03ec11 commit 41602da
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -32,6 +32,7 @@ group :development do
gem "tzinfo", "0.3.22"
gem "mongo_mapper", "0.8.2"
gem "couch_potato", "0.3.0"
gem "sequel", "~>3.21.0"
#gem "ibm_db" # I don't want to add this dependency, even as a dev one since it requires DB2 to be installed
end

Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -127,6 +127,7 @@ GEM
linecache (>= 0.3)
rubyforge (2.0.4)
json_pure (>= 1.1.7)
sequel (3.21.0)
sqlite3-ruby (1.3.1)
stringex (1.1.0)
sys-uname (0.8.4)
Expand Down Expand Up @@ -157,5 +158,6 @@ DEPENDENCIES
rspactor
rspec
ruby-debug
sequel (~> 3.21.0)
sqlite3-ruby
tzinfo (= 0.3.22)
6 changes: 4 additions & 2 deletions lib/database_cleaner/base.rb
Expand Up @@ -118,15 +118,17 @@ def autodetect
:mongoid
elsif defined? ::CouchPotato
:couch_potato
elsif defined? ::Sequel
:sequel
else
raise NoORMDetected, "No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, or CouchPotato loaded?"
raise NoORMDetected, "No known ORM was detected! Is ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, or CouchPotato loaded?"
end
end
end

def set_default_orm_strategy
case orm
when :active_record, :data_mapper
when :active_record, :data_mapper, :sequel
self.strategy = :transaction
when :mongo_mapper, :mongoid, :couch_potato
self.strategy = :truncation
Expand Down
2 changes: 2 additions & 0 deletions lib/database_cleaner/configuration.rb
Expand Up @@ -86,6 +86,8 @@ def orm_module(symbol)
DatabaseCleaner::MongoMapper
when :couch_potato
DatabaseCleaner::CouchPotato
when :sequel
DatabaseCleaner::Sequel
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions lib/database_cleaner/sequel/base.rb
@@ -0,0 +1,22 @@
require 'database_cleaner/generic/base'
module DatabaseCleaner
module Sequel
def self.available_strategies
%w[truncation transaction]
end

module Base
include ::DatabaseCleaner::Generic::Base

def db=(desired_db)
@db = desired_db
end

def db
return @db if @db && @db != :default
raise "As you have more than one active sequel database you have to specify the one to use manually!" if ::Sequel::DATABASES.count > 1
::Sequel::DATABASES.first
end
end
end
end
20 changes: 20 additions & 0 deletions lib/database_cleaner/sequel/transaction.rb
@@ -0,0 +1,20 @@
require 'database_cleaner/sequel/base'
module DatabaseCleaner
module Sequel
class Transaction
include ::DatabaseCleaner::Sequel::Base

def start
@transactions ||= []
db.send(:add_transaction)
@transactions << db.send(:begin_transaction, db)
end

def clean
transaction = @transactions.pop
db.send(:rollback_transaction, transaction)
db.send(:remove_transaction, transaction)
end
end
end
end
31 changes: 31 additions & 0 deletions lib/database_cleaner/sequel/truncation.rb
@@ -0,0 +1,31 @@
require "database_cleaner/generic/truncation"
require 'database_cleaner/sequel/base'

module DatabaseCleaner
module Sequel
class Truncation
include ::DatabaseCleaner::Sequel::Base
include ::DatabaseCleaner::Generic::Truncation

def clean
each_table do |db, table|
db[table].truncate
end
end

def each_table
tables_to_truncate(db).each do |table|
yield db, table
end
end

private

def tables_to_truncate(db)
(@only || db.tables) - @tables_to_exclude
end
end
end
end


31 changes: 31 additions & 0 deletions spec/database_cleaner/sequel/base_spec.rb
@@ -0,0 +1,31 @@
require 'spec_helper'
require 'database_cleaner/sequel/base'
require 'database_cleaner/shared_strategy_spec'
require 'sequel'

module DatabaseCleaner
describe Sequel do
it { should respond_to(:available_strategies) }
end

module Sequel
class ExampleStrategy
include ::DatabaseCleaner::Sequel::Base
end

describe ExampleStrategy do
it_should_behave_like "a generic strategy"
it { should respond_to(:db) }
it { should respond_to(:db=) }

it "should store my desired db" do
subject.db = :my_db
subject.db.should == :my_db
end

it "should default to :default" do
subject.db.should == :default
end
end
end
end
21 changes: 21 additions & 0 deletions spec/database_cleaner/sequel/transaction_spec.rb
@@ -0,0 +1,21 @@
require 'spec_helper'
require 'database_cleaner/sequel/transaction'
require 'database_cleaner/shared_strategy_spec'
require 'sequel'

module DatabaseCleaner
module Sequel
describe Transaction do
it_should_behave_like "a generic strategy"
it_should_behave_like "a generic transaction strategy"

describe "start" do
it "should start a transaction"
end

describe "clean" do
it "should finish a transaction"
end
end
end
end
13 changes: 13 additions & 0 deletions spec/database_cleaner/sequel/truncation_spec.rb
@@ -0,0 +1,13 @@
require 'spec_helper'
require 'database_cleaner/sequel/truncation'
require 'database_cleaner/shared_strategy_spec'
require 'sequel'

module DatabaseCleaner
module Sequel
describe Truncation do
it_should_behave_like "a generic strategy"
it_should_behave_like "a generic truncation strategy"
end
end
end

0 comments on commit 41602da

Please sign in to comment.