shoulda-matchers 4.0.0.rc1 → 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (209) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +72 -40
  4. data/lib/shoulda/matchers/action_controller.rb +2 -0
  5. data/lib/shoulda/matchers/active_model.rb +2 -3
  6. data/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb +1 -0
  7. data/lib/shoulda/matchers/active_record.rb +2 -0
  8. data/lib/shoulda/matchers/active_record/association_matcher.rb +34 -0
  9. data/lib/shoulda/matchers/active_record/association_matchers/optional_matcher.rb +27 -4
  10. data/lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb +27 -4
  11. data/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb +1 -1
  12. data/lib/shoulda/matchers/independent.rb +2 -1
  13. data/lib/shoulda/matchers/rails_shim.rb +5 -9
  14. data/lib/shoulda/matchers/version.rb +1 -1
  15. data/lib/shoulda/matchers/warn.rb +1 -0
  16. data/shoulda-matchers.gemspec +11 -3
  17. metadata +13 -340
  18. data/.gitignore +0 -12
  19. data/.hound.yml +0 -3
  20. data/.hound/ruby.yml +0 -1062
  21. data/.python-version +0 -1
  22. data/.rubocop.yml +0 -15
  23. data/.travis.yml +0 -21
  24. data/.yardopts +0 -10
  25. data/Appraisals +0 -105
  26. data/CONTRIBUTING.md +0 -172
  27. data/Gemfile +0 -15
  28. data/Gemfile.lock +0 -59
  29. data/MAINTAINING.md +0 -250
  30. data/NEWS.md +0 -1235
  31. data/Rakefile +0 -46
  32. data/bin/setup +0 -190
  33. data/custom_plan.rb +0 -104
  34. data/doc_config/gh-pages/index.html.erb +0 -9
  35. data/doc_config/yard/setup.rb +0 -22
  36. data/doc_config/yard/templates/default/fulldoc/html/css/bootstrap.css +0 -5967
  37. data/doc_config/yard/templates/default/fulldoc/html/css/full_list.css +0 -12
  38. data/doc_config/yard/templates/default/fulldoc/html/css/global.css +0 -66
  39. data/doc_config/yard/templates/default/fulldoc/html/css/solarized.css +0 -69
  40. data/doc_config/yard/templates/default/fulldoc/html/css/style.css +0 -312
  41. data/doc_config/yard/templates/default/fulldoc/html/full_list.erb +0 -26
  42. data/doc_config/yard/templates/default/fulldoc/html/full_list_class.erb +0 -1
  43. data/doc_config/yard/templates/default/fulldoc/html/full_list_method.erb +0 -8
  44. data/doc_config/yard/templates/default/fulldoc/html/js/app.js +0 -281
  45. data/doc_config/yard/templates/default/fulldoc/html/js/full_list.js +0 -1
  46. data/doc_config/yard/templates/default/fulldoc/html/js/jquery.stickyheaders.js +0 -289
  47. data/doc_config/yard/templates/default/fulldoc/html/js/underscore.min.js +0 -6
  48. data/doc_config/yard/templates/default/fulldoc/html/setup.rb +0 -35
  49. data/doc_config/yard/templates/default/layout/html/breadcrumb.erb +0 -14
  50. data/doc_config/yard/templates/default/layout/html/fonts.erb +0 -1
  51. data/doc_config/yard/templates/default/layout/html/footer.erb +0 -6
  52. data/doc_config/yard/templates/default/layout/html/layout.erb +0 -23
  53. data/doc_config/yard/templates/default/layout/html/search.erb +0 -13
  54. data/doc_config/yard/templates/default/layout/html/setup.rb +0 -40
  55. data/doc_config/yard/templates/default/method_details/html/source.erb +0 -10
  56. data/doc_config/yard/templates/default/module/html/box_info.erb +0 -31
  57. data/gemfiles/4.2.gemfile +0 -39
  58. data/gemfiles/4.2.gemfile.lock +0 -246
  59. data/gemfiles/5.0.gemfile +0 -37
  60. data/gemfiles/5.0.gemfile.lock +0 -238
  61. data/gemfiles/5.1.gemfile +0 -38
  62. data/gemfiles/5.1.gemfile.lock +0 -254
  63. data/gemfiles/5.2.gemfile +0 -40
  64. data/gemfiles/5.2.gemfile.lock +0 -273
  65. data/script/install_gems_in_all_appraisals +0 -16
  66. data/script/run_all_tests +0 -16
  67. data/script/supported_ruby_versions +0 -7
  68. data/script/update_gem_in_all_appraisals +0 -17
  69. data/script/update_gems_in_all_appraisals +0 -16
  70. data/spec/acceptance/active_model_integration_spec.rb +0 -23
  71. data/spec/acceptance/independent_matchers_spec.rb +0 -125
  72. data/spec/acceptance/multiple_libraries_integration_spec.rb +0 -55
  73. data/spec/acceptance/rails_integration_spec.rb +0 -156
  74. data/spec/acceptance_spec_helper.rb +0 -23
  75. data/spec/doublespeak_spec_helper.rb +0 -2
  76. data/spec/report_warnings.rb +0 -7
  77. data/spec/spec_helper.rb +0 -20
  78. data/spec/support/acceptance/adds_shoulda_matchers_to_project.rb +0 -133
  79. data/spec/support/acceptance/helpers.rb +0 -33
  80. data/spec/support/acceptance/helpers/active_model_helpers.rb +0 -11
  81. data/spec/support/acceptance/helpers/array_helpers.rb +0 -13
  82. data/spec/support/acceptance/helpers/base_helpers.rb +0 -19
  83. data/spec/support/acceptance/helpers/command_helpers.rb +0 -68
  84. data/spec/support/acceptance/helpers/file_helpers.rb +0 -19
  85. data/spec/support/acceptance/helpers/gem_helpers.rb +0 -31
  86. data/spec/support/acceptance/helpers/minitest_helpers.rb +0 -11
  87. data/spec/support/acceptance/helpers/n_unit_helpers.rb +0 -25
  88. data/spec/support/acceptance/helpers/pluralization_helpers.rb +0 -13
  89. data/spec/support/acceptance/helpers/rails_migration_helpers.rb +0 -21
  90. data/spec/support/acceptance/helpers/rails_version_helpers.rb +0 -11
  91. data/spec/support/acceptance/helpers/rspec_helpers.rb +0 -24
  92. data/spec/support/acceptance/helpers/ruby_version_helpers.rb +0 -9
  93. data/spec/support/acceptance/helpers/step_helpers.rb +0 -127
  94. data/spec/support/acceptance/matchers/have_output.rb +0 -31
  95. data/spec/support/acceptance/matchers/indicate_number_of_tests_was_run_matcher.rb +0 -55
  96. data/spec/support/acceptance/matchers/indicate_that_tests_were_run_matcher.rb +0 -103
  97. data/spec/support/tests/bundle.rb +0 -94
  98. data/spec/support/tests/command_runner.rb +0 -230
  99. data/spec/support/tests/current_bundle.rb +0 -55
  100. data/spec/support/tests/database.rb +0 -28
  101. data/spec/support/tests/database_adapters/postgresql.rb +0 -25
  102. data/spec/support/tests/database_adapters/sqlite3.rb +0 -26
  103. data/spec/support/tests/database_configuration.rb +0 -33
  104. data/spec/support/tests/database_configuration_registry.rb +0 -28
  105. data/spec/support/tests/filesystem.rb +0 -100
  106. data/spec/support/tests/version.rb +0 -45
  107. data/spec/support/unit/active_record/create_table.rb +0 -54
  108. data/spec/support/unit/attribute.rb +0 -45
  109. data/spec/support/unit/capture.rb +0 -46
  110. data/spec/support/unit/change_value.rb +0 -111
  111. data/spec/support/unit/create_model_arguments/basic.rb +0 -135
  112. data/spec/support/unit/create_model_arguments/has_many.rb +0 -15
  113. data/spec/support/unit/create_model_arguments/uniqueness_matcher.rb +0 -74
  114. data/spec/support/unit/helpers/action_pack_versions.rb +0 -22
  115. data/spec/support/unit/helpers/active_model_helpers.rb +0 -27
  116. data/spec/support/unit/helpers/active_model_versions.rb +0 -32
  117. data/spec/support/unit/helpers/active_record_versions.rb +0 -44
  118. data/spec/support/unit/helpers/active_resource_builder.rb +0 -27
  119. data/spec/support/unit/helpers/allow_value_matcher_helpers.rb +0 -15
  120. data/spec/support/unit/helpers/class_builder.rb +0 -90
  121. data/spec/support/unit/helpers/column_type_helpers.rb +0 -26
  122. data/spec/support/unit/helpers/confirmation_matcher_helpers.rb +0 -17
  123. data/spec/support/unit/helpers/controller_builder.rb +0 -63
  124. data/spec/support/unit/helpers/database_helpers.rb +0 -20
  125. data/spec/support/unit/helpers/i18n_faker.rb +0 -15
  126. data/spec/support/unit/helpers/mailer_builder.rb +0 -12
  127. data/spec/support/unit/helpers/message_helpers.rb +0 -19
  128. data/spec/support/unit/helpers/model_builder.rb +0 -114
  129. data/spec/support/unit/helpers/rails_versions.rb +0 -42
  130. data/spec/support/unit/helpers/validation_matcher_scenario_helpers.rb +0 -44
  131. data/spec/support/unit/i18n.rb +0 -7
  132. data/spec/support/unit/load_environment.rb +0 -12
  133. data/spec/support/unit/matchers/deprecate.rb +0 -60
  134. data/spec/support/unit/matchers/fail_with_message_including_matcher.rb +0 -51
  135. data/spec/support/unit/matchers/fail_with_message_matcher.rb +0 -64
  136. data/spec/support/unit/matchers/print_warning_including.rb +0 -67
  137. data/spec/support/unit/model_creation_strategies/active_model.rb +0 -111
  138. data/spec/support/unit/model_creation_strategies/active_record.rb +0 -77
  139. data/spec/support/unit/model_creators.rb +0 -19
  140. data/spec/support/unit/model_creators/active_model.rb +0 -39
  141. data/spec/support/unit/model_creators/active_record.rb +0 -42
  142. data/spec/support/unit/model_creators/active_record/has_and_belongs_to_many.rb +0 -95
  143. data/spec/support/unit/model_creators/active_record/has_many.rb +0 -67
  144. data/spec/support/unit/model_creators/active_record/uniqueness_matcher.rb +0 -42
  145. data/spec/support/unit/model_creators/basic.rb +0 -102
  146. data/spec/support/unit/rails_application.rb +0 -151
  147. data/spec/support/unit/record_builder_with_i18n_validation_message.rb +0 -69
  148. data/spec/support/unit/record_validating_confirmation_builder.rb +0 -54
  149. data/spec/support/unit/record_with_different_error_attribute_builder.rb +0 -92
  150. data/spec/support/unit/shared_examples/ignoring_interference_by_writer.rb +0 -79
  151. data/spec/support/unit/shared_examples/numerical_submatcher.rb +0 -17
  152. data/spec/support/unit/shared_examples/set_session_or_flash.rb +0 -360
  153. data/spec/support/unit/validation_matcher_scenario.rb +0 -60
  154. data/spec/unit/shoulda/matchers/action_controller/callback_matcher_spec.rb +0 -82
  155. data/spec/unit/shoulda/matchers/action_controller/filter_param_matcher_spec.rb +0 -28
  156. data/spec/unit/shoulda/matchers/action_controller/permit_matcher_spec.rb +0 -629
  157. data/spec/unit/shoulda/matchers/action_controller/redirect_to_matcher_spec.rb +0 -42
  158. data/spec/unit/shoulda/matchers/action_controller/render_template_matcher_spec.rb +0 -76
  159. data/spec/unit/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb +0 -62
  160. data/spec/unit/shoulda/matchers/action_controller/rescue_from_matcher_spec.rb +0 -90
  161. data/spec/unit/shoulda/matchers/action_controller/respond_with_matcher_spec.rb +0 -31
  162. data/spec/unit/shoulda/matchers/action_controller/route_matcher_spec.rb +0 -330
  163. data/spec/unit/shoulda/matchers/action_controller/route_params_spec.rb +0 -30
  164. data/spec/unit/shoulda/matchers/action_controller/set_flash_matcher_spec.rb +0 -67
  165. data/spec/unit/shoulda/matchers/action_controller/set_session_matcher_spec.rb +0 -17
  166. data/spec/unit/shoulda/matchers/action_controller/set_session_or_flash_matcher_spec.rb +0 -562
  167. data/spec/unit/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb +0 -117
  168. data/spec/unit/shoulda/matchers/active_model/allow_value_matcher_spec.rb +0 -829
  169. data/spec/unit/shoulda/matchers/active_model/disallow_value_matcher_spec.rb +0 -86
  170. data/spec/unit/shoulda/matchers/active_model/have_secure_password_matcher_spec.rb +0 -20
  171. data/spec/unit/shoulda/matchers/active_model/helpers_spec.rb +0 -162
  172. data/spec/unit/shoulda/matchers/active_model/validate_absence_of_matcher_spec.rb +0 -290
  173. data/spec/unit/shoulda/matchers/active_model/validate_acceptance_of_matcher_spec.rb +0 -109
  174. data/spec/unit/shoulda/matchers/active_model/validate_confirmation_of_matcher_spec.rb +0 -172
  175. data/spec/unit/shoulda/matchers/active_model/validate_exclusion_of_matcher_spec.rb +0 -264
  176. data/spec/unit/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb +0 -1049
  177. data/spec/unit/shoulda/matchers/active_model/validate_length_of_matcher_spec.rb +0 -335
  178. data/spec/unit/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +0 -1865
  179. data/spec/unit/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb +0 -406
  180. data/spec/unit/shoulda/matchers/active_record/accept_nested_attributes_for_matcher_spec.rb +0 -107
  181. data/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb +0 -1672
  182. data/spec/unit/shoulda/matchers/active_record/association_matchers/model_reflection_spec.rb +0 -251
  183. data/spec/unit/shoulda/matchers/active_record/define_enum_for_matcher_spec.rb +0 -690
  184. data/spec/unit/shoulda/matchers/active_record/have_db_column_matcher_spec.rb +0 -111
  185. data/spec/unit/shoulda/matchers/active_record/have_db_index_matcher_spec.rb +0 -85
  186. data/spec/unit/shoulda/matchers/active_record/have_readonly_attributes_matcher_spec.rb +0 -41
  187. data/spec/unit/shoulda/matchers/active_record/have_secure_token_matcher_spec.rb +0 -169
  188. data/spec/unit/shoulda/matchers/active_record/serialize_matcher_spec.rb +0 -86
  189. data/spec/unit/shoulda/matchers/active_record/validate_uniqueness_of_matcher_spec.rb +0 -1682
  190. data/spec/unit/shoulda/matchers/doublespeak/double_collection_spec.rb +0 -190
  191. data/spec/unit/shoulda/matchers/doublespeak/double_implementation_registry_spec.rb +0 -21
  192. data/spec/unit/shoulda/matchers/doublespeak/double_spec.rb +0 -271
  193. data/spec/unit/shoulda/matchers/doublespeak/object_double_spec.rb +0 -77
  194. data/spec/unit/shoulda/matchers/doublespeak/proxy_implementation_spec.rb +0 -72
  195. data/spec/unit/shoulda/matchers/doublespeak/stub_implementation_spec.rb +0 -101
  196. data/spec/unit/shoulda/matchers/doublespeak/world_spec.rb +0 -78
  197. data/spec/unit/shoulda/matchers/doublespeak_spec.rb +0 -27
  198. data/spec/unit/shoulda/matchers/independent/delegate_method_matcher/stubbed_target_spec.rb +0 -43
  199. data/spec/unit/shoulda/matchers/independent/delegate_method_matcher_spec.rb +0 -650
  200. data/spec/unit/shoulda/matchers/routing/route_matcher_spec.rb +0 -406
  201. data/spec/unit/shoulda/matchers/util/word_wrap_spec.rb +0 -252
  202. data/spec/unit_spec_helper.rb +0 -54
  203. data/spec/warnings_spy.rb +0 -64
  204. data/spec/warnings_spy/filesystem.rb +0 -45
  205. data/spec/warnings_spy/partitioner.rb +0 -36
  206. data/spec/warnings_spy/reader.rb +0 -53
  207. data/spec/warnings_spy/reporter.rb +0 -88
  208. data/tasks/documentation.rb +0 -199
  209. data/zeus.json +0 -11
@@ -1,251 +0,0 @@
1
- require 'unit_spec_helper'
2
-
3
- describe Shoulda::Matchers::ActiveRecord::AssociationMatchers::ModelReflection do
4
- it 'delegates other methods to the given Reflection object' do
5
- define_model(:country)
6
- person_model = define_model(:person, country_id: :integer) do
7
- belongs_to :country
8
- end
9
- delegate_reflection = person_model.reflect_on_association(:country)
10
- allow(delegate_reflection).to receive(:foo).and_return('bar')
11
- reflection = described_class.new(delegate_reflection)
12
-
13
- expect(reflection.foo).to eq 'bar'
14
- end
15
-
16
- describe '#associated_class' do
17
- it 'returns the model that the association refers to' do
18
- define_model(:country)
19
- person_model = define_model(:person, country_id: :integer) do
20
- belongs_to :country
21
- end
22
- delegate_reflection = person_model.reflect_on_association(:country)
23
- reflection = described_class.new(delegate_reflection)
24
-
25
- expect(reflection.associated_class).to be Country
26
- end
27
- end
28
-
29
- describe '#through?' do
30
- it 'returns true if the reflection is for a has_many :through association' do
31
- define_model(:city, person_id: :integer)
32
- define_model(:person, country_id: :integer) do
33
- has_many :cities
34
- end
35
- country_model = define_model(:country) do
36
- has_many :people
37
- has_many :cities, through: :people
38
- end
39
- delegate_reflection = country_model.reflect_on_association(:cities)
40
- reflection = described_class.new(delegate_reflection)
41
-
42
- expect(reflection).to be_through
43
- end
44
-
45
- it 'returns false if not' do
46
- define_model(:person, country_id: :integer)
47
- country_model = define_model(:country) do
48
- has_many :people
49
- end
50
- delegate_reflection = country_model.reflect_on_association(:people)
51
- reflection = described_class.new(delegate_reflection)
52
-
53
- expect(reflection).not_to be_through
54
- end
55
- end
56
-
57
- describe '#join_table_name' do
58
- context 'when the association was defined with a :join_table option' do
59
- it 'returns the value of the option' do
60
- create_table :foos, id: false do |t|
61
- t.integer :person_id
62
- t.integer :country_id
63
- end
64
- define_model(:person, country_id: :integer)
65
- country_model = define_model(:country) do
66
- has_and_belongs_to_many :people, join_table: 'foos'
67
- end
68
- delegate_reflection = country_model.reflect_on_association(:people)
69
- reflection = described_class.new(delegate_reflection)
70
-
71
- expect(reflection.join_table_name).to eq 'foos'
72
- end
73
- end
74
-
75
- context 'when the association was not defined with :join_table' do
76
- it 'returns the default join_table that ActiveRecord generates' do
77
- define_model(:person, country_id: :integer)
78
- country_model = define_model(:country) do
79
- has_and_belongs_to_many :people
80
- end
81
- delegate_reflection = country_model.reflect_on_association(:people)
82
- reflection = described_class.new(delegate_reflection)
83
-
84
- expect(reflection.join_table_name).to eq 'countries_people'
85
- end
86
- end
87
- end
88
-
89
- describe '#association_relation' do
90
- if rails_4_x?
91
- context 'when the reflection object has a #scope method' do
92
- context 'when the scope is a block' do
93
- it 'executes the block in the context of an empty scope' do
94
- define_model(:country, mood: :string)
95
- person_model = define_model(:person, country_id: :integer) do
96
- belongs_to :country, -> { where(mood: 'nice') }
97
- end
98
- delegate_reflection = person_model.reflect_on_association(:country)
99
- reflection = described_class.new(delegate_reflection)
100
-
101
- actual_sql = reflection.association_relation.to_sql
102
- expected_sql = Country.where(mood: 'nice').to_sql
103
- expect(actual_sql).to eq expected_sql
104
- end
105
- end
106
-
107
- context 'when the scope is nil' do
108
- it 'returns an empty scope' do
109
- define_model(:country)
110
- person_model = define_model(:person, country_id: :integer) do
111
- belongs_to :country
112
- end
113
- delegate_reflection = person_model.reflect_on_association(:country)
114
- reflection = described_class.new(delegate_reflection)
115
-
116
- actual_sql = reflection.association_relation.to_sql
117
- expected_sql = Country.all.to_sql
118
- expect(actual_sql).to eq expected_sql
119
- end
120
- end
121
- end
122
- end
123
-
124
- if rails_3_x?
125
- context 'when the reflection object does not have a #scope method' do
126
- context 'when the reflection options contain :conditions' do
127
- it 'creates an ActiveRecord::Relation from the conditions' do
128
- define_model(:country, mood: :string)
129
- person_model = define_model(:person, country_id: :integer) do
130
- belongs_to :country, conditions: { mood: 'nice' }
131
- end
132
- delegate_reflection = person_model.reflect_on_association(:country)
133
- reflection = described_class.new(delegate_reflection)
134
-
135
- actual_sql = reflection.association_relation.to_sql
136
- expected_sql = Country.where(mood: 'nice').to_sql
137
- expect(actual_sql).to eq expected_sql
138
- end
139
- end
140
-
141
- context 'when the reflection options contain :order' do
142
- it 'creates an ActiveRecord::Relation from the order' do
143
- define_model(:person, country_id: :integer, age: :integer)
144
- country_model = define_model(:country) do
145
- has_many :people, order: 'age'
146
- end
147
- delegate_reflection = country_model.reflect_on_association(:people)
148
- reflection = described_class.new(delegate_reflection)
149
-
150
- actual_sql = reflection.association_relation.to_sql
151
- expected_sql = Person.order('age').to_sql
152
- expect(actual_sql).to eq expected_sql
153
- end
154
- end
155
-
156
- context 'when the reflection options contain :include' do
157
- it 'creates an ActiveRecord::Relation from the include' do
158
- define_model(:city, country_id: :integer)
159
- define_model(:country) do
160
- has_many :cities
161
- end
162
- person_model = define_model(:person, country_id: :integer) do
163
- belongs_to :country, include: :cities
164
- end
165
- delegate_reflection = person_model.reflect_on_association(:country)
166
- reflection = described_class.new(delegate_reflection)
167
-
168
- actual_includes = reflection.association_relation.includes_values
169
- expected_includes = Country.includes(:cities).includes_values
170
- expect(actual_includes).to eq expected_includes
171
- end
172
- end
173
-
174
- context 'when the reflection options contain :group' do
175
- it 'creates an ActiveRecord::Relation from the group' do
176
- country_model = define_model(:country, mood: :string) do
177
- has_many :people, group: 'age'
178
- end
179
- define_model(:person, country_id: :integer, age: :integer)
180
- delegate_reflection = country_model.reflect_on_association(:people)
181
- reflection = described_class.new(delegate_reflection)
182
-
183
- actual_sql = reflection.association_relation.to_sql
184
- expected_sql = Person.group('age').to_sql
185
- expect(actual_sql).to eq expected_sql
186
- end
187
- end
188
-
189
- context 'when the reflection options contain :having' do
190
- it 'creates an ActiveRecord::Relation from the having' do
191
- country_model = define_model(:country) do
192
- has_many :people, having: 'country_id > 1'
193
- end
194
- define_model(:person, country_id: :integer)
195
- delegate_reflection = country_model.reflect_on_association(:people)
196
- reflection = described_class.new(delegate_reflection)
197
-
198
- actual_sql = reflection.association_relation.to_sql
199
- expected_sql = Person.having('country_id > 1').to_sql
200
- expect(actual_sql).to eq expected_sql
201
- end
202
- end
203
-
204
- context 'when the reflection options contain :limit' do
205
- it 'creates an ActiveRecord::Relation from the limit' do
206
- country_model = define_model(:country) do
207
- has_many :people, limit: 10
208
- end
209
- define_model(:person, country_id: :integer)
210
- delegate_reflection = country_model.reflect_on_association(:people)
211
- reflection = described_class.new(delegate_reflection)
212
-
213
- actual_sql = reflection.association_relation.to_sql
214
- expected_sql = Person.limit(10).to_sql
215
- expect(actual_sql).to eq expected_sql
216
- end
217
- end
218
-
219
- context 'when the reflection options contain :offset' do
220
- it 'creates an ActiveRecord::Relation from the offset' do
221
- country_model = define_model(:country) do
222
- has_many :people, offset: 5
223
- end
224
- define_model(:person, country_id: :integer)
225
- delegate_reflection = country_model.reflect_on_association(:people)
226
- reflection = described_class.new(delegate_reflection)
227
-
228
- actual_sql = reflection.association_relation.to_sql
229
- expected_sql = Person.offset(5).to_sql
230
- expect(actual_sql).to eq expected_sql
231
- end
232
- end
233
-
234
- context 'when the reflection options contain :select' do
235
- it 'creates an ActiveRecord::Relation from the select' do
236
- country_model = define_model(:country) do
237
- has_many :people, select: 'age'
238
- end
239
- define_model(:person, country_id: :integer, age: :integer)
240
- delegate_reflection = country_model.reflect_on_association(:people)
241
- reflection = described_class.new(delegate_reflection)
242
-
243
- actual_sql = reflection.association_relation.to_sql
244
- expected_sql = Person.select('age').to_sql
245
- expect(actual_sql).to eq expected_sql
246
- end
247
- end
248
- end
249
- end
250
- end
251
- end
@@ -1,690 +0,0 @@
1
- require 'unit_spec_helper'
2
-
3
- describe Shoulda::Matchers::ActiveRecord::DefineEnumForMatcher, type: :model do
4
- context 'if the attribute is given in plural form accidentally' do
5
- it 'rejects with an appropriate failure message' do
6
- record = build_record_with_array_values(
7
- model_name: 'Example',
8
- attribute_name: :attr,
9
- column_type: :integer,
10
- )
11
- message = format_message(<<-MESSAGE)
12
- Expected Example to define :attrs as an enum, backed by an integer.
13
- However, no such enum exists in Example.
14
- MESSAGE
15
-
16
- assertion = lambda do
17
- expect(record).to define_enum_for(:attrs)
18
- end
19
-
20
- expect(&assertion).to fail_with_message(message)
21
- end
22
- end
23
-
24
- context 'if a method to hold enum values exists on the model but was not created via the enum macro' do
25
- it 'rejects with an appropriate failure message' do
26
- model = define_model 'Example' do
27
- def self.statuses; end
28
- end
29
-
30
- message = format_message(<<-MESSAGE)
31
- Expected Example to define :attr as an enum, backed by an integer.
32
- However, no such enum exists in Example.
33
- MESSAGE
34
-
35
- assertion = lambda do
36
- expect(model.new).to define_enum_for(:attr)
37
- end
38
-
39
- expect(&assertion).to fail_with_message(message)
40
- end
41
- end
42
-
43
- describe 'with only the attribute name specified' do
44
- context 'if the attribute is not defined as an enum' do
45
- it 'rejects with an appropriate failure message' do
46
- record = build_record_with_non_enum_attribute(
47
- model_name: 'Example',
48
- attribute_name: :attr,
49
- )
50
- message = format_message(<<-MESSAGE)
51
- Expected Example to define :attr as an enum, backed by an integer.
52
- However, no such enum exists in Example.
53
- MESSAGE
54
-
55
- assertion = lambda do
56
- expect(record).to define_enum_for(:attr)
57
- end
58
-
59
- expect(&assertion).to fail_with_message(message)
60
- end
61
- end
62
-
63
- context 'if the column storing the attribute is not an integer type' do
64
- it 'rejects with an appropriate failure message' do
65
- record = build_record_with_array_values(
66
- model_name: 'Example',
67
- attribute_name: :attr,
68
- column_type: :string,
69
- )
70
- message = format_message(<<-MESSAGE)
71
- Expected Example to define :attr as an enum, backed by an integer.
72
- However, :attr is a string column.
73
- MESSAGE
74
-
75
- assertion = lambda do
76
- expect(record).to define_enum_for(:attr)
77
- end
78
-
79
- expect(&assertion).to fail_with_message(message)
80
- end
81
- end
82
-
83
- context 'if the attribute is defined as an enum' do
84
- it 'accepts' do
85
- record = build_record_with_array_values(attribute_name: :attr)
86
-
87
- expect(record).to define_enum_for(:attr)
88
- end
89
-
90
- context 'and the matcher is negated' do
91
- it 'rejects with an appropriate failure message' do
92
- record = build_record_with_array_values(
93
- model_name: 'Example',
94
- attribute_name: :attr,
95
- column_type: :integer,
96
- )
97
- message = format_message(<<-MESSAGE)
98
- Expected Example not to define :attr as an enum, backed by an integer,
99
- but it did.
100
- MESSAGE
101
-
102
- assertion = lambda do
103
- expect(record).not_to define_enum_for(:attr)
104
- end
105
-
106
- expect(&assertion).to fail_with_message(message)
107
- end
108
- end
109
- end
110
- end
111
-
112
- describe 'with both attribute name and enum values specified' do
113
- context 'when the actual enum values are an array' do
114
- context 'if the attribute is not defined as an enum' do
115
- it 'rejects with an appropriate failure message' do
116
- record = build_record_with_non_enum_attribute(
117
- model_name: 'Example',
118
- attribute_name: :attr,
119
- )
120
- message = format_message(<<-MESSAGE)
121
- Expected Example to define :attr as an enum, backed by an integer,
122
- with possible values ‹["open", "close"]›. However, no such enum
123
- exists in Example.
124
- MESSAGE
125
-
126
- assertion = lambda do
127
- expect(record).
128
- to define_enum_for(:attr).
129
- with_values(['open', 'close'])
130
- end
131
-
132
- expect(&assertion).to fail_with_message(message)
133
- end
134
- end
135
-
136
- context 'if the attribute is defined as an enum' do
137
- context 'but the enum values do not match' do
138
- it 'rejects with an appropriate failure message' do
139
- record = build_record_with_array_values(
140
- model_name: 'Example',
141
- attribute_name: :attr,
142
- values: ['published', 'unpublished', 'draft'],
143
- )
144
- message = format_message(<<-MESSAGE)
145
- Expected Example to define :attr as an enum, backed by an integer,
146
- with possible values ‹["open", "close"]›. However, the actual
147
- enum values for :attr are ‹["published", "unpublished", "draft"]›.
148
- MESSAGE
149
-
150
- assertion = lambda do
151
- expect(record).
152
- to define_enum_for(:attr).
153
- with_values(['open', 'close'])
154
- end
155
-
156
- expect(&assertion).to fail_with_message(message)
157
- end
158
- end
159
-
160
- context 'and the enum values match' do
161
- it 'accepts' do
162
- record = build_record_with_array_values(
163
- attribute_name: :attr,
164
- values: ['published', 'unpublished', 'draft'],
165
- )
166
-
167
- expect(record).to define_enum_for(:attr).
168
- with_values(['published', 'unpublished', 'draft'])
169
- end
170
- end
171
- end
172
- end
173
-
174
- context 'when the actual enum values are a hash' do
175
- context 'if the attribute is not defined as an enum' do
176
- it 'rejects with an appropriate failure message' do
177
- record = build_record_with_non_enum_attribute(
178
- model_name: 'Example',
179
- attribute_name: :attr,
180
- )
181
- message = format_message(<<-MESSAGE)
182
- Expected Example to define :attr as an enum, backed by an integer,
183
- with possible values ‹{active: 5, archived: 10}›. However, no such
184
- enum exists in Example.
185
- MESSAGE
186
-
187
- assertion = lambda do
188
- expect(record).
189
- to define_enum_for(:attr).
190
- with_values(active: 5, archived: 10)
191
- end
192
-
193
- expect(&assertion).to fail_with_message(message)
194
- end
195
- end
196
-
197
- context 'if the attribute is defined as an enum' do
198
- context 'but the enum values do not match' do
199
- it 'rejects with an appropriate failure message' do
200
- record = build_record_with_hash_values(
201
- model_name: 'Example',
202
- attribute_name: :attr,
203
- values: { active: 0, archived: 1 },
204
- )
205
- message = format_message(<<-MESSAGE)
206
- Expected Example to define :attr as an enum, backed by an integer,
207
- with possible values ‹{active: 5, archived: 10}›. However, the
208
- actual enum values for :attr are ‹{active: 0, archived: 1}›.
209
- MESSAGE
210
-
211
- assertion = lambda do
212
- expect(record).
213
- to define_enum_for(:attr).
214
- with_values(active: 5, archived: 10)
215
- end
216
-
217
- expect(&assertion).to fail_with_message(message)
218
- end
219
- end
220
-
221
- context 'and the enum values match' do
222
- context 'when expected enum values are a hash' do
223
- it 'accepts' do
224
- record = build_record_with_hash_values(
225
- attribute_name: :attr,
226
- values: { active: 0, archived: 1 },
227
- )
228
-
229
- expect(record).
230
- to define_enum_for(:attr).
231
- with_values(active: 0, archived: 1)
232
- end
233
- end
234
-
235
- context 'when expected enum values are an array' do
236
- it 'accepts' do
237
- record = build_record_with_hash_values(
238
- attribute_name: :attr,
239
- values: { active: 0, archived: 1 },
240
- )
241
-
242
- expect(record).
243
- to define_enum_for(:attr).
244
- with_values(['active', 'archived'])
245
- end
246
- end
247
- end
248
- end
249
- end
250
- end
251
-
252
- context 'with values specified using #with' do
253
- it 'produces a warning' do
254
- record = build_record_with_array_values(
255
- attribute_name: :attr,
256
- values: [:foo, :bar],
257
- )
258
-
259
- assertion = lambda do
260
- expect(record).to define_enum_for(:attr).with([:foo, :bar])
261
- end
262
-
263
- expect(&assertion).to deprecate(
264
- 'The `with` qualifier on `define_enum_for`',
265
- '`with_values`',
266
- )
267
- end
268
- end
269
-
270
- describe 'with the backing column specified to be of some type' do
271
- context 'if the column storing the attribute is of a different type' do
272
- it 'rejects with an appropriate failure message' do
273
- record = build_record_with_array_values(
274
- model_name: 'Example',
275
- attribute_name: :attr,
276
- column_type: :integer,
277
- )
278
- message = format_message(<<-MESSAGE)
279
- Expected Example to define :attr as an enum, backed by a string.
280
- However, :attr is an integer column.
281
- MESSAGE
282
-
283
- assertion = lambda do
284
- expect(record).
285
- to define_enum_for(:attr).
286
- backed_by_column_of_type(:string)
287
- end
288
-
289
- expect(&assertion).to fail_with_message(message)
290
- end
291
- end
292
-
293
- context 'if the column storing the attribute is of the same type' do
294
- it 'accepts' do
295
- record = build_record_with_array_values(
296
- attribute_name: :attr,
297
- column_type: :string,
298
- )
299
-
300
- expect(record).
301
- to define_enum_for(:attr).
302
- backed_by_column_of_type(:string)
303
- end
304
- end
305
- end
306
-
307
- if active_record_enum_supports_prefix_and_suffix?
308
- context 'qualified with #with_prefix' do
309
- context 'when the prefix is explicit' do
310
- context 'if the attribute was not defined with a prefix' do
311
- it 'rejects with an appropriate failure message' do
312
- record = build_record_with_array_values(
313
- model_name: 'Example',
314
- attribute_name: :attr,
315
- column_type: :integer,
316
- values: [:active, :archived],
317
- )
318
-
319
- assertion = lambda do
320
- expect(record).
321
- to define_enum_for(:attr).
322
- with_values([:active, :archived]).
323
- with_prefix(:foo)
324
- end
325
-
326
- message = format_message(<<-MESSAGE)
327
- Expected Example to define :attr as an enum, backed by an integer,
328
- using a prefix of :foo, with possible values ‹[:active,
329
- :archived]›. However, it was defined with either a different
330
- prefix or none at all.
331
- MESSAGE
332
-
333
- expect(&assertion).to fail_with_message(message)
334
- end
335
- end
336
-
337
- context 'if the attribute was defined with a different prefix' do
338
- it 'rejects with an appropriate failure message' do
339
- record = build_record_with_array_values(
340
- model_name: 'Example',
341
- attribute_name: :attr,
342
- column_type: :integer,
343
- values: [:active, :archived],
344
- prefix: :foo,
345
- )
346
-
347
- assertion = lambda do
348
- expect(record).
349
- to define_enum_for(:attr).
350
- with_values([:active, :archived]).
351
- with_prefix(:bar)
352
- end
353
-
354
- message = format_message(<<-MESSAGE)
355
- Expected Example to define :attr as an enum, backed by an integer,
356
- using a prefix of :bar, with possible values ‹[:active,
357
- :archived]›. However, it was defined with either a different
358
- prefix or none at all.
359
- MESSAGE
360
-
361
- expect(&assertion).to fail_with_message(message)
362
- end
363
- end
364
-
365
- context 'if the attribute was defined with the same prefix' do
366
- it 'accepts' do
367
- record = build_record_with_array_values(
368
- model_name: 'Example',
369
- attribute_name: :attr,
370
- values: [:active, :archived],
371
- prefix: :foo,
372
- )
373
-
374
- expect(record).
375
- to define_enum_for(:attr).
376
- with_values([:active, :archived]).
377
- with_prefix(:foo)
378
- end
379
- end
380
- end
381
-
382
- context 'when the prefix is implicit' do
383
- context 'if the attribute was not defined with a prefix' do
384
- it 'rejects with an appropriate failure message' do
385
- record = build_record_with_array_values(
386
- model_name: 'Example',
387
- attribute_name: :attr,
388
- column_type: :integer,
389
- values: [:active, :archived],
390
- )
391
-
392
- assertion = lambda do
393
- expect(record).
394
- to define_enum_for(:attr).
395
- with_values([:active, :archived]).
396
- with_prefix
397
- end
398
-
399
- message = format_message(<<-MESSAGE)
400
- Expected Example to define :attr as an enum, backed by an integer,
401
- using a prefix of :attr, with possible values ‹[:active,
402
- :archived]›. However, it was defined with either a different
403
- prefix or none at all.
404
- MESSAGE
405
-
406
- expect(&assertion).to fail_with_message(message)
407
- end
408
- end
409
-
410
- context 'if the attribute was defined with a prefix' do
411
- it 'accepts' do
412
- record = build_record_with_array_values(
413
- model_name: 'Example',
414
- attribute_name: :attr,
415
- values: [:active, :archived],
416
- prefix: true,
417
- )
418
-
419
- expect(record).
420
- to define_enum_for(:attr).
421
- with_values([:active, :archived]).
422
- with_prefix
423
- end
424
- end
425
- end
426
- end
427
-
428
- context 'qualified with #with_suffix' do
429
- context 'when the suffix is explicit' do
430
- context 'if the attribute was not defined with a suffix' do
431
- it 'rejects with an appropriate failure message' do
432
- record = build_record_with_array_values(
433
- model_name: 'Example',
434
- attribute_name: :attr,
435
- column_type: :integer,
436
- values: [:active, :archived],
437
- )
438
-
439
- assertion = lambda do
440
- expect(record).
441
- to define_enum_for(:attr).
442
- with_values([:active, :archived]).
443
- with_suffix(:foo)
444
- end
445
-
446
- message = format_message(<<-MESSAGE)
447
- Expected Example to define :attr as an enum, backed by an integer,
448
- using a suffix of :foo, with possible values ‹[:active,
449
- :archived]›. However, it was defined with either a different
450
- suffix or none at all.
451
- MESSAGE
452
-
453
- expect(&assertion).to fail_with_message(message)
454
- end
455
- end
456
-
457
- context 'if the attribute was defined with a different suffix' do
458
- it 'rejects with an appropriate failure message' do
459
- record = build_record_with_array_values(
460
- model_name: 'Example',
461
- attribute_name: :attr,
462
- column_type: :integer,
463
- values: [:active, :archived],
464
- suffix: :foo,
465
- )
466
-
467
- assertion = lambda do
468
- expect(record).
469
- to define_enum_for(:attr).
470
- with_values([:active, :archived]).
471
- with_suffix(:bar)
472
- end
473
-
474
- message = format_message(<<-MESSAGE)
475
- Expected Example to define :attr as an enum, backed by an integer,
476
- using a suffix of :bar, with possible values ‹[:active,
477
- :archived]›. However, it was defined with either a different
478
- suffix or none at all.
479
- MESSAGE
480
-
481
- expect(&assertion).to fail_with_message(message)
482
- end
483
- end
484
-
485
- context 'if the attribute was defined with the same suffix' do
486
- it 'accepts' do
487
- record = build_record_with_array_values(
488
- model_name: 'Example',
489
- attribute_name: :attr,
490
- values: [:active, :archived],
491
- suffix: :foo,
492
- )
493
-
494
- expect(record).
495
- to define_enum_for(:attr).
496
- with_values([:active, :archived]).
497
- with_suffix(:foo)
498
- end
499
- end
500
- end
501
-
502
- context 'when the suffix is implicit' do
503
- context 'if the attribute was not defined with a suffix' do
504
- it 'rejects with an appropriate failure message' do
505
- record = build_record_with_array_values(
506
- model_name: 'Example',
507
- attribute_name: :attr,
508
- column_type: :integer,
509
- values: [:active, :archived],
510
- )
511
-
512
- assertion = lambda do
513
- expect(record).
514
- to define_enum_for(:attr).
515
- with_values([:active, :archived]).
516
- with_suffix
517
- end
518
-
519
- message = format_message(<<-MESSAGE)
520
- Expected Example to define :attr as an enum, backed by an integer,
521
- using a suffix of :attr, with possible values ‹[:active,
522
- :archived]›. However, it was defined with either a different
523
- suffix or none at all.
524
- MESSAGE
525
-
526
- expect(&assertion).to fail_with_message(message)
527
- end
528
- end
529
-
530
- context 'if the attribute was defined with a suffix' do
531
- it 'accepts' do
532
- record = build_record_with_array_values(
533
- model_name: 'Example',
534
- attribute_name: :attr,
535
- values: [:active, :archived],
536
- suffix: true,
537
- )
538
-
539
- expect(record).
540
- to define_enum_for(:attr).
541
- with_values([:active, :archived]).
542
- with_suffix
543
- end
544
- end
545
- end
546
- end
547
-
548
- context 'qualified with both #with_prefix and #with_suffix' do
549
- context 'if the attribute was not defined with a different prefix' do
550
- it 'rejects with an appropriate failure message' do
551
- record = build_record_with_array_values(
552
- model_name: 'Example',
553
- attribute_name: :attr,
554
- column_type: :integer,
555
- values: [:active, :archived],
556
- prefix: :foo,
557
- suffix: :bar,
558
- )
559
-
560
- assertion = lambda do
561
- expect(record).
562
- to define_enum_for(:attr).
563
- with_values([:active, :archived]).
564
- with_prefix(:whatever).
565
- with_suffix(:bar)
566
- end
567
-
568
- message = format_message(<<-MESSAGE)
569
- Expected Example to define :attr as an enum, backed by an integer,
570
- using a prefix of :whatever and a suffix of :bar, with possible
571
- values ‹[:active, :archived]›. However, it was defined with either
572
- a different prefix, a different suffix, or neither one at all.
573
- MESSAGE
574
-
575
- expect(&assertion).to fail_with_message(message)
576
- end
577
-
578
- context 'if the attribute was defined with a different suffix' do
579
- it 'rejects with an appropriate failure message' do
580
- record = build_record_with_array_values(
581
- model_name: 'Example',
582
- attribute_name: :attr,
583
- column_type: :integer,
584
- values: [:active, :archived],
585
- prefix: :foo,
586
- suffix: :bar,
587
- )
588
-
589
- assertion = lambda do
590
- expect(record).
591
- to define_enum_for(:attr).
592
- with_values([:active, :archived]).
593
- with_prefix(:foo).
594
- with_suffix(:whatever)
595
- end
596
-
597
- message = format_message(<<-MESSAGE)
598
- Expected Example to define :attr as an enum, backed by an integer,
599
- using a prefix of :foo and a suffix of :whatever, with possible
600
- values ‹[:active, :archived]›. However, it was defined with
601
- either a different prefix, a different suffix, or neither one at
602
- all.
603
- MESSAGE
604
-
605
- expect(&assertion).to fail_with_message(message)
606
- end
607
- end
608
-
609
- context 'if the attribute was defined with the same prefix and suffix' do
610
- it 'accepts' do
611
- record = build_record_with_array_values(
612
- model_name: 'Example',
613
- attribute_name: :attr,
614
- values: [:active, :archived],
615
- prefix: :foo,
616
- suffix: :bar,
617
- )
618
-
619
- expect(record).
620
- to define_enum_for(:attr).
621
- with_values([:active, :archived]).
622
- with_prefix(:foo).
623
- with_suffix(:bar)
624
- end
625
- end
626
- end
627
- end
628
- end
629
-
630
- def build_record_with_array_values(
631
- model_name: 'Example',
632
- attribute_name: :attr,
633
- column_type: :integer,
634
- values: ['published', 'unpublished', 'draft'],
635
- prefix: false,
636
- suffix: false
637
- )
638
- build_record_with_enum_attribute(
639
- model_name: model_name,
640
- attribute_name: attribute_name,
641
- column_type: column_type,
642
- values: values,
643
- prefix: prefix,
644
- suffix: suffix,
645
- )
646
- end
647
-
648
- def build_record_with_hash_values(
649
- model_name: 'Example',
650
- attribute_name: :attr,
651
- values: { active: 0, archived: 1 },
652
- prefix: false,
653
- suffix: false
654
- )
655
- build_record_with_enum_attribute(
656
- model_name: model_name,
657
- attribute_name: attribute_name,
658
- column_type: :integer,
659
- values: values,
660
- prefix: prefix,
661
- suffix: suffix,
662
- )
663
- end
664
-
665
- def build_record_with_enum_attribute(
666
- model_name:,
667
- attribute_name:,
668
- column_type:,
669
- values:,
670
- prefix: false,
671
- suffix: false
672
- )
673
- model = define_model(
674
- model_name,
675
- attribute_name => { type: column_type },
676
- )
677
-
678
- if active_record_enum_supports_prefix_and_suffix?
679
- model.enum(attribute_name => values, _prefix: prefix, _suffix: suffix)
680
- else
681
- model.enum(attribute_name => values)
682
- end
683
-
684
- model.new
685
- end
686
-
687
- def build_record_with_non_enum_attribute(model_name:, attribute_name:)
688
- define_model(model_name, attribute_name => :integer).new
689
- end
690
- end