blueprints 0.8.2 → 0.9.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/.gitignore +4 -1
- data/Gemfile +2 -0
- data/Gemfile.lock +82 -18
- data/LICENSE +1 -1
- data/README.rdoc +38 -8
- data/Rakefile +11 -35
- data/blueprints.gemspec +29 -123
- data/features/support/env.rb +7 -10
- data/lib/blueprints.rb +68 -12
- data/lib/blueprints/blueprint.rb +39 -19
- data/lib/blueprints/buildable.rb +92 -74
- data/lib/blueprints/configuration.rb +18 -4
- data/lib/blueprints/context.rb +148 -5
- data/lib/blueprints/database_cleaner_fix.rb +9 -0
- data/lib/blueprints/dependency.rb +32 -21
- data/lib/blueprints/eval_context.rb +51 -0
- data/lib/blueprints/extensions.rb +115 -0
- data/lib/blueprints/extensions/rspec.rb +3 -1
- data/lib/blueprints/helper.rb +52 -20
- data/lib/blueprints/namespace.rb +31 -12
- data/lib/blueprints/root_namespace.rb +24 -25
- data/lib/blueprints/version.rb +3 -0
- data/spec/{active_record/blueprint.rb → blueprint.rb} +14 -17
- data/spec/{active_record/blueprints_spec.rb → blueprints_spec.rb} +40 -59
- data/spec/spec_helper.rb +34 -0
- data/spec/support/active_record/database.yml.example +7 -0
- data/spec/support/active_record/initializer.rb +15 -0
- data/spec/{active_record/fixtures → support/active_record}/schema.rb +0 -0
- data/spec/support/dm-core/initializer.rb +31 -0
- data/spec/support/mongo_mapper/database.yml.example +2 -0
- data/spec/support/mongo_mapper/initializer.rb +20 -0
- data/spec/support/mongoid/database.yml.example +2 -0
- data/spec/support/mongoid/initializer.rb +23 -0
- data/spec/support/none/initializer.rb +63 -0
- data/spec/unit/active_record_spec.rb +1 -6
- data/spec/unit/blueprint_spec.rb +91 -20
- data/spec/unit/blueprints_spec.rb +44 -0
- data/spec/unit/buildable_spec.rb +37 -6
- data/spec/unit/configuration_spec.rb +11 -0
- data/spec/unit/context_spec.rb +100 -0
- data/spec/unit/dependency_spec.rb +24 -19
- data/spec/unit/eval_context_spec.rb +56 -0
- data/spec/unit/fixtures.rb +61 -0
- data/spec/unit/namespace_spec.rb +59 -11
- data/spec/unit/spec_helper.rb +8 -16
- data/test/blueprints_test.rb +40 -59
- data/test/test_helper.rb +6 -16
- data/test_all.sh +45 -0
- metadata +178 -61
- data/VERSION +0 -1
- data/lib/blueprints/core_ext.rb +0 -69
- data/lib/blueprints/file_context.rb +0 -37
- data/spec/active_record/fixtures/database.yml.example +0 -8
- data/spec/active_record/fixtures/fruit.rb +0 -3
- data/spec/active_record/fixtures/tree.rb +0 -4
- data/spec/active_record/spec_helper.rb +0 -37
- data/spec/no_db/blueprint.rb +0 -9
- data/spec/no_db/blueprints_spec.rb +0 -45
- data/spec/no_db/fixtures/fruit.rb +0 -15
- data/spec/no_db/spec_helper.rb +0 -14
- data/spec/test_all.sh +0 -39
@@ -36,4 +36,15 @@ describe Blueprints::Configuration do
|
|
36
36
|
@config.filename = "my_file.rb"
|
37
37
|
@config.filename.should == [Pathname.new("my_file.rb")]
|
38
38
|
end
|
39
|
+
|
40
|
+
describe "default attributes" do
|
41
|
+
it "should be name by default" do
|
42
|
+
@config.default_attributes.should == [:name]
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should automatically convert them to array" do
|
46
|
+
@config.default_attributes = :id
|
47
|
+
@config.default_attributes.should == [:id]
|
48
|
+
end
|
49
|
+
end
|
39
50
|
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Blueprints::Context do
|
4
|
+
subject do
|
5
|
+
context_with_attrs_and_deps
|
6
|
+
end
|
7
|
+
|
8
|
+
let :child_context do
|
9
|
+
Blueprints::Context.new(:parent => subject, :attributes => {:attr3 => 30, :attr2 => 20}, :dependencies => [:dep3, 'dep2'])
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "initializing" do
|
13
|
+
it "should have attributes and and dependencies merged with parent" do
|
14
|
+
child_context.attributes.should == {:attr1 => 1, :attr2 => 20, :attr3 => 30}
|
15
|
+
child_context.dependencies.should == [:dep1, :dep2, :dep3]
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should return own or parent file" do
|
19
|
+
subject.file.should == file
|
20
|
+
child_context.file.should == file
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should return own or parent namespace" do
|
24
|
+
subject.namespace.should == namespace
|
25
|
+
child_context.namespace.should == namespace
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "with context" do
|
30
|
+
it "should allow eval within context" do
|
31
|
+
File.expects(:read).with('my_file1').returns('method(1)')
|
32
|
+
Blueprints::Context.any_instance.expects(:method).with(1)
|
33
|
+
Blueprints::Context.eval_within_context(:file => 'my_file1')
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should return context" do
|
37
|
+
Blueprints::Context.eval_within_context(:dependencies => [:within_dep]).dependencies.should == [:within_dep]
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should instance eval with current context set" do
|
41
|
+
Blueprints::Context.any_instance.expects(:method).with(1)
|
42
|
+
Blueprints::Context.send(:class_variable_get, :@@chain) << context
|
43
|
+
|
44
|
+
Blueprints::Context.eval_within_context(:dependencies => [:within_dep]) do
|
45
|
+
method(1)
|
46
|
+
Blueprints::Context.current.dependencies.should == [:within_dep]
|
47
|
+
end
|
48
|
+
Blueprints::Context.current.should == context
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "child contexts" do
|
53
|
+
it "should yield and return child context with attributes" do
|
54
|
+
yielded_context = nil
|
55
|
+
subject.attributes(:attr3 => 3) { yielded_context = self }.should == yielded_context
|
56
|
+
yielded_context.should be_instance_of(Blueprints::Context)
|
57
|
+
yielded_context.attributes.should == {:attr1 => 1, :attr2 => 2, :attr3 => 3}
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should yield and return child context with dependencies" do
|
61
|
+
yielded_context = nil
|
62
|
+
subject.depends_on('the_dep') { yielded_context = self }.should == yielded_context
|
63
|
+
yielded_context.should be_instance_of(Blueprints::Context)
|
64
|
+
yielded_context.dependencies.should == [:dep1, :dep2, :the_dep]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "blueprints and namespaces" do
|
69
|
+
it "should allow creating blueprint" do
|
70
|
+
blueprint = subject.blueprint :blueprint
|
71
|
+
blueprint.should be_instance_of(Blueprints::Blueprint)
|
72
|
+
blueprint.name.should == :blueprint
|
73
|
+
blueprint.instance_variable_get(:@context).should == subject
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should allow creating namespace" do
|
77
|
+
namespace = subject.namespace :namespace
|
78
|
+
namespace.should be_instance_of(Blueprints::Namespace)
|
79
|
+
namespace.name.should == :namespace
|
80
|
+
namespace.instance_variable_get(:@context).should == subject
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should allow creating blueprint inside namespace" do
|
84
|
+
bp = nil
|
85
|
+
namespace = subject.namespace(:namespace) { bp = self.blueprint :blueprint }
|
86
|
+
bp.namespace.should == namespace
|
87
|
+
namespace.children.should == {:blueprint => bp}
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should allow creating blueprint with inferred name" do
|
91
|
+
blueprint = subject.attributes(:name => 'bp').blueprint
|
92
|
+
blueprint.name.should == :bp
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should allow setting dependency" do
|
96
|
+
dep = context.d(:bp, :option => 'val')
|
97
|
+
dep.should be_instance_of(Blueprints::Dependency)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -2,48 +2,53 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe Blueprints::Dependency do
|
4
4
|
before do
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
stage.instance_variable_set(:@value, :value)
|
6
|
+
options_blueprint
|
7
|
+
end
|
8
|
+
|
9
|
+
let :stage do
|
10
|
+
Blueprints::Namespace.root.eval_context
|
11
|
+
end
|
8
12
|
|
9
|
-
|
10
|
-
|
11
|
-
@blueprint = Blueprints::Blueprint.new(:blueprint, __FILE__) do
|
12
|
-
@value = value
|
13
|
-
options.present? ? options : mock
|
14
|
-
end
|
13
|
+
def value(dep)
|
14
|
+
stage.instance_eval(context, {}, &dep)
|
15
15
|
end
|
16
16
|
|
17
17
|
it "should allow getting instance variable value" do
|
18
|
-
Blueprints::Dependency.new(:
|
18
|
+
value(Blueprints::Dependency.new(:options_blueprint)).should == mock1
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should allow getting another instance variable" do
|
22
|
-
Blueprints::Dependency.new(:
|
22
|
+
value(Blueprints::Dependency.new(:options_blueprint, :value)).should == mock2
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should replace . in instance variable name with _" do
|
26
|
+
namespace_blueprint
|
27
|
+
value(Blueprints::Dependency.new(:'namespace.blueprint')).should == mock1
|
23
28
|
end
|
24
29
|
|
25
30
|
it "should allow passing options for building" do
|
26
|
-
Blueprints::Dependency.new(:
|
31
|
+
value(Blueprints::Dependency.new(:options_blueprint, :option => 'value')).should == {:option => 'value'}
|
27
32
|
end
|
28
33
|
|
29
34
|
it "should record all missing methods" do
|
30
|
-
dependency = Blueprints::Dependency.new(:
|
35
|
+
dependency = Blueprints::Dependency.new(:options_blueprint)
|
31
36
|
dependency.method1.method2(1).method3 {|val| val.method4 }
|
32
37
|
|
33
|
-
|
34
|
-
|
35
|
-
|
38
|
+
mock1.expects(:method1).with().returns(mock2 = mock)
|
39
|
+
mock2.expects(:method2).with(1).returns(mock3 = mock)
|
40
|
+
mock3.expects(:method3).with().yields(mock(:method4 => true)).returns(result = mock)
|
36
41
|
|
37
|
-
dependency.
|
42
|
+
value(dependency).should == result
|
38
43
|
end
|
39
44
|
|
40
45
|
it "should record to_s, id and other standard methods" do
|
41
|
-
dependency = Blueprints::Dependency.new(:
|
46
|
+
dependency = Blueprints::Dependency.new(:options_blueprint)
|
42
47
|
dependency.id.to_s
|
43
48
|
|
44
49
|
@mock.expects(:id).returns(mock1 = mock)
|
45
50
|
mock1.expects(:to_s).returns(result = mock)
|
46
51
|
|
47
|
-
dependency.
|
52
|
+
value(dependency).should == result
|
48
53
|
end
|
49
54
|
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Blueprints::EvalContext do
|
4
|
+
subject do
|
5
|
+
Blueprints::EvalContext.new
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should contain no instance variables" do
|
9
|
+
subject.instance_variables.should == []
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "instance variables" do
|
13
|
+
it "should allow copying variables to other context" do
|
14
|
+
subject.instance_variable_set(:@var, :value)
|
15
|
+
subject.copy_instance_variables(context = Object.new)
|
16
|
+
context.instance_variables.collect(&:to_sym).should == [:@var]
|
17
|
+
context.instance_variable_get(:@var).should == :value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "instance eval" do
|
22
|
+
subject do
|
23
|
+
Blueprints::EvalContext.new
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should allow to access to options" do
|
27
|
+
subject.instance_eval(context, :option => 'value') do
|
28
|
+
options.should == {:option => 'value'}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should allow to access to attributes" do
|
33
|
+
subject.instance_eval(context_with_attrs_and_deps, :option => 'value') do
|
34
|
+
attributes.should == {:option => 'value', :attr1 => 1, :attr2 => 2}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should normalize options and attributes" do
|
39
|
+
blueprint.build(subject)
|
40
|
+
subject.instance_variable_set(:@value, 2)
|
41
|
+
context = Blueprints::Context.new(:attributes => {:attr => Blueprints::Dependency.new(:blueprint)})
|
42
|
+
|
43
|
+
subject.instance_eval(context, :attr2 => lambda { @value + 2 }, :attr3 => :value) do
|
44
|
+
options.should == {:attr2 => 4, :attr3 => :value}
|
45
|
+
attributes.should == {:attr => @blueprint, :attr2 => 4, :attr3 => :value}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "build" do
|
51
|
+
it "should allow building blueprint" do
|
52
|
+
blueprint
|
53
|
+
subject.build(:blueprint).should == mock1
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
def file
|
2
|
+
__FILE__
|
3
|
+
end
|
4
|
+
|
5
|
+
def mock1
|
6
|
+
@mock ||= Mocha::Mockery.instance.unnamed_mock
|
7
|
+
end
|
8
|
+
|
9
|
+
def mock2
|
10
|
+
@mock2 ||= Mocha::Mockery.instance.unnamed_mock
|
11
|
+
end
|
12
|
+
|
13
|
+
def stage
|
14
|
+
@stage ||= Blueprints::EvalContext.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def context
|
18
|
+
@context ||= Blueprints::Context.new(:file => file, :namespace => Blueprints::Namespace.root)
|
19
|
+
end
|
20
|
+
|
21
|
+
def context2
|
22
|
+
@context2 ||= Blueprints::Context.new(:parent => context, :namespace => namespace)
|
23
|
+
end
|
24
|
+
|
25
|
+
def context_with_attrs_and_deps
|
26
|
+
@context_with_attrs_and_deps ||= Blueprints::Context.new(:attributes => {:attr1 => 1, :attr2 => 2}, :dependencies => [:dep1, :dep2], :file => file, :namespace => namespace)
|
27
|
+
end
|
28
|
+
|
29
|
+
def blueprint(&block)
|
30
|
+
result = mock1
|
31
|
+
block ||= proc { result }
|
32
|
+
@blueprint ||= context.blueprint(:blueprint, &block)
|
33
|
+
end
|
34
|
+
|
35
|
+
def blueprint2(&block)
|
36
|
+
result = mock1
|
37
|
+
block ||= proc { result }
|
38
|
+
@blueprint2 ||= context.blueprint(:blueprint2, &block)
|
39
|
+
end
|
40
|
+
|
41
|
+
def options_blueprint
|
42
|
+
result, value = mock1, mock2
|
43
|
+
@options_blueprint ||= Blueprints::Blueprint.new(:options_blueprint, context) do
|
44
|
+
@value = value
|
45
|
+
options.present? ? options : result
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def namespace
|
50
|
+
@namespace ||= context.namespace(:namespace)
|
51
|
+
end
|
52
|
+
|
53
|
+
def namespace_blueprint
|
54
|
+
result = mock1
|
55
|
+
@namespace_blueprint ||= context2.blueprint(:blueprint) { result }
|
56
|
+
end
|
57
|
+
|
58
|
+
def namespace_blueprint2
|
59
|
+
result = mock2
|
60
|
+
@namespace_blueprint2 ||= context2.blueprint(:blueprint2) { result }
|
61
|
+
end
|
data/spec/unit/namespace_spec.rb
CHANGED
@@ -1,23 +1,71 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe Blueprints::Namespace do
|
4
|
-
|
5
|
-
|
4
|
+
describe "children" do
|
5
|
+
it "should allow adding children to namespace" do
|
6
|
+
namespace.add_child(blueprint)
|
7
|
+
namespace[:blueprint].should == blueprint
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should warn when adding children overwrites existing one" do
|
11
|
+
namespace_blueprint
|
12
|
+
Blueprints.expects(:warn).with("Overwriting existing blueprint", blueprint)
|
13
|
+
namespace.add_child(blueprint)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should allow recursive finding of children" do
|
17
|
+
namespace_blueprint
|
18
|
+
Blueprints::Namespace.root['namespace.blueprint'].should == namespace_blueprint
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should raise error if blueprint can't be found" do
|
22
|
+
namespace_blueprint
|
23
|
+
expect {
|
24
|
+
Blueprints::Namespace.root['namespace.blueprint2']
|
25
|
+
}.to raise_error(Blueprints::BlueprintNotFoundError)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "children context" do
|
30
|
+
before do
|
31
|
+
namespace_blueprint
|
32
|
+
end
|
6
33
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
34
|
+
it "should update attributes and dependencies of children when updating those of namespace" do
|
35
|
+
namespace.attributes(:parent => 1).depends_on(:parent_depends)
|
36
|
+
namespace_blueprint.attributes[:parent].should == 1
|
37
|
+
namespace_blueprint.dependencies.should include(:parent_depends)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "build" do
|
42
|
+
before do
|
43
|
+
blueprint
|
44
|
+
namespace_blueprint
|
45
|
+
namespace_blueprint2
|
46
|
+
end
|
12
47
|
|
13
|
-
|
14
|
-
|
48
|
+
it "should set result to results of all blueprints in namespace" do
|
49
|
+
result = namespace.build(stage)
|
50
|
+
result.should =~ [mock1, mock2]
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should pass build once and eval context params" do
|
54
|
+
namespace_blueprint.expects(:build).with(instance_of(Blueprints::EvalContext), false, :option => 'value')
|
55
|
+
namespace_blueprint2.expects(:build).with(instance_of(Blueprints::EvalContext), false, :option => 'value')
|
56
|
+
namespace.build(stage, false, :option => 'value')
|
57
|
+
end
|
15
58
|
end
|
16
59
|
|
17
60
|
describe "demolish" do
|
18
61
|
it "should allow to demolish namespace" do
|
19
|
-
|
20
|
-
|
62
|
+
blueprint
|
63
|
+
namespace_blueprint
|
64
|
+
namespace_blueprint2
|
65
|
+
results = namespace.build stage
|
66
|
+
results.each { |result| result.expects(:destroy) }
|
67
|
+
|
68
|
+
@namespace.demolish(stage)
|
21
69
|
end
|
22
70
|
end
|
23
71
|
end
|
data/spec/unit/spec_helper.rb
CHANGED
@@ -1,27 +1,19 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
Root = Pathname.new(__FILE__).dirname.join('..', '..')
|
2
|
+
$: << Root.join('lib').to_s
|
3
3
|
|
4
|
-
|
4
|
+
require 'rspec'
|
5
|
+
require 'blueprints'
|
6
|
+
require File.dirname(__FILE__) + '/fixtures'
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
#databases = YAML::load(IO.read("spec/active_record/fixtures/database.yml"))
|
9
|
-
#db_info = databases[ENV["DB"] || "test"]
|
10
|
-
#ActiveRecord::Base.establish_connection(db_info)
|
11
|
-
|
12
|
-
require 'spec'
|
13
|
-
require 'lib/blueprints'
|
14
|
-
|
15
|
-
Spec::Runner.configure do |config|
|
8
|
+
RSpec.configure do |config|
|
16
9
|
config.mock_with :mocha
|
17
10
|
|
18
11
|
config.before do
|
19
|
-
Blueprints::Namespace.root.
|
20
|
-
@mock = Mocha::Mockery.instance.unnamed_mock
|
12
|
+
Blueprints::Namespace.root.eval_context = Blueprints::EvalContext.new
|
21
13
|
end
|
22
14
|
|
23
15
|
config.after do
|
24
|
-
Blueprints::Namespace.root.children.clear
|
16
|
+
Blueprints::Namespace.root.instance_variable_get(:@children).clear
|
25
17
|
Blueprints::Namespace.root.executed_blueprints.clear
|
26
18
|
end
|
27
19
|
end
|
data/test/blueprints_test.rb
CHANGED
@@ -67,16 +67,30 @@ class BlueprintsTest < ActiveSupport::TestCase
|
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
|
+
context "build per describe" do
|
71
|
+
unless File.dirname(__FILE__) =~ /test$/
|
72
|
+
build_blueprint :apple
|
73
|
+
|
74
|
+
should "have cherry" do
|
75
|
+
assert(!(@apple.nil?))
|
76
|
+
end
|
77
|
+
|
78
|
+
should "have correct cherry species" do
|
79
|
+
assert(@apple.species == 'apple')
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
70
84
|
context 'with preloaded cherry scenario' do
|
71
85
|
should "have correct size after changed by second test" do
|
72
86
|
assert(@cherry.average_diameter == 3)
|
73
|
-
@cherry.
|
87
|
+
@cherry.blueprint(:average_diameter => 1)
|
74
88
|
assert(@cherry.average_diameter == 1)
|
75
89
|
end
|
76
90
|
|
77
91
|
should "have correct size" do
|
78
92
|
assert(@cherry.average_diameter == 3)
|
79
|
-
@cherry.
|
93
|
+
@cherry.blueprint(:average_diameter => 5)
|
80
94
|
assert(@cherry.average_diameter == 5)
|
81
95
|
end
|
82
96
|
|
@@ -94,7 +108,7 @@ class BlueprintsTest < ActiveSupport::TestCase
|
|
94
108
|
demolish :apple
|
95
109
|
assert(!Fruit.all.include?(@apple))
|
96
110
|
build :apple
|
97
|
-
assert(Fruit.
|
111
|
+
assert(Fruit.all.include?(@apple))
|
98
112
|
end
|
99
113
|
|
100
114
|
should "overwrite auto created instance variable with another auto created one" do
|
@@ -120,11 +134,11 @@ class BlueprintsTest < ActiveSupport::TestCase
|
|
120
134
|
end
|
121
135
|
|
122
136
|
should "create only one apple" do
|
123
|
-
assert(Fruit.all(:conditions =>
|
137
|
+
assert(Fruit.all(:conditions => {:species => "apple"}).size == 1)
|
124
138
|
end
|
125
139
|
|
126
140
|
should "create only two cherries even if they were preloaded" do
|
127
|
-
assert(Fruit.all(:conditions =>
|
141
|
+
assert(Fruit.all(:conditions => {:species => "cherry"}).size == 2)
|
128
142
|
end
|
129
143
|
|
130
144
|
should "contain cherries in basket if basket is loaded in test and cherries preloaded" do
|
@@ -132,26 +146,6 @@ class BlueprintsTest < ActiveSupport::TestCase
|
|
132
146
|
end
|
133
147
|
end
|
134
148
|
|
135
|
-
context 'transactions' do
|
136
|
-
setup do
|
137
|
-
build :apple
|
138
|
-
end
|
139
|
-
|
140
|
-
should "drop only inner transaction" do
|
141
|
-
assert(!(@apple.reload.nil?))
|
142
|
-
begin
|
143
|
-
ActiveRecord::Base.transaction do
|
144
|
-
f = Fruit.create(:species => 'orange')
|
145
|
-
assert(!(f.reload.nil?))
|
146
|
-
raise 'some error'
|
147
|
-
end
|
148
|
-
rescue
|
149
|
-
end
|
150
|
-
assert(!(@apple.reload.nil?))
|
151
|
-
assert(Fruit.find_by_species('orange').nil?)
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
149
|
context 'errors' do
|
156
150
|
should 'raise ScenarioNotFoundError when scenario could not be found' do
|
157
151
|
assert_raise(Blueprints::BlueprintNotFoundError) do
|
@@ -164,12 +158,6 @@ class BlueprintsTest < ActiveSupport::TestCase
|
|
164
158
|
build :parent_not_existing
|
165
159
|
end
|
166
160
|
end
|
167
|
-
|
168
|
-
should 'raise TypeError when scenario name is not symbol or string' do
|
169
|
-
assert_raise(TypeError, "Pass blueprint names as strings or symbols only, cannot define blueprint 1") do
|
170
|
-
Blueprints::Blueprint.new(1, __FILE__)
|
171
|
-
end
|
172
|
-
end
|
173
161
|
end
|
174
162
|
|
175
163
|
context 'with active record blueprints extensions' do
|
@@ -202,7 +190,7 @@ class BlueprintsTest < ActiveSupport::TestCase
|
|
202
190
|
|
203
191
|
should "normalize attributes when updating with blueprint method" do
|
204
192
|
build :cherry, :oak
|
205
|
-
|
193
|
+
build :cherry => {:tree => d(:oak)}
|
206
194
|
assert(@cherry.tree == @oak)
|
207
195
|
end
|
208
196
|
|
@@ -213,8 +201,7 @@ class BlueprintsTest < ActiveSupport::TestCase
|
|
213
201
|
end
|
214
202
|
|
215
203
|
should "allow to pass array of hashes to blueprint method" do
|
216
|
-
Fruit.
|
217
|
-
fruits = Fruit.blueprint([{:species => 'fruit1'}, {:species => 'fruit2'}])
|
204
|
+
fruits = Fruit.blueprint({:species => 'fruit1'}, {:species => 'fruit2'})
|
218
205
|
assert(fruits.collect(&:species) == %w{fruit1 fruit2})
|
219
206
|
end
|
220
207
|
|
@@ -331,23 +318,27 @@ class BlueprintsTest < ActiveSupport::TestCase
|
|
331
318
|
end
|
332
319
|
end
|
333
320
|
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
321
|
+
context "build!" do
|
322
|
+
should "allow to building same blueprint again" do
|
323
|
+
build! :big_cherry, :big_cherry => {:species => 'not so big cherry'}
|
324
|
+
assert(Fruit.count == 4)
|
325
|
+
assert(!(Fruit.first(:conditions => {:species => 'not so big cherry'}).nil?))
|
326
|
+
end
|
339
327
|
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
328
|
+
should "allow building same blueprint n times" do
|
329
|
+
oaks = build! 5, :oak
|
330
|
+
assert(oaks.size == 5)
|
331
|
+
oaks.each do |oak|
|
332
|
+
assert(oak.instance_of?(Tree))
|
333
|
+
assert(oak.name == 'Oak')
|
334
|
+
end
|
335
|
+
end
|
345
336
|
end
|
346
337
|
|
347
338
|
context 'attributes' do
|
348
339
|
should "allow to extract attributes from blueprint" do
|
349
|
-
assert(build_attributes('attributes.cherry') == {:species => 'cherry'})
|
350
|
-
assert(build_attributes('attributes.shortened_cherry') == {:species => 'cherry'})
|
340
|
+
assert(build_attributes('attributes.cherry') == {:species => 'cherry', :average_diameter => 10})
|
341
|
+
assert(build_attributes('attributes.shortened_cherry') == {:species => 'cherry', :average_diameter => 10})
|
351
342
|
assert(build_attributes(:big_cherry) == {})
|
352
343
|
end
|
353
344
|
|
@@ -367,13 +358,7 @@ class BlueprintsTest < ActiveSupport::TestCase
|
|
367
358
|
end
|
368
359
|
|
369
360
|
should "return build attributes for dependencies" do
|
370
|
-
attrs = build_attributes('attributes.
|
371
|
-
assert(!(@the_pine.nil?))
|
372
|
-
assert(attrs[:tree] == @the_pine)
|
373
|
-
end
|
374
|
-
|
375
|
-
should "return build attributes for :@var" do
|
376
|
-
attrs = build_attributes('attributes.dependent_cherry2')
|
361
|
+
attrs = build_attributes('attributes.dependent_cherry')
|
377
362
|
assert(!(@the_pine.nil?))
|
378
363
|
assert(attrs[:tree] == @the_pine)
|
379
364
|
end
|
@@ -383,11 +368,7 @@ class BlueprintsTest < ActiveSupport::TestCase
|
|
383
368
|
build :circular_reference
|
384
369
|
end
|
385
370
|
|
386
|
-
should "
|
387
|
-
|
388
|
-
build :error
|
389
|
-
rescue RuntimeError => e
|
390
|
-
assert(e.backtrace[0] == "spec/active_record/blueprint.rb:2:in blueprint 'error'")
|
391
|
-
end
|
371
|
+
should "allow inferring blueprint name" do
|
372
|
+
assert(build(:infered).name == 'infered')
|
392
373
|
end
|
393
374
|
end
|