blueprints 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +31 -27
- data/lib/blueprints.rb +1 -0
- data/lib/blueprints/ar_extensions.rb +20 -0
- data/spec/blueprints.rb +7 -1
- data/spec/blueprints_spec.rb +23 -7
- data/spec/db/schema.rb +1 -0
- data/spec/spec_helper.rb +1 -0
- data/test/blueprints_test.rb +22 -0
- data/test/test_helper.rb +1 -0
- metadata +3 -2
data/README.rdoc
CHANGED
@@ -4,27 +4,27 @@ Another replacement for factories and fixtures that focuses on being DRY and mak
|
|
4
4
|
|
5
5
|
== Usage
|
6
6
|
|
7
|
-
|
7
|
+
Blueprints look like this:
|
8
8
|
|
9
|
-
|
9
|
+
blueprint :apple do
|
10
10
|
Fruit.create! :species => 'apple'
|
11
11
|
end
|
12
12
|
|
13
|
-
|
13
|
+
blueprint :orange do
|
14
14
|
Fruit.create! :species => 'orange'
|
15
15
|
end
|
16
16
|
|
17
|
-
|
17
|
+
blueprint :fruitbowl => [:apple, :orange] do
|
18
18
|
FruitBowl.create! :fruit => [@apple,@orange]
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
blueprint :kitchen => :fruitbowl do
|
22
22
|
Kitchen.create! :fruitbowl => @fruitbowl
|
23
23
|
end
|
24
24
|
|
25
25
|
...and you use them in specs like:
|
26
26
|
|
27
|
-
describe Fruit, "
|
27
|
+
describe Fruit, "apple" do
|
28
28
|
before do
|
29
29
|
build :apple
|
30
30
|
end
|
@@ -44,23 +44,26 @@ Plans look like:
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
Result of '
|
48
|
-
inside '
|
47
|
+
Result of 'blueprint' block is assigned to an instance variable with the same name. You can also assign your own instance variables
|
48
|
+
inside 'blueprint' block and they will be accessible in tests that build this blueprint.
|
49
49
|
|
50
|
-
|
51
|
-
|
50
|
+
Instead of SomeModel.create! you can also use SomeModel.blueprint, which does the same thing but also bypasses attr_protected
|
51
|
+
and attr_accessible restrictions (which is what you usually want in tests).
|
52
|
+
|
53
|
+
There's also a shorthand for these type of scenarios:
|
54
|
+
|
55
|
+
blueprint :something do
|
56
|
+
@something = SomeModel.blueprint :field => 'value'
|
57
|
+
end
|
58
|
+
|
59
|
+
You can just type:
|
52
60
|
|
53
|
-
|
54
|
-
You can also pass it table names, that will be cleared of any data. Beware that any scenarios already executed will still be
|
55
|
-
marked as executed, so you won't be able to execute them again. If you want to execute those scenarios later in test, you
|
56
|
-
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.
|
61
|
+
SomeModel.blueprint :something, :field => 'value'
|
57
62
|
|
58
|
-
|
59
|
-
|
60
|
-
demolish :fruits, :undo => :apples # Deletes fruits table and marks :apples scenario as not executed
|
61
|
-
demolish :undo => :all # Deletes all tables and marks all scenario as not executed (fresh start)
|
63
|
+
All blueprints are run only once, no matter how many times they were called, meaning that you don't need to worry about
|
64
|
+
duplicating data.
|
62
65
|
|
63
|
-
Blueprints searches for
|
66
|
+
Blueprints searches for blueprints files in this particular order in Rails (Merb) root:
|
64
67
|
* blueprints.rb
|
65
68
|
* blueprints/*.rb
|
66
69
|
* blueprint.rb
|
@@ -79,22 +82,23 @@ You can pass :root option to override framework root and :filename option to pas
|
|
79
82
|
|
80
83
|
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):
|
81
84
|
|
82
|
-
config.gem '
|
85
|
+
config.gem 'blueprints', :source => 'http://gemcutter.org'
|
83
86
|
|
84
87
|
If you’re not using rails, then you can install it through command line
|
85
88
|
|
86
|
-
gem sources -a http://
|
87
|
-
sudo gem install
|
89
|
+
gem sources -a http://gemcutter.org
|
90
|
+
sudo gem install blueprints
|
88
91
|
|
89
92
|
Lastly you could use it as plugin:
|
90
93
|
|
91
94
|
ruby script/plugin install git://github.com/sinsiliux/blueprints.git
|
92
95
|
|
93
|
-
|
94
|
-
|
95
|
-
* root - custom framework root if automatic detection fails for some reason (eg. not rails/merb project)
|
96
|
-
* filename - custom files pattern with blueprints plans
|
97
|
-
* prebuild - list of blueprints plans that should be preloaded (available in all tests, never reloaded so they're much faster)
|
96
|
+
Blueprints is activated by calling enable_blueprints. For specifics on how to call that in your testing framework see a little lower.
|
97
|
+
enable_blueprints supports these parameters:
|
98
|
+
* :root - custom framework root if automatic detection fails for some reason (eg. not rails/merb project)
|
99
|
+
* :filename - custom files pattern with blueprints plans
|
100
|
+
* :prebuild - list of blueprints plans that should be preloaded (available in all tests, never reloaded so they're much faster)
|
101
|
+
* :delete_policy - set custom delete policy when deleting everything from tables (before test suite or when calling demolish). Can be :truncate or :delete, defaults to :delete
|
98
102
|
|
99
103
|
=== RSpec
|
100
104
|
|
data/lib/blueprints.rb
CHANGED
@@ -2,6 +2,7 @@ require File.join(File.dirname(__FILE__), 'blueprints/plan')
|
|
2
2
|
require File.join(File.dirname(__FILE__), 'blueprints/file_context')
|
3
3
|
require File.join(File.dirname(__FILE__), 'blueprints/helper')
|
4
4
|
require File.join(File.dirname(__FILE__), 'blueprints/errors')
|
5
|
+
require File.join(File.dirname(__FILE__), 'blueprints/ar_extensions')
|
5
6
|
if defined? Spec
|
6
7
|
require File.join(File.dirname(__FILE__), 'blueprints/rspec_extensions')
|
7
8
|
else
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
class Base
|
5
|
+
def self.blueprint(*args)
|
6
|
+
options = args.extract_options!
|
7
|
+
if args.present?
|
8
|
+
klass = self
|
9
|
+
Blueprints::Plan.new(*args) do
|
10
|
+
klass.blueprint options
|
11
|
+
end
|
12
|
+
else
|
13
|
+
returning(self.new) do |object|
|
14
|
+
options.each {|attr, value| object.send("#{attr}=", value)}
|
15
|
+
object.save!
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/blueprints.rb
CHANGED
@@ -32,4 +32,10 @@ blueprint :cherry_basket => [:big_cherry, :cherry] do
|
|
32
32
|
[@cherry, @big_cherry]
|
33
33
|
end
|
34
34
|
|
35
|
-
blueprint :parent_not_existing => :not_existing
|
35
|
+
blueprint :parent_not_existing => :not_existing
|
36
|
+
|
37
|
+
Tree.blueprint :oak, :name => 'Oak', :size => 'large'
|
38
|
+
|
39
|
+
blueprint :pine do
|
40
|
+
@the_pine = Tree.blueprint :name => 'Pine', :size => 'medium'
|
41
|
+
end
|
data/spec/blueprints_spec.rb
CHANGED
@@ -125,21 +125,21 @@ describe Blueprints do
|
|
125
125
|
|
126
126
|
describe 'delete policies' do
|
127
127
|
before do
|
128
|
-
Blueprints::Plan.plans.
|
129
|
-
Blueprints.
|
130
|
-
Blueprints::Plan.
|
128
|
+
Blueprints::Plan.plans.expects(:empty?).returns(true)
|
129
|
+
Blueprints.expects(:load_scenarios_files).with(Blueprints::PLAN_FILES)
|
130
|
+
Blueprints::Plan.expects(:prebuild).with(nil)
|
131
131
|
end
|
132
132
|
|
133
133
|
it "should allow using custom delete policy" do
|
134
|
-
ActiveRecord::Base.connection.
|
135
|
-
ActiveRecord::Base.connection.
|
134
|
+
ActiveRecord::Base.connection.expects(:delete).with("TRUNCATE fruits")
|
135
|
+
ActiveRecord::Base.connection.expects(:delete).with("TRUNCATE trees")
|
136
136
|
|
137
137
|
Blueprints.load(:delete_policy => :truncate)
|
138
138
|
end
|
139
139
|
|
140
140
|
it "should default to :delete policy if unexisting policy given" do
|
141
|
-
ActiveRecord::Base.connection.
|
142
|
-
ActiveRecord::Base.connection.
|
141
|
+
ActiveRecord::Base.connection.expects(:delete).with("DELETE FROM fruits")
|
142
|
+
ActiveRecord::Base.connection.expects(:delete).with("DELETE FROM trees")
|
143
143
|
|
144
144
|
Blueprints.load(:delete_policy => :ukndown)
|
145
145
|
end
|
@@ -203,6 +203,22 @@ describe Blueprints do
|
|
203
203
|
end
|
204
204
|
end
|
205
205
|
|
206
|
+
describe 'with active record blueprints extensions' do
|
207
|
+
it "should build oak correctly" do
|
208
|
+
build :oak
|
209
|
+
@oak.should_not be_nil
|
210
|
+
@oak.name.should == 'Oak'
|
211
|
+
@oak.size.should == 'large'
|
212
|
+
end
|
213
|
+
|
214
|
+
it "should build pine correctly" do
|
215
|
+
build :pine
|
216
|
+
@the_pine.should_not be_nil
|
217
|
+
@the_pine.name.should == 'Pine'
|
218
|
+
@the_pine.size.should == 'medium'
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
206
222
|
#describe "with pitted namespace" do
|
207
223
|
# before do
|
208
224
|
# Hornsby.build('pitted:peach').copy_ivars(self)
|
data/spec/db/schema.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
data/test/blueprints_test.rb
CHANGED
@@ -123,6 +123,28 @@ class BlueprintsTest < ActiveSupport::TestCase
|
|
123
123
|
end
|
124
124
|
end
|
125
125
|
|
126
|
+
context 'delete policies' do
|
127
|
+
setup do
|
128
|
+
Blueprints::Plan.plans.expects(:empty?).returns(true)
|
129
|
+
Blueprints.expects(:load_scenarios_files).with(Blueprints::PLAN_FILES)
|
130
|
+
Blueprints::Plan.expects(:prebuild).with(nil)
|
131
|
+
end
|
132
|
+
|
133
|
+
should "allow using custom delete policy" do
|
134
|
+
ActiveRecord::Base.connection.expects(:delete).with("TRUNCATE fruits")
|
135
|
+
ActiveRecord::Base.connection.expects(:delete).with("TRUNCATE trees")
|
136
|
+
|
137
|
+
Blueprints.load(:delete_policy => :truncate)
|
138
|
+
end
|
139
|
+
|
140
|
+
should "default to :delete policy if unexisting policy given" do
|
141
|
+
ActiveRecord::Base.connection.expects(:delete).with("DELETE FROM fruits")
|
142
|
+
ActiveRecord::Base.connection.expects(:delete).with("DELETE FROM trees")
|
143
|
+
|
144
|
+
Blueprints.load(:delete_policy => :ukndown)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
126
148
|
context 'with many apples scenario' do
|
127
149
|
setup do
|
128
150
|
build :many_apples, :cherry, :cherry_basket
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: blueprints
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrius Chamentauskas
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-10-01 00:00:00 +03:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -36,6 +36,7 @@ files:
|
|
36
36
|
- lib/blueprints/file_context.rb
|
37
37
|
- lib/blueprints/helper.rb
|
38
38
|
- lib/blueprints/plan.rb
|
39
|
+
- lib/blueprints/ar_extensions.rb
|
39
40
|
- lib/blueprints/rspec_extensions.rb
|
40
41
|
- lib/blueprints/test_unit_extensions.rb
|
41
42
|
- README.rdoc
|