shoulda-matchers 4.3.0 → 4.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (24) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +157 -77
  3. data/lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb +5 -1
  4. data/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb +2 -21
  5. data/lib/shoulda/matchers/active_model/validate_length_of_matcher.rb +27 -3
  6. data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +32 -0
  7. data/lib/shoulda/matchers/active_model/validation_matcher.rb +25 -0
  8. data/lib/shoulda/matchers/active_record.rb +2 -0
  9. data/lib/shoulda/matchers/active_record/association_matcher.rb +20 -4
  10. data/lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb +2 -2
  11. data/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb +12 -6
  12. data/lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb +20 -3
  13. data/lib/shoulda/matchers/active_record/have_attached_matcher.rb +147 -0
  14. data/lib/shoulda/matchers/active_record/have_implicit_order_column.rb +106 -0
  15. data/lib/shoulda/matchers/active_record/have_secure_token_matcher.rb +28 -9
  16. data/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb +2 -2
  17. data/lib/shoulda/matchers/independent.rb +0 -1
  18. data/lib/shoulda/matchers/rails_shim.rb +4 -0
  19. data/lib/shoulda/matchers/util.rb +9 -2
  20. data/lib/shoulda/matchers/util/word_wrap.rb +1 -1
  21. data/lib/shoulda/matchers/version.rb +1 -1
  22. metadata +8 -8
  23. data/MIT-LICENSE +0 -22
  24. data/lib/shoulda/matchers/independent/delegate_method_matcher/stubbed_target.rb +0 -37
@@ -0,0 +1,106 @@
1
+ module Shoulda
2
+ module Matchers
3
+ module ActiveRecord
4
+ # The `have_implicit_order_column` matcher tests that the model has `implicit_order_column`
5
+ # assigned to one of the table columns. (Rails 6+ only)
6
+ #
7
+ # class Product < ApplicationRecord
8
+ # self.implicit_order_column = :created_at
9
+ # end
10
+ #
11
+ # # RSpec
12
+ # RSpec.describe Product, type: :model do
13
+ # it { should have_implicit_order_column(:created_at) }
14
+ # end
15
+ #
16
+ # # Minitest (Shoulda)
17
+ # class ProductTest < ActiveSupport::TestCase
18
+ # should have_implicit_order_column(:created_at)
19
+ # end
20
+ #
21
+ # @return [HaveImplicitOrderColumnMatcher]
22
+ #
23
+ if RailsShim.active_record_gte_6?
24
+ def have_implicit_order_column(column_name)
25
+ HaveImplicitOrderColumnMatcher.new(column_name)
26
+ end
27
+ end
28
+
29
+ # @private
30
+ class HaveImplicitOrderColumnMatcher
31
+ attr_reader :failure_message
32
+
33
+ def initialize(column_name)
34
+ @column_name = column_name
35
+ end
36
+
37
+ def matches?(subject)
38
+ @subject = subject
39
+ check_column_exists!
40
+ check_implicit_order_column_matches!
41
+ true
42
+ rescue SecondaryCheckFailedError => error
43
+ @failure_message = Shoulda::Matchers.word_wrap(
44
+ "Expected #{model.name} to #{expectation}, " +
45
+ "but that could not be proved: #{error.message}."
46
+ )
47
+ false
48
+ rescue PrimaryCheckFailedError => error
49
+ @failure_message = Shoulda::Matchers.word_wrap(
50
+ "Expected #{model.name} to #{expectation}, but #{error.message}."
51
+ )
52
+ false
53
+ end
54
+
55
+ def failure_message_when_negated
56
+ Shoulda::Matchers.word_wrap(
57
+ "Expected #{model.name} not to #{expectation}, but it did."
58
+ )
59
+ end
60
+
61
+ def description
62
+ expectation
63
+ end
64
+
65
+ private
66
+
67
+ attr_reader :column_name, :subject
68
+
69
+ def check_column_exists!
70
+ matcher = HaveDbColumnMatcher.new(column_name)
71
+
72
+ if !matcher.matches?(@subject)
73
+ raise SecondaryCheckFailedError.new(
74
+ "The :#{model.table_name} table does not have a " +
75
+ ":#{column_name} column"
76
+ )
77
+ end
78
+ end
79
+
80
+ def check_implicit_order_column_matches!
81
+ if model.implicit_order_column.to_s != column_name.to_s
82
+ message =
83
+ if model.implicit_order_column.nil?
84
+ "implicit_order_column is not set"
85
+ else
86
+ "it is :#{model.implicit_order_column}"
87
+ end
88
+
89
+ raise PrimaryCheckFailedError.new(message)
90
+ end
91
+ end
92
+
93
+ def model
94
+ subject.class
95
+ end
96
+
97
+ def expectation
98
+ "have an implicit_order_column of :#{column_name}"
99
+ end
100
+
101
+ class SecondaryCheckFailedError < StandardError; end
102
+ class PrimaryCheckFailedError < StandardError; end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -4,12 +4,7 @@ module Shoulda
4
4
  # The `have_secure_token` matcher tests usage of the
