test/schemacop_validator_test.rb in schemacop-1.0.0 vs test/schemacop_validator_test.rb in schemacop-1.0.1

- old
+ new

@@ -8,14 +8,17 @@ assert_schema({ type: :hash, null: true }, nil) end def test_short_type_schema assert_schema({ type: :array, array: :string }, %w(Hello world)) + assert_schema({ type: [Array], array: :string }, %w(Hello world)) assert_schema({ array: :string }, %w(Hello world)) assert_schema({ hash: { first_name: :string, last_name: :string } }, first_name: 'John', last_name: 'Doe') - assert_schema_failure({ type: :array }, %w(Hello world)) + assert_schema_failure({ type: :array, array: :integer }, %w(Hello world)) + assert_schema_failure({ type: Array, array: :string }, [1, 2, 3]) + assert_schema_failure({ type: [Array], array: :integer }, %w(Hello world)) end def test_many_types_schema assert_schema({ type: [String, :integer] }, 'Hello world') assert_schema({ type: [:string, 'integer'] }, 123) @@ -48,10 +51,67 @@ assert_schema(schema_rules, id: 123, name: 'John Doe', meta: { groups: [1, 2, 3], birthday: Date.today, comment: 'Hello world' }) assert_schema(schema_rules, id: 'XYZ', name: 'John Doe', meta: { groups: [1, 2, 3], birthday: Date.today }) end + def test_hash_without_specification + schema_rules = { + type: :hash + } + + assert_schema(schema_rules, user: 'hello') + assert_schema(schema_rules, user: :world) + assert_schema(schema_rules, user: 123) + end + + def test_array_without_specification + schema_rules = { + type: :array + } + + assert_schema(schema_rules, [1, 'hello', :world]) + assert_schema(schema_rules, []) + end + + def test_hash_attributes_without_specification + schema_rules = { + type: :hash, + hash: { + user: { type: :hash, null: true } + } + } + + assert_schema(schema_rules, user: { foo: :bar }) + assert_schema(schema_rules, user: nil) + end + + def test_array_in_hash_without_specification + schema_rules = { + type: Hash, + hash: { + types: { type: :array, null: true } + } + } + + assert_schema(schema_rules, types: ['test', 1, :hello]) + assert_schema(schema_rules, types: []) + assert_schema(schema_rules, types: nil) + end + + def test_array_in_hash_with_multiple_types + schema_rules = { + type: :hash, + hash: { + types: { type: Array, array: [Integer, String] } + } + } + + assert_schema(schema_rules, types: ['test', 1]) + assert_schema(schema_rules, types: []) + assert_schema_failure(schema_rules, types: ['test', 1, :hello_world]) + end + def test_nested_hash_schema schema_rules = { type: :hash, hash: { name: { @@ -75,17 +135,17 @@ type: :string, null: true } } } - assert_schema schema_rules, name: 'John Doe' - assert_schema schema_rules, name: '' - assert_schema schema_rules, name: nil + assert_schema(schema_rules, name: 'John Doe') + assert_schema(schema_rules, name: '') + assert_schema(schema_rules, name: nil) - assert_schema_failure schema_rules, name: 123 - assert_schema_failure schema_rules, name: true - assert_schema_failure schema_rules, name: [] + assert_schema_failure(schema_rules, name: 123) + assert_schema_failure(schema_rules, name: true) + assert_schema_failure(schema_rules, name: []) end def test_array_hash_old_schema schema_rules = { type: :array, @@ -96,17 +156,17 @@ name: { type: :string } } } } - assert_schema schema_rules, [{ name: 'John', required: true }, { name: 'Alex', required: false }] + assert_schema(schema_rules, [{ name: 'John', required: true }, { name: 'Alex', required: false }]) - assert_schema_failure schema_rules, name: 'John Doe' - assert_schema_failure schema_rules, [{ name: 'John Doe' }] - assert_schema_failure schema_rules, [{ required: true }] - assert_schema_failure schema_rules, [{ name: 0, required: true }] - assert_schema_failure schema_rules, [{ name: 'John Doe', required: 'string' }] + assert_schema_failure(schema_rules, name: 'John Doe') + assert_schema_failure(schema_rules, [{ name: 'John Doe' }]) + assert_schema_failure(schema_rules, [{ required: true }]) + assert_schema_failure(schema_rules, [{ name: 0, required: true }]) + assert_schema_failure(schema_rules, [{ name: 'John Doe', required: 'string' }]) end def test_fields_array_old_schema schema_rules = { type: :hash, @@ -118,16 +178,16 @@ } } } } - assert_schema schema_rules, ids: [] - assert_schema schema_rules, ids: [1, 2, 3] + assert_schema(schema_rules, ids: []) + assert_schema(schema_rules, ids: [1, 2, 3]) - assert_schema_failure schema_rules, ids: [1, '2'] - assert_schema_failure schema_rules, ids: '3' - assert_schema_failure schema_rules, ids: nil + assert_schema_failure(schema_rules, ids: [1, '2']) + assert_schema_failure(schema_rules, ids: '3') + assert_schema_failure(schema_rules, ids: nil) end def test_validate_fields_old_schema schema_rules = { type: :hash, @@ -138,17 +198,17 @@ ids: { type: :array, array: { type: :integer }, null: true }, currency: { type: :string, allowed_values: %w(CHF USD EUR) } } } - assert_schema schema_rules, name: 'John', id: 2, ids: [1, 2, 3], currency: 'CHF' - assert_schema schema_rules, name: nil, id: 2, ids: [1, 2, 3], currency: 'CHF' - assert_schema schema_rules, name: nil, age: 2, id: 2, ids: nil, currency: 'CHF' - assert_schema schema_rules, name: nil, id: 2, ids: nil, currency: 'CHF' + assert_schema(schema_rules, name: 'John', id: 2, ids: [1, 2, 3], currency: 'CHF') + assert_schema(schema_rules, name: nil, id: 2, ids: [1, 2, 3], currency: 'CHF') + assert_schema(schema_rules, name: nil, age: 2, id: 2, ids: nil, currency: 'CHF') + assert_schema(schema_rules, name: nil, id: 2, ids: nil, currency: 'CHF') - assert_schema_failure schema_rules, id: 2, ids: [1, 2, 3], currency: 'CHF' - assert_schema_failure schema_rules, name: nil, id: nil, ids: [1, 2, 3], currency: 'CHF' - assert_schema_failure schema_rules, name: nil, id: 2, ids: [1, 2, 3], currency: 'JPN' + assert_schema_failure(schema_rules, id: 2, ids: [1, 2, 3], currency: 'CHF') + assert_schema_failure(schema_rules, name: nil, id: nil, ids: [1, 2, 3], currency: 'CHF') + assert_schema_failure(schema_rules, name: nil, id: 2, ids: [1, 2, 3], currency: 'JPN') end def test_nested_old_schema schema_rules = { type: :array,