mr 0.35.2
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.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/Gemfile +13 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/bench/all.rb +4 -0
- data/bench/factory.rb +68 -0
- data/bench/fake_record.rb +174 -0
- data/bench/model.rb +201 -0
- data/bench/read_model.rb +191 -0
- data/bench/results/factory.txt +21 -0
- data/bench/results/fake_record.txt +37 -0
- data/bench/results/model.txt +44 -0
- data/bench/results/read_model.txt +46 -0
- data/bench/setup.rb +132 -0
- data/lib/mr.rb +11 -0
- data/lib/mr/after_commit.rb +49 -0
- data/lib/mr/after_commit/fake_record.rb +39 -0
- data/lib/mr/after_commit/record.rb +48 -0
- data/lib/mr/after_commit/record_procs_methods.rb +82 -0
- data/lib/mr/factory.rb +82 -0
- data/lib/mr/factory/config.rb +240 -0
- data/lib/mr/factory/model_factory.rb +103 -0
- data/lib/mr/factory/model_stack.rb +28 -0
- data/lib/mr/factory/read_model_factory.rb +104 -0
- data/lib/mr/factory/record_factory.rb +130 -0
- data/lib/mr/factory/record_stack.rb +219 -0
- data/lib/mr/fake_query.rb +53 -0
- data/lib/mr/fake_record.rb +58 -0
- data/lib/mr/fake_record/associations.rb +257 -0
- data/lib/mr/fake_record/attributes.rb +168 -0
- data/lib/mr/fake_record/persistence.rb +116 -0
- data/lib/mr/json_field.rb +180 -0
- data/lib/mr/json_field/fake_record.rb +31 -0
- data/lib/mr/json_field/record.rb +38 -0
- data/lib/mr/model.rb +67 -0
- data/lib/mr/model/associations.rb +161 -0
- data/lib/mr/model/configuration.rb +67 -0
- data/lib/mr/model/fields.rb +177 -0
- data/lib/mr/model/persistence.rb +79 -0
- data/lib/mr/query.rb +126 -0
- data/lib/mr/read_model.rb +83 -0
- data/lib/mr/read_model/data.rb +38 -0
- data/lib/mr/read_model/fields.rb +218 -0
- data/lib/mr/read_model/query_expression.rb +188 -0
- data/lib/mr/read_model/querying.rb +214 -0
- data/lib/mr/read_model/set_querying.rb +82 -0
- data/lib/mr/read_model/subquery.rb +98 -0
- data/lib/mr/record.rb +35 -0
- data/lib/mr/test_helpers.rb +229 -0
- data/lib/mr/type_converter.rb +85 -0
- data/lib/mr/version.rb +3 -0
- data/log/.gitkeep +0 -0
- data/mr.gemspec +29 -0
- data/test/helper.rb +21 -0
- data/test/support/db.rb +10 -0
- data/test/support/factory.rb +13 -0
- data/test/support/factory/area.rb +6 -0
- data/test/support/factory/comment.rb +14 -0
- data/test/support/factory/image.rb +6 -0
- data/test/support/factory/user.rb +6 -0
- data/test/support/models/area.rb +58 -0
- data/test/support/models/comment.rb +60 -0
- data/test/support/models/image.rb +53 -0
- data/test/support/models/user.rb +96 -0
- data/test/support/read_model/querying.rb +150 -0
- data/test/support/read_models/comment_with_user_data.rb +27 -0
- data/test/support/read_models/set_data.rb +49 -0
- data/test/support/read_models/subquery_data.rb +41 -0
- data/test/support/read_models/user_with_area_data.rb +15 -0
- data/test/support/schema.rb +39 -0
- data/test/support/setup_test_db.rb +10 -0
- data/test/system/factory/model_factory_tests.rb +87 -0
- data/test/system/factory/model_stack_tests.rb +30 -0
- data/test/system/factory/record_factory_tests.rb +84 -0
- data/test/system/factory/record_stack_tests.rb +51 -0
- data/test/system/factory_tests.rb +32 -0
- data/test/system/read_model_tests.rb +199 -0
- data/test/system/with_model_tests.rb +275 -0
- data/test/unit/after_commit/fake_record_tests.rb +110 -0
- data/test/unit/after_commit/record_procs_methods_tests.rb +177 -0
- data/test/unit/after_commit/record_tests.rb +134 -0
- data/test/unit/after_commit_tests.rb +113 -0
- data/test/unit/factory/config_tests.rb +651 -0
- data/test/unit/factory/model_factory_tests.rb +473 -0
- data/test/unit/factory/model_stack_tests.rb +97 -0
- data/test/unit/factory/read_model_factory_tests.rb +195 -0
- data/test/unit/factory/record_factory_tests.rb +446 -0
- data/test/unit/factory/record_stack_tests.rb +549 -0
- data/test/unit/factory_tests.rb +213 -0
- data/test/unit/fake_query_tests.rb +137 -0
- data/test/unit/fake_record/associations_tests.rb +585 -0
- data/test/unit/fake_record/attributes_tests.rb +265 -0
- data/test/unit/fake_record/persistence_tests.rb +239 -0
- data/test/unit/fake_record_tests.rb +106 -0
- data/test/unit/json_field/fake_record_tests.rb +75 -0
- data/test/unit/json_field/record_tests.rb +80 -0
- data/test/unit/json_field_tests.rb +302 -0
- data/test/unit/model/associations_tests.rb +346 -0
- data/test/unit/model/configuration_tests.rb +92 -0
- data/test/unit/model/fields_tests.rb +278 -0
- data/test/unit/model/persistence_tests.rb +114 -0
- data/test/unit/model_tests.rb +137 -0
- data/test/unit/query_tests.rb +300 -0
- data/test/unit/read_model/data_tests.rb +56 -0
- data/test/unit/read_model/fields_tests.rb +416 -0
- data/test/unit/read_model/query_expression_tests.rb +381 -0
- data/test/unit/read_model/querying_tests.rb +613 -0
- data/test/unit/read_model/set_querying_tests.rb +149 -0
- data/test/unit/read_model/subquery_tests.rb +242 -0
- data/test/unit/read_model_tests.rb +187 -0
- data/test/unit/record_tests.rb +45 -0
- data/test/unit/test_helpers_tests.rb +431 -0
- data/test/unit/type_converter_tests.rb +207 -0
- metadata +285 -0
@@ -0,0 +1,549 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'mr/factory/record_stack'
|
3
|
+
|
4
|
+
require 'mr/factory/model_factory'
|
5
|
+
require 'mr/factory/record_factory'
|
6
|
+
require 'mr/fake_record'
|
7
|
+
require 'mr/model'
|
8
|
+
|
9
|
+
class MR::Factory::RecordStack
|
10
|
+
|
11
|
+
class UnitTests < Assert::Context
|
12
|
+
desc "MR::Factory::RecordStack"
|
13
|
+
setup do
|
14
|
+
@factory_config_class = if Factory.boolean
|
15
|
+
MR::Factory::RecordFactory::Config
|
16
|
+
else
|
17
|
+
MR::Factory::ModelFactory::Config
|
18
|
+
end
|
19
|
+
|
20
|
+
@stack_class = MR::Factory::RecordStack
|
21
|
+
end
|
22
|
+
subject{ @stack_class }
|
23
|
+
|
24
|
+
should have_imeths :for_record
|
25
|
+
|
26
|
+
should "know how to build itself from a record" do
|
27
|
+
record_class = TestFakeRecord
|
28
|
+
record = record_class.new
|
29
|
+
factory_config = @factory_config_class.new(record_class)
|
30
|
+
|
31
|
+
record_data = RecordData.new(record)
|
32
|
+
Assert.stub(RecordData, :new).with(record){ record_data }
|
33
|
+
|
34
|
+
stack_built_with = nil
|
35
|
+
Assert.stub(subject, :new){ |*args| stack_built_with = args }
|
36
|
+
|
37
|
+
subject.for_record(record, factory_config)
|
38
|
+
|
39
|
+
assert_equal [record_data, factory_config], stack_built_with
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
class InitSetupTests < UnitTests
|
45
|
+
desc "when init"
|
46
|
+
subject{ @record_stack }
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
class InitTests < InitSetupTests
|
51
|
+
setup do
|
52
|
+
@record_class = FakeFourRecord
|
53
|
+
@record = @record_class.new(:fake_poly_type => FakeThreeRecord.to_s)
|
54
|
+
@factory_config = @factory_config_class.new(@record_class)
|
55
|
+
@record_data = RecordData.new(@record)
|
56
|
+
|
57
|
+
# record stacks are recursive, only stub association datas that are built
|
58
|
+
# for our record
|
59
|
+
@get_record_data_called_with = []
|
60
|
+
@association_record_datas = []
|
61
|
+
@record_data.association_datas.each do |association_data|
|
62
|
+
Assert.stub(association_data, :get_record_data) do |*args|
|
63
|
+
@get_record_data_called_with << args
|
64
|
+
record = Assert.stub_send(association_data, :get_record_data, *args)
|
65
|
+
@association_record_datas << record
|
66
|
+
record
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
@record_stack = @stack_class.new(@record_data, @factory_config)
|
71
|
+
end
|
72
|
+
|
73
|
+
should have_readers :record_data, :factory_config, :record_lookup
|
74
|
+
should have_readers :record_stacks
|
75
|
+
should have_imeths :record
|
76
|
+
should have_imeths :create, :destroy
|
77
|
+
should have_imeths :create_dependencies, :create_deps
|
78
|
+
should have_imeths :destroy_dependencies, :destroy_deps
|
79
|
+
|
80
|
+
should "know its record data, factory config, record lookup and its record" do
|
81
|
+
assert_equal @record_data, subject.record_data
|
82
|
+
assert_equal @factory_config, subject.factory_config
|
83
|
+
assert_equal @record_data.record, subject.record
|
84
|
+
|
85
|
+
assert_instance_of Hash, subject.record_lookup
|
86
|
+
end
|
87
|
+
|
88
|
+
should "know its record stacks" do
|
89
|
+
association_datas = @record_data.association_datas.select do |data|
|
90
|
+
data.required?(@factory_config)
|
91
|
+
end
|
92
|
+
|
93
|
+
assert_equal association_datas.size, subject.record_lookup.size
|
94
|
+
assert_equal association_datas.size, subject.record_stacks.size
|
95
|
+
|
96
|
+
subject.record_stacks.each_with_index do |record_stack, n|
|
97
|
+
association_data = association_datas[n]
|
98
|
+
associated_record_data = @association_record_datas[n]
|
99
|
+
get_record_data_called_with = @get_record_data_called_with[n]
|
100
|
+
assoc_factory_config = @factory_config.factory_config_for(
|
101
|
+
association_data.name,
|
102
|
+
associated_record_data.record_class
|
103
|
+
)
|
104
|
+
|
105
|
+
# get the associated record data
|
106
|
+
exp = [@factory_config, subject.record_lookup]
|
107
|
+
assert_equal exp, get_record_data_called_with
|
108
|
+
|
109
|
+
# set the association on the record
|
110
|
+
exp = associated_record_data.record
|
111
|
+
assert_equal exp, @record.send(association_data.name)
|
112
|
+
|
113
|
+
# update the record lookup
|
114
|
+
record_data = subject.record_lookup[associated_record_data.record.class]
|
115
|
+
assert_same associated_record_data, record_data
|
116
|
+
|
117
|
+
# build a record stack
|
118
|
+
assert_instance_of @stack_class, record_stack
|
119
|
+
assert_equal associated_record_data, record_stack.record_data
|
120
|
+
assert_equal assoc_factory_config, record_stack.factory_config
|
121
|
+
assert_equal subject.record_lookup, record_stack.record_lookup
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
should "know how to create the full stack" do
|
126
|
+
call_order = 0
|
127
|
+
|
128
|
+
create_record_called_at = nil
|
129
|
+
Assert.stub(subject.record_data, :create_record) do
|
130
|
+
create_record_called_at = call_order += 1
|
131
|
+
end
|
132
|
+
create_deps_called_at = nil
|
133
|
+
Assert.stub(subject, :create_dependencies) do
|
134
|
+
create_deps_called_at = call_order += 1
|
135
|
+
end
|
136
|
+
|
137
|
+
assert_true subject.create
|
138
|
+
assert_equal 1, create_deps_called_at
|
139
|
+
assert_equal 2, create_record_called_at
|
140
|
+
end
|
141
|
+
|
142
|
+
should "know how to destroy the full stack" do
|
143
|
+
call_order = 0
|
144
|
+
|
145
|
+
destroy_record_called_at = nil
|
146
|
+
Assert.stub(subject.record_data, :destroy_record) do
|
147
|
+
destroy_record_called_at = call_order += 1
|
148
|
+
end
|
149
|
+
destroy_deps_called_at = nil
|
150
|
+
Assert.stub(subject, :destroy_dependencies) do
|
151
|
+
destroy_deps_called_at = call_order += 1
|
152
|
+
end
|
153
|
+
|
154
|
+
assert_true subject.destroy
|
155
|
+
assert_equal 1, destroy_record_called_at
|
156
|
+
assert_equal 2, destroy_deps_called_at
|
157
|
+
end
|
158
|
+
|
159
|
+
should "know how to create its dependencies" do
|
160
|
+
call_order = 0
|
161
|
+
|
162
|
+
refresh_associations_called_at = nil
|
163
|
+
Assert.stub(subject.record_data, :refresh_record_associations) do
|
164
|
+
refresh_associations_called_at = call_order += 1
|
165
|
+
end
|
166
|
+
record_stacks_create_called_at = nil
|
167
|
+
created_record_stacks = []
|
168
|
+
subject.record_stacks.each do |record_stack|
|
169
|
+
Assert.stub(record_stack, :create) do
|
170
|
+
record_stacks_create_called_at ||= call_order += 1
|
171
|
+
created_record_stacks << record_stack
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
assert_true subject.create
|
176
|
+
assert_equal 1, record_stacks_create_called_at
|
177
|
+
assert_equal 2, refresh_associations_called_at
|
178
|
+
assert_equal subject.record_stacks, created_record_stacks
|
179
|
+
end
|
180
|
+
|
181
|
+
should "know how to destroy its dependencies" do
|
182
|
+
destroyed_record_stacks = []
|
183
|
+
subject.record_stacks.each do |record_stack|
|
184
|
+
Assert.stub(record_stack, :destroy) do
|
185
|
+
destroyed_record_stacks << record_stack
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
assert_true subject.destroy
|
190
|
+
assert_equal subject.record_stacks, destroyed_record_stacks
|
191
|
+
end
|
192
|
+
|
193
|
+
class FakeOneRecord; include MR::FakeRecord; end
|
194
|
+
class FakeTwoRecord; include MR::FakeRecord; end
|
195
|
+
class FakeThreeRecord; include MR::FakeRecord; end
|
196
|
+
|
197
|
+
class FakeFourRecord
|
198
|
+
include MR::FakeRecord
|
199
|
+
|
200
|
+
attribute :fake_poly_type, :string, :null => false
|
201
|
+
attribute :fake_poly_id, :integer, :null => false
|
202
|
+
attribute :fake_one_id, :integer, :null => false
|
203
|
+
attribute :fake_two_id, :integer, :null => false
|
204
|
+
|
205
|
+
attribute :not_required_poly_type, :string, :null => true
|
206
|
+
attribute :not_required_poly_id, :integer, :null => true
|
207
|
+
attribute :not_required_id, :integer, :null => true
|
208
|
+
|
209
|
+
belongs_to :fake_poly, :polymorphic => true
|
210
|
+
belongs_to :fake_one, :class_name => FakeOneRecord.to_s
|
211
|
+
belongs_to :fake_two, :class_name => FakeTwoRecord.to_s
|
212
|
+
|
213
|
+
belongs_to :not_required_poly, :polymorphic => true
|
214
|
+
belongs_to :not_required, :class_name => FakeOneRecord.to_s
|
215
|
+
end
|
216
|
+
|
217
|
+
class FakeOneModel
|
218
|
+
include MR::Model
|
219
|
+
record_class FakeOneRecord
|
220
|
+
end
|
221
|
+
|
222
|
+
class FakeTwoModel
|
223
|
+
include MR::Model
|
224
|
+
record_class FakeTwoRecord
|
225
|
+
end
|
226
|
+
|
227
|
+
class FakeThreeModel
|
228
|
+
include MR::Model
|
229
|
+
record_class FakeThreeRecord
|
230
|
+
end
|
231
|
+
|
232
|
+
class FakeFourModel
|
233
|
+
include MR::Model
|
234
|
+
record_class FakeFourRecord
|
235
|
+
|
236
|
+
field_accessor :fake_poly_type, :fake_poly_id, :fake_one_id, :fake_two_id
|
237
|
+
|
238
|
+
polymorphic_belongs_to :fake_poly
|
239
|
+
belongs_to :fake_one
|
240
|
+
belongs_to :fake_two
|
241
|
+
end
|
242
|
+
|
243
|
+
end
|
244
|
+
|
245
|
+
class InitWithDeeplyNestedPresetsTests < InitSetupTests
|
246
|
+
desc "with deeply nested preset associations"
|
247
|
+
setup do
|
248
|
+
@record_class = FakeThreeRecord
|
249
|
+
@record = @record_class.new
|
250
|
+
@factory_config = @factory_config_class.new(@record_class)
|
251
|
+
|
252
|
+
@fake_one_record = FakeOneRecord.new
|
253
|
+
|
254
|
+
@fake_two_record = FakeTwoRecord.new
|
255
|
+
@fake_two_record.fake_one = @fake_one_record
|
256
|
+
|
257
|
+
@record.fake_two = @fake_two_record
|
258
|
+
|
259
|
+
@record_data = RecordData.new(@record)
|
260
|
+
@record_stack = @stack_class.new(@record_data, @factory_config)
|
261
|
+
end
|
262
|
+
|
263
|
+
should "use the preset record for all matching associations" do
|
264
|
+
assert_equal @fake_one_record, @record.fake_one
|
265
|
+
end
|
266
|
+
|
267
|
+
class FakeOneRecord; include MR::FakeRecord; end
|
268
|
+
|
269
|
+
class FakeTwoRecord
|
270
|
+
include MR::FakeRecord
|
271
|
+
|
272
|
+
attribute :fake_one_id, :integer, :null => false
|
273
|
+
|
274
|
+
belongs_to :fake_one, :class_name => FakeOneRecord.to_s
|
275
|
+
end
|
276
|
+
|
277
|
+
class FakeThreeRecord
|
278
|
+
include MR::FakeRecord
|
279
|
+
|
280
|
+
attribute :fake_one_id, :integer, :null => false
|
281
|
+
attribute :fake_two_id, :integer, :null => false
|
282
|
+
|
283
|
+
belongs_to :fake_one, :class_name => FakeOneRecord.to_s
|
284
|
+
belongs_to :fake_two, :class_name => FakeTwoRecord.to_s
|
285
|
+
end
|
286
|
+
|
287
|
+
end
|
288
|
+
|
289
|
+
class RecordDataAndAssociationDataSetupTests < UnitTests
|
290
|
+
setup do
|
291
|
+
@record_class = TestFakeRecord
|
292
|
+
@record = @record_class.new
|
293
|
+
@factory_config = @factory_config_class.new(@record_class)
|
294
|
+
end
|
295
|
+
|
296
|
+
end
|
297
|
+
|
298
|
+
class RecordDataTests < RecordDataAndAssociationDataSetupTests
|
299
|
+
desc "Record"
|
300
|
+
setup do
|
301
|
+
@record_data_class = RecordData
|
302
|
+
@record_data = @record_data_class.new(@record)
|
303
|
+
end
|
304
|
+
subject{ @record_data }
|
305
|
+
|
306
|
+
should have_readers :record, :association_datas
|
307
|
+
should have_imeths :record_class, :create_record, :destroy_record
|
308
|
+
should have_imeths :set_association, :refresh_record_associations
|
309
|
+
|
310
|
+
should "know its record and record class" do
|
311
|
+
assert_equal @record, subject.record
|
312
|
+
assert_equal @record.class, subject.record_class
|
313
|
+
end
|
314
|
+
|
315
|
+
should "build association datas from its records associations" do
|
316
|
+
exp_association_names = [:another, :not_required, :other]
|
317
|
+
|
318
|
+
assert_equal exp_association_names.size, subject.association_datas.size
|
319
|
+
subject.association_datas.each_with_index do |association_data, n|
|
320
|
+
exp_association_name = exp_association_names[n]
|
321
|
+
|
322
|
+
assert_instance_of AssociationData, association_data
|
323
|
+
assert_equal exp_association_name, association_data.name
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
327
|
+
should "save a record using `create_record` if it's a new record" do
|
328
|
+
assert_true @record.new_record?
|
329
|
+
subject.create_record
|
330
|
+
assert_false @record.new_record?
|
331
|
+
|
332
|
+
Assert.stub(@record, :save!){ raise "shouldn't be called" }
|
333
|
+
assert_nothing_raised{ subject.create_record }
|
334
|
+
end
|
335
|
+
|
336
|
+
should "reset a fake records save called flag using `create_record`" do
|
337
|
+
assert_false @record.save_called
|
338
|
+
subject.create_record
|
339
|
+
assert_false @record.save_called
|
340
|
+
end
|
341
|
+
|
342
|
+
should "set an association from a record data using `set_association`" do
|
343
|
+
another_record = AnotherFakeRecord.new.tap(&:save!)
|
344
|
+
another_record_data = @record_data_class.new(another_record)
|
345
|
+
|
346
|
+
subject.set_association(:another, another_record_data)
|
347
|
+
assert_equal another_record, @record.another
|
348
|
+
end
|
349
|
+
|
350
|
+
should "reset associations to properly set foreign keys " \
|
351
|
+
"using `refresh_associations`" do
|
352
|
+
another_record = AnotherFakeRecord.new
|
353
|
+
@record.another = another_record
|
354
|
+
another_record.save!
|
355
|
+
|
356
|
+
assert_nil @record.another_id
|
357
|
+
subject.refresh_record_associations
|
358
|
+
assert_equal another_record.id, @record.another_id
|
359
|
+
end
|
360
|
+
|
361
|
+
end
|
362
|
+
|
363
|
+
class AssociationDataTests < UnitTests
|
364
|
+
desc "AssociationData"
|
365
|
+
setup do
|
366
|
+
@record_class = FakeTwoRecord
|
367
|
+
@record = @record_class.new
|
368
|
+
@association = @record.association(:fake_one)
|
369
|
+
|
370
|
+
@associated_record_class = FakeOneRecord
|
371
|
+
@factory_config = @factory_config_class.new(@record_class)
|
372
|
+
@record_lookup = {}
|
373
|
+
|
374
|
+
@association_data_class = AssociationData
|
375
|
+
@association_data = @association_data_class.new(@association)
|
376
|
+
end
|
377
|
+
subject{ @association_data }
|
378
|
+
|
379
|
+
should have_readers :association, :preset_record_data
|
380
|
+
should have_imeths :name, :record_class, :preset?, :required?
|
381
|
+
should have_imeths :get_record_data
|
382
|
+
|
383
|
+
should "know its attributes" do
|
384
|
+
assert_equal @association, subject.association
|
385
|
+
assert_equal @association.reflection.name, subject.name
|
386
|
+
assert_equal @association.klass, subject.record_class
|
387
|
+
end
|
388
|
+
|
389
|
+
should "know if it's preset" do
|
390
|
+
assert_false subject.preset?
|
391
|
+
|
392
|
+
preset_record = @associated_record_class.new
|
393
|
+
@record.send("#{subject.name}=", preset_record)
|
394
|
+
|
395
|
+
association_data = @association_data_class.new(@association)
|
396
|
+
assert_true association_data.preset?
|
397
|
+
end
|
398
|
+
|
399
|
+
should "know if it's required" do
|
400
|
+
assert_true subject.required?(@factory_config)
|
401
|
+
|
402
|
+
association = @record.association(:fake_poly)
|
403
|
+
association_data = @association_data_class.new(association)
|
404
|
+
assert_true association_data.required?(@factory_config)
|
405
|
+
|
406
|
+
association = @record.association(:required_key_poly)
|
407
|
+
association_data = @association_data_class.new(association)
|
408
|
+
assert_true association_data.required?(@factory_config)
|
409
|
+
|
410
|
+
association = @record.association(:required_type_poly)
|
411
|
+
association_data = @association_data_class.new(association)
|
412
|
+
assert_true association_data.required?(@factory_config)
|
413
|
+
|
414
|
+
association = @record.association(:not_required)
|
415
|
+
association_data = @association_data_class.new(association)
|
416
|
+
assert_false association_data.required?(@factory_config)
|
417
|
+
|
418
|
+
@factory_config.force_in_stack_association_names << association_data.name
|
419
|
+
assert_true association_data.required?(@factory_config)
|
420
|
+
|
421
|
+
association = @record.association(:not_required_poly)
|
422
|
+
association_data = @association_data_class.new(association)
|
423
|
+
assert_false association_data.required?(@factory_config)
|
424
|
+
|
425
|
+
@factory_config.force_in_stack_association_names << association_data.name
|
426
|
+
assert_true association_data.required?(@factory_config)
|
427
|
+
end
|
428
|
+
|
429
|
+
should "return its preset when getting record data for a preset association" do
|
430
|
+
preset_record = @associated_record_class.new
|
431
|
+
@association.owner.send("#{@association.reflection.name}=", preset_record)
|
432
|
+
|
433
|
+
association_data = @association_data_class.new(@association)
|
434
|
+
assert_true association_data.preset?
|
435
|
+
exp = association_data.preset_record_data
|
436
|
+
assert_same exp, association_data.get_record_data(@factory_config, @record_lookup)
|
437
|
+
end
|
438
|
+
|
439
|
+
should "return a matching lookup value when not preset and getting record data" do
|
440
|
+
lookup_record = @associated_record_class.new
|
441
|
+
@record_lookup[lookup_record.class] = RecordData.new(lookup_record)
|
442
|
+
|
443
|
+
association_data = @association_data_class.new(@association)
|
444
|
+
assert_false association_data.preset?
|
445
|
+
exp = @record_lookup[lookup_record.class]
|
446
|
+
assert_same exp, association_data.get_record_data(@factory_config, @record_lookup)
|
447
|
+
end
|
448
|
+
|
449
|
+
should "return a matching lookup value for poly assoc when getting record data" do
|
450
|
+
association = @record.association(:fake_poly)
|
451
|
+
|
452
|
+
lookup_record = @associated_record_class.new
|
453
|
+
@record_lookup[lookup_record.class] = RecordData.new(lookup_record)
|
454
|
+
|
455
|
+
record_classes = [@associated_record_class, @record_class]
|
456
|
+
Assert.stub(@factory_config, :record_classes_for).with(
|
457
|
+
association.reflection.name
|
458
|
+
){ record_classes }
|
459
|
+
Assert.stub(record_classes, :sample){ @associated_record_class }
|
460
|
+
|
461
|
+
association_data = @association_data_class.new(association)
|
462
|
+
assert_false association_data.preset?
|
463
|
+
assert_nil association_data.record_class
|
464
|
+
exp = @record_lookup[lookup_record.class]
|
465
|
+
assert_same exp, association_data.get_record_data(@factory_config, @record_lookup)
|
466
|
+
end
|
467
|
+
|
468
|
+
should "build a record when getting record data with no lookup or preset" do
|
469
|
+
associated_record = @associated_record_class.new
|
470
|
+
Assert.stub(@factory_config, :build_associated_record).with(
|
471
|
+
@association.reflection.name,
|
472
|
+
@association.klass
|
473
|
+
){ associated_record }
|
474
|
+
|
475
|
+
association_data = @association_data_class.new(@association)
|
476
|
+
assert_false association_data.preset?
|
477
|
+
record_data = association_data.get_record_data(@factory_config, @record_lookup)
|
478
|
+
assert_instance_of RecordData, record_data
|
479
|
+
assert_same associated_record, record_data.record
|
480
|
+
end
|
481
|
+
|
482
|
+
should "be sortable" do
|
483
|
+
association_datas = [:fake_one, :fake_poly].shuffle.map do |association_name|
|
484
|
+
@association_data_class.new(@record.association(association_name))
|
485
|
+
end.sort
|
486
|
+
|
487
|
+
assert_equal [:fake_one, :fake_poly], association_datas.map(&:name)
|
488
|
+
end
|
489
|
+
|
490
|
+
class FakeOneRecord; include MR::FakeRecord; end
|
491
|
+
|
492
|
+
class FakeTwoRecord
|
493
|
+
include MR::FakeRecord
|
494
|
+
|
495
|
+
attribute :fake_poly_type, :string, :null => false
|
496
|
+
attribute :fake_poly_id, :integer, :null => false
|
497
|
+
attribute :fake_one_id, :integer, :null => false
|
498
|
+
|
499
|
+
attribute :required_type_poly_type, :string, :null => false
|
500
|
+
attribute :required_type_poly_id, :integer, :null => true
|
501
|
+
|
502
|
+
attribute :required_key_poly_type, :string, :null => true
|
503
|
+
attribute :required_key_poly_id, :integer, :null => false
|
504
|
+
|
505
|
+
attribute :not_required_poly_type, :string, :null => true
|
506
|
+
attribute :not_required_poly_id, :integer, :null => true
|
507
|
+
attribute :not_required_id, :integer, :null => true
|
508
|
+
|
509
|
+
belongs_to :fake_poly, :polymorphic => true
|
510
|
+
belongs_to :fake_one, :class_name => FakeOneRecord.to_s
|
511
|
+
|
512
|
+
belongs_to :required_type_poly, :polymorphic => true
|
513
|
+
belongs_to :required_key_poly, :polymorphic => true
|
514
|
+
|
515
|
+
belongs_to :not_required_poly, :polymorphic => true
|
516
|
+
belongs_to :not_required, :class_name => FakeOneRecord.to_s
|
517
|
+
end
|
518
|
+
|
519
|
+
end
|
520
|
+
|
521
|
+
class TestFakeRecord
|
522
|
+
include MR::FakeRecord
|
523
|
+
|
524
|
+
attribute :id, :primary_key
|
525
|
+
attribute :other_id, :integer, :null => false
|
526
|
+
attribute :another_id, :integer, :null => false
|
527
|
+
attribute :not_required_id, :integer, :null => true
|
528
|
+
|
529
|
+
belongs_to :other, :class_name => 'MR::Factory::RecordStack::OtherFakeRecord'
|
530
|
+
belongs_to :another, :class_name => 'MR::Factory::RecordStack::AnotherFakeRecord'
|
531
|
+
belongs_to :not_required, :class_name => 'MR::Factory::RecordStack::OtherFakeRecord'
|
532
|
+
end
|
533
|
+
|
534
|
+
class OtherFakeRecord
|
535
|
+
include MR::FakeRecord
|
536
|
+
|
537
|
+
attribute :id, :primary_key
|
538
|
+
end
|
539
|
+
|
540
|
+
class AnotherFakeRecord
|
541
|
+
include MR::FakeRecord
|
542
|
+
|
543
|
+
attribute :id, :primary_key
|
544
|
+
attribute :other_id, :integer, :null => false
|
545
|
+
|
546
|
+
belongs_to :other, :class_name => 'MR::Factory::RecordStack::OtherFakeRecord'
|
547
|
+
end
|
548
|
+
|
549
|
+
end
|