Skip to content

Commit

Permalink
[Matcher] Make a few adjustments related to limit validator matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Mth0158 committed Oct 14, 2024
1 parent 4c1a7e3 commit c56f038
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ Very simple example of validation with file attached, content type check and cus
[![Sample](https://raw.githubusercontent.com/igorkasyanchuk/active_storage_validations/master/docs/preview.png)](https://raw.githubusercontent.com/igorkasyanchuk/active_storage_validations/master/docs/preview.png)

## Test matchers
Provides RSpec-compatible and Minitest-compatible matchers for testing the validators. Only `aspect_ratio`, `attached`, `content_type`, `limit`, `dimension` and `size` validators currently have their matcher developed.
Provides RSpec-compatible and Minitest-compatible matchers for testing the validators.

### RSpec

Expand Down
25 changes: 17 additions & 8 deletions lib/active_storage_validations/matchers/concerns/attachable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ def attach_file(file = dummy_file)
@subject.public_send(@attribute_name)
end

def attach_files(count)
return unless count.positive?

file_array = Array.new(count, dummy_file)

attach_file(file_array)
end

def detach_file
@subject.attachment_changes.delete(@attribute_name.to_s)
end
alias :detach_files :detach_file

def file_attached?
@subject.public_send(@attribute_name).attached?
end

def dummy_file
{
io: io,
Expand Down Expand Up @@ -35,14 +52,6 @@ def not_processable_image
def io
@io ||= Tempfile.new('Hello world!')
end

def detach_file
@subject.attachment_changes.delete(@attribute_name.to_s)
end

def file_attached?
@subject.public_send(@attribute_name).attached?
end
end
end
end
56 changes: 18 additions & 38 deletions lib/active_storage_validations/matchers/limit_validator_matcher.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'pry'

require_relative 'concerns/active_storageable'
require_relative 'concerns/allow_blankable'
require_relative 'concerns/attachable'
Expand Down Expand Up @@ -42,13 +42,13 @@ def failure_message
message.join("\n")
end

def min(number)
@min = number
def min(count)
@min = count
self
end

def max(number)
@max = number
def max(count)
@max = count
self
end

Expand All @@ -58,12 +58,12 @@ def matches?(subject)
is_a_valid_active_storage_attribute? &&
is_context_valid? &&
is_custom_message_valid? &&
file_number_not_smaller_than_min? &&
file_number_equal_min? &&
file_number_larger_than_min? &&
file_number_smaller_than_max? &&
file_number_equal_max? &&
file_number_not_larger_than_max?
file_count_not_smaller_than_min? &&
file_count_equal_min? &&
file_count_larger_than_min? &&
file_count_smaller_than_max? &&
file_count_equal_max? &&
file_count_not_larger_than_max?
end

private
Expand All @@ -73,32 +73,32 @@ def build_failure_message(message)

message << " but there seem to have issues with the matcher methods you used, since:"
@failure_message_artefacts.each do |error_case|
message << " validation failed when provided with a #{error_case[:count]} file(s)"
message << " validation failed when provided with #{error_case[:count]} file(s)"
end
message << " whereas it should have passed"
end

def file_number_not_smaller_than_min?
def file_count_not_smaller_than_min?
@min.nil? || @min.zero? || !passes_validation_with_limits(@min - 1)
end

def file_number_equal_min?
def file_count_equal_min?
@min.nil? || @min.zero? || passes_validation_with_limits(@min)
end

def file_number_larger_than_min?
def file_count_larger_than_min?
@min.nil? || @min.zero? || @min == @max || passes_validation_with_limits(@min + 1)
end

def file_number_smaller_than_max?
def file_count_smaller_than_max?
@max.nil? || @min == @max || passes_validation_with_limits(@max - 1)
end

def file_number_equal_max?
def file_count_equal_max?
@max.nil? || passes_validation_with_limits(@max)
end

def file_number_not_larger_than_max?
def file_count_not_larger_than_max?
@max.nil? || !passes_validation_with_limits(@max + 1)
end

Expand Down Expand Up @@ -126,26 +126,6 @@ def add_failure_message_artefact(count)
@failure_message_artefacts << { count: count }
false
end

def attach_files(count)
return if count.negative? || count.zero?

file_array = []
(1..count).each do |i|
dummy_file = {
io: Tempfile.new('.'),
filename: "dummy_#{i}.txt",
content_type: 'text/plain'
}
file_array << dummy_file
end

@subject.public_send(@attribute_name).attach(file_array)
end

def detach_files
@subject.attachment_changes.delete(@attribute_name.to_s)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module HasValidRspecMessageMethods
<<~FAILURE_MESSAGE
is expected to validate limit file of :#{model_attribute}
but there seem to have issues with the matcher methods you used, since:
validation failed when provided with a 6 file(s)
validation failed when provided with 6 file(s)
whereas it should have passed
FAILURE_MESSAGE
when :content_type
Expand Down Expand Up @@ -110,8 +110,8 @@ module HasValidRspecMessageMethods
<<~FAILURE_MESSAGE
is expected not to validate limit file of :#{model_attribute}
but there seem to have issues with the matcher methods you used, since:
validation failed when provided with a 0 file(s)
validation failed when provided with a 6 file(s)
validation failed when provided with 0 file(s)
validation failed when provided with 6 file(s)
whereas it should have passed
FAILURE_MESSAGE
when :content_type
Expand Down

0 comments on commit c56f038

Please sign in to comment.