blueprints 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
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
- Plans look like:
7
+ Blueprints look like this:
8
8
 
9
- plan :apple do
9
+ blueprint :apple do
10
10
  Fruit.create! :species => 'apple'
11
11
  end
12
12
 
13
- plan :orange do
13
+ blueprint :orange do
14
14
  Fruit.create! :species => 'orange'
15
15
  end
16
16
 
17
- plan :fruitbowl => [:apple, :orange] do
17
+ blueprint :fruitbowl => [:apple, :orange] do
18
18
  FruitBowl.create! :fruit => [@apple,@orange]
19
19
  end
20
20
 
21
- plan :kitchen => :fruitbowl do
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, "@apple" do
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 'plan' block is evaluated and assigned to instance variable with the same name. You can also assign your own instance variables
48
- inside 'plan' block and they will be accessible in tests that build this plan.
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
- All scenarios are run only once, no matter how many times they were called, meaning that you don't need to worry about
51
- duplicating data.
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
- There's also a possibility to delete preloaded data with demolish. When called without arguments it will drop all data.
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
- demolish :fruits, :trees # Deletes trees and fruits tables
59
- demolish # Deletes all data except tables that are defined by Hornsby.skip_tables
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 scenario files in this particular order in Rails (Merb) root:
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 'sinsiliux-blueprints', :lib => 'blueprints', :source => 'http://gems.github.com'
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://gems.github.com
87
- sudo gem install sinsiliux-blueprints
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
- Hornsby scenarios is activated by calling enable_blueprints. For specifics on how to call that in your testing framework see a little lower.
94
- enable_blueprintssupports these parameters:
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
@@ -125,21 +125,21 @@ describe Blueprints do
125
125
 
126
126
  describe 'delete policies' do
127
127
  before do
128
- Blueprints::Plan.plans.should_receive(:empty?).and_return(true)
129
- Blueprints.should_receive(:load_scenarios_files).with(Blueprints::PLAN_FILES)
130
- Blueprints::Plan.should_receive(:prebuild).with(nil)
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.should_receive(:delete).with("TRUNCATE fruits")
135
- ActiveRecord::Base.connection.should_receive(:delete).with("TRUNCATE trees")
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.should_receive(:delete).with("DELETE FROM fruits")
142
- ActiveRecord::Base.connection.should_receive(:delete).with("DELETE FROM trees")
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
@@ -6,5 +6,6 @@ ActiveRecord::Schema.define(:version => 0) do
6
6
 
7
7
  create_table :trees, :force => true do |t|
8
8
  t.string :name
9
+ t.string :size
9
10
  end
10
11
  end
data/spec/spec_helper.rb CHANGED
@@ -19,5 +19,6 @@ require 'spec/db/fruit'
19
19
  require 'spec/db/tree'
20
20
 
21
21
  Spec::Runner.configure do |config|
22
+ config.mock_with :mocha
22
23
  config.enable_blueprints :root => File.expand_path(File.join(File.dirname(__FILE__), '..')), :prebuild => :big_cherry
23
24
  end
@@ -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
@@ -3,6 +3,7 @@ require 'activerecord'
3
3
  require 'test/unit'
4
4
  require 'active_record/test_case'
5
5
  require 'shoulda'
6
+ require 'mocha'
6
7
  begin
7
8
  require 'mysqlplus'
8
9
  rescue LoadError
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.1.1
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-09-27 00:00:00 +03:00
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