mr 0.35.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/Gemfile +13 -0
  4. data/LICENSE +22 -0
  5. data/README.md +29 -0
  6. data/bench/all.rb +4 -0
  7. data/bench/factory.rb +68 -0
  8. data/bench/fake_record.rb +174 -0
  9. data/bench/model.rb +201 -0
  10. data/bench/read_model.rb +191 -0
  11. data/bench/results/factory.txt +21 -0
  12. data/bench/results/fake_record.txt +37 -0
  13. data/bench/results/model.txt +44 -0
  14. data/bench/results/read_model.txt +46 -0
  15. data/bench/setup.rb +132 -0
  16. data/lib/mr.rb +11 -0
  17. data/lib/mr/after_commit.rb +49 -0
  18. data/lib/mr/after_commit/fake_record.rb +39 -0
  19. data/lib/mr/after_commit/record.rb +48 -0
  20. data/lib/mr/after_commit/record_procs_methods.rb +82 -0
  21. data/lib/mr/factory.rb +82 -0
  22. data/lib/mr/factory/config.rb +240 -0
  23. data/lib/mr/factory/model_factory.rb +103 -0
  24. data/lib/mr/factory/model_stack.rb +28 -0
  25. data/lib/mr/factory/read_model_factory.rb +104 -0
  26. data/lib/mr/factory/record_factory.rb +130 -0
  27. data/lib/mr/factory/record_stack.rb +219 -0
  28. data/lib/mr/fake_query.rb +53 -0
  29. data/lib/mr/fake_record.rb +58 -0
  30. data/lib/mr/fake_record/associations.rb +257 -0
  31. data/lib/mr/fake_record/attributes.rb +168 -0
  32. data/lib/mr/fake_record/persistence.rb +116 -0
  33. data/lib/mr/json_field.rb +180 -0
  34. data/lib/mr/json_field/fake_record.rb +31 -0
  35. data/lib/mr/json_field/record.rb +38 -0
  36. data/lib/mr/model.rb +67 -0
  37. data/lib/mr/model/associations.rb +161 -0
  38. data/lib/mr/model/configuration.rb +67 -0
  39. data/lib/mr/model/fields.rb +177 -0
  40. data/lib/mr/model/persistence.rb +79 -0
  41. data/lib/mr/query.rb +126 -0
  42. data/lib/mr/read_model.rb +83 -0
  43. data/lib/mr/read_model/data.rb +38 -0
  44. data/lib/mr/read_model/fields.rb +218 -0
  45. data/lib/mr/read_model/query_expression.rb +188 -0
  46. data/lib/mr/read_model/querying.rb +214 -0
  47. data/lib/mr/read_model/set_querying.rb +82 -0
  48. data/lib/mr/read_model/subquery.rb +98 -0
  49. data/lib/mr/record.rb +35 -0
  50. data/lib/mr/test_helpers.rb +229 -0
  51. data/lib/mr/type_converter.rb +85 -0
  52. data/lib/mr/version.rb +3 -0
  53. data/log/.gitkeep +0 -0
  54. data/mr.gemspec +29 -0
  55. data/test/helper.rb +21 -0
  56. data/test/support/db.rb +10 -0
  57. data/test/support/factory.rb +13 -0
  58. data/test/support/factory/area.rb +6 -0
  59. data/test/support/factory/comment.rb +14 -0
  60. data/test/support/factory/image.rb +6 -0
  61. data/test/support/factory/user.rb +6 -0
  62. data/test/support/models/area.rb +58 -0
  63. data/test/support/models/comment.rb +60 -0
  64. data/test/support/models/image.rb +53 -0
  65. data/test/support/models/user.rb +96 -0
  66. data/test/support/read_model/querying.rb +150 -0
  67. data/test/support/read_models/comment_with_user_data.rb +27 -0
  68. data/test/support/read_models/set_data.rb +49 -0
  69. data/test/support/read_models/subquery_data.rb +41 -0
  70. data/test/support/read_models/user_with_area_data.rb +15 -0
  71. data/test/support/schema.rb +39 -0
  72. data/test/support/setup_test_db.rb +10 -0
  73. data/test/system/factory/model_factory_tests.rb +87 -0
  74. data/test/system/factory/model_stack_tests.rb +30 -0
  75. data/test/system/factory/record_factory_tests.rb +84 -0
  76. data/test/system/factory/record_stack_tests.rb +51 -0
  77. data/test/system/factory_tests.rb +32 -0
  78. data/test/system/read_model_tests.rb +199 -0
  79. data/test/system/with_model_tests.rb +275 -0
  80. data/test/unit/after_commit/fake_record_tests.rb +110 -0
  81. data/test/unit/after_commit/record_procs_methods_tests.rb +177 -0
  82. data/test/unit/after_commit/record_tests.rb +134 -0
  83. data/test/unit/after_commit_tests.rb +113 -0
  84. data/test/unit/factory/config_tests.rb +651 -0
  85. data/test/unit/factory/model_factory_tests.rb +473 -0
  86. data/test/unit/factory/model_stack_tests.rb +97 -0
  87. data/test/unit/factory/read_model_factory_tests.rb +195 -0
  88. data/test/unit/factory/record_factory_tests.rb +446 -0
  89. data/test/unit/factory/record_stack_tests.rb +549 -0
  90. data/test/unit/factory_tests.rb +213 -0
  91. data/test/unit/fake_query_tests.rb +137 -0
  92. data/test/unit/fake_record/associations_tests.rb +585 -0
  93. data/test/unit/fake_record/attributes_tests.rb +265 -0
  94. data/test/unit/fake_record/persistence_tests.rb +239 -0
  95. data/test/unit/fake_record_tests.rb +106 -0
  96. data/test/unit/json_field/fake_record_tests.rb +75 -0
  97. data/test/unit/json_field/record_tests.rb +80 -0
  98. data/test/unit/json_field_tests.rb +302 -0
  99. data/test/unit/model/associations_tests.rb +346 -0
  100. data/test/unit/model/configuration_tests.rb +92 -0
  101. data/test/unit/model/fields_tests.rb +278 -0
  102. data/test/unit/model/persistence_tests.rb +114 -0
  103. data/test/unit/model_tests.rb +137 -0
  104. data/test/unit/query_tests.rb +300 -0
  105. data/test/unit/read_model/data_tests.rb +56 -0
  106. data/test/unit/read_model/fields_tests.rb +416 -0
  107. data/test/unit/read_model/query_expression_tests.rb +381 -0
  108. data/test/unit/read_model/querying_tests.rb +613 -0
  109. data/test/unit/read_model/set_querying_tests.rb +149 -0
  110. data/test/unit/read_model/subquery_tests.rb +242 -0
  111. data/test/unit/read_model_tests.rb +187 -0
  112. data/test/unit/record_tests.rb +45 -0
  113. data/test/unit/test_helpers_tests.rb +431 -0
  114. data/test/unit/type_converter_tests.rb +207 -0
  115. metadata +285 -0
