mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
cucumber feature and example app done. Got the AR transaction strategy done as well.
This commit is contained in:
parent
13a59fc3af
commit
5e8df327d8
6 changed files with 81 additions and 20 deletions
1
cucumber.yml
Normal file
1
cucumber.yml
Normal file
|
@ -0,0 +1 @@
|
||||||
|
default: features
|
|
@ -1,19 +1,16 @@
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'spec/expectations'
|
require 'spec/expectations'
|
||||||
|
|
||||||
require 'activerecord'
|
begin
|
||||||
require '../../lib/database_cleaner'
|
require "#{File.dirname(__FILE__)}/../../lib/#{ENV['ORM']}"
|
||||||
|
rescue LoadError
|
||||||
|
raise "I don't have the setup for the '#{ENV['ORM']}' ORM!"
|
||||||
|
end
|
||||||
|
|
||||||
|
$:.unshift(File.dirname(__FILE__) + '/../../../lib')
|
||||||
|
require 'database_cleaner'
|
||||||
require 'database_cleaner/cucumber'
|
require 'database_cleaner/cucumber'
|
||||||
|
|
||||||
DatabaseCleaner.strategy = :transaction #DatabaseCleaner::ActiveRecord::Transaction.new
|
DatabaseCleaner.strategy = ENV['STRATEGY'].to_sym
|
||||||
|
|
||||||
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
|
|
||||||
|
|
12
examples/lib/activerecord.rb
Normal file
12
examples/lib/activerecord.rb
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
require 'activerecord'
|
||||||
|
|
||||||
|
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
|
|
@ -1,9 +1,15 @@
|
||||||
Feature: something something
|
Feature: database cleaning
|
||||||
In order to something something
|
In order to ease example and feature writing
|
||||||
A user something something
|
As a developer
|
||||||
something something something
|
I want to have my database in a clean state
|
||||||
|
|
||||||
Scenario: something something
|
Scenario Outline: ruby app
|
||||||
Given inspiration
|
Given I am using <ORM>
|
||||||
When I create a sweet new gem
|
And the <Strategy> cleaning strategy
|
||||||
Then everyone should see how awesome I am
|
|
||||||
|
When I run my scenarios that rely on a clean database
|
||||||
|
Then I should see all green
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
| ORM | Strategy |
|
||||||
|
| ActiveRecord | transaction |
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
Given /^I am using (ActiveRecord|DataMapper)$/ do |orm|
|
||||||
|
@orm = orm
|
||||||
|
end
|
||||||
|
|
||||||
|
Given /^the (.+) cleaning strategy$/ do |strategy|
|
||||||
|
@strategy = strategy
|
||||||
|
end
|
||||||
|
|
||||||
|
When "I run my scenarios that rely on a clean database" do
|
||||||
|
full_dir ||= File.expand_path(File.dirname(__FILE__) + "/../../examples/")
|
||||||
|
Dir.chdir(full_dir) do
|
||||||
|
ENV['ORM'] = @orm.downcase
|
||||||
|
ENV['STRATEGY'] = @strategy
|
||||||
|
@out = `cucumber features`
|
||||||
|
@status = $?.exitstatus
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Then "I should see all green" do
|
||||||
|
unless @status == 0
|
||||||
|
raise "Expected to see all green but we saw FAIL! Output:\n#{@out}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,26 @@
|
||||||
module DatabaseCleaner::ActiveRecord
|
module DatabaseCleaner::ActiveRecord
|
||||||
class Transaction
|
class Transaction
|
||||||
|
|
||||||
|
def start
|
||||||
|
if ActiveRecord::Base.connection.respond_to?(:increment_open_transactions)
|
||||||
|
ActiveRecord::Base.connection.increment_open_transactions
|
||||||
|
else
|
||||||
|
ActiveRecord::Base.__send__(:increment_open_transactions)
|
||||||
|
end
|
||||||
|
|
||||||
|
ActiveRecord::Base.connection.begin_db_transaction
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def clean
|
||||||
|
ActiveRecord::Base.connection.rollback_db_transaction
|
||||||
|
|
||||||
|
if ActiveRecord::Base.connection.respond_to?(:decrement_open_transactions)
|
||||||
|
ActiveRecord::Base.connection.decrement_open_transactions
|
||||||
|
else
|
||||||
|
ActiveRecord::Base.__send__(:decrement_open_transactions)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue