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,110 @@
1
+ require 'assert'
2
+ require 'mr/after_commit/fake_record'
3
+
4
+ require 'much-plugin'
5
+ require 'mr/after_commit'
6
+ require 'mr/fake_record'
7
+
8
+ module MR::AfterCommit::FakeRecord
9
+
10
+ class UnitTests < Assert::Context
11
+ desc "FakeRecord"
12
+ subject{ MR::AfterCommit::FakeRecord }
13
+
14
+ should "use much-plugin" do
15
+ assert_includes MuchPlugin, subject
16
+ end
17
+
18
+ end
19
+
20
+ class MixinTests < UnitTests
21
+ desc "when mixed in"
22
+ setup do
23
+ @fake_record_class = Class.new do
24
+ include MR::AfterCommit::FakeRecord
25
+ end
26
+ end
27
+ subject{ @fake_record_class }
28
+
29
+ should "be an MR record" do
30
+ assert_includes MR::FakeRecord, subject
31
+ end
32
+
33
+ should "use the records procs methods mixin" do
34
+ assert_includes MR::AfterCommit::RecordProcsMethods, subject
35
+ end
36
+
37
+ end
38
+
39
+ class InitTests < MixinTests
40
+ desc "and init"
41
+ setup do
42
+ @fake_record = @fake_record_class.new
43
+
44
+ i = 0
45
+ @fake_record.add_after_commit_proc(:save){ @save_call_num = i += 1 }
46
+ @fake_record.add_after_commit_proc(:create){ @create_call_num = i += 1 }
47
+ @fake_record.add_after_commit_proc(:update){ @update_call_num = i += 1 }
48
+
49
+ @destroy_called = false
50
+ @fake_record.add_after_commit_proc(:destroy){ @destroy_called = true }
51
+ end
52
+ subject{ @fake_record }
53
+
54
+ should have_imeths :called_after_commit_procs
55
+
56
+ should "call and clear its save and create procs when a new fake record is saved" do
57
+ subject.save!
58
+
59
+ assert_equal 1, @create_call_num
60
+ assert_equal 2, @save_call_num
61
+ assert_nil @update_call_num
62
+ assert_false @destroy_called
63
+
64
+ assert_empty subject.after_commit_procs(:create, :save)
65
+ assert_not_empty subject.after_commit_procs(:update, :destroy)
66
+
67
+ assert_equal 1, subject.called_after_commit_procs(:create).size
68
+ assert_equal 1, subject.called_after_commit_procs(:save).size
69
+ assert_equal 0, subject.called_after_commit_procs(:update).size
70
+ assert_equal 0, subject.called_after_commit_procs(:destroy).size
71
+ end
72
+
73
+ should "call and clear its save and update procs when an existing fake record is saved" do
74
+ subject.id = Factory.integer # make it an existing
75
+ subject.save!
76
+
77
+ assert_equal 1, @update_call_num
78
+ assert_equal 2, @save_call_num
79
+ assert_nil @create_call_num
80
+ assert_false @destroy_called
81
+
82
+ assert_empty subject.after_commit_procs(:update, :save)
83
+ assert_not_empty subject.after_commit_procs(:create, :destroy)
84
+
85
+ assert_equal 1, subject.called_after_commit_procs(:update).size
86
+ assert_equal 1, subject.called_after_commit_procs(:save).size
87
+ assert_equal 0, subject.called_after_commit_procs(:create).size
88
+ assert_equal 0, subject.called_after_commit_procs(:destroy).size
89
+ end
90
+
91
+ should "call and clear its destroy procs after commit on destroy" do
92
+ subject.destroy
93
+
94
+ assert_true @destroy_called
95
+ assert_nil @save_call_num
96
+ assert_nil @create_call_num
97
+ assert_nil @update_call_num
98
+
99
+ assert_empty subject.after_commit_procs(:destroy)
100
+ assert_not_empty subject.after_commit_procs(:create, :update, :save)
101
+
102
+ assert_equal 1, subject.called_after_commit_procs(:destroy).size
103
+ assert_equal 0, subject.called_after_commit_procs(:update).size
104
+ assert_equal 0, subject.called_after_commit_procs(:save).size
105
+ assert_equal 0, subject.called_after_commit_procs(:create).size
106
+ end
107
+
108
+ end
109
+
110
+ end
@@ -0,0 +1,177 @@
1
+ require 'assert'
2
+ require 'mr/after_commit/record_procs_methods'
3
+
4
+ module MR::AfterCommit::RecordProcsMethods
5
+
6
+ class UnitTests < Assert::Context
7
+ desc "MR::AfterCommit::RecordProcsMethods"
8
+ subject{ MR::AfterCommit::RecordProcsMethods }
9
+
10
+ should "know its valid callback types" do
11
+ exp = [
12
+ :create,
13
+ :update,
14
+ :save,
15
+ :destroy
16
+ ]
17
+ assert_equal exp, VALID_CALLBACK_TYPES
18
+ end
19
+
20
+ should "know its default callback type" do
21
+ assert_equal :save, DEFAULT_CALLBACK_TYPE
22
+ end
23
+
24
+ end
25
+
26
+ class MixinAndInitTests < UnitTests
27
+ desc "when mixed in and init"
28
+ setup do
29
+ record_class = Class.new do
30
+ include MR::AfterCommit::RecordProcsMethods
31
+ end
32
+ @record = record_class.new
33
+ end
34
+ subject{ @record }
35
+
36
+ should have_imeths :after_commit_procs
37
+ should have_imeths :add_after_commit_proc, :prepend_after_commit_proc
38
+ should have_imeths :clear_after_commit_procs
39
+
40
+ should "not have any after commit procs by default" do
41
+ assert_equal [], subject.after_commit_procs
42
+ assert_equal [], subject.after_commit_procs(VALID_CALLBACK_TYPES.sample)
43
+ end
44
+
45
+ should "allow adding after commit procs" do
46
+ callback_type = VALID_CALLBACK_TYPES.sample
47
+ callback_proc = proc{ Factory.string }
48
+ subject.add_after_commit_proc(callback_type, &callback_proc)
49
+
50
+ assert_equal 1, subject.after_commit_procs.size
51
+ assert_includes callback_proc, subject.after_commit_procs
52
+ assert_equal 1, subject.after_commit_procs(callback_type).size
53
+ assert_includes callback_proc, subject.after_commit_procs(callback_type)
54
+ end
55
+
56
+ should "add the after commit proc to save by default" do
57
+ callback_proc = proc{ Factory.string }
58
+ subject.add_after_commit_proc(&callback_proc)
59
+
60
+ assert_equal 1, subject.after_commit_procs.size
61
+ assert_includes callback_proc, subject.after_commit_procs
62
+ assert_equal 1, subject.after_commit_procs(DEFAULT_CALLBACK_TYPE).size
63
+ assert_includes callback_proc, subject.after_commit_procs(DEFAULT_CALLBACK_TYPE)
64
+ end
65
+
66
+ should "append after commit procs when adding them" do
67
+ callback_type = VALID_CALLBACK_TYPES.sample
68
+ callbacks_procs = Factory.integer(3).times.map do
69
+ cp = proc{ Factory.string }
70
+ subject.add_after_commit_proc(callback_type, &cp)
71
+ cp
72
+ end
73
+
74
+ assert_equal callbacks_procs, subject.after_commit_procs(callback_type)
75
+ end
76
+
77
+ should "allow prepending after commit procs" do
78
+ callback_type = VALID_CALLBACK_TYPES.sample
79
+ callbacks_procs = Factory.integer(3).times.map do
80
+ cp = proc{ Factory.string }
81
+ subject.prepend_after_commit_proc(callback_type, &cp)
82
+ cp
83
+ end
84
+
85
+ exp = callbacks_procs.reverse
86
+ assert_equal exp, subject.after_commit_procs(callback_type)
87
+ end
88
+
89
+ should "prepend after commit procs to save by default" do
90
+ callback_proc = proc{ Factory.string }
91
+ subject.prepend_after_commit_proc(&callback_proc)
92
+
93
+ assert_equal 1, subject.after_commit_procs.size
94
+ assert_includes callback_proc, subject.after_commit_procs
95
+ assert_equal 1, subject.after_commit_procs(DEFAULT_CALLBACK_TYPE).size
96
+ assert_includes callback_proc, subject.after_commit_procs(DEFAULT_CALLBACK_TYPE)
97
+ end
98
+
99
+ should "allow getting after commit procs" do
100
+ callbacks_hash = VALID_CALLBACK_TYPES.inject({}) do |h, ct|
101
+ cps = Factory.integer(3).times.map do
102
+ cp = proc{ Factory.string }
103
+ subject.add_after_commit_proc(ct, &cp)
104
+ cp
105
+ end
106
+ h.merge!(ct => cps)
107
+ end
108
+
109
+ callback_type, exp = callbacks_hash.to_a.sample
110
+ assert_equal exp, subject.after_commit_procs(callback_type)
111
+
112
+ callback_types = callbacks_hash.keys.shuffle[0, 2]
113
+ exp = callback_types.map{ |ct| callbacks_hash[ct] }.flatten
114
+ assert_equal exp, subject.after_commit_procs(*callback_types)
115
+ end
116
+
117
+ should "allow clearing after commit procs" do
118
+ VALID_CALLBACK_TYPES.each do |ct|
119
+ Factory.integer(3).times.each do
120
+ subject.add_after_commit_proc(ct){ Factory.string }
121
+ end
122
+ end
123
+
124
+ callback_types = VALID_CALLBACK_TYPES.shuffle[0, 2]
125
+ assert_not_empty subject.after_commit_procs(*callback_types)
126
+ subject.clear_after_commit_procs(*callback_types)
127
+ assert_empty subject.after_commit_procs(*callback_types)
128
+
129
+ assert_not_empty subject.after_commit_procs
130
+ subject.clear_after_commit_procs
131
+ assert_empty subject.after_commit_procs
132
+ end
133
+
134
+ should "provide a helper for calling after commit procs" do
135
+ callback_type = VALID_CALLBACK_TYPES.sample
136
+ callback_called = false
137
+ callback_proc = proc{ callback_called = true }
138
+ subject.add_after_commit_proc(callback_type, &callback_proc)
139
+
140
+ subject.instance_eval{ mr_after_commit_call_procs(callback_type) }
141
+ assert_true callback_called
142
+ assert_equal [], subject.after_commit_procs(callback_type)
143
+ assert_equal [callback_proc], subject.called_after_commit_procs(callback_type)
144
+ end
145
+
146
+ should "allow checking which after commit procs have been called" do
147
+ callback_type = VALID_CALLBACK_TYPES.sample
148
+ callback_proc = proc{ Factory.string }
149
+ subject.add_after_commit_proc(callback_type, &callback_proc)
150
+
151
+ assert_equal [], subject.called_after_commit_procs
152
+ assert_equal [], subject.called_after_commit_procs(callback_type)
153
+
154
+ subject.instance_eval{ mr_after_commit_call_procs(callback_type) }
155
+
156
+ assert_equal [callback_proc], subject.called_after_commit_procs
157
+ assert_equal [callback_proc], subject.called_after_commit_procs(callback_type)
158
+ end
159
+
160
+ should "raise an argument error when using an invalid callback type" do
161
+ assert_raises(ArgumentError) do
162
+ subject.add_after_commit_proc(Factory.string.to_sym){ Factory.string }
163
+ end
164
+ assert_raises(ArgumentError) do
165
+ subject.prepend_after_commit_proc(Factory.string.to_sym){ Factory.string }
166
+ end
167
+ assert_raises(ArgumentError) do
168
+ subject.after_commit_procs(Factory.string.to_sym)
169
+ end
170
+ assert_raises(ArgumentError) do
171
+ subject.called_after_commit_procs(Factory.string.to_sym)
172
+ end
173
+ end
174
+
175
+ end
176
+
177
+ end
@@ -0,0 +1,134 @@
1
+ require 'assert'
2
+ require 'mr/after_commit/record'
3
+
4
+ require 'ardb/record_spy'
5
+ require 'much-plugin'
6
+ require 'mr/record'
7
+
8
+ module MR::AfterCommit::Record
9
+
10
+ class UnitTests < Assert::Context
11
+ desc "MR::AfterCommit::Record"
12
+ subject{ MR::AfterCommit::Record }
13
+
14
+ should "use much-plugin" do
15
+ assert_includes MuchPlugin, subject
16
+ end
17
+
18
+ end
19
+
20
+ class MixinTests < UnitTests
21
+ desc "when mixed in"
22
+ setup do
23
+ @record_class = Ardb::RecordSpy.new do
24
+ include MR::AfterCommit::Record
25
+ end
26
+ end
27
+ subject{ @record_class }
28
+
29
+ should "be an MR record" do
30
+ assert_includes MR::Record, subject
31
+ end
32
+
33
+ should "use the records procs methods mixin" do
34
+ assert_includes MR::AfterCommit::RecordProcsMethods, subject
35
+ end
36
+
37
+ should "add an after commit callback on create" do
38
+ callback = subject.callbacks.find do |c|
39
+ c.type == :after_commit && c.options[:on] == :create
40
+ end
41
+
42
+ assert_not_nil callback
43
+ assert_equal :mr_after_commit_call_procs_for_create, callback.args.first
44
+ end
45
+
46
+ should "add an after commit callback on update" do
47
+ callback = subject.callbacks.find do |c|
48
+ c.type == :after_commit && c.options[:on] == :update
49
+ end
50
+
51
+ assert_not_nil callback
52
+ assert_equal :mr_after_commit_call_procs_for_update, callback.args.first
53
+ end
54
+
55
+ should "add an after commit callback on destroy" do
56
+ callback = subject.callbacks.find do |c|
57
+ c.type == :after_commit && c.options[:on] == :destroy
58
+ end
59
+
60
+ assert_not_nil callback
61
+ assert_equal :mr_after_commit_call_procs_for_destroy, callback.args.first
62
+ end
63
+
64
+ end
65
+
66
+ class InitTests < MixinTests
67
+ desc "and init"
68
+ setup do
69
+ @record = @record_class.new
70
+
71
+ i = 0
72
+ @record.add_after_commit_proc(:save){ @save_call_num = i += 1 }
73
+ @record.add_after_commit_proc(:create){ @create_call_num = i += 1 }
74
+ @record.add_after_commit_proc(:update){ @update_call_num = i += 1 }
75
+
76
+ @destroy_called = false
77
+ @record.add_after_commit_proc(:destroy){ @destroy_called = true }
78
+ end
79
+ subject{ @record }
80
+
81
+ should "call and clear its save and create procs after commit on create" do
82
+ subject.instance_eval{ mr_after_commit_call_procs_for_create }
83
+
84
+ assert_equal 1, @create_call_num
85
+ assert_equal 2, @save_call_num
86
+ assert_nil @update_call_num
87
+ assert_false @destroy_called
88
+
89
+ assert_empty subject.after_commit_procs(:create, :save)
90
+ assert_not_empty subject.after_commit_procs(:update, :destroy)
91
+
92
+ assert_equal 1, subject.called_after_commit_procs(:create).size
93
+ assert_equal 1, subject.called_after_commit_procs(:save).size
94
+ assert_equal 0, subject.called_after_commit_procs(:update).size
95
+ assert_equal 0, subject.called_after_commit_procs(:destroy).size
96
+ end
97
+
98
+ should "call and clear its save and update procs after commit on update" do
99
+ subject.instance_eval{ mr_after_commit_call_procs_for_update }
100
+
101
+ assert_equal 1, @update_call_num
102
+ assert_equal 2, @save_call_num
103
+ assert_nil @create_call_num
104
+ assert_false @destroy_called
105
+
106
+ assert_empty subject.after_commit_procs(:update, :save)
107
+ assert_not_empty subject.after_commit_procs(:create, :destroy)
108
+
109
+ assert_equal 1, subject.called_after_commit_procs(:update).size
110
+ assert_equal 1, subject.called_after_commit_procs(:save).size
111
+ assert_equal 0, subject.called_after_commit_procs(:create).size
112
+ assert_equal 0, subject.called_after_commit_procs(:destroy).size
113
+ end
114
+
115
+ should "call and clear its destroy procs after commit on destroy" do
116
+ subject.instance_eval{ mr_after_commit_call_procs_for_destroy }
117
+
118
+ assert_true @destroy_called
119
+ assert_nil @save_call_num
120
+ assert_nil @create_call_num
121
+ assert_nil @update_call_num
122
+
123
+ assert_empty subject.after_commit_procs(:destroy)
124
+ assert_not_empty subject.after_commit_procs(:create, :update, :save)
125
+
126
+ assert_equal 1, subject.called_after_commit_procs(:destroy).size
127
+ assert_equal 0, subject.called_after_commit_procs(:update).size
128
+ assert_equal 0, subject.called_after_commit_procs(:save).size
129
+ assert_equal 0, subject.called_after_commit_procs(:create).size
130
+ end
131
+
132
+ end
133
+
134
+ end
@@ -0,0 +1,113 @@
1
+ require 'assert'
2
+ require 'mr/after_commit'
3
+
4
+ require 'much-plugin'
5
+ require 'mr/after_commit/fake_record'
6
+ require 'mr/after_commit/record_procs_methods'
7
+ require 'mr/model'
8
+
9
+ module MR::AfterCommit
10
+
11
+ class UnitTests < Assert::Context
12
+ desc "MR::AfterCommit"
13
+ subject{ MR::AfterCommit }
14
+
15
+ should "use much-plugin" do
16
+ assert_includes MuchPlugin, subject
17
+ end
18
+
19
+ should "know its valid callback types" do
20
+ exp = RecordProcsMethods::VALID_CALLBACK_TYPES
21
+ assert_equal exp, VALID_CALLBACK_TYPES
22
+ end
23
+
24
+ should "know its default callback type" do
25
+ exp = RecordProcsMethods::DEFAULT_CALLBACK_TYPE
26
+ assert_equal exp, DEFAULT_CALLBACK_TYPE
27
+ end
28
+
29
+ end
30
+
31
+ class MixinTests < UnitTests
32
+ desc "when mixed in"
33
+ setup do
34
+ @model_class = Class.new do
35
+ include MR::AfterCommit
36
+ end
37
+ end
38
+ subject{ @model_class }
39
+
40
+ should "be an MR model" do
41
+ assert_includes MR::Model, subject
42
+ end
43
+
44
+ end
45
+
46
+ class InitTests < MixinTests
47
+ desc "and init"
48
+ setup do
49
+ @fake_record_class = Class.new do
50
+ include MR::AfterCommit::FakeRecord
51
+ end
52
+ @fake_record = @fake_record_class.new
53
+
54
+ @model = @model_class.new(@fake_record)
55
+ end
56
+ subject{ @model }
57
+
58
+ should have_imeths :after_commit_procs, :after_commit, :prepend_after_commit
59
+ should have_imeths :clear_after_commit_procs, :called_after_commit_procs
60
+
61
+ should "demeter it's record" do
62
+ callback_type, callback_proc = add_after_commit_proc
63
+
64
+ assert_includes callback_proc, subject.after_commit_procs(callback_type)
65
+ exp = @fake_record.after_commit_procs(callback_type)
66
+ assert_equal exp, subject.after_commit_procs(callback_type)
67
+
68
+ subject.clear_after_commit_procs(callback_type)
69
+ assert_equal [], subject.after_commit_procs(callback_type)
70
+
71
+ callback_procs = Factory.integer(3).times.map do
72
+ callback_type, callback_proc = add_after_commit_proc
73
+ callback_proc
74
+ end
75
+
76
+ callback_procs.each do |callback_proc|
77
+ assert_includes callback_proc, subject.after_commit_procs
78
+ end
79
+ assert_equal @fake_record.after_commit_procs, subject.after_commit_procs
80
+
81
+ subject.clear_after_commit_procs
82
+ assert_equal [], subject.after_commit_procs
83
+
84
+ callback_procs = Factory.integer(3).times.map do
85
+ callback_type, callback_proc = add_after_commit_proc
86
+ callback_proc
87
+ end
88
+
89
+ assert_equal [], subject.called_after_commit_procs
90
+ subject.save # create
91
+ subject.save # update
92
+ subject.destroy
93
+ callback_procs.each do |callback_proc|
94
+ assert_includes callback_proc, subject.called_after_commit_procs
95
+ end
96
+ end
97
+
98
+ private
99
+
100
+ def add_after_commit_proc
101
+ callback_type = VALID_CALLBACK_TYPES.sample
102
+ callback_proc = proc{ Factory.string }
103
+ if Factory.boolean
104
+ subject.after_commit(callback_type, &callback_proc)
105
+ else
106
+ subject.prepend_after_commit(callback_type, &callback_proc)
107
+ end
108
+ [callback_type, callback_proc]
109
+ end
110
+
111
+ end
112
+
113
+ end