Commit Graph

15 Commits

Author SHA1 Message Date
Matheus Sales 09bc2609d7
Add `in: range` matcher to validate_numericality_of (#1512)
Closes: #1493

In Rails 7 was added a new option to validate numericality. You can use
`in: range` to specify a range to validate an attribute.

```ruby
class User < ApplicationRecord
  validates :age, numericality: { greater_than_or_equal_to: 18, less_than_or_equal_to: 65 }
end

class User < ApplicationRecord
  validates :age, numericality: { in: 18..65 }
end
```

In this commit we are adding the support matcher to this new
functionality, while also making a refactor on the numericality
matchers that use the concept of submatchers.

We've created a new class (`NumericalityMatchers::Submatcher`) that's
been used by `NumericalityMatchers::RangeMatcher` and
`NumericalityMatchers::ComparisonMatcher`, this new class wil handle
shared logic regarding having submatchers that will check if the parent
matcher is valid or not.

Our new class `Numericality::Matchers::RangeMatcher` is using as
submatchers two `NumericalityMatchers::ComparisonMatcher` instances to
avoid creating new logic to handle this new option and also to replicate
what was being used before this option existed in Rails (see example
above)

In this commit we are adding:

* NumericalityMatchers::RangeMatcher file to support the new `in: range`
  option.
* Specs on ValidateNumericalityOfMatcherSpec file for the new supported
  option, only running on rails_versions > 7.
* NumericalityMatchers::Submatchers file to handle having submatchers
  inside a matcher file.
* Refactors to NumericalityMatchers::ComparisonMatcher.
2022-10-29 08:17:09 -03:00
Pedro Paiva 50085f03d9
Remove rails 5.1 support (#1429)
* Remove Rails 5.1 support

* Remove active_record_supports_active_storage? method

* Remove rails_gte_5_2? method
2021-04-17 17:13:09 -03:00
Pedro Paiva fc08a73b4c
Remove unnecessary conditions for rails 5.0 (#1426)
* Remove rails_lt_5? method

* Remove rails_5_x? method

* Remove conditionals for rails version equal to or greater than 5

* Remove action_pack_gte_5? method of RailsShim module

* Remove action_pack_lt_5? methods

* Remove active_record_gte_5? method

* Remove make_controller_request method

* Remove tables_and_views method

* Remove validation_message_key_for_association_required_option method
2021-03-23 14:06:23 -03:00
Stef Schenkelaars e4a268b5b4 Add have_attached matcher for ActiveStorage (#1102) 2020-07-24 09:34:25 -06:00
Gui Vieira 0e6db542be
Remove code for unsupported versions (#1270) 2020-01-01 19:10:35 -08:00
Brendan Thomas 795ca688bf add index_errors association matcher 2018-04-04 14:29:08 -06:00
Elliot Winkler 6ea9afc106 Drop legacy callback matchers under Rails 5
Rails 5 dropped the legacy controller callbacks `before_filter`,
`after_filter` and `around_filter`, so the matchers for these callbacks
are useless.
2017-09-26 17:17:18 -05:00
Elliot Winkler d59a6263ce Remove typo 2017-09-17 17:01:50 -05:00
Elliot Winkler c1a81d4b13 Disable allow_mass_assignment_of tests under Rails 5
The `allow_mass_assignment_of` matcher tests `attr_accessible` and
`attr_protected` which was moved to the `protected_attributes` gem in
Rails 4. The gem does not work in Rails 5 and neither does the matcher,
so there is no need to test it. (We should probably remove the matcher
altogether eventually -- Strong Parameters have long replaced
`protected_attributes`.)
2017-09-17 17:01:50 -05:00
Elliot Winkler 98ef8f67da Don't require ActiveResource tests under Rails 5 2017-09-17 17:01:50 -05:00
Elliot Winkler e708789714 Fix failure message for numericality matcher
Secondary author: Mauro George <maurogot@gmail.com>

Sometimes the failure message did not always provide the reason for
failure. For instance, given this validation:

    validates :ano, numericality: { only_integer: true, greater_than_or_equal_to: 1900 }

this test:

    it { should validate_numericality_of(:ano).is_greater_than_or_equal_to(1900) }

would fail with the following:

    Did not expect errors  when ano is set to 1900.000000000001, got error:

as you can see, the failure message doesn't contain the validation
error.

In the process of making this fix, we changed how the matcher fails so
that it will so when the first submatcher fails, not the last. This
changes what the failure message looks like, but not the basic
functionality of the matcher itself.
2015-06-01 01:38:35 -06:00
Elliot Winkler a43b8e44dd Rewrite tests for validate_uniqueness_of
* The main problem I had with the old tests is that information that
  the reader didn't need to care about was not properly abstracted away.
  For instance, a helper method used by almost all tests will always
  create a model called Example, and will always use an attribute called
  "attr" (on which the validation is present). However, in some tests
  the class or attribute is referred to directly. The reader shouldn't
  have to care about either of these things, since they are constant --
  the tests should be readable enough so that this information is not
  necessary to understand the case being tested against.

* Speaking of this helper method, some of the tests used it and some
  didn't. Some defined their own helper methods to represent a
  particular case (`case_sensitive: true`, `allow_nil`, etc.). This is
  now fixed so that all but two tests use the same helper method to
  define a model. This model is completely customizable -- one can
  specify the type of the attribute being validated, the names and types
  of scoped attributes, etc.

* The tests around scoped attributes and different types are all
  basically the same, so they are now compressed into a shared context.

* Related to this, we no longer have to worry about setting a proper
  value for a scope attribute. One had to know which type that attribute
  had and come up with a reasonable default for that type. Now there is
  a helper method that worries about this automatically.

* Finally, we remove tests around case_insensitive against an integer
  attribute (these don't make any sense, and don't work).
2015-02-11 16:09:40 -07:00
Elliot Winkler 9ba21381d7 Handle RangeErrors emitted now in ActiveRecord 4.2
In Rails 4.2, ActiveRecord was changed such that if you attempt to set
an attribute to a value and that value is outside the range of the
column, then it will raise a RangeError. For instance, an integer column
with a limit of 2 (i.e. a smallint) only accepts values between -32768
and +32767.

This means that if you try to do any of these three things, a RangeError
could be raised:

* Use validate_numericality_of along with any of the comparison
  submatchers and a value that sits on either side of the boundary.
* Use allow_value with a value that sits outside the range.
* Use validates_inclusion_of against an integer column. (Here we attempt
  to set that column to a non-integer value to verify that the attribute
  does not allow said value. That value is really a string version of a
  large number, so if the column does not take large numbers then the
  matcher could blow up.)

Ancillary changes in this commit:

* Remove ValidationMessageFinder and ExceptionMessageFinder in favor of
  Validator, StrictValidator, and ValidatorWithCapturedRangeError.
* The allow_value matcher now uses an instance of Validator under the
  hood. StrictValidator and/or ValidatorWithCapturedRangeError may be
  mixed into the Validator object as needed.
2015-01-22 21:05:09 -07:00
Elliot Winkler f922613386 Reorganize unit tests, part II
* Change 'spec' Rake task to 'spec:unit'
* Require unit_spec_helper.rb in unit tests, not spec_helper.rb
* Re-namespace files in spec/support/unit under UnitTests
* Files in spec/support/unit/helpers no longer automatically add
  themselves to RSpec - this happens in unit_spec_helper.rb
* Extract RecordWithDifferentErrorAttributeBuilder and
  RecordValidatingConfirmationBuilder to separate files
2014-11-05 09:53:20 -07:00
Elliot Winkler bbdf8a807e Reorganize unit tests, part I
* Move spec/shoulda to spec/unit_tests/shoulda
* Move spec/support/*.rb to spec/support/unit_tests/{helpers,matchers}
* Move spec_helper.rb to unit_spec_helper.rb
2014-11-04 14:43:59 -07:00