5
5
  # `has_secure_token` macro.
6
6
  #
7
- # #### Example
8
- #
9
7
  # class User < ActiveRecord
10
- # attr_accessor :token
11
- # attr_accessor :auth_token
12
- #
13
8
  # has_secure_token
14
9
  # has_secure_token :auth_token
15
10
  # end
@@ -26,14 +21,32 @@ module Shoulda
26
21
  # should have_secure_token(:auth_token)
27
22
  # end
28
23
  #
24
+ # #### Qualifiers
25
+ #
26
+ # ##### ignoring_check_for_db_index
27
+ #
28
+ # By default, this matcher tests that an index is defined on your token
29
+ # column. Use `ignoring_check_for_db_index` if this is not the case.
30
+ #
31
+ # class User < ActiveRecord
32
+ # has_secure_token :auth_token
33
+ # end
34
+ #
35
+ # # RSpec
36
+ # RSpec.describe User, type: :model do
37
+ # it { should have_secure_token(:auth_token).ignoring_check_for_db_index }
38
+ # end
39
+ #
40
+ # # Minitest (Shoulda)
41
+ # class UserTest < ActiveSupport::TestCase
42
+ # should have_secure_token(:auth_token).ignoring_check_for_db_index
43
+ # end
44
+ #
29
45
  # @return [HaveSecureToken]
30
46
  #
31
-
32
- # rubocop:disable Style/PredicateName
33
47
  def have_secure_token(token_attribute = :token)
34
48
  HaveSecureTokenMatcher.new(token_attribute)
35
49
  end
36
- # rubocop:enable Style/PredicateName
37
50
 
38
51
  # @private
39
52
  class HaveSecureTokenMatcher
@@ -41,6 +54,7 @@ module Shoulda
41
54
 
42
55
  def initialize(token_attribute)
43
56
  @token_attribute = token_attribute
57
+ @options = { ignore_check_for_db_index: false }
44
58
  end
45
59
 
46
60
  def description
@@ -65,6 +79,11 @@ module Shoulda
65
79
  @errors.empty?
66
80
  end
67
81
 
82
+ def ignoring_check_for_db_index
83
+ @options[:ignore_check_for_db_index] = true
84
+ self
85
+ end
86
+
68
87
  private
69
88
 
70
89
  def run_checks
@@ -75,7 +94,7 @@ module Shoulda
75
94
  if !has_expected_db_column?
76
95
  @errors << "missing correct column #{token_attribute}:string"
77
96
  end
78
- if !has_expected_db_index?
97
+ if !@options[:ignore_check_for_db_index] && !has_expected_db_index?
79
98
  @errors << "missing unique index for #{table_and_column}"
80
99
  end
81
100
  @errors
@@ -396,7 +396,7 @@ module Shoulda
396
396
  end
