-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Validator] Add test for proc validation based on file attribute (#177)
- Loading branch information
Showing
8 changed files
with
117 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Integration | ||
module Validator | ||
def self.table_name_prefix | ||
'integration_validator_' | ||
end | ||
end | ||
end |
17 changes: 17 additions & 0 deletions
17
test/dummy/app/models/integration/validator/based_on_a_file_property.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: integration_validator_based_on_a_file_properties | ||
# | ||
# id :integer not null, primary key | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
|
||
class Integration::Validator::BasedOnAFileProperty < ApplicationRecord | ||
has_one_attached :picture | ||
validates :picture, | ||
content_type: ['image/png', 'image/jpg', 'image/gif'], | ||
size: { less_than: -> (record) { record.picture.blob.content_type == "image/png" ? 15.kilobytes : 5.kilobytes} } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
describe 'Integration tests' do | ||
include ValidatorHelpers | ||
|
||
let(:params) { {} } | ||
let(:model) { klass.new(params) } | ||
|
||
describe 'based on a file property' do | ||
let(:klass) { Integration::Validator::BasedOnAFileProperty } | ||
|
||
describe 'when setting size constraints based on the content type' do | ||
describe "when passed a file with the right size and content content type" do | ||
subject { model.picture.attach(file_1ko_and_png) and model } | ||
|
||
it { is_expected_to_be_valid } | ||
end | ||
|
||
describe "when passed a file with a content type that should accept higher file size (<= 15.kilobytes)" do | ||
describe "and with a higher size that the one that can be accepted for all content types" do | ||
subject { model.picture.attach(file_17ko_and_png) and model } | ||
|
||
let(:error_options) do | ||
{ | ||
file_size: '17 KB', | ||
min_size: nil, | ||
max_size: '15 KB' | ||
} | ||
end | ||
|
||
it { is_expected_not_to_be_valid } | ||
it { is_expected_to_have_error_message("file_size_not_less_than", error_options: error_options, validator_sym: :size) } | ||
it { is_expected_to_have_error_options(error_options, validator_sym: :size) } | ||
end | ||
end | ||
|
||
describe "when passed a file with a content type that should accept less file size (<= 5.kilobytes)" do | ||
describe "and with a higher size that the one that should be accepted" do | ||
subject { model.picture.attach(file_7ko_and_jpg) and model } | ||
|
||
let(:error_options) do | ||
{ | ||
file_size: '7 KB', | ||
min_size: nil, | ||
max_size: '5 KB' | ||
} | ||
end | ||
|
||
it { is_expected_not_to_be_valid } | ||
it { is_expected_to_have_error_message("file_size_not_less_than", error_options: error_options, validator_sym: :size) } | ||
it { is_expected_to_have_error_options(error_options, validator_sym: :size) } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters