Skip to content

Commit

Permalink
chore: add tests on validation & column
Browse files Browse the repository at this point in the history
  • Loading branch information
matthv committed Jan 28, 2025
1 parent d687f57 commit f591cef
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

module ForestAdminDatasourceMongoid
module Parser
# include ForestAdminDatasourceToolkit
# include ForestAdminDatasourceToolkit::Components::Query::ConditionTree
include ForestAdminDatasourceToolkit::Components::Query::ConditionTree
describe Column do
let(:dummy_class) { Class.new { extend Column } }
# let(:dummy_class) { Class.new { extend ForestAdminDatasourceMongoid::Parser::Column } }

let(:columns) do
{
Expand Down Expand Up @@ -94,15 +92,21 @@ module Parser
expect(result).to eq({})
end

# {
# 'Boolean' => [Operators::PRESENT, Operators::EQUAL, Operators::NOT_EQUAL],
# 'Date' => [Operators::PRESENT, Operators::EQUAL, Operators::NOT_EQUAL, Operators::IN, Operators::NOT_IN, Operators::GREATER_THAN, Operators::LESS_THAN],
# 'String' => [Operators::PRESENT, Operators::EQUAL, Operators::NOT_EQUAL, Operators::IN, Operators::NOT_IN, Operators::MATCH, Operators::NOT_CONTAINS, Operators::NOT_I_CONTAINS]
# }.each do |type, expected_operators|
# it "returns correct operators for type #{type}" do
# expect(dummy_class.operators_for_column_type(type)).to eq expected_operators
# end
# end
{
'Boolean' => [Operators::PRESENT, Operators::EQUAL, Operators::NOT_EQUAL],
'Binary' => [Operators::PRESENT, Operators::EQUAL, Operators::NOT_EQUAL],
'Json' => [Operators::PRESENT, Operators::EQUAL, Operators::NOT_EQUAL],
'Array' => [Operators::PRESENT, Operators::EQUAL, Operators::NOT_EQUAL],
'Date' => [Operators::PRESENT, Operators::EQUAL, Operators::NOT_EQUAL, Operators::IN, Operators::NOT_IN, Operators::GREATER_THAN, Operators::LESS_THAN],
'Dateonly' => [Operators::PRESENT, Operators::EQUAL, Operators::NOT_EQUAL, Operators::IN, Operators::NOT_IN, Operators::GREATER_THAN, Operators::LESS_THAN],
'Number' => [Operators::PRESENT, Operators::EQUAL, Operators::NOT_EQUAL, Operators::IN, Operators::NOT_IN, Operators::GREATER_THAN, Operators::LESS_THAN],
'String' => [Operators::PRESENT, Operators::EQUAL, Operators::NOT_EQUAL, Operators::IN, Operators::NOT_IN, Operators::MATCH, Operators::NOT_CONTAINS, Operators::NOT_I_CONTAINS],
'Enum' => [Operators::PRESENT, Operators::EQUAL, Operators::NOT_EQUAL, Operators::IN, Operators::NOT_IN]
}.each do |type, expected_operators|
it "returns correct operators for type #{type}" do
expect(dummy_class.operators_for_column_type(type)).to eq expected_operators
end
end
end
end
end
Expand Down
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

0 comments on commit f591cef

Please sign in to comment.