397
397
 
398
398
  def validations
399
- model._validators[@attribute].select do |validator|
399
+ model.validators_on(@attribute).select do |validator|
400
400
  validator.is_a?(::ActiveRecord::Validations::UniquenessValidator)
401
401
  end
402
402
  end
@@ -820,7 +820,7 @@ module Shoulda
820
820
  elsif previous_value.respond_to?(:next)
821
821
  previous_value.next
822
822
  elsif previous_value.respond_to?(:to_datetime)
823
- previous_value.to_datetime.next
823
+ previous_value.to_datetime.in(60).next
824
824
  elsif boolean_value?(previous_value)
825
825
  !previous_value
826
826
  else
@@ -1,5 +1,4 @@
1
1
  require 'shoulda/matchers/independent/delegate_method_matcher'
2
- require 'shoulda/matchers/independent/delegate_method_matcher/stubbed_target'
3
2
  require 'shoulda/matchers/independent/delegate_method_matcher/target_not_defined_error'
4
3
 
5
4
  module Shoulda
@@ -21,6 +21,10 @@ module Shoulda
21
21
  Gem::Requirement.new('>= 5').satisfied_by?(active_record_version)
22
22
  end
23
23
 
24
+ def active_record_gte_6?
25
+ Gem::Requirement.new('>= 6').satisfied_by?(active_record_version)
26
+ end
27
+
24
28
  def active_record_version
25
29
  Gem::Version.new(::ActiveRecord::VERSION::STRING)
26
30
  rescue NameError
@@ -4,6 +4,8 @@ module Shoulda
4
4
  module Matchers
5
5
  # @private
6
6
  module Util
7
+ MAXIMUM_LENGTH_OF_VALUE_TO_DISPLAY = 500
8
+
7
9
  def self.deconstantize(path)
8
10
  if defined?(ActiveSupport::Inflector) &&
9
11
  ActiveSupport::Inflector.respond_to?(:deconstantize)
@@ -47,7 +49,12 @@ module Shoulda
47
49
  when Range
48
50
  inspect_range(value)
49
51
  else
50
- "‹#{value.inspect}›"
52
+ inspected_value = value.inspect
53
+ if inspected_value.length > MAXIMUM_LENGTH_OF_VALUE_TO_DISPLAY
54
+ "‹#{inspected_value[0, MAXIMUM_LENGTH_OF_VALUE_TO_DISPLAY]}...›"
55
+ else
56
+ "‹#{inspected_value}›"
57
+ end
51
58
  end
52
59
  end
53
60
 
@@ -85,7 +92,7 @@ module Shoulda
85
92
  when :datetime, :timestamp
86
93
  DateTime.new(2100, 1, 1)
87
94
  when :time
88
- Time.new(2100, 1, 1)
95
+ Time.new(2000, 1, 1)
89
96
  when :uuid
90
97
  SecureRandom.uuid
91
98
  when :boolean
@@ -5,7 +5,7 @@ module Shoulda
5
5
  TERMINAL_WIDTH = 72
6
6
 
7
7
  def word_wrap(document, options = {})
8
- Document.new(document, options).wrap
8
+ Document.new(document, **options).wrap
9
9
  end
10
10
  end
11
11
 
@@ -1,6 +1,6 @@
1
1
  module Shoulda
2
2
  module Matchers
3
3
  # @private
4
- VERSION = '4.3.0'.freeze
4
+ VERSION = '4.4.1'.freeze
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shoulda-matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.3.0
4
+ version: 4.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tammer Saleh
@@ -11,10 +11,10 @@ authors:
11
11
  - Matt Jankowski
12
12
  - Stafford Brunk
13
13
  - Elliot Winkler
14
- autorequire:
14
+ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2020-02-17 00:00:00.000000000 Z
17
+ date: 2020-08-26 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: activesupport
@@ -38,7 +38,6 @@ executables: []
38
38
  extensions: []
