mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
initial work done. Have failing example feauture and main config module done.
This commit is contained in:
parent
00299245c8
commit
13a59fc3af
14 changed files with 189 additions and 9 deletions
11
examples/features/example.feature
Normal file
11
examples/features/example.feature
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
Feature: example
|
||||||
|
In order to test DataBase Cleaner
|
||||||
|
Here are some scenarios that rely on the DB being clean!
|
||||||
|
|
||||||
|
Scenario: dirty the db
|
||||||
|
When I create a widget
|
||||||
|
Then I should see 1 widget
|
||||||
|
|
||||||
|
Scenario: assume a clean db
|
||||||
|
When I create a widget
|
||||||
|
Then I should see 1 widget
|
8
examples/features/step_definitions/example_steps.rb
Normal file
8
examples/features/step_definitions/example_steps.rb
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
When /^I create a widget$/ do
|
||||||
|
Widget.create!
|
||||||
|
end
|
||||||
|
|
||||||
|
Then /^I should see 1 widget$/ do
|
||||||
|
Widget.count.should == 1
|
||||||
|
end
|
||||||
|
|
19
examples/features/support/env.rb
Normal file
19
examples/features/support/env.rb
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
require 'rubygems'
|
||||||
|
require 'spec/expectations'
|
||||||
|
|
||||||
|
require 'activerecord'
|
||||||
|
require '../../lib/database_cleaner'
|
||||||
|
require 'database_cleaner/cucumber'
|
||||||
|
|
||||||
|
DatabaseCleaner.strategy = :transaction #DatabaseCleaner::ActiveRecord::Transaction.new
|
||||||
|
|
||||||
|
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
|
||||||
|
|
||||||
|
ActiveRecord::Schema.define(:version => 1) do
|
||||||
|
create_table :widgets do |t|
|
||||||
|
t.string :name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Widget < ActiveRecord::Base
|
||||||
|
end
|
|
@ -0,0 +1,2 @@
|
||||||
|
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
|
||||||
|
require 'database_cleaner/configuration'
|
6
lib/database_cleaner/active_record/transaction.rb
Normal file
6
lib/database_cleaner/active_record/transaction.rb
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
module DatabaseCleaner::ActiveRecord
|
||||||
|
class Transaction
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
66
lib/database_cleaner/configuration.rb
Normal file
66
lib/database_cleaner/configuration.rb
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
module DatabaseCleaner
|
||||||
|
|
||||||
|
class NoStrategySetError < StandardError; end
|
||||||
|
class NoORMDetected < StandardError; end
|
||||||
|
class UnknownStrategySpecified < ArgumentError; end
|
||||||
|
|
||||||
|
class << self
|
||||||
|
def strategy=(args)
|
||||||
|
strategy, *strategy_args = args
|
||||||
|
if strategy.is_a?(Symbol)
|
||||||
|
@strategy = orm_strategy(strategy).new(*strategy_args)
|
||||||
|
else
|
||||||
|
raise UnknownStrategySpecified, "need good error message"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def clear_strategy
|
||||||
|
@strategy = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def start
|
||||||
|
strategy.start
|
||||||
|
end
|
||||||
|
|
||||||
|
def clean
|
||||||
|
strategy.clean
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def strategy
|
||||||
|
return @strategy if @strategy
|
||||||
|
raise NoStrategySetError, "Please set a strategy with DatabaseCleaner.strategy=."
|
||||||
|
end
|
||||||
|
|
||||||
|
def orm_strategy(strategy)
|
||||||
|
require "database_cleaner/#{orm}/#{strategy}"
|
||||||
|
orm_module.const_get(strategy.to_s.capitalize)
|
||||||
|
rescue LoadError => e
|
||||||
|
raise UnknownStrategySpecified, "foo"
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def orm
|
||||||
|
if defined? ::ActiveRecord
|
||||||
|
'active_record'
|
||||||
|
elsif defined? ::DataMapper
|
||||||
|
'data_mapper'
|
||||||
|
else
|
||||||
|
raise NoORMDetected, "No known ORM was detected! Is ActiveRecord or DataMapper loaded?"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def orm_module
|
||||||
|
case orm
|
||||||
|
when 'active_record'
|
||||||
|
DatabaseCleaner::ActiveRecord
|
||||||
|
when 'data_mapper'
|
||||||
|
DatabaseCleaner::DataMapper
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
8
lib/database_cleaner/cucumber.rb
Normal file
8
lib/database_cleaner/cucumber.rb
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
|
||||||
|
Before do
|
||||||
|
DatabaseCleaner.start
|
||||||
|
end
|
||||||
|
|
||||||
|
After do
|
||||||
|
DatabaseCleaner.clean
|
||||||
|
end
|
6
lib/database_cleaner/data_mapper/transaction.rb
Normal file
6
lib/database_cleaner/data_mapper/transaction.rb
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
module DatabaseCleaner::DataMapper
|
||||||
|
class Transaction
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
54
spec/database_cleaner/configuration_spec.rb
Normal file
54
spec/database_cleaner/configuration_spec.rb
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
require File.dirname(__FILE__) + '/../spec_helper'
|
||||||
|
require 'database_cleaner/active_record/transaction'
|
||||||
|
require 'database_cleaner/data_mapper/transaction'
|
||||||
|
|
||||||
|
describe DatabaseCleaner do
|
||||||
|
before(:each) do
|
||||||
|
DatabaseCleaner::ActiveRecord::Transaction.stub!(:new).and_return(@strategy = mock('strategy'))
|
||||||
|
Object.const_set('ActiveRecord', "just mocking out the constant here...") unless defined?(::ActiveRecord)
|
||||||
|
DatabaseCleaner.clear_strategy
|
||||||
|
end
|
||||||
|
|
||||||
|
describe ".strategy=" do
|
||||||
|
it "should initialize the appropirate strategy based on the ORM adapter detected" do
|
||||||
|
DatabaseCleaner::ActiveRecord::Transaction.should_receive(:new).with('options' => 'hash')
|
||||||
|
DatabaseCleaner.strategy = :transaction, {'options' => 'hash'}
|
||||||
|
|
||||||
|
Object.send(:remove_const, 'ActiveRecord')
|
||||||
|
Object.const_set('DataMapper', "just mocking out the constant here...")
|
||||||
|
|
||||||
|
DatabaseCleaner::DataMapper::Transaction.should_receive(:new).with(no_args)
|
||||||
|
DatabaseCleaner.strategy = :transaction
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should raise an error when no ORM is detected" do
|
||||||
|
Object.send(:remove_const, 'ActiveRecord') if defined?(::ActiveRecord)
|
||||||
|
Object.send(:remove_const, 'DataMapper') if defined?(::DataMapper)
|
||||||
|
|
||||||
|
running { DatabaseCleaner.strategy = :transaction }.should raise_error(DatabaseCleaner::NoORMDetected)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should raise an error when the specified strategy is not found" do
|
||||||
|
running { DatabaseCleaner.strategy = :foo }.should raise_error(DatabaseCleaner::UnknownStrategySpecified)
|
||||||
|
running { DatabaseCleaner.strategy = Array }.should raise_error(DatabaseCleaner::UnknownStrategySpecified)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
%w[start clean].each do |strategy_method|
|
||||||
|
describe ".#{strategy_method}" do
|
||||||
|
it "should be delgated to the strategy set with strategy=" do
|
||||||
|
DatabaseCleaner.strategy = :transaction
|
||||||
|
|
||||||
|
@strategy.should_receive(strategy_method)
|
||||||
|
|
||||||
|
DatabaseCleaner.send(strategy_method)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should raise en error when no strategy has been set" do
|
||||||
|
running { DatabaseCleaner.send(strategy_method) }.should raise_error(DatabaseCleaner::NoStrategySetError)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,7 +0,0 @@
|
||||||
require File.dirname(__FILE__) + '/spec_helper'
|
|
||||||
|
|
||||||
describe "DatabaseCleaner" do
|
|
||||||
it "fails" do
|
|
||||||
fail "hey buddy, you should probably rename this file and start specing for real"
|
|
||||||
end
|
|
||||||
end
|
|
5
spec/spec.opts
Normal file
5
spec/spec.opts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
--colour
|
||||||
|
--format nested
|
||||||
|
--loadby
|
||||||
|
mtime
|
||||||
|
--reverse
|
|
@ -1,9 +1,11 @@
|
||||||
|
require 'rubygems'
|
||||||
require 'spec'
|
require 'spec'
|
||||||
|
|
||||||
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
||||||
|
|
||||||
require 'database_cleaner'
|
require 'database_cleaner'
|
||||||
|
|
||||||
Spec::Runner.configure do |config|
|
Spec::Runner.configure do |config|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
alias running lambda
|
||||||
|
|
Loading…
Reference in a new issue