sinsiliux-hornsby 0.2.5 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +31 -0
- data/lib/hornsby.rb +54 -23
- data/spec/db/schema.rb +4 -0
- data/spec/hornsby_spec.rb +135 -98
- data/spec/spec_helper.rb +1 -0
- metadata +1 -11
data/README.rdoc
CHANGED
@@ -47,6 +47,16 @@ Scenarios look like:
|
|
47
47
|
All scenarios are run only once, no matter how many times they were called, meaning that you don't need to worry about
|
48
48
|
duplicating data.
|
49
49
|
|
50
|
+
There's also a possibility to delete preloaded data with hornsby_clear. When called without arguments it will drop all data.
|
51
|
+
You can also pass it table names, that will be cleared of any data. Beware that any scenarios already executed will still be
|
52
|
+
marked as executed, so you won't be able to execute them again. If you want to execute those scenarios later in test, you
|
53
|
+
can pass :undo option with list of scenarios to mark as not executed or :all if you want to mark that no scenario has been executed.
|
54
|
+
|
55
|
+
hornsby_clear :fruits, :trees # Deletes trees and fruits tables
|
56
|
+
hornsby_clear # Deletes all data except tables that are defined by Hornsby.skip_tables
|
57
|
+
hornsby_clear :fruits, :undo => :apples # Deletes fruits table and marks :apples scenario as not executed
|
58
|
+
hornsby_clear :undo => :all # Deletes all tables and marks all scenario as not executed (fresh start)
|
59
|
+
|
50
60
|
== Setup
|
51
61
|
|
52
62
|
The easiest way to install this gem for Ruby on Rails is just add this line to config/environment.rb (or config/environments/test.rb):
|
@@ -62,6 +72,8 @@ Lastly you could use it as plugin:
|
|
62
72
|
|
63
73
|
ruby script/plugin install git://github.com/sinsiliux/hornsby.git
|
64
74
|
|
75
|
+
=== RSpec
|
76
|
+
|
65
77
|
Add the following to spec_helper.rb
|
66
78
|
|
67
79
|
Spec::Runner.configure do |config|
|
@@ -74,6 +86,25 @@ configure_rspec supports two parameters:
|
|
74
86
|
* filename - file with hornsby scenarios (by default Rails.root/spec/hornsby_scenarios.rb)
|
75
87
|
* scenarios - list of hornsby scenarios that should be preloaded (available in all tests, never reloaded so they're much faster)
|
76
88
|
|
89
|
+
=== Test::Unit
|
90
|
+
|
91
|
+
Add the following lines to test_helper.rb
|
92
|
+
|
93
|
+
Hornsby.load(:filename => File.join('..', 'spec', 'hornsby_scenario.rb'), :scenarios => :preloaded_scenario)
|
94
|
+
class ActiveSupport::TestCase
|
95
|
+
include HornsbyHelper
|
96
|
+
|
97
|
+
def setup
|
98
|
+
Hornsby.setup(self)
|
99
|
+
end
|
100
|
+
|
101
|
+
def teardown
|
102
|
+
Hornsby.teardown
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
Hornsby.load supports same options as configure_rspec.
|
107
|
+
|
77
108
|
== Advanced Usage
|
78
109
|
|
79
110
|
Its just ruby, right? So go nuts:
|
data/lib/hornsby.rb
CHANGED
@@ -8,6 +8,7 @@ class Hornsby
|
|
8
8
|
end
|
9
9
|
|
10
10
|
cattr_reader :scenarios
|
11
|
+
cattr_accessor :executed_scenarios
|
11
12
|
# @@namespaces = {}
|
12
13
|
@@scenarios = {}
|
13
14
|
@@executed_scenarios = Set.new
|
@@ -17,29 +18,43 @@ class Hornsby
|
|
17
18
|
@@context = nil
|
18
19
|
|
19
20
|
def self.configure_rspec(config, options = {})
|
20
|
-
load(options
|
21
|
-
|
22
|
-
@@context = @@global_context
|
23
|
-
@@global_scenarios = Hornsby.build(options[:scenarios]) if options[:scenarios]
|
24
|
-
@@global_executed_scenarios = @@executed_scenarios.to_a
|
25
|
-
|
26
|
-
config.include(HornsbySpecHelper)
|
21
|
+
load(options)
|
27
22
|
|
23
|
+
config.include(HornsbyHelper)
|
28
24
|
config.before do
|
29
|
-
Hornsby.
|
30
|
-
Hornsby.send(:class_variable_set, '@@executed_scenarios', Set.new(Hornsby.send(:class_variable_get, '@@global_executed_scenarios')))
|
31
|
-
Hornsby.copy_ivars(self, true)
|
32
|
-
ActiveRecord::Base.connection.increment_open_transactions
|
33
|
-
ActiveRecord::Base.connection.transaction_joinable = false
|
34
|
-
ActiveRecord::Base.connection.begin_db_transaction
|
25
|
+
Hornsby.setup(self)
|
35
26
|
end
|
36
|
-
|
37
27
|
config.after do
|
38
|
-
|
39
|
-
|
28
|
+
Hornsby.teardown
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.configure_test(config, options)
|
33
|
+
load(options)
|
34
|
+
|
35
|
+
config.send(:include, HornsbyHelper)
|
36
|
+
config.setup do
|
37
|
+
Hornsby.setup(self)
|
38
|
+
end
|
39
|
+
config.teardown do
|
40
|
+
Hornsby.teardown
|
40
41
|
end
|
41
42
|
end
|
42
43
|
|
44
|
+
def self.setup(current_context)
|
45
|
+
@@context = @@global_context.clone
|
46
|
+
@@executed_scenarios = Set.new(@@global_executed_scenarios)
|
47
|
+
copy_ivars(current_context, true)
|
48
|
+
ActiveRecord::Base.connection.increment_open_transactions
|
49
|
+
ActiveRecord::Base.connection.transaction_joinable = false
|
50
|
+
ActiveRecord::Base.connection.begin_db_transaction
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.teardown
|
54
|
+
ActiveRecord::Base.connection.rollback_db_transaction
|
55
|
+
ActiveRecord::Base.connection.decrement_open_transactions
|
56
|
+
end
|
57
|
+
|
43
58
|
def self.build(*names)
|
44
59
|
scenarios = names.map {|name| @@scenarios[name.to_sym] or raise "scenario #{name} not found"}
|
45
60
|
|
@@ -49,20 +64,25 @@ class Hornsby
|
|
49
64
|
def self.[](name)
|
50
65
|
end
|
51
66
|
|
52
|
-
def self.load(
|
67
|
+
def self.load(options = {})
|
53
68
|
return unless @@scenarios.empty?
|
54
69
|
|
55
70
|
delete_tables
|
56
|
-
scenarios_file
|
71
|
+
scenarios_file = options[:filename] || File.join(framework_root, 'spec', 'hornsby_scenarios.rb')
|
57
72
|
self.module_eval File.read(scenarios_file)
|
73
|
+
|
74
|
+
@@context = @@global_context
|
75
|
+
@@global_scenarios = Hornsby.build(options[:scenarios]) if options[:scenarios]
|
76
|
+
@@global_executed_scenarios = @@executed_scenarios.to_a
|
58
77
|
end
|
59
78
|
|
60
79
|
def self.scenario(scenario, &block)
|
61
80
|
self.new(scenario, &block)
|
62
81
|
end
|
63
82
|
|
64
|
-
def self.delete_tables
|
65
|
-
tables
|
83
|
+
def self.delete_tables(*args)
|
84
|
+
args = tables if args.blank?
|
85
|
+
args.each { |t| ActiveRecord::Base.connection.delete(@@delete_sql % t) }
|
66
86
|
end
|
67
87
|
|
68
88
|
def self.tables
|
@@ -134,13 +154,24 @@ class Hornsby
|
|
134
154
|
end
|
135
155
|
|
136
156
|
|
137
|
-
module
|
157
|
+
module HornsbyHelper
|
138
158
|
def hornsby_scenario(*names)
|
139
159
|
Hornsby.build(*names)
|
140
160
|
Hornsby.copy_ivars(self)
|
141
161
|
end
|
142
162
|
|
143
|
-
def hornsby_clear
|
144
|
-
|
163
|
+
def hornsby_clear(*args)
|
164
|
+
options = args.extract_options!
|
165
|
+
Hornsby.delete_tables(*args)
|
166
|
+
|
167
|
+
if options[:undo] == :all
|
168
|
+
Hornsby.executed_scenarios.clear
|
169
|
+
else
|
170
|
+
undo = [options[:undo]].flatten.compact
|
171
|
+
unless (not_found = undo - Hornsby.executed_scenarios.to_a).blank?
|
172
|
+
raise(ArgumentError, "Scenario(s) #{not_found} not found")
|
173
|
+
end
|
174
|
+
Hornsby.executed_scenarios -= undo
|
175
|
+
end
|
145
176
|
end
|
146
177
|
end
|
data/spec/db/schema.rb
CHANGED
data/spec/hornsby_spec.rb
CHANGED
@@ -1,130 +1,167 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
2
|
|
3
|
-
describe Hornsby
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
it "should create @apple" do
|
9
|
-
@apple.should_not be_nil
|
10
|
-
end
|
11
|
-
|
12
|
-
it "should create Fruit @apple" do
|
13
|
-
@apple.should be_instance_of(Fruit)
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should not create @banana" do
|
17
|
-
@banana.should be_nil
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should have correct species" do
|
21
|
-
@apple.species.should == 'apple'
|
22
|
-
end
|
23
|
-
end
|
3
|
+
describe Hornsby do
|
4
|
+
describe "with just_apple scenario" do
|
5
|
+
before do
|
6
|
+
hornsby_scenario :just_apple
|
7
|
+
end
|
24
8
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should have correct @apple species" do
|
31
|
-
@apple.species.should == 'apple'
|
32
|
-
end
|
33
|
-
|
34
|
-
it "should have correct @banana species" do
|
35
|
-
@banana.species.should == 'banana'
|
36
|
-
end
|
37
|
-
end
|
9
|
+
it "should create @apple" do
|
10
|
+
@apple.should_not be_nil
|
11
|
+
end
|
38
12
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end
|
13
|
+
it "should create Fruit @apple" do
|
14
|
+
@apple.should be_instance_of(Fruit)
|
15
|
+
end
|
43
16
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
it "should have an @apple" do
|
49
|
-
@apple.species.should == 'apple'
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should have an @orange" do
|
53
|
-
@orange.species.should == 'orange'
|
54
|
-
end
|
55
|
-
|
56
|
-
it "should have no @banana" do
|
57
|
-
@banana.should be_nil
|
58
|
-
end
|
59
|
-
end
|
17
|
+
it "should not create @banana" do
|
18
|
+
@banana.should be_nil
|
19
|
+
end
|
60
20
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
@cherry.update_attribute(:average_diameter, 1)
|
65
|
-
@cherry.average_diameter.should == 1
|
21
|
+
it "should have correct species" do
|
22
|
+
@apple.species.should == 'apple'
|
23
|
+
end
|
66
24
|
end
|
67
25
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
end
|
26
|
+
describe "with bananas_and_apples scenario" do
|
27
|
+
before do
|
28
|
+
hornsby_scenario :bananas_and_apples
|
29
|
+
end
|
73
30
|
|
74
|
-
|
75
|
-
|
76
|
-
|
31
|
+
it "should have correct @apple species" do
|
32
|
+
@apple.species.should == 'apple'
|
33
|
+
end
|
77
34
|
|
78
|
-
|
79
|
-
|
80
|
-
|
35
|
+
it "should have correct @banana species" do
|
36
|
+
@banana.species.should == 'banana'
|
37
|
+
end
|
81
38
|
end
|
82
|
-
end
|
83
39
|
|
84
|
-
describe
|
85
|
-
|
86
|
-
|
87
|
-
|
40
|
+
describe "with fruit scenario" do
|
41
|
+
before do
|
42
|
+
hornsby_scenario :fruit
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should have 2 fruits" do
|
46
|
+
@fruit.should have(2).items
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should have an @apple" do
|
50
|
+
@apple.species.should == 'apple'
|
51
|
+
end
|
88
52
|
|
89
|
-
|
90
|
-
|
53
|
+
it "should have an @orange" do
|
54
|
+
@orange.species.should == 'orange'
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should have no @banana" do
|
58
|
+
@banana.should be_nil
|
59
|
+
end
|
91
60
|
end
|
92
61
|
|
93
|
-
|
94
|
-
|
62
|
+
describe 'with preloaded cherry scenario' do
|
63
|
+
it "should have correct size after changed by second test" do
|
64
|
+
@cherry.average_diameter.should == 3
|
65
|
+
@cherry.update_attribute(:average_diameter, 1)
|
66
|
+
@cherry.average_diameter.should == 1
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should have correct size" do
|
70
|
+
@cherry.average_diameter.should == 3
|
71
|
+
@cherry.update_attribute(:average_diameter, 5)
|
72
|
+
@cherry.average_diameter.should == 5
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should create big cherry" do
|
76
|
+
@big_cherry.species.should == 'cherry'
|
77
|
+
end
|
95
78
|
end
|
96
79
|
|
97
|
-
|
98
|
-
|
80
|
+
describe 'hornsby_clear' do
|
81
|
+
before do
|
82
|
+
hornsby_scenario :just_apple
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should clear scenarios when calling hornsby_clear" do
|
86
|
+
hornsby_clear
|
87
|
+
Fruit.count.should == 0
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should clear only tables passed" do
|
91
|
+
Tree.create!(:name => 'oak')
|
92
|
+
hornsby_clear :fruits
|
93
|
+
Tree.count.should == 1
|
94
|
+
Fruit.count.should == 0
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should mark scenarios as undone when passed :undone option" do
|
98
|
+
hornsby_scenario :fruit
|
99
|
+
hornsby_clear :undo => [:just_apple]
|
100
|
+
Fruit.count.should == 0
|
101
|
+
hornsby_scenario :fruit
|
102
|
+
Fruit.count.should == 1
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should mark all scenarios as undone when passed :undone option as :all" do
|
106
|
+
hornsby_scenario :fruit
|
107
|
+
hornsby_clear :undo => :all
|
108
|
+
Fruit.count.should == 0
|
109
|
+
hornsby_scenario :fruit
|
110
|
+
Fruit.count.should == 2
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should raise error when not executed scenarios passed to :undo option" do
|
114
|
+
lambda {
|
115
|
+
hornsby_clear :undo => :just_orange
|
116
|
+
}.should raise_error(ArgumentError)
|
117
|
+
end
|
99
118
|
end
|
100
|
-
end
|
101
119
|
|
102
|
-
describe
|
103
|
-
|
104
|
-
|
120
|
+
describe 'with many apples scenario' do
|
121
|
+
before do
|
122
|
+
hornsby_scenario :many_apples, :cherry, :cherry_basket
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should create only one apple" do
|
126
|
+
Fruit.all(:conditions => 'species = "apple"').size.should == 1
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should create only two cherries even if they were preloaded" do
|
130
|
+
Fruit.all(:conditions => 'species = "cherry"').size.should == 2
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should contain cherries in basket if basket is loaded in test and cherries preloaded" do
|
134
|
+
@basket.should == [@cherry, @big_cherry]
|
135
|
+
end
|
105
136
|
end
|
106
137
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
138
|
+
describe 'transactions' do
|
139
|
+
before do
|
140
|
+
hornsby_scenario :just_apple
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should drop only inner transaction" do
|
144
|
+
@apple.reload.should_not be_nil
|
145
|
+
begin
|
146
|
+
ActiveRecord::Base.transaction do
|
147
|
+
f = Fruit.create(:species => 'orange')
|
148
|
+
f.reload.should_not be_nil
|
149
|
+
raise 'some error'
|
150
|
+
end
|
151
|
+
rescue
|
114
152
|
end
|
115
|
-
|
153
|
+
@apple.reload.should_not be_nil
|
154
|
+
Fruit.find_by_species('orange').should be_nil
|
116
155
|
end
|
117
|
-
@apple.reload.should_not be_nil
|
118
|
-
Fruit.find_by_species('orange').should be_nil
|
119
156
|
end
|
120
|
-
end
|
121
157
|
|
122
|
-
#describe
|
158
|
+
#describe "with pitted namespace" do
|
123
159
|
# before do
|
124
160
|
# Hornsby.build('pitted:peach').copy_ivars(self)
|
125
161
|
# end
|
126
|
-
|
162
|
+
|
127
163
|
# it "should have @peach" do
|
128
164
|
# @peach.species.should == 'peach'
|
129
165
|
# end
|
130
166
|
#end
|
167
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -18,6 +18,7 @@ load(File.join("db", "schema.rb"))
|
|
18
18
|
require 'spec/autorun'
|
19
19
|
require '../lib/hornsby'
|
20
20
|
require 'db/fruit'
|
21
|
+
require 'db/tree'
|
21
22
|
|
22
23
|
Spec::Runner.configure do |config|
|
23
24
|
Hornsby.configure_rspec(config, :filename => File.join('hornsby_scenario.rb'), :scenarios => :big_cherry)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinsiliux-hornsby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrius Chamentauskas
|
@@ -13,16 +13,6 @@ cert_chain: []
|
|
13
13
|
date: 2009-05-16 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
|
-
- !ruby/object:Gem::Dependency
|
17
|
-
name: rspec
|
18
|
-
type: :runtime
|
19
|
-
version_requirement:
|
20
|
-
version_requirements: !ruby/object:Gem::Requirement
|
21
|
-
requirements:
|
22
|
-
- - ">="
|
23
|
-
- !ruby/object:Gem::Version
|
24
|
-
version: 1.2.0
|
25
|
-
version:
|
26
16
|
- !ruby/object:Gem::Dependency
|
27
17
|
name: activerecord
|
28
18
|
type: :runtime
|