39
39
  extra_rdoc_files: []
40
40
  files:
41
- - MIT-LICENSE
42
41
  - README.md
43
42
  - docs/errors/NonCaseSwappableValueError.md
44
43
  - lib/shoulda-matchers.rb
@@ -113,8 +112,10 @@ files:
113
112
  - lib/shoulda/matchers/active_record/association_matchers/source_matcher.rb
114
113
  - lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb
115
114
  - lib/shoulda/matchers/active_record/define_enum_for_matcher.rb
115
+ - lib/shoulda/matchers/active_record/have_attached_matcher.rb
116
116
  - lib/shoulda/matchers/active_record/have_db_column_matcher.rb
117
117
  - lib/shoulda/matchers/active_record/have_db_index_matcher.rb
118
+ - lib/shoulda/matchers/active_record/have_implicit_order_column.rb
118
119
  - lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb
119
120
  - lib/shoulda/matchers/active_record/have_rich_text_matcher.rb
120
121
  - lib/shoulda/matchers/active_record/have_secure_token_matcher.rb
@@ -138,7 +139,6 @@ files:
138
139
  - lib/shoulda/matchers/error.rb
139
140
  - lib/shoulda/matchers/independent.rb
140
141
  - lib/shoulda/matchers/independent/delegate_method_matcher.rb
141
- - lib/shoulda/matchers/independent/delegate_method_matcher/stubbed_target.rb
142
142
  - lib/shoulda/matchers/independent/delegate_method_matcher/target_not_defined_error.rb
143
143
  - lib/shoulda/matchers/integrations.rb
144
144
  - lib/shoulda/matchers/integrations/configuration.rb
@@ -177,7 +177,7 @@ metadata:
177
177
  documentation_uri: https://matchers.shoulda.io/docs
178
178
  homepage_uri: https://matchers.shoulda.io
179
179
  source_code_uri: https://github.com/thoughtbot/shoulda-matchers
180
- post_install_message:
180
+ post_install_message:
181
181
  rdoc_options: []
182
182
  require_paths:
183
183
  - lib
@@ -192,8 +192,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
192
  - !ruby/object:Gem::Version
193
193
  version: '0'
194
194
  requirements: []
195
- rubygems_version: 3.0.3
196
- signing_key:
195
+ rubygems_version: 3.1.2
196
+ signing_key:
197
197
  specification_version: 4
198
198
  summary: Simple one-liner tests for common Rails functionality
199
199
  test_files: []
@@ -1,22 +0,0 @@
1
- Copyright (c) 2006-2020, Tammer Saleh and thoughtbot, inc.
2
-
3
- Permission is hereby granted, free of charge, to any person
4
- obtaining a copy of this software and associated documentation
5
- files (the "Software"), to deal in the Software without
6
- restriction, including without limitation the rights to use,
7
- copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- copies of the Software, and to permit persons to whom the
9
- Software is furnished to do so, subject to the following
10
- conditions:
11
-
12
- The above copyright notice and this permission notice shall be
13
- included in all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
- OTHER DEALINGS IN THE SOFTWARE.
@@ -1,37 +0,0 @@
1
- module Shoulda
2
- module Matchers
3
- module Independent
4
- class DelegateMethodMatcher
5
- # @private
6
- class StubbedTarget
7
- def initialize(method)
8
- @received_method = false
9
- @received_arguments = []
10
- stub_method(method)
11
- end
12
-
13
- def has_received_method?
14
- received_method
15
- end
16
-
17
- def has_received_arguments?(*args)
18
- args == received_arguments
19
- end
20
-
21
- protected
22
-
23
- def stub_method(method)
24
- class_eval do
25
- define_method method do |*args|
26
- @received_method = true
27
- @received_arguments = args
28
- end
29
- end
30
- end
31
-
32
- attr_reader :received_method, :received_arguments
33
- end
34
- end
35
- end
36
- end
37
- end