@@ -0,0 +1,97 @@
1
+ require 'assert'
2
+ require 'mr/factory/model_stack'
3
+
4
+ require 'mr/fake_record'
5
+ require 'mr/model'
6
+ require 'mr/factory/model_factory'
7
+ require 'mr/factory/record_stack'
8
+
9
+ class MR::Factory::ModelStack
10
+
11
+ class UnitTests < Assert::Context
12
+ desc "MR::Factory::ModelStack"
13
+ setup do
14
+ @record_stack_spy = nil
15
+ Assert.stub(MR::Factory::RecordStack, :for_record) do |*args|
16
+ @record_stack_spy = RecordStackSpy.new(*args)
17
+ end
18
+
19
+ @record_class = Class.new{ include MR::FakeRecord }
20
+ @model_class = Class.new{ include MR::Model }
21
+ @model_class.record_class(@record_class)
22
+
23
+ @model = @model_class.new
24
+ @factory_config = MR::Factory::ModelFactory::Config.new(@record_class)
25
+
26
+ @stack_class = MR::Factory::ModelStack
27
+ @stack = @stack_class.new(@model, @factory_config)
28
+ end
29
+ subject{ @stack }
30
+
31
+ should have_readers :model
32
+ should have_imeths :factory_config
33
+ should have_imeths :create, :destroy
34
+ should have_imeths :create_dependencies, :create_deps
35
+ should have_imeths :destroy_dependencies, :destroy_deps
36
+
37
+ should "know its model and factory config" do
38
+ assert_equal @model, subject.model
39
+ assert_equal @factory_config, subject.factory_config
40
+ end
41
+
42
+ should "build a record stack" do
43
+ assert_not_nil @record_stack_spy
44
+ assert_equal @model.record, @record_stack_spy.record
45
+ assert_equal @factory_config, @record_stack_spy.factory_config
46
+ end
47
+
48
+ should "demeter its record stack" do
49
+ assert_false @record_stack_spy.create_called
50
+ subject.create
51
+ assert_true @record_stack_spy.create_called
52
+
53
+ assert_false @record_stack_spy.destroy_called
54
+ subject.destroy
55
+ assert_true @record_stack_spy.destroy_called
56
+
57
+ assert_false @record_stack_spy.create_dependencies_called
58
+ subject.create_dependencies
59
+ assert_true @record_stack_spy.create_dependencies_called
60
+
61
+ @record_stack_spy.create_dependencies_called = false
62
+ subject.create_deps
63
+ assert_true @record_stack_spy.create_dependencies_called
64
+
65
+ assert_false @record_stack_spy.destroy_dependencies_called
66
+ subject.destroy_dependencies
67
+ assert_true @record_stack_spy.destroy_dependencies_called
68
+
69
+ @record_stack_spy.destroy_dependencies_called = false
70
+ subject.destroy_dependencies
71
+ assert_true @record_stack_spy.destroy_dependencies_called
72
+ end
73
+
74
+ end
75
+
76
+ class RecordStackSpy
77
+ attr_reader :record, :factory_config
78
+ attr_accessor :create_called, :destroy_called
79
+ attr_accessor :create_dependencies_called, :destroy_dependencies_called
80
+
81
+ def initialize(record, factory_config)
82
+ @record = record
83
+ @factory_config = factory_config
84
+ @create_called = false
85
+ @destroy_called = false
86
+ @create_dependencies_called = false
87
+ @destroy_dependencies_called = false
88
+ end
89
+
90
+ def create; @create_called = true; end
91
+ def destroy; @destroy_called = true; end
92
+
93
+ def create_dependencies; @create_dependencies_called = true; end
94
+ def destroy_dependencies; @destroy_dependencies_called = true; end
95
+ end
96
+
97
+ end
@@ -0,0 +1,195 @@
1
+ require 'assert'
2
+ require 'mr/factory/read_model_factory'
3
+
4
+ require 'mr/json_field'
5
+ require 'mr/read_model'
6
+
7
+ class MR::Factory::ReadModelFactory
8
+
9
+ class UnitTests < Assert::Context
10
+ desc "MR::Factory::ReadModelFactory"
11
+ setup do
12
+ @factory_class = MR::Factory::ReadModelFactory
13
+ end
14
+ subject{ @factory_class }
15
+
16
+ end
17
+
18
+ class InitTests < UnitTests
19
+ desc "when init"
20
+ setup do
21
+ @read_model_class = TestReadModel
22
+ @factory = @factory_class.new(@read_model_class)
23
+ end
24
+ subject{ @factory }
25
+
26
+ should have_readers :read_model_class, :config
27
+ should have_imeths :read_model_data, :read_model, :instance
28
+
29
+ should "instance eval any block it's initialized with" do
30
+ yielded = nil
31
+ factory = @factory_class.new(@read_model_class){ yielded = self }
32
+ assert_equal factory, yielded
33
+ end
34
+
35
+ should "raise an argument error if init without a read model or read model struct" do
36
+ assert_nothing_raised{ @factory_class.new(@read_model_class) }
37
+ assert_nothing_raised{ @factory_class.new(@read_model_class::StructReadModel) }
38
+
39
+ assert_raises(ArgumentError){ @factory_class.new(Factory.string) }
40
+ end
41
+
42
+ should "know its read model class and config" do
43
+ assert_equal @read_model_class, subject.read_model_class
44
+
45
+ assert_instance_of Config, subject.config
46
+ assert_equal subject.read_model_class, subject.config.read_model_class
47
+ end
48
+
49
+ should "allow configuring default args" do
50
+ name = Factory.string
51
+ default_args_proc = proc{ set(:name, name) }
52
+ factory = @factory_class.new(@read_model_class)
53
+
54
+ set_default_args_called_with = nil
55
+ Assert.stub(factory.config, :set_default_args) do |&block|
56
+ set_default_args_called_with = block
57
+ Assert.stub_send(factory.config, :set_default_args, &block)
58
+ end
59
+
60
+ factory.instance_eval{ default_args(&default_args_proc) }
61
+ assert_equal default_args_proc, set_default_args_called_with
62
+
63
+ read_model = factory.read_model
64
+ assert_equal name, read_model.name
65
+ end
66
+
67
+ end
68
+
69
+ class ReadModelDataTests < InitTests
70
+ desc "read_model_data"
71
+
72
+ should "build a read model data object" do
73
+ read_model_data = subject.read_model_data
74
+ assert_instance_of Data, read_model_data
75
+ assert_kind_of String, read_model_data['name']
76
+ end
77
+
78
+ should "apply passed args to the read model data" do
79
+ name = Factory.string
80
+ read_model_data = subject.read_model_data(:name => name)
81
+ assert_equal name, read_model_data['name']
82
+ end
83
+
84
+ should "apply default args to the record" do
85
+ name = Factory.string
86
+ subject.config.set_default_args{ set(:name, name) }
87
+ read_model_data = subject.read_model_data
88
+ assert_equal name, read_model_data['name']
89
+ end
90
+
91
+ should "apply passed args over default args to the read model data" do
92
+ name = Factory.string
93
+ subject.config.set_default_args{ set(:name, name) }
94
+ new_name = Factory.string
95
+ read_model_data = subject.read_model_data(:name => new_name)
96
+ assert_equal new_name, read_model_data['name']
97
+ end
98
+
99
+ end
100
+
101
+ class ReadModelMethodTests < InitTests
102
+ desc "read_model"
103
+ setup do
104
+ @read_model_data_called_with = nil
105
+ @built_read_model_data = nil
106
+ Assert.stub(subject, :read_model_data) do |*args|
107
+ @read_model_data_called_with = args
108
+ @built_read_model_data = Assert.stub_send(subject, :read_model_data, *args)
109
+ end
110
+ end
111
+
112
+ should "build an instance of the read model clast" do
113
+ read_model = subject.read_model
114
+ assert_instance_of @read_model_class, read_model
115
+ assert_same @built_read_model_data, read_model.instance_eval{ read_model_data }
116
+ end
117
+
118
+ should "be aliased" do
119
+ read_model = subject.instance
120
+ assert_instance_of @read_model_class, read_model
121
+ assert_same @built_read_model_data, read_model.instance_eval{ read_model_data }
122
+ end
123
+
124
+ should "allow passing args when building a read model" do
125
+ args = { :name => Factory.string }
126
+ saved_record = subject.read_model(args)
127
+ assert_equal [args], @read_model_data_called_with
128
+ end
129
+
130
+ end
131
+
132
+ class ConfigTests < UnitTests
133
+ desc "Config"
134
+ setup do
135
+ @config_class = Config
136
+ end
137
+ subject{ @config_class }
138
+
139
+ should "be a factory config" do
140
+ assert_includes MR::Factory::Config, subject
141
+ end
142
+
143
+ end
144
+
145
+ class ConfigInitTests < ConfigTests
146
+ desc "when init"
147
+ setup do
148
+ @read_model_class = TestReadModel
149
+ @config = @config_class.new(@read_model_class)
150
+ end
151
+ subject{ @config }
152
+
153
+ should have_imeths :read_model_class
154
+
155
+ should "know its read model class" do
156
+ assert_equal @read_model_class, subject.read_model_class
157
+ end
158
+
159
+ should "know how to apply field default args to a read model data" do
160
+ read_model_data = @factory_class::Data.new
161
+ subject.apply_args(read_model_data, {})
162
+
163
+ assert_kind_of String, read_model_data['name']
164
+
165
+ assert_instance_of Array, read_model_data['struct_list']
166
+ assert_includes read_model_data['struct_list'].size, [1, 2, 3]
167
+
168
+ read_model_data['struct_list'].each do |struct_list_data|
169
+ assert_instance_of Data, struct_list_data
170
+ assert_kind_of String, struct_list_data['name']
171
+ end
172
+
173
+ assert_instance_of Data, read_model_data['struct_obj']
174
+ assert_kind_of String, read_model_data['struct_obj']['name']
175
+ end
176
+
177
+ end
178
+
179
+ class TestReadModel
180
+ include MR::ReadModel
181
+
182
+ field :name, :string
183
+
184
+ json_struct_obj :struct_obj, 'StructReadModel'
185
+
186
+ json_struct_list :struct_list, 'StructReadModel'
187
+
188
+ class StructReadModel
189
+ include MR::ReadModelStruct
190
+
191
+ field :name, :string
192
+ end
193
+ end
194
+
195
+ end
@@ -0,0 +1,446 @@
1
+ require 'assert'
2
+ require 'mr/factory/record_factory'
3
+
4
+ require 'mr/factory/config'
5
+ require 'mr/fake_record'
6
+ require 'mr/record'
7
+
8
+ class MR::Factory::RecordFactory
9
+
10
+ class UnitTests < Assert::Context
11
+ desc "MR::Factory::RecordFactory"
12
+ setup do
13
+ @factory_class = MR::Factory::RecordFactory
14
+ end
15
+ subject{ @factory_class }
16
+
17
+ end
18
+
19
+ class InitTests < UnitTests
20
+ desc "when init"
21
+ setup do
22
+ @record_class = FakeTwoRecord
23
+ @factory = @factory_class.new(@record_class)
24
+ end
25
+ subject{ @factory }
26
+
27
+ should have_readers :record_class, :config
28
+ should have_imeths :record, :instance
29
+ should have_imeths :saved_record, :saved_instance
30
+ should have_imeths :stack, :instance_stack
31
+ should have_imeths :stack_record, :saved_stack, :saved_stack_record
32
+ should have_imeths :saved_dependencies_stack, :saved_deps_stack
33
+ should have_imeths :saved_dependencies_stack_record, :saved_deps_stack_record
34
+
35
+ should "instance eval any block it's initialized with" do
36
+ yielded = nil
37
+ factory = @factory_class.new(@record_class){ yielded = self }
38
+ assert_equal factory, yielded
39
+ end
40
+
41
+ should "know its record class and config" do
42
+ assert_equal @record_class, subject.record_class
43
+
44
+ assert_instance_of Config, subject.config
45
+ assert_equal subject.record_class, subject.config.record_class
46
+ end
47
+
48
+ should "allow configuring a default association" do
49
+ association_name = Factory.boolean ? :fake_one : :fake_poly
50
+ assoc_record_class = FakeOneRecord
51
+ associated_factory = @factory_class.new(assoc_record_class)
52
+ factory = @factory_class.new(@record_class) do
53
+ default_association(association_name, associated_factory, {
54
+ :force_in_stack => true
55
+ })
56
+ end
57
+
58
+ associated_record = associated_factory.record
59
+ Assert.stub(associated_factory, :record){ associated_record }
60
+
61
+ assert_same associated_record, factory.config.build_associated_record(
62
+ association_name,
63
+ assoc_record_class
64
+ )
65
+
66
+ assert_includes association_name, factory.config.force_in_stack_association_names
67
+ end
68
+
69
+ should "allow configuring default args" do
70
+ name = Factory.string
71
+ default_args_proc = proc{ set(:name, name) }
72
+ factory = @factory_class.new(@record_class)
73
+
74
+ set_default_args_called_with = nil
75
+ Assert.stub(factory.config, :set_default_args) do |&block|
76
+ set_default_args_called_with = block
77
+ Assert.stub_send(factory.config, :set_default_args, &block)
78
+ end
79
+
80
+ factory.instance_eval{ default_args(&default_args_proc) }
81
+ assert_equal default_args_proc, set_default_args_called_with
82
+
83
+ record = factory.record
84
+ assert_equal name, record.name
85
+ end
86
+
87
+ end
88
+
89
+ class RecordMethodTests < InitTests
90
+ desc "record"
91
+ setup do
92
+ @built_record = nil
93
+ Assert.stub(@record_class, :new) do |*args|
94
+ @built_record = Assert.stub_send(@record_class, :new, *args)
95
+ end
96
+ end
97
+
98
+ should "return an instance of the record class" do
99
+ record = subject.record
100
+ assert_same @built_record, record
101
+ assert_instance_of String, record.name
102
+ end
103
+
104
+ should "be aliased" do
105
+ record = subject.instance
106
+ assert_same @built_record, record
107
+ assert_instance_of String, record.name
108
+ end
109
+
110
+ should "apply passed args to the record" do
111
+ name = Factory.string
112
+ record = subject.record(:name => name)
113
+ assert_equal name, record.name
114
+ end
115
+
116
+ should "apply default args to the record" do
117
+ name = Factory.string
118
+ subject.config.set_default_args{ set(:name, name) }
119
+ record = subject.record
120
+ assert_equal name, record.name
121
+ end
122
+
123
+ should "apply passed args over default args to the record" do
124
+ name = Factory.string
125
+ subject.config.set_default_args{ set(:name, name) }
126
+ new_name = Factory.string
127
+ record = subject.record(:name => new_name)
128
+ assert_equal new_name, record.name
129
+ end
130
+
131
+ end
132
+
133
+ class SavedRecordMethodTests < InitTests
134
+ desc "saved_record"
135
+ setup do
136
+ @record_called_with = nil
137
+ @built_record = nil
138
+ Assert.stub(subject, :record) do |*args|
139
+ @record_called_with = args
140
+ @built_record = Assert.stub_send(subject, :record, *args)
141
+ end
142
+ end
143
+
144
+ should "build an instance of the record class and save it" do
145
+ saved_record = subject.saved_record
146
+ assert_same @built_record, saved_record
147
+ assert_false saved_record.new_record?
148
+ end
149
+
150
+ should "be aliased" do
151
+ saved_record = subject.saved_instance
152
+ assert_same @built_record, saved_record
153
+ assert_false saved_record.new_record?
154
+ end
155
+
156
+ should "allow passing args when building a saved record" do
157
+ args = { :name => Factory.string }
158
+ saved_record = subject.saved_record(args)
159
+ assert_equal [args], @record_called_with
160
+ end
161
+
162
+ should "reset the fake records save called flag" do
163
+ assert_false subject.saved_record.save_called
164
+ end
165
+
166
+ end
167
+
168
+ class StackMethodTests < InitTests
169
+ desc "stack"
170
+ setup do
171
+ @record_called_with = nil
172
+ @built_record = nil
173
+ Assert.stub(subject, :record) do |*args|
174
+ @record_called_with = args
175
+ @built_record = Assert.stub_send(subject, :record, *args)
176
+ end
177
+ end
178
+
179
+ should "build a record stack" do
180
+ stack = subject.stack
181
+ assert_instance_of MR::Factory::RecordStack, stack
182
+ assert_equal [nil], @record_called_with
183
+ assert_same @built_record, stack.record
184
+ assert_same subject.config, stack.factory_config
185
+ end
186
+
187
+ should "be aliased" do
188
+ stack = subject.instance_stack
189
+ assert_instance_of MR::Factory::RecordStack, stack
190
+ assert_equal [nil], @record_called_with
191
+ assert_same @built_record, stack.record
192
+ assert_same subject.config, stack.factory_config
193
+ end
194
+
195
+ should "allow passing args when building a stack" do
196
+ args = { :name => Factory.string }
197
+ stack = subject.stack(args)
198
+ assert_equal [args], @record_called_with
199
+ end
200
+
201
+ end
202
+
203
+ class StackRecordMethodTests < InitTests
204
+ desc "stack_record"
205
+ setup do
206
+ @stack_called_with = nil
207
+ @built_stack = nil
208
+ Assert.stub(subject, :stack) do |*args|
209
+ @stack_called_with = args
210
+ @built_stack = Assert.stub_send(subject, :stack, *args)
211
+ end
212
+ end
213
+
214
+ should "build a stack record" do
215
+ record = subject.stack_record
216
+ assert_equal [nil], @stack_called_with
217
+ assert_same @built_stack.record, record
218
+ end
219
+
220
+ should "allow passing args when building a stack record" do
221
+ args = { :name => Factory.string }
222
+ record = subject.stack_record(args)
223
+ assert_equal [args], @stack_called_with
224
+ end
225
+
226
+ end
227
+
228
+ class SavedStackMethodTests < InitTests
229
+ desc "saved_stack"
230
+ setup do
231
+ @stack_called_with = nil
232
+ @built_stack = nil
233
+ @stack_create_called = false
234
+ Assert.stub(subject, :stack) do |*args|
235
+ @stack_called_with = args
236
+ @built_stack = Assert.stub_send(subject, :stack, *args)
237
+ Assert.stub(@built_stack, :create) do
238
+ @stack_create_called = true
239
+ Assert.stub_send(@built_stack, :create)
240
+ end
241
+ @built_stack
242
+ end
243
+ end
244
+
245
+ should "build a saved stack" do
246
+ saved_stack = subject.saved_stack
247
+ assert_equal [nil], @stack_called_with
248
+ assert_same @built_stack, saved_stack
249
+ assert_true @stack_create_called
250
+ end
251
+
252
+ should "allow passing args when building a saved stack record" do
253
+ args = { :name => Factory.string }
254
+ saved_stack = subject.saved_stack(args)
255
+ assert_equal [args], @stack_called_with
256
+ end
257
+
258
+ end
259
+
260
+ class SavedStackModelMethodTests < InitTests
261
+ desc "saved_stack_record"
262
+ setup do
263
+ @saved_stack_called_with = nil
264
+ @built_saved_stack = nil
265
+ Assert.stub(subject, :saved_stack) do |*args|
266
+ @saved_stack_called_with = args
267
+ @built_saved_stack = Assert.stub_send(subject, :saved_stack, *args)
268
+ end
269
+ end
270
+
271
+ should "build a saved stack record" do
272
+ saved_record = subject.saved_stack_record
273
+ assert_equal [nil], @saved_stack_called_with
274
+ assert_same @built_saved_stack.record, saved_record
275
+ end
276
+
277
+ should "allow passing args when building a saved stack record" do
278
+ args = { :name => Factory.string }
279
+ saved_record = subject.saved_stack_record(args)
280
+ assert_equal [args], @saved_stack_called_with
281
+ end
282
+
283
+ end
284
+
285
+ class SavedDepsStackMethodTests < InitTests
286
+ desc "saved_dependencies_stack"
287
+ setup do
288
+ @stack_called_with = nil
289
+ @built_stack = nil
290
+ @stack_create_deps_called = false
291
+ Assert.stub(subject, :stack) do |*args|
292
+ @stack_called_with = args
293
+ @built_stack = Assert.stub_send(subject, :stack, *args)
294
+ Assert.stub(@built_stack, :create_deps) do
295
+ @stack_create_deps_called = true
296
+ Assert.stub_send(@built_stack, :create_deps)
297
+ end
298
+ @built_stack
299
+ end
300
+ end
301
+
302
+ should "build a saved dependencies stack" do
303
+ saved_deps_stack = subject.saved_dependencies_stack
304
+ assert_equal [nil], @stack_called_with
305
+ assert_same @built_stack, saved_deps_stack
306
+ assert_true @stack_create_deps_called
307
+ end
308
+
309
+ should "be aliased" do
310
+ saved_deps_stack = subject.saved_deps_stack
311
+ assert_equal [nil], @stack_called_with
312
+ assert_same @built_stack, saved_deps_stack
313
+ assert_true @stack_create_deps_called
314
+ end
315
+
316
+ should "allow passing args when building a saved dependencies stack" do
317
+ args = { :name => Factory.string }
318
+ saved_deps_stack = subject.saved_dependencies_stack(args)
319
+ assert_equal [args], @stack_called_with
320
+ end
321
+
322
+ end
323
+
324
+ class SavedDepsStackModelTests < InitTests
325
+ desc "saved_dependencies_stack_record"
326
+ setup do
327
+ @saved_deps_stack_called_with = nil
328
+ @built_saved_deps_stack = nil
329
+ Assert.stub(subject, :saved_deps_stack) do |*args|
330
+ @saved_deps_stack_called_with = args
331
+ @built_saved_deps_stack = Assert.stub_send(subject, :saved_deps_stack, *args)
332
+ end
333
+ end
334
+
335
+ should "know how to build a saved dependencies stack record" do
336
+ saved_deps_record = subject.saved_dependencies_stack_record
337
+ assert_equal [nil], @saved_deps_stack_called_with
338
+ assert_same @built_saved_deps_stack.record, saved_deps_record
339
+ end
340
+
341
+ should "be aliased" do
342
+ saved_deps_record = subject.saved_deps_stack_record
343
+ assert_equal [nil], @saved_deps_stack_called_with
344
+ assert_same @built_saved_deps_stack.record, saved_deps_record
345
+ end
346
+
347
+ should "allow passing args when building a saved dependencies stack record" do
348
+ args = { :name => Factory.string }
349
+ saved_deps_record = subject.saved_dependencies_stack_record(args)
350
+ assert_equal [args], @saved_deps_stack_called_with
351
+ assert_same @built_saved_deps_stack.record, saved_deps_record
352
+ end
353
+
354
+ end
355
+
356
+ class ConfigTests < UnitTests
357
+ desc "Config"
358
+ setup do
359
+ @config_class = Config
360
+ end
361
+ subject{ @config_class }
362
+
363
+ should "be a with-associations factory config" do
364
+ assert_includes MR::Factory::WithAssociationsConfig, subject
365
+ end
366
+
367
+ end
368
+
369
+ class ConfigInitTests < ConfigTests
370
+ desc "when init"
371
+ setup do
372
+ @record_class = FakeTwoRecord
373
+ @config = @config_class.new(@record_class)
374
+ end
375
+ subject{ @config }
376
+
377
+ should have_imeths :build_associated_record, :ar_association_for
378
+
379
+ should "know how to build an associated record" do
380
+ factory_for_called_with = []
381
+ built_factory = nil
382
+ record = nil
383
+ Assert.stub(subject, :factory_for) do |*args|
384
+ factory_for_called_with = args
385
+ built_factory = Assert.stub_send(subject, :factory_for, *args)
386
+ record = built_factory.record
387
+ Assert.stub(built_factory, :record){ record }
388
+ built_factory
389
+ end
390
+
391
+ association_name = Factory.boolean ? :fake_one : :fake_poly
392
+ assoc_record_class = FakeOneRecord
393
+ result = subject.build_associated_record(
394
+ association_name,
395
+ assoc_record_class
396
+ )
397
+
398
+ assert_equal [association_name, assoc_record_class], factory_for_called_with
399
+ assert_instance_of MR::Factory::RecordFactory, built_factory
400
+ assert_equal assoc_record_class, built_factory.record_class
401
+
402
+ assert_same record, result
403
+ end
404
+
405
+ should "know how to get an activerecord association for a record" do
406
+ record = @record_class.new
407
+
408
+ exp = record.association(:fake_one)
409
+ assert_equal exp, subject.ar_association_for(record, :fake_one)
410
+
411
+ assert_nil subject.ar_association_for(record, Factory.string)
412
+ end
413
+
414
+ should "know how to apply column default args to a record" do
415
+ record = @record_class.new
416
+ subject.apply_args(record, {})
417
+
418
+ assert_instance_of String, record.name
419
+ assert_nil record.id
420
+ assert_nil record.fake_poly_type
421
+ assert_nil record.fake_poly_id
422
+ assert_nil record.fake_one_id
423
+ assert_nil record.bad_column
424
+ end
425
+
426
+ end
427
+
428
+ class FakeOneRecord
429
+ include MR::FakeRecord
430
+ end
431
+
432
+ class FakeTwoRecord
433
+ include MR::FakeRecord
434
+
435
+ attribute :name, :string
436
+ attribute :fake_poly_type, :string
437
+ attribute :fake_poly_id, :integer
438
+ attribute :fake_one_id, :integer
439
+
440
+ attribute :bad_column, :invalid_type
441
+
442
+ belongs_to :fake_poly, :polymorphic => true
443
+ belongs_to :fake_one, :class_name => FakeOneRecord.to_s
444
+ end
445
+
446
+ end