-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add tests on validation & column
- Loading branch information
Showing
2 changed files
with
215 additions
and
12 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
199 changes: 199 additions & 0 deletions
199
...min_datasource_mongoid/spec/lib/forest_admin_datasource_mongoid/parser/validation_spec.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,199 @@ | ||
require 'spec_helper' | ||
|
||
module ForestAdminDatasourceMongoid | ||
module Parser | ||
include ForestAdminDatasourceToolkit::Components::Query::ConditionTree | ||
describe Validation do | ||
let(:datasource) { ForestAdminDatasourceMongoid::Datasource.new } | ||
let(:collection) { ForestAdminDatasourceMongoid::Collection.new(datasource, model) } | ||
|
||
context 'when the model has a before_validation callback' do | ||
let(:model) do | ||
model_class = Class.new do | ||
include Mongoid::Document | ||
before_validation :some_before_validation_callback | ||
validates :email, presence: true | ||
|
||
def some_before_validation_callback | ||
# Simulate callback | ||
end | ||
end | ||
Object.const_set(:PostDocument1, model_class) | ||
model_class | ||
end | ||
|
||
it 'returns empty validations' do | ||
result = collection.get_validations('email') | ||
expect(result).to eq([]) | ||
end | ||
end | ||
|
||
context 'when the model does not have a before_validation callback' do | ||
let(:model) do | ||
model_class = Class.new do | ||
include Mongoid::Document | ||
field :email, type: String | ||
validates :email, presence: true | ||
end | ||
|
||
Object.const_set(:PostDocument2, model_class) | ||
model_class | ||
end | ||
|
||
it 'returns validations' do | ||
email_column = model.fields['email'] | ||
result = collection.get_validations(email_column) | ||
|
||
expect(result).to eq([{ operator: Operators::PRESENT }]) | ||
end | ||
end | ||
|
||
context 'when the model has a numericality validator' do | ||
context 'with a greater_than validation' do | ||
let(:model) do | ||
model_class = Class.new do | ||
include Mongoid::Document | ||
field :age, type: Integer | ||
validates :age, numericality: { greater_than: 20 } | ||
end | ||
|
||
Object.const_set(:PostDocument3, model_class) | ||
model_class | ||
end | ||
|
||
it 'returns a greater_than validation' do | ||
age_column = model.fields['age'] | ||
result = collection.get_validations(age_column) | ||
|
||
expect(result).to include({ operator: Operators::GREATER_THAN, value: 20 }) | ||
end | ||
end | ||
|
||
context 'with a less_than validation' do | ||
let(:model) do | ||
model_class = Class.new do | ||
include Mongoid::Document | ||
field :age, type: Integer | ||
validates :age, numericality: { less_than: 50 } | ||
end | ||
|
||
Object.const_set(:PostDocument4, model_class) | ||
model_class | ||
end | ||
|
||
it 'returns a less_than validation' do | ||
age_column = model.fields['age'] | ||
result = collection.get_validations(age_column) | ||
|
||
expect(result).to include({ operator: Operators::LESS_THAN, value: 50 }) | ||
end | ||
end | ||
end | ||
|
||
context 'when the model has a length validator' do | ||
context 'with a minimum length validation' do | ||
let(:model) do | ||
model_class = Class.new do | ||
include Mongoid::Document | ||
field :title, type: String | ||
validates :title, length: { minimum: 5 } | ||
end | ||
|
||
Object.const_set(:PostDocument5, model_class) | ||
model_class | ||
end | ||
|
||
it 'returns a minimum length validation' do | ||
title_column = model.fields['title'] | ||
result = collection.get_validations(title_column) | ||
|
||
expect(result).to include({ operator: Operators::LONGER_THAN, value: 5 }) | ||
end | ||
end | ||
|
||
context 'with a maximum length validation' do | ||
let(:model) do | ||
model_class = Class.new do | ||
include Mongoid::Document | ||
field :title, type: String | ||
validates :title, length: { maximum: 10 } | ||
end | ||
|
||
Object.const_set(:PostDocument6, model_class) | ||
model_class | ||
end | ||
|
||
it 'returns a maximum length validation' do | ||
title_column = model.fields['title'] | ||
result = collection.get_validations(title_column) | ||
|
||
expect(result).to include({ operator: Operators::SHORTER_THAN, value: 10 }) | ||
end | ||
end | ||
|
||
context 'with an exact length validator' do | ||
let(:model) do | ||
model_class = Class.new do | ||
include Mongoid::Document | ||
field :title, type: String | ||
validates :title, length: { is: 8 } | ||
end | ||
|
||
Object.const_set(:PostDocument7, model_class) | ||
model_class | ||
end | ||
|
||
it 'returns an exact length validation' do | ||
title_column = model.fields['title'] | ||
result = collection.get_validations(title_column) | ||
|
||
expect(result).to include({ operator: Operators::LONGER_THAN, value: 8 }) | ||
expect(result).to include({ operator: Operators::SHORTER_THAN, value: 8 }) | ||
end | ||
end | ||
end | ||
|
||
context 'when the model has a format validator' do | ||
context 'with a valid email format' do | ||
let(:model) do | ||
model_class = Class.new do | ||
include Mongoid::Document | ||
field :email, type: String | ||
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP } | ||
end | ||
|
||
Object.const_set(:PostDocument8, model_class) | ||
model_class | ||
end | ||
|
||
it 'returns a format validation for email' do | ||
email_column = model.fields['email'] | ||
result = collection.get_validations(email_column) | ||
|
||
expect(result).to include({ operator: Operators::CONTAINS, value: "/^[a-zA-Z0-9.!\\\#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/" }) | ||
end | ||
end | ||
|
||
context 'with a custom format validation' do | ||
let(:model) do | ||
model_class = Class.new do | ||
include Mongoid::Document | ||
field :username, type: String | ||
validates :username, format: { with: /\A[a-zA-Z0-9_]+\z/, message: 'only allows letters, numbers, and underscores' } | ||
end | ||
|
||
Object.const_set(:PostDocument9, model_class) | ||
model_class | ||
end | ||
|
||
it 'returns a custom format validation' do | ||
username_column = model.fields['username'] | ||
result = collection.get_validations(username_column) | ||
|
||
expect(result).to include({ operator: Operators::CONTAINS, value: '/^[a-zA-Z0-9_]+$/' }) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |