Skip to content

Commit

Permalink
[Validator] Add extra tests for dimension validator and update matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Mth0158 committed Feb 3, 2025
1 parent 8ded29c commit ef50a69
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 66 deletions.
7 changes: 7 additions & 0 deletions test/dummy/app/models/dimension/validator/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class Dimension::Validator::Check < ApplicationRecord
validates :"#{bound}_proc", dimension: { "#{bound}": -> (record) { 500..500 } }
end

# Integration tests
has_one_attached :width_height_exact
validates :width_height_exact, dimension: { width: 600, height: 600 }
has_one_attached :width_height_in
validates :width_height_in, dimension: { width: { in: 550..750 }, height: { in: 550..750 } }

# Edge cases
has_one_attached :with_invalid_media_file
validates :with_invalid_media_file, dimension: { width: 150, height: 150 }
end
Binary file added test/dummy/public/image_500x700.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/dummy/public/image_600x600.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions test/support/files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ def image_500x500_file
}
end

def image_600x600_file
{
io: File.open(Rails.root.join('public', 'image_600x600.png')),
filename: 'image_600x600_file.png',
content_type: 'image/png'
}
end

def image_500x700_file
{
io: File.open(Rails.root.join('public', 'image_500x700.png')),
filename: 'image_500x700_file.png',
content_type: 'image/png'
}
end

def image_700x500_file
{
io: File.open(Rails.root.join('public', 'image_700x500.png')),
Expand Down
8 changes: 4 additions & 4 deletions test/validators/aspect_ratio_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
end

it { is_expected_not_to_be_valid }
it { is_expected_to_have_error_message("aspect_ratio_not_#{named_aspect_ratio}", error_options: error_options) }
it { is_expected_to_include_error_message("aspect_ratio_not_#{named_aspect_ratio}", error_options: error_options) }
it { is_expected_to_have_error_options(error_options) }
end
end
Expand Down Expand Up @@ -183,7 +183,7 @@
end

it { is_expected_not_to_be_valid }
it { is_expected_to_have_error_message("aspect_ratio_not_x_y", error_options: error_options) }
it { is_expected_to_include_error_message("aspect_ratio_not_x_y", error_options: error_options) }
it { is_expected_to_have_error_options(error_options) }
end
end
Expand Down Expand Up @@ -218,7 +218,7 @@
end

it { is_expected_not_to_be_valid }
it { is_expected_to_have_error_message('aspect_ratio_invalid', error_options: error_options) }
it { is_expected_to_include_error_message('aspect_ratio_invalid', error_options: error_options) }
it { is_expected_to_have_error_options(error_options) }
end
end
Expand All @@ -237,7 +237,7 @@
end

it { is_expected_not_to_be_valid }
it { is_expected_to_have_error_message("media_metadata_missing", error_options: error_options) }
it { is_expected_to_include_error_message("media_metadata_missing", error_options: error_options) }
it { is_expected_to_have_error_options(error_options) }
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/validators/attached_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
subject { model }

it { is_expected_not_to_be_valid }
it { is_expected_to_have_error_message("blank", error_options: {}) }
it { is_expected_to_include_error_message("blank", error_options: {}) }
end

describe 'when provided with a file that is marked for destruction' do
# validates :has_to_be_attached, attached: true
subject { model.has_to_be_attached.attach(image_1920x1080_file) and model.has_to_be_attached.mark_for_destruction and model }

it { is_expected_not_to_be_valid }
it { is_expected_to_have_error_message("blank", error_options: {}) }
it { is_expected_to_include_error_message("blank", error_options: {}) }
end
end

Expand Down
18 changes: 9 additions & 9 deletions test/validators/content_type_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
# end

# it { is_expected_not_to_be_valid }
# it { is_expected_to_have_error_message("content_type_invalid", error_options: error_options) }
# it { is_expected_to_include_error_message("content_type_invalid", error_options: error_options) }
# it { is_expected_to_have_error_options(error_options) }
# end

Expand All @@ -153,7 +153,7 @@
# end

# it { is_expected_not_to_be_valid }
# it { is_expected_to_have_error_message("content_type_invalid", error_options: error_options) }
# it { is_expected_to_include_error_message("content_type_invalid", error_options: error_options) }
# it { is_expected_to_have_error_options(error_options) }
# end

Expand Down Expand Up @@ -203,7 +203,7 @@
# end

# it { is_expected_not_to_be_valid }
# it { is_expected_to_have_error_message("content_type_invalid", error_options: error_options) }
# it { is_expected_to_include_error_message("content_type_invalid", error_options: error_options) }
# it { is_expected_to_have_error_options(error_options) }
# end
# end
Expand Down Expand Up @@ -244,7 +244,7 @@
end

it { is_expected_not_to_be_valid }
it { is_expected_to_have_error_message("content_type_invalid", error_options: error_options) }
it { is_expected_to_include_error_message("content_type_invalid", error_options: error_options) }
it { is_expected_to_have_error_options(error_options) }
end
end
Expand Down Expand Up @@ -289,7 +289,7 @@
end

it { is_expected_not_to_be_valid }
it { is_expected_to_have_error_message("content_type_invalid", error_options: error_options) }
it { is_expected_to_include_error_message("content_type_invalid", error_options: error_options) }
it { is_expected_to_have_error_options(error_options) }
end
end
Expand Down Expand Up @@ -353,7 +353,7 @@
end

it { is_expected_not_to_be_valid }
it { is_expected_to_have_error_message("content_type_spoofed", error_options: error_options, validator: :content_type) }
it { is_expected_to_include_error_message("content_type_spoofed", error_options: error_options, validator: :content_type) }
it { is_expected_to_have_error_options(error_options, validator: :content_type) }
end

Expand All @@ -375,7 +375,7 @@
end

it { is_expected_not_to_be_valid }
it { is_expected_to_have_error_message("content_type_spoofed", error_options: error_options, validator: :content_type) }
it { is_expected_to_include_error_message("content_type_spoofed", error_options: error_options, validator: :content_type) }
it { is_expected_to_have_error_options(error_options, validator: :content_type) }
end

Expand All @@ -397,7 +397,7 @@
end

it { is_expected_not_to_be_valid }
it { is_expected_to_have_error_message("content_type_spoofed", error_options: error_options, validator: :content_type) }
it { is_expected_to_include_error_message("content_type_spoofed", error_options: error_options, validator: :content_type) }
it { is_expected_to_have_error_options(error_options, validator: :content_type) }
end

Expand Down Expand Up @@ -457,7 +457,7 @@
end

it { is_expected_not_to_be_valid }
it { is_expected_to_have_error_message("content_type_spoofed", error_options: error_options, validator: :content_type) }
it { is_expected_to_include_error_message("content_type_spoofed", error_options: error_options, validator: :content_type) }
it { is_expected_to_have_error_options(error_options, validator: :content_type) }
end
end
Expand Down
Loading

0 comments on commit ef50a69

Please sign in to comment.