spec/compel/builder_spec.rb in compel-0.3.6 vs spec/compel/builder_spec.rb in compel-0.3.7

- old
+ new

@@ -6,23 +6,36 @@ it 'should build new Schema for givin type' do builder = Compel.string expect(builder.type).to be(Compel::Coercion::String) - expect(builder.options.keys).to eq([]) + expect(builder.options.keys).to include('required') expect(builder.required?).to be false expect(builder.default_value).to be nil end context 'Builder::CommonValue' do - context '#in, #range, #min, #max' do + context '#in, #range' do subject(:builder) { Compel.string } it 'should have value' do - [:in, :range, :min, :max].each do |method| + [:in, :range].each do |method| + builder.send(method, ["#{method}"]) + expect(builder.options[method]).to eq(["#{method}"]) + end + end + + end + + context '#min, #max' do + + subject(:builder) { Compel.string } + + it 'should have value' do + [:min, :max].each do |method| builder.send(method, "#{method}") expect(builder.options[method]).to eq("#{method}") end end @@ -95,16 +108,133 @@ expect(builder.options[:format]).to eq('%Y-%m-%d') end end + context '#max' do + + it 'should set max value with string and coerce' do + builder.max('2016-01-01') + + expect(builder.options[:max]).to eq(Date.new(2016, 1, 1)) + end + + end + + context '#in' do + + it 'should set max value with string and coerce' do + builder.in(['2016-01-01', '2016-01-02']) + + expect(builder.options[:in]).to include(Date.new(2016, 1, 1)) + expect(builder.options[:in]).to include(Date.new(2016, 1, 2)) + end + + end + end + context 'Date, DateTime and Time' do + + def each_date_builder + [Date, DateTime, Time].each do |klass| + builder = Compel.send("#{klass.to_s.downcase}") + + yield builder, klass + end + end + + context 'in' do + + def expect_raise_error_for(builder, values, klass) + expect{ builder.in(values) }.to \ + raise_error \ + Compel::TypeError, + "All Builder::#{klass} #in values must be a valid #{klass}" + end + + it 'should set in value' do + each_date_builder do |builder, klass| + builder.in([klass.new(2016, 1, 1), klass.new(2016, 1, 2)]) + + expect(builder.options[:in].length).to eq 2 + end + end + + it 'should raise exception for invalid #in values' do + each_date_builder do |builder, klass| + expect_raise_error_for(builder, [klass.new(2016, 1, 1), 'invalid_date'], klass) + end + end + + end + + context '#max' do + + it 'should set max value' do + each_date_builder do |builder, klass| + builder.max(klass.new(2016, 1, 1)) + + expect(builder.options[:max]).to eq(klass.new(2016, 1, 1)) + end + end + + it 'should raise exception for invalid value' do + each_date_builder do |builder, klass| + expect{ builder.max(1) }.to \ + raise_error \ + Compel::TypeError, + "Builder::#{klass} #max value must be a valid #{klass}" + end + end + + end + + context '#min' do + + it 'should set min value' do + each_date_builder do |builder, klass| + builder.min(klass.new(2016, 1, 1)) + + expect(builder.options[:min]).to eq(klass.new(2016, 1, 1)) + end + end + + it 'should raise exception for invalid value' do + each_date_builder do |builder, klass| + expect{ builder.min(1) }.to \ + raise_error \ + Compel::TypeError, + "Builder::#{klass} #min value must be a valid #{klass}" + end + end + + end + + end + context 'String' do subject(:builder) { Compel.string } + context 'in' do + + it 'should set in value' do + builder.in(['a', 'b']) + + expect(builder.options[:in].length).to eq 2 + end + + it 'should raise exception for invalid item on array' do + expect{ builder.in([1, 'b']) }.to \ + raise_error \ + Compel::TypeError, + "All Builder::String #in values must be a valid String" + end + + end + context '#format' do it 'should raise exception for invalid type' do expect { builder.format('abc') }.to \ raise_error Compel::TypeError, "'abc' is not a valid Regexp" @@ -193,10 +323,50 @@ raise_error(Compel::TypeError, 'All Builder::Hash keys must be a valid Schema') end end + context 'Array' do + + it 'should raise exception for invalid type' do + expect { Compel.array.items('a') }.to \ + raise_error Compel::TypeError, "#items must be a valid Schema" + end + + it 'should raise exception for invalid type' do + expect { Compel.array.items('a') }.to \ + raise_error Compel::TypeError, "#items must be a valid Schema" + end + + it 'should have value' do + builder = Compel.array.items(Compel.integer) + + expect(builder.options[:items].class).to be(Compel::Builder::Integer) + end + + end + + context 'Integer' do + + context 'min' do + + it 'should build schema' do + builder = Compel.integer.min(10) + + expect(builder.options[:min]).to eq(10) + end + + it 'should raise exception for invalid value' do + expect{ Compel.integer.min('ten') }.to \ + raise_error \ + Compel::TypeError, 'Builder::Integer #min value must be a valid Integer' + end + + end + + end + end context 'Validate' do context 'Any' do @@ -412,10 +582,30 @@ expect(schema.validate({ a: nil }).errors.a).to \ include('is required') end + context '#required' do + + it 'should validate empty keys option' do + schema = Compel.hash.required + + expect(schema.validate({ a: 1 }).valid?).to be true + end + + it 'should validate nil' do + schema = Compel.hash.required + + result = schema.validate(nil) + + expect(result.valid?).to be false + expect(result.errors[:base]).to \ + include('is required') + end + + end + context '#is' do it 'should validate with errors' do value = { a: 1, b: 2, c: { d: 3, e: 4 }} schema = Compel.hash.is(value) @@ -436,10 +626,22 @@ expect(result.valid?).to be true end end + context '#length' do + + it 'should validate empty keys with errors' do + result = Compel.hash.length(2).validate({ a: 1 }) + + expect(result.valid?).to be false + expect(result.errors[:base]).to \ + include('cannot have length different than 2') + end + + end + end context 'String' do it 'should validate Type schema' do @@ -448,80 +650,108 @@ expect(result.errors).to \ include("must match format ^\\d{4}-\\d{3}$") end + context '#url' do + + it 'should validate' do + result = Compel.string.url.validate('http://example.com') + + expect(result.valid?).to be true + end + + it 'should validate' do + result = Compel.string.url.validate('http://app.com/posts/1/comments') + + expect(result.valid?).to be true + end + + it 'should not validate' do + result = Compel.string.url.validate('www.example.com') + + expect(result.valid?).to be false + end + + it 'should not validate' do + result = Compel.string.url.validate('url') + + expect(result.valid?).to be false + end + + end + + context '#email' do + + it 'should validate' do + result = Compel.string.email.validate('example@gmail.com') + + expect(result.valid?).to be true + end + + it 'should not validate' do + result = Compel.string.email.validate('example@gmail') + + expect(result.valid?).to be false + end + + it 'should not validate' do + result = Compel.string.email.validate('email') + + expect(result.valid?).to be false + end + + end + end context 'Array' do - subject(:builder) { Compel.array } - it 'should validate nil without errors' do - result = builder.validate(nil) + result = Compel.array.validate(nil) expect(result.valid?).to be true end it 'should validate nil with errors' do - result = builder.required.validate(nil) + result = Compel.array.required.validate(nil) expect(result.errors[:base]).to include('is required') end it 'should validate with errors for invalid array' do - result = builder.required.validate(1) + result = Compel.array.required.validate(1) expect(result.errors[:base]).to include("'1' is not a valid Array") end context '#items' do it 'should validate without items' do - result = builder.validate([1, 2, 3]) + result = Compel.array.validate([1, 2, 3]) expect(result.valid?).to be true expect(result.value).to eq([1, 2, 3]) end - it 'should raise exception for invalid type' do - expect { builder.items('a') }.to \ - raise_error Compel::TypeError, "#items must be a valid Schema" - end - - it 'should raise exception for invalid type' do - expect { builder.items('a') }.to \ - raise_error Compel::TypeError, "#items must be a valid Schema" - end - - it 'should have value' do - builder.items(Compel.integer) - - expect(builder.options[:items].class).to be(Compel::Builder::Integer) - end - it 'should validate all items' do - builder.items(Compel.integer) + result = Compel.array.items(Compel.integer).validate([1, '2', nil]) - result = builder.validate([1, '2', nil]) - expect(result.valid?).to be true expect(result.value).to eq([1, 2]) end it 'should validate all items with errors' do - builder.items(Compel.float.required) + result = Compel.array.items(Compel.float.required).validate([1, 'a', nil]) - result = builder.validate([1, 'a', nil]) - expect(result.valid?).to be false expect(result.errors['1']).to include("'a' is not a valid Float") expect(result.errors['2']).to include('is required') end it 'should coerce all hash items' do - builder.items(Compel.hash.keys({ + builder = Compel.array.items(Compel.hash.keys({ a: Compel.string.required, b: Compel.integer })) result = builder.validate([ @@ -538,11 +768,11 @@ Hashie::Mash.new({ a: 'C', b: 3 }) ] end it 'should coerce all hash items with errors' do - builder.items(Compel.hash.keys({ + builder = Compel.array.items(Compel.hash.keys({ a: Compel.string.required, b: Compel.string.format(/^abc$/).required })) result = builder.validate([ @@ -567,11 +797,11 @@ expect(result.value[2][:errors][:b]).to \ include('must match format ^abc$') end it 'should coerce array with hash items and nested array keys with errors' do - builder.items(Compel.hash.keys({ + builder = Compel.array.items(Compel.hash.keys({ a: Compel.string, b: Compel.array.items(Compel.integer.required).required })) result = builder.validate([ @@ -624,55 +854,33 @@ context '#is' do it 'should validate with errors' do value = [1, 2, 3] - schema = builder.is(value) - result = schema.validate([1, 2]) + result = Compel.array.is(value).validate([1, 2]) expect(result.valid?).to be false expect(result.errors[:base]).to include("must be #{value}") end it 'should validate without errors' do - schema = builder.is(['a', 'b', 'c']) - result = schema.validate(['a', 'b', 'c']) + result = Compel.array.is(['a', 'b', 'c']).validate(['a', 'b', 'c']) expect(result.valid?).to be true end end end - context 'Hash' do + context 'DateTime' do - subject(:builder) { Compel.hash } + it 'should validate with errors' do + result = Compel.datetime.validate('1989-0') - it 'should validate empty keys option' do - schema = builder.required - - expect(schema.validate({ a: 1 }).valid?).to be true - end - - it 'should validate nil' do - schema = builder.required - - result = schema.validate(nil) - expect(result.valid?).to be false - expect(result.errors[:base]).to \ - include('is required') - end - - it 'should validate empty keys with errors' do - schema = builder.required.length(2) - - result = schema.validate({ a: 1 }) - - expect(result.valid?).to be false - expect(result.errors[:base]).to \ - include('cannot have length different than 2') + expect(result.errors).to \ + include("'1989-0' is not a parsable datetime with format: %FT%T") end end end