initial work done. Have failing example feauture and main config module done.

This commit is contained in:
Ben Mabey 2009-03-01 01:45:58 -07:00
parent 00299245c8
commit 13a59fc3af
14 changed files with 189 additions and 9 deletions

View 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

View 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

View 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

View file

@ -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'

View file

@ -0,0 +1,6 @@
module DatabaseCleaner::ActiveRecord
class Transaction
end
end

View 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

View file

@ -0,0 +1,8 @@
Before do
DatabaseCleaner.start
end
After do
DatabaseCleaner.clean
end

View file

@ -0,0 +1,6 @@
module DatabaseCleaner::DataMapper
class Transaction
end
end

View 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

View file

@ -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
View file

@ -0,0 +1,5 @@
--colour
--format nested
--loadby
mtime
--reverse

View file

@ -1,9 +1,11 @@
require 'rubygems'
require 'spec'
$LOAD_PATH.unshift(File.dirname(__FILE__))
$:.unshift(File.dirname(__FILE__) + '/../lib')
require 'database_cleaner'
Spec::Runner.configure do |config|
end
alias running lambda