Skip to content

Commit

Permalink
[Validator] Refacto the size_validator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mth0158 committed Nov 24, 2023
1 parent 727a230 commit e1dd3d5
Show file tree
Hide file tree
Showing 18 changed files with 438 additions and 385 deletions.
15 changes: 15 additions & 0 deletions test/dummy/app/models/aspect_ratio/validator/with_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: aspect_ratio_validator_with_messages
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class AspectRatio::Validator::WithMessage < ApplicationRecord
has_one_attached :with_message
validates :with_message, aspect_ratio: { with: :square , message: 'Custom message' }
end
15 changes: 15 additions & 0 deletions test/dummy/app/models/attached/validator/with_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: attached_validator_with_messages
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class Attached::Validator::WithMessage < ApplicationRecord
has_one_attached :with_message
validates :with_message, attached: { message: 'Custom message' }
end
15 changes: 15 additions & 0 deletions test/dummy/app/models/content_type/validator/with_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: content_type_validator_with_messages
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class ContentType::Validator::WithMessage < ApplicationRecord
has_one_attached :with_message
validates :with_message, content_type: { with: :webp, message: 'Custom message' }
end
15 changes: 15 additions & 0 deletions test/dummy/app/models/dimension/validator/with_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: dimension_validator_with_messages
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class Dimension::Validator::WithMessage < ApplicationRecord
has_one_attached :with_message
validates :with_message, dimension: { width: 150, height: 150, message: 'Custom message' }
end
15 changes: 15 additions & 0 deletions test/dummy/app/models/limit/validator/with_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: limit_validator_with_messages
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class Limit::Validator::WithMessage < ApplicationRecord
has_one_attached :with_message
validates :with_message, limit: { max: 0, message: 'Custom message' }
end
15 changes: 15 additions & 0 deletions test/dummy/app/models/processable_image/validator/with_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: processable_image_validator_with_messages
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class ProcessableImage::Validator::WithMessage < ApplicationRecord
has_one_attached :with_message
validates :with_message, processable_image: { message: 'Custom message' }
end
43 changes: 0 additions & 43 deletions test/dummy/app/models/size/portfolio.rb

This file was deleted.

19 changes: 0 additions & 19 deletions test/dummy/app/models/size/several_validator.rb

This file was deleted.

19 changes: 0 additions & 19 deletions test/dummy/app/models/size/several_validator_proc.rb

This file was deleted.

37 changes: 37 additions & 0 deletions test/dummy/app/models/size/validator/check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: size_validator_checks
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class Size::Validator::Check < ApplicationRecord
has_one_attached :less_than
has_one_attached :less_than_or_equal_to
has_one_attached :greater_than
has_one_attached :greater_than_or_equal_to
has_one_attached :between
validates :less_than, size: { less_than: 2.kilobytes }
validates :less_than_or_equal_to, size: { less_than_or_equal_to: 2.kilobytes }
validates :greater_than, size: { greater_than: 7.kilobytes }
validates :greater_than_or_equal_to, size: { greater_than_or_equal_to: 7.kilobytes }
validates :between, size: { between: 2.kilobytes..7.kilobytes }

has_one_attached :less_than_proc
has_one_attached :less_than_or_equal_to_proc
has_one_attached :greater_than_proc
has_one_attached :greater_than_or_equal_to_proc
has_one_attached :between_proc
validates :less_than_proc, size: { less_than: -> (record) { 2.kilobytes } }
validates :less_than_or_equal_to_proc, size: { less_than_or_equal_to: -> (record) { 2.kilobytes } }
validates :greater_than_proc, size: { greater_than: -> (record) { 7.kilobytes } }
validates :greater_than_or_equal_to_proc, size: { greater_than_or_equal_to: -> (record) { 7.kilobytes } }
validates :between_proc, size: { between: -> { 2.kilobytes..7.kilobytes } }

has_one_attached :with_message
validates :with_message, size: { less_than_or_equal_to: 5.megabytes, message: 'Custom message' }
end
15 changes: 15 additions & 0 deletions test/dummy/app/models/size/validator/with_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: size_validator_with_messages
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class Size::Validator::WithMessage < ApplicationRecord
has_one_attached :with_message
validates :with_message, size: { less_than: 2.kilobytes , message: 'Custom message' }
end
19 changes: 0 additions & 19 deletions test/dummy/app/models/size/zero_validator.rb

