cockroach 0.0.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/.rvmrc +1 -0
- data/Gemfile +28 -0
- data/LICENSE.txt +22 -0
- data/README.md +124 -0
- data/Rakefile +37 -0
- data/VERSION +1 -0
- data/cockroach.gemspec +96 -0
- data/lib/cockroach.rb +43 -0
- data/lib/cockroach/base/load_nodes.rb +38 -0
- data/lib/cockroach/base/node.rb +192 -0
- data/lib/cockroach/base/node_structure.rb +11 -0
- data/lib/cockroach/config.rb +54 -0
- data/lib/cockroach/config/loader.rb +35 -0
- data/lib/cockroach/fixtures/factory_girl.rb +4 -0
- data/lib/cockroach/fixtures/factory_girl/loader.rb +43 -0
- data/lib/cockroach/fixtures/factory_girl/node.rb +51 -0
- data/lib/cockroach/fixtures/factory_girl/profiler.rb +34 -0
- data/lib/cockroach/railtie.rb +7 -0
- data/lib/cockroach/source.rb +40 -0
- data/lib/cockroach/source/model.rb +33 -0
- data/lib/cockroach/source/node.rb +27 -0
- data/lib/cockroach/version.rb +3 -0
- data/lib/generators/cockroach/install_generator.rb +20 -0
- data/lib/generators/cockroach/templates/cockroach.rb +8 -0
- data/lib/generators/cockroach/templates/faker.yml +16 -0
- data/lib/tasks/faker.rake +35 -0
- data/test/config/config_test.rb +129 -0
- data/test/config/loader_test.rb +42 -0
- data/test/fixturers/factory_girl/aliasing_test.rb +151 -0
- data/test/fixturers/factory_girl/loading_test.rb +45 -0
- data/test/fixturers/factory_girl/node_test.rb +386 -0
- data/test/fixturers/factory_girl/profiler_test.rb +124 -0
- data/test/fixturers/source_test.rb +153 -0
- data/test/generators/install_generator_test.rb +14 -0
- data/test/support/active_record.rb +11 -0
- data/test/support/data/correct_with_option.yml +13 -0
- data/test/support/data/correct_without_option.yml +10 -0
- data/test/support/data/dummy_structure/config/faker.yml +13 -0
- data/test/support/data/dummy_structure/config/user_only.yml +1 -0
- data/test/support/data/dummy_structure/config/witness.yml +6 -0
- data/test/support/data/dummy_structure/test/factories/user_factory.rb +16 -0
- data/test/support/database_cleaner.rb +13 -0
- data/test/support/factory_girl_mocked.rb +32 -0
- data/test/support/models/user.rb +23 -0
- data/test/test_helper.rb +26 -0
- metadata +151 -0
@@ -0,0 +1,42 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
module Cockroach
|
4
|
+
class Config::LoaderTest < Test::Unit::TestCase
|
5
|
+
context "Parser" do
|
6
|
+
context "and Unexisting config" do
|
7
|
+
should "raise an error for empty string" do
|
8
|
+
assert_raise Cockroach::Config::ConfigNotExistsError do
|
9
|
+
Cockroach::Config::Loader.parse ""
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
should "raise an error for inexisting file" do
|
14
|
+
assert_raise Cockroach::Config::ConfigNotExistsError do
|
15
|
+
Cockroach::Config::Loader.parse "/no_file.yml"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "and Correct config" do
|
21
|
+
should "return array" do
|
22
|
+
config = Cockroach::Config::Loader.parse File.expand_path("../support/data/correct_without_option.yml", File.dirname(__FILE__))
|
23
|
+
|
24
|
+
assert config.is_a? Array
|
25
|
+
assert config.first.is_a? Hash
|
26
|
+
assert config.last.is_a? NilClass
|
27
|
+
assert_equal 2, config.size
|
28
|
+
end
|
29
|
+
|
30
|
+
should "return array with options" do
|
31
|
+
config = Cockroach::Config::Loader.parse File.expand_path("../support/data/correct_with_option.yml", File.dirname(__FILE__))
|
32
|
+
|
33
|
+
assert config.is_a? Array
|
34
|
+
assert config.first.is_a? Hash
|
35
|
+
assert config.last.is_a? Hash
|
36
|
+
assert_equal 2, config.size
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
@@ -0,0 +1,151 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "user"
|
3
|
+
|
4
|
+
module Cockroach
|
5
|
+
class FactoryGirlAliasingTest < Test::Unit::TestCase
|
6
|
+
setup do
|
7
|
+
before_setup
|
8
|
+
mock_factory_girl
|
9
|
+
end
|
10
|
+
|
11
|
+
teardown do
|
12
|
+
after_teardown
|
13
|
+
end
|
14
|
+
|
15
|
+
context "Complex aliase" do
|
16
|
+
context "Heritage" do
|
17
|
+
setup do
|
18
|
+
@users_node = Cockroach::FactoryGirl::Node.new('users' => {
|
19
|
+
'amount' => '5',
|
20
|
+
'as' => 'person',
|
21
|
+
'places' => {
|
22
|
+
'amount' => '10',
|
23
|
+
'owner_as' => 'person'
|
24
|
+
}})
|
25
|
+
end
|
26
|
+
|
27
|
+
should "send load! call to subnodes" do
|
28
|
+
mocks = ((1..5).to_a.collect {|i| stub('user', :id => i) })
|
29
|
+
|
30
|
+
::FactoryGirl.stubs("create").with("user").returns( *mocks )
|
31
|
+
|
32
|
+
subnode = @users_node.nodes['place']
|
33
|
+
subnode.expects(:load!).with({"person" => mocks[0]})
|
34
|
+
subnode.expects(:load!).with({"person" => mocks[1]})
|
35
|
+
subnode.expects(:load!).with({"person" => mocks[2]})
|
36
|
+
subnode.expects(:load!).with({"person" => mocks[3]})
|
37
|
+
subnode.expects(:load!).with({"person" => mocks[4]})
|
38
|
+
|
39
|
+
@users_node.__send__(:load!)
|
40
|
+
end
|
41
|
+
|
42
|
+
should "initiate child records exact times" do
|
43
|
+
mocks = ((1..5).to_a.collect {|i| stub('user', :id => i) })
|
44
|
+
place_mock = stub('place', :id => 0)
|
45
|
+
|
46
|
+
::FactoryGirl.stubs("create").with("user").returns( *mocks )
|
47
|
+
|
48
|
+
::FactoryGirl.expects(:create).with("place", {"owner" => mocks[0]}).times(10).returns(place_mock)
|
49
|
+
::FactoryGirl.expects(:create).with("place", {"owner" => mocks[1]}).times(10).returns(place_mock)
|
50
|
+
::FactoryGirl.expects(:create).with("place", {"owner" => mocks[2]}).times(10).returns(place_mock)
|
51
|
+
::FactoryGirl.expects(:create).with("place", {"owner" => mocks[3]}).times(10).returns(place_mock)
|
52
|
+
::FactoryGirl.expects(:create).with("place", {"owner" => mocks[4]}).times(10).returns(place_mock)
|
53
|
+
|
54
|
+
@users_node.nodes.each_value {|node| node.stubs(:allowed_options).returns(['owner']) }
|
55
|
+
|
56
|
+
@users_node.__send__(:load!)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "Simple alias" do
|
62
|
+
setup do
|
63
|
+
@users_node = Cockroach::FactoryGirl::Node.new('users' => {
|
64
|
+
'amount' => '5',
|
65
|
+
'places' => {
|
66
|
+
'amount' => '10',
|
67
|
+
'owner_as' => 'user'
|
68
|
+
}})
|
69
|
+
end
|
70
|
+
|
71
|
+
context "Heritage" do
|
72
|
+
should "send load! call to subnodes" do
|
73
|
+
mocks = ((1..5).to_a.collect {|i| stub('user', :id => i) })
|
74
|
+
|
75
|
+
::FactoryGirl.stubs("create").with("user").returns( *mocks )
|
76
|
+
|
77
|
+
subnode = @users_node.nodes['place']
|
78
|
+
subnode.expects(:load!).with({"user" => mocks[0]})
|
79
|
+
subnode.expects(:load!).with({"user" => mocks[1]})
|
80
|
+
subnode.expects(:load!).with({"user" => mocks[2]})
|
81
|
+
subnode.expects(:load!).with({"user" => mocks[3]})
|
82
|
+
subnode.expects(:load!).with({"user" => mocks[4]})
|
83
|
+
|
84
|
+
@users_node.__send__(:load!)
|
85
|
+
end
|
86
|
+
|
87
|
+
should "initiate child records exact times" do
|
88
|
+
mocks = ((1..5).to_a.collect {|i| stub('user', :id => i) })
|
89
|
+
place_mock = stub('place', :id => 0)
|
90
|
+
|
91
|
+
::FactoryGirl.stubs("create").with("user").returns( *mocks )
|
92
|
+
|
93
|
+
::FactoryGirl.expects(:create).with("place", {"owner" => mocks[0]}).times(10).returns(place_mock)
|
94
|
+
::FactoryGirl.expects(:create).with("place", {"owner" => mocks[1]}).times(10).returns(place_mock)
|
95
|
+
::FactoryGirl.expects(:create).with("place", {"owner" => mocks[2]}).times(10).returns(place_mock)
|
96
|
+
::FactoryGirl.expects(:create).with("place", {"owner" => mocks[3]}).times(10).returns(place_mock)
|
97
|
+
::FactoryGirl.expects(:create).with("place", {"owner" => mocks[4]}).times(10).returns(place_mock)
|
98
|
+
|
99
|
+
@users_node.nodes.each_value {|node| node.stubs(:allowed_options).returns(['owner']) }
|
100
|
+
|
101
|
+
@users_node.__send__(:load!)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context "Node alias" do
|
107
|
+
setup do
|
108
|
+
@users_node = Cockroach::FactoryGirl::Node.new(
|
109
|
+
'users' => {
|
110
|
+
'amount' => '5',
|
111
|
+
'as' => 'person',
|
112
|
+
'places_amount' => '10'
|
113
|
+
})
|
114
|
+
end
|
115
|
+
|
116
|
+
context "Heritage" do
|
117
|
+
should "send load! call to subnodes" do
|
118
|
+
mocks = ((1..5).to_a.collect {|i| stub('user', :id => i) })
|
119
|
+
|
120
|
+
::FactoryGirl.stubs("create").with("user").returns( *mocks )
|
121
|
+
|
122
|
+
subnode = @users_node.nodes['place']
|
123
|
+
subnode.expects(:load!).with({"person" => mocks[0]})
|
124
|
+
subnode.expects(:load!).with({"person" => mocks[1]})
|
125
|
+
subnode.expects(:load!).with({"person" => mocks[2]})
|
126
|
+
subnode.expects(:load!).with({"person" => mocks[3]})
|
127
|
+
subnode.expects(:load!).with({"person" => mocks[4]})
|
128
|
+
|
129
|
+
@users_node.__send__(:load!)
|
130
|
+
end
|
131
|
+
|
132
|
+
should "initiate child records exact times" do
|
133
|
+
mocks = ((1..5).to_a.collect {|i| stub('user', :id => i) })
|
134
|
+
place_mock = stub('place', :id => 0)
|
135
|
+
|
136
|
+
::FactoryGirl.stubs("create").with("user").returns( *mocks )
|
137
|
+
|
138
|
+
::FactoryGirl.expects(:create).with("place", {"person" => mocks[0]}).times(10).returns(place_mock)
|
139
|
+
::FactoryGirl.expects(:create).with("place", {"person" => mocks[1]}).times(10).returns(place_mock)
|
140
|
+
::FactoryGirl.expects(:create).with("place", {"person" => mocks[2]}).times(10).returns(place_mock)
|
141
|
+
::FactoryGirl.expects(:create).with("place", {"person" => mocks[3]}).times(10).returns(place_mock)
|
142
|
+
::FactoryGirl.expects(:create).with("place", {"person" => mocks[4]}).times(10).returns(place_mock)
|
143
|
+
|
144
|
+
@users_node.nodes.each_value {|node| node.stubs(:allowed_options).returns(['person']) }
|
145
|
+
|
146
|
+
@users_node.__send__(:load!)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "user"
|
3
|
+
|
4
|
+
module Cockroach
|
5
|
+
class FactoryGirlLoadingTest < Test::Unit::TestCase
|
6
|
+
context "Fixture loading" do
|
7
|
+
setup do
|
8
|
+
mock_factory_girl
|
9
|
+
@old = Cockroach::Config.dup
|
10
|
+
|
11
|
+
Cockroach.setup do |c|
|
12
|
+
c.root = File.expand_path("../../../support/data/dummy_structure", __FILE__)
|
13
|
+
c.config_path = "./config/user_only.yml"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
teardown do
|
18
|
+
silence_warnings { Cockroach.const_set('Config', @old) }
|
19
|
+
end
|
20
|
+
|
21
|
+
should "register fixtures" do
|
22
|
+
::FactoryGirl.expects(:find_definitions)
|
23
|
+
|
24
|
+
Cockroach::FactoryGirl::Loader.load
|
25
|
+
end
|
26
|
+
|
27
|
+
context "Factories path is different" do
|
28
|
+
setup do
|
29
|
+
Cockroach.config.instance_variable_set(:@fixtures_path, "/path/to/fixtures")
|
30
|
+
end
|
31
|
+
|
32
|
+
teardown do
|
33
|
+
Cockroach.config.instance_variable_set(:@fixtures_path, nil)
|
34
|
+
end
|
35
|
+
should "register fixtures" do
|
36
|
+
::FactoryGirl.expects(:definition_file_paths=).with(["/path/to/fixtures"])
|
37
|
+
::File.expects(:directory?).returns(true)
|
38
|
+
::FactoryGirl.expects(:definition_file_paths=).with(['factories', 'test/factories', 'spec/factories'])
|
39
|
+
|
40
|
+
Cockroach::FactoryGirl::Loader.load
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,386 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "user"
|
3
|
+
|
4
|
+
module Cockroach
|
5
|
+
class FactoryGirlNodeTest < Test::Unit::TestCase
|
6
|
+
setup do
|
7
|
+
before_setup
|
8
|
+
mock_factory_girl
|
9
|
+
end
|
10
|
+
|
11
|
+
teardown do
|
12
|
+
after_teardown
|
13
|
+
end
|
14
|
+
|
15
|
+
context "Node" do
|
16
|
+
context "Structure Validation" do
|
17
|
+
should "raise error for empty hash" do
|
18
|
+
assert_raise Cockroach::InvalideStructureError do
|
19
|
+
Cockroach::FactoryGirl::Node.new({})
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
should "raise error for two-keys hash" do
|
24
|
+
assert_raise Cockroach::InvalideStructureError do
|
25
|
+
Cockroach::FactoryGirl::Node.new({"one" => "1", "two" => "2"})
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
should "raise error for not a hash" do
|
30
|
+
[[], nil, 1, ""].each do |invalid_object|
|
31
|
+
assert_raise Cockroach::InvalideStructureError do
|
32
|
+
Cockroach::FactoryGirl::Node.new(invalid_object)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "Initialization" do
|
39
|
+
should "should accept hash" do
|
40
|
+
user_node = nil
|
41
|
+
|
42
|
+
assert_nothing_thrown do
|
43
|
+
user_node = Cockroach::FactoryGirl::Node.new({ "users" => { 'amount' => '100', 'places_amount' => '10' }})
|
44
|
+
end
|
45
|
+
|
46
|
+
assert_equal({'amount' => '100'}, user_node.instance_variable_get(:@options))
|
47
|
+
assert_equal({'places_amount' => '10'}, user_node.instance_variable_get(:@structure))
|
48
|
+
end
|
49
|
+
|
50
|
+
should "should accept params" do
|
51
|
+
user_node = nil
|
52
|
+
|
53
|
+
assert_nothing_thrown do
|
54
|
+
user_node = Cockroach::FactoryGirl::Node.new "users", { 'amount' => '100', 'places_amount' => '10' }
|
55
|
+
end
|
56
|
+
|
57
|
+
assert_equal({'amount' => '100'}, user_node.instance_variable_get(:@options))
|
58
|
+
assert_equal({'places_amount' => '10'}, user_node.instance_variable_get(:@structure))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "Name definition" do
|
63
|
+
context "Info extracting" do
|
64
|
+
should "get options" do
|
65
|
+
structure = { "users" => { 'amount' => '100', 'places_amount' => '10' }}
|
66
|
+
|
67
|
+
user_node = Cockroach::FactoryGirl::Node.new structure
|
68
|
+
|
69
|
+
assert_equal({'amount' => '100'}, user_node.instance_variable_get(:@options))
|
70
|
+
assert_equal({'places_amount' => '10'}, user_node.instance_variable_get(:@structure))
|
71
|
+
end
|
72
|
+
|
73
|
+
should "get user name without approach" do
|
74
|
+
assert_equal ["user",nil], Cockroach::FactoryGirl::Node.extract_info("users")
|
75
|
+
end
|
76
|
+
|
77
|
+
should "get user name with approach: amount" do
|
78
|
+
assert_equal ["user", "amount"], Cockroach::FactoryGirl::Node.extract_info("users_amount")
|
79
|
+
end
|
80
|
+
|
81
|
+
should "get user name with approach: ratio" do
|
82
|
+
assert_equal ["user", "ratio"], Cockroach::FactoryGirl::Node.extract_info("users_ratio")
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
should "define name from simple structure" do
|
87
|
+
users_node = Cockroach::FactoryGirl::Node.new('users_amount' => '100')
|
88
|
+
assert_equal 'user', users_node.name
|
89
|
+
assert_equal 100, users_node.amount
|
90
|
+
|
91
|
+
|
92
|
+
users_node = Cockroach::FactoryGirl::Node.new('users_ratio' => '100')
|
93
|
+
assert_equal 'user', users_node.name
|
94
|
+
assert users_node.amount.kind_of? Numeric
|
95
|
+
end
|
96
|
+
|
97
|
+
should "define name from subsequntil structure" do
|
98
|
+
users_node = Cockroach::FactoryGirl::Node.new('users' => { 'amount' => '100'})
|
99
|
+
assert_equal 'user', users_node.name
|
100
|
+
assert_equal 100, users_node.amount
|
101
|
+
|
102
|
+
users_node = Cockroach::FactoryGirl::Node.new('users' => { 'ratio' => '100'})
|
103
|
+
assert_equal 'user', users_node.name
|
104
|
+
assert users_node.amount.kind_of? Numeric
|
105
|
+
|
106
|
+
users_node = Cockroach::FactoryGirl::Node.new('users' => { 'ratio' => { 'lower_limit' => '50', 'upper_limit' => '300'}})
|
107
|
+
assert_equal 'user', users_node.name
|
108
|
+
assert users_node.amount.kind_of? Numeric
|
109
|
+
end
|
110
|
+
|
111
|
+
should "raise error if factory missing" do
|
112
|
+
assert_raise ArgumentError do
|
113
|
+
users_node = Cockroach::FactoryGirl::Node.new("test_amount" => '100')
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
should "raise error if approach missing" do
|
118
|
+
assert_raise Cockroach::InvalideStructureError do
|
119
|
+
users_node = Cockroach::FactoryGirl::Node.new("users" => {})
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
context "Amount generated" do
|
125
|
+
should "simple amount" do
|
126
|
+
users_node = Cockroach::FactoryGirl::Node.new('users_amount' => '100')
|
127
|
+
assert_equal 100, users_node.amount
|
128
|
+
end
|
129
|
+
|
130
|
+
should "simple relation" do
|
131
|
+
users_node = Cockroach::FactoryGirl::Node.new('users_ratio' => '100')
|
132
|
+
users_node.expects(:get_limits).returns([50, 200])
|
133
|
+
assert users_node.amount.is_a?(Integer)
|
134
|
+
end
|
135
|
+
|
136
|
+
should "simple relation is not persistent" do
|
137
|
+
users_node = Cockroach::FactoryGirl::Node.new('users_ratio' => '100')
|
138
|
+
users_node.stubs(:get_limits).returns([50, 200])
|
139
|
+
|
140
|
+
assert_not_equal users_node.amount, users_node.amount
|
141
|
+
end
|
142
|
+
|
143
|
+
should "subsequent amount" do
|
144
|
+
users_node = Cockroach::FactoryGirl::Node.new('users' => {'amount' => '100'})
|
145
|
+
assert_equal 100, users_node.amount
|
146
|
+
end
|
147
|
+
|
148
|
+
should "subsequent relation" do
|
149
|
+
users_node = Cockroach::FactoryGirl::Node.new('users' => {'ratio' => '100'})
|
150
|
+
users_node.expects(:get_limits).returns([50, 200])
|
151
|
+
assert users_node.amount.is_a?(Integer)
|
152
|
+
end
|
153
|
+
|
154
|
+
should "subsequent relation with limits" do
|
155
|
+
users_node = Cockroach::FactoryGirl::Node.new('users' => {'ratio' => {'lower_limit' => 50, 'upper_limit' => 300}})
|
156
|
+
users_node.expects(:get_limits).returns([50, 300])
|
157
|
+
assert users_node.amount.is_a?(Integer)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
context "Source" do
|
163
|
+
setup do
|
164
|
+
@lands_node = Cockroach::FactoryGirl::Node.new(
|
165
|
+
'places' => {
|
166
|
+
'as' => 'lands',
|
167
|
+
'amount' => '10'
|
168
|
+
})
|
169
|
+
end
|
170
|
+
|
171
|
+
# should "keep all top level names" do
|
172
|
+
# assert_equal @lands_node, Cockroach::FactoryGirl::Node['lands']
|
173
|
+
# end
|
174
|
+
|
175
|
+
should "keeps all ids" do
|
176
|
+
mocks = ((1..10).to_a.collect {|i| stub('place', :id => i) })
|
177
|
+
|
178
|
+
::FactoryGirl.stubs("create").with("place").returns( *mocks )
|
179
|
+
@lands_node.__send__(:load!)
|
180
|
+
|
181
|
+
assert_equal (1..10).to_a, @lands_node.ids
|
182
|
+
end
|
183
|
+
|
184
|
+
should "define return class from factory" do
|
185
|
+
place_stub = stub('place_instance')
|
186
|
+
|
187
|
+
factory = @lands_node.instance_variable_get(:@factory)
|
188
|
+
factory.stubs(:send).with(:class_name).returns(place_stub)
|
189
|
+
|
190
|
+
assert_equal place_stub, @lands_node.__send__(:orm_class)
|
191
|
+
end
|
192
|
+
|
193
|
+
should "define_source" do
|
194
|
+
profiler = stub('profiler')
|
195
|
+
some_node = stub('some')
|
196
|
+
fancy_node = stub('fancy')
|
197
|
+
path_node = stub('path')
|
198
|
+
fancy_node.stubs(:[]).with('path').returns(path_node)
|
199
|
+
some_node.stubs(:[]).with('fancy').returns(fancy_node)
|
200
|
+
profiler.stubs(:[]).with('some').returns(some_node)
|
201
|
+
::Cockroach.stubs(:profiler).returns(profiler)
|
202
|
+
|
203
|
+
users_node = Cockroach::FactoryGirl::Node.new(
|
204
|
+
'users' => {
|
205
|
+
'amount' => '5',
|
206
|
+
'places' => {
|
207
|
+
'amount' => '10',
|
208
|
+
'source' => {
|
209
|
+
'some' => {
|
210
|
+
'fancy' => 'path'
|
211
|
+
}
|
212
|
+
}
|
213
|
+
}
|
214
|
+
})
|
215
|
+
|
216
|
+
places_node = users_node['place']
|
217
|
+
|
218
|
+
source = places_node.instance_variable_get(:@source)
|
219
|
+
|
220
|
+
assert_instance_of Cockroach::Source::Node, source
|
221
|
+
assert_equal path_node, source.node
|
222
|
+
end
|
223
|
+
|
224
|
+
context "Database" do
|
225
|
+
setup do
|
226
|
+
profiler = stub('profiler')
|
227
|
+
@lands_node = stub('lands')
|
228
|
+
profiler.stubs(:[]).with('lands').returns(@lands_node)
|
229
|
+
::Cockroach.stubs(:profiler).returns(profiler)
|
230
|
+
|
231
|
+
@old_const = Object.const_get(:Place) if Object.const_defined?(:Place)
|
232
|
+
silence_warnings { Object.const_set(:Place, stub('Place')) }
|
233
|
+
|
234
|
+
@users_node = Cockroach::FactoryGirl::Node.new(
|
235
|
+
'users' => {
|
236
|
+
'amount' => '5',
|
237
|
+
'places' => {
|
238
|
+
'amount' => '10',
|
239
|
+
'source' => {
|
240
|
+
'model' => 'Place'
|
241
|
+
}
|
242
|
+
}
|
243
|
+
})
|
244
|
+
end
|
245
|
+
|
246
|
+
teardown do
|
247
|
+
if @old_const
|
248
|
+
silence_warnings { Object.const_set('Place', @old_const) }
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
should "define_source" do
|
253
|
+
places_node = @users_node['place']
|
254
|
+
|
255
|
+
source = places_node.instance_variable_get(:@source)
|
256
|
+
|
257
|
+
assert_instance_of Cockroach::Source::Model, source
|
258
|
+
assert_equal 'Place'.constantize, source.model
|
259
|
+
end
|
260
|
+
|
261
|
+
should "assign record to parrent if source defined" do
|
262
|
+
places_node = @users_node['place']
|
263
|
+
places_node.stubs(:allowed_options).returns(["user"])
|
264
|
+
|
265
|
+
places = ((1..10).to_a.collect {|i| stub('place_instance', :id => i) })
|
266
|
+
source = places_node.instance_variable_get(:@source)
|
267
|
+
source.stubs(:sample).returns(p = places[4])
|
268
|
+
|
269
|
+
p.expects(:update_attributes).with({"user" => @users_node}).times(10)
|
270
|
+
|
271
|
+
places_node.__send__(:load!, {"user" => @users_node})
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
context "Other node" do
|
276
|
+
setup do
|
277
|
+
profiler = stub('profiler')
|
278
|
+
@lands_node = stub('lands')
|
279
|
+
profiler.stubs(:[]).with('lands').returns(@lands_node)
|
280
|
+
::Cockroach.stubs(:profiler).returns(profiler)
|
281
|
+
|
282
|
+
@users_node = Cockroach::FactoryGirl::Node.new(
|
283
|
+
'users' => {
|
284
|
+
'amount' => '5',
|
285
|
+
'places' => {
|
286
|
+
'amount' => '10',
|
287
|
+
'source' => 'lands'
|
288
|
+
}
|
289
|
+
})
|
290
|
+
end
|
291
|
+
|
292
|
+
should "define_source" do
|
293
|
+
places_node = @users_node['place']
|
294
|
+
|
295
|
+
source = places_node.instance_variable_get(:@source)
|
296
|
+
|
297
|
+
assert_instance_of Cockroach::Source::Node, source
|
298
|
+
assert_equal @lands_node, source.node
|
299
|
+
end
|
300
|
+
|
301
|
+
should "assign record to parrent if source defined" do
|
302
|
+
places_node = @users_node['place']
|
303
|
+
places_node.stubs(:allowed_options).returns(["user"])
|
304
|
+
|
305
|
+
places = ((1..10).to_a.collect {|i| stub('place_instance', :id => i) })
|
306
|
+
source = places_node.instance_variable_get(:@source)
|
307
|
+
source.stubs(:sample).returns(p = places[4])
|
308
|
+
|
309
|
+
p.expects(:update_attributes).with({"user" => @users_node}).times(10)
|
310
|
+
|
311
|
+
places_node.__send__(:load!, {"user" => @users_node})
|
312
|
+
end
|
313
|
+
end
|
314
|
+
end
|
315
|
+
|
316
|
+
context "Subnode" do
|
317
|
+
setup do
|
318
|
+
@users_node = Cockroach::FactoryGirl::Node.new('users' => {'amount' => '5', 'places_amount' => '10'})
|
319
|
+
end
|
320
|
+
|
321
|
+
should "create subnode" do
|
322
|
+
assert_equal 1, @users_node.nodes.size
|
323
|
+
|
324
|
+
subnode = @users_node.nodes['place']
|
325
|
+
|
326
|
+
assert_instance_of Cockroach::FactoryGirl::Node, subnode
|
327
|
+
assert_equal 'place', subnode.name
|
328
|
+
assert_equal 'amount', subnode.approach
|
329
|
+
assert_equal 10, subnode.amount
|
330
|
+
end
|
331
|
+
|
332
|
+
should "recive .load! call from supnode" do
|
333
|
+
subnode = @users_node.nodes['place']
|
334
|
+
subnode.expects(:load!)
|
335
|
+
|
336
|
+
@users_node.__send__(:load_nodes!)
|
337
|
+
end
|
338
|
+
|
339
|
+
context "Heritage" do
|
340
|
+
should "send load! call to subnodes" do
|
341
|
+
mocks = ((1..5).to_a.collect {|i| stub('user', :id => i) })
|
342
|
+
|
343
|
+
::FactoryGirl.stubs("create").with("user").returns( *mocks )
|
344
|
+
|
345
|
+
subnode = @users_node.nodes['place']
|
346
|
+
subnode.expects(:load!).with({"user" => mocks[0]})
|
347
|
+
subnode.expects(:load!).with({"user" => mocks[1]})
|
348
|
+
subnode.expects(:load!).with({"user" => mocks[2]})
|
349
|
+
subnode.expects(:load!).with({"user" => mocks[3]})
|
350
|
+
subnode.expects(:load!).with({"user" => mocks[4]})
|
351
|
+
|
352
|
+
@users_node.__send__(:load!)
|
353
|
+
end
|
354
|
+
|
355
|
+
should "initiate parrent record exact times" do
|
356
|
+
mocks = ((1..5).to_a.collect {|i| stub('user', :id => i) })
|
357
|
+
place_mock = stub('place', :id => 0)
|
358
|
+
|
359
|
+
::FactoryGirl.expects(:create).with("user").times(5).returns( *mocks )
|
360
|
+
::FactoryGirl.stubs(:create).with("place", any_parameters).returns(place_mock)
|
361
|
+
|
362
|
+
@users_node.nodes.each_value {|node| node.stubs(:allowed_options).returns(['user']) }
|
363
|
+
|
364
|
+
@users_node.__send__(:load!)
|
365
|
+
end
|
366
|
+
|
367
|
+
should "initiate child records exact times" do
|
368
|
+
mocks = ((1..5).to_a.collect {|i| stub('user', :id => i) })
|
369
|
+
place_mock = stub('place', :id => 0)
|
370
|
+
|
371
|
+
::FactoryGirl.stubs("create").with("user").returns( *mocks )
|
372
|
+
|
373
|
+
::FactoryGirl.expects(:create).with("place", {"user" => mocks[0]}).times(10).returns(place_mock)
|
374
|
+
::FactoryGirl.expects(:create).with("place", {"user" => mocks[1]}).times(10).returns(place_mock)
|
375
|
+
::FactoryGirl.expects(:create).with("place", {"user" => mocks[2]}).times(10).returns(place_mock)
|
376
|
+
::FactoryGirl.expects(:create).with("place", {"user" => mocks[3]}).times(10).returns(place_mock)
|
377
|
+
::FactoryGirl.expects(:create).with("place", {"user" => mocks[4]}).times(10).returns(place_mock)
|
378
|
+
|
379
|
+
@users_node.nodes.each_value {|node| node.stubs(:allowed_options).returns(['user']) }
|
380
|
+
|
381
|
+
@users_node.__send__(:load!)
|
382
|
+
end
|
383
|
+
end
|
384
|
+
end
|
385
|
+
end
|
386
|
+
end
|