From 020a1660bf6b48c4cbeb3e77b20bcf5845741dd0 Mon Sep 17 00:00:00 2001 From: Dmitriy Nesteryuk Date: Tue, 14 Jan 2020 20:37:45 +0200 Subject: [PATCH] more tests for Grape::Validations::Types::PrimitiveCoercer Unit tests better focus on corner cases in this case than integration tests which might be too high level. --- .../validations/types/primitive_coercer.rb | 5 +- .../types/primitive_coercer_spec.rb | 63 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 spec/grape/validations/types/primitive_coercer_spec.rb diff --git a/lib/grape/validations/types/primitive_coercer.rb b/lib/grape/validations/types/primitive_coercer.rb index 393087ba3..4205b72d0 100644 --- a/lib/grape/validations/types/primitive_coercer.rb +++ b/lib/grape/validations/types/primitive_coercer.rb @@ -6,12 +6,13 @@ module Grape module Validations module Types # Coerces the given value to a type defined via a +type+ argument during - # initialization. + # initialization. When +strict+ is true, it doesn't coerce a value but check + # that it has the proper type. class PrimitiveCoercer < DryTypeCoercer MAPPING = { Grape::API::Boolean => DryTypes::Params::Bool, - # unfortunatelly, a +Params+ scope doesn't contain String + # unfortunately, a +Params+ scope doesn't contain String String => DryTypes::Coercible::String, BigDecimal => DryTypes::Coercible::Decimal }.freeze diff --git a/spec/grape/validations/types/primitive_coercer_spec.rb b/spec/grape/validations/types/primitive_coercer_spec.rb new file mode 100644 index 000000000..987ab11ec --- /dev/null +++ b/spec/grape/validations/types/primitive_coercer_spec.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Grape::Validations::Types::PrimitiveCoercer do + let(:strict) { false } + + subject { described_class.new(type, strict) } + + describe '.call' do + context 'Boolean' do + let(:type) { Grape::API::Boolean } + + it 'coerces to Boolean' do + expect(subject.call(0)).to eq(false) + end + end + + context 'String' do + let(:type) { String } + + it 'coerces to String' do + expect(subject.call(10)).to eq('10') + end + end + + context 'BigDecimal' do + let(:type) { BigDecimal } + + it 'coerces to BigDecimal' do + expect(subject.call(5)).to eq(BigDecimal(5)) + end + end + + context 'the strict mode' do + let(:strict) { true } + + context 'Boolean' do + let(:type) { Grape::API::Boolean } + + it 'returns an error when the given value is not Boolean' do + expect(subject.call(1)).to be_instance_of(Grape::Validations::Types::InvalidValue) + end + + it 'returns a value as it is when the given value is Boolean' do + expect(subject.call(true)).to eq(true) + end + end + + context 'BigDecimal' do + let(:type) { BigDecimal } + + it 'returns an error when the given value is not BigDecimal' do + expect(subject.call(1)).to be_instance_of(Grape::Validations::Types::InvalidValue) + end + + it 'returns a value as it is when the given value is BigDecimal' do + expect(subject.call(BigDecimal(0))).to eq(BigDecimal(0)) + end + end + end + end +end