This file was deleted.

19 changes: 0 additions & 19 deletions test/dummy/app/models/size/zero_validator_proc.rb

This file was deleted.

37 changes: 6 additions & 31 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@
end
end

%i(allow_nil allow_blank if on strict unless).each do |option|
create_table :"#{validator}_validator_checks", force: :cascade do |t|
t.datetime :created_at, null: false
t.datetime :updated_at, null: false
end

%i(allow_nil allow_blank if on strict unless message).each do |option|
create_table :"#{validator}_validator_with_#{option.to_s.pluralize}", force: :cascade do |t|
t.string :title if option == :if
t.integer :rating if option == :unless
Expand Down Expand Up @@ -104,36 +109,6 @@
t.datetime :updated_at, precision: 6, null: false
end

create_table :size_portfolios, force: :cascade do |t|
t.string :title
t.datetime :created_at, null: false
t.datetime :updated_at, null: false
end

create_table :size_several_validator_procs, force: :cascade do |t|
t.string :title
t.datetime :created_at, null: false
t.datetime :updated_at, null: false
end

create_table :size_several_validators, force: :cascade do |t|
t.string :title
t.datetime :created_at, null: false
t.datetime :updated_at, null: false
end

create_table :size_zero_validator_procs, force: :cascade do |t|
t.string :title
t.datetime :created_at, null: false
t.datetime :updated_at, null: false
end

create_table :size_zero_validators, force: :cascade do |t|
t.string :title
t.datetime :created_at, null: false
t.datetime :updated_at, null: false
end

create_table :users, force: :cascade do |t|
t.string :name
t.datetime :created_at, null: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'validators/shared_examples/works_with_allow_blank_option'
require 'validators/shared_examples/works_with_allow_nil_option'
require 'validators/shared_examples/works_with_if_option'
require 'validators/shared_examples/works_with_message_option'
require 'validators/shared_examples/works_with_on_option'
require 'validators/shared_examples/works_with_unless_option'
require 'validators/shared_examples/works_with_strict_option'
Expand All @@ -12,7 +13,7 @@ module WorksWithAllRailsCommonValidationOptions
# https://guides.rubyonrails.org/active_record_validations.html#common-validation-options

included do
%i(allow_nil allow_blank if on strict unless).each do |validation_option|
%i(allow_nil allow_blank if on strict unless message).each do |validation_option|
describe ":#{validation_option}" do
include "WorksWith#{validation_option.to_s.camelize}Option".constantize
end
Expand Down
46 changes: 46 additions & 0 deletions test/validators/shared_examples/works_with_message_option.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module WorksWithMessageOption
extend ActiveSupport::Concern

included do
subject { validator_test_class::WithMessage.new(params) }

let(:file_matching_requirements) do
case validator_sym
when :aspect_ratio then image_150x150_file
when :attached then image_150x150_file
when :content_type then webp_file
when :dimension then image_150x150_file
when :limit then nil
when :processable_image then image_150x150_file
when :size then file_1ko
end
end
let(:file_not_matching_requirements) do
case validator_sym
when :aspect_ratio then image_700x500_file
when :attached then nil
when :content_type then html_file
when :dimension then image_700x500_file
when :limit then file_5ko
when :processable_image then tar_file_with_image_content_type
when :size then file_5ko
end
end

describe 'when passed a file matching the requirements' do
before { subject.with_message.attach(file_matching_requirements) }

it { is_expected_to_be_valid }
end

describe 'when passed a file not matching the requirements' do
let(:error_options) { { custom_message: 'Custom message' } }

before { subject.with_message.attach(file_not_matching_requirements) }

it { is_expected_not_to_be_valid }
it { is_expected_to_have_error_message("Custom message", error_options:) }
it { is_expected_to_have_error_options(error_options) }
end
end
end
Loading

0 comments on commit e1dd3d5

Please sign in to comment.