Skip to content

Commit

Permalink
Merge pull request #168 from pd/test-helpers
Browse files Browse the repository at this point in the history
Add some test helpers for common patterns
  • Loading branch information
pd committed Nov 1, 2014
2 parents 90b40eb + dafdfd6 commit fef4526
Show file tree
Hide file tree
Showing 35 changed files with 1,017 additions and 3,239 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ rvm:
- "2.1"
- "jruby"
- "rbx"
install:
- bundle install --retry=3
matrix:
include:
- rvm: "2.1"
Expand Down
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ source "https://rubygems.org"

gemspec

gem "json", :platform => :ruby_18
gem "json", :platforms => :mri_18

group :development do
gem "rake"
gem "minitest", '~> 5.0'
end
39 changes: 19 additions & 20 deletions lib/json-schema/attributes/additionalproperties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,33 @@ def self.validate(current_schema, data, fragments, processor, validator, options
end

def self.remove_valid_properties(extra_properties, current_schema, validator)
if current_schema.schema['properties']
extra_properties = extra_properties - current_schema.schema['properties'].keys
end

if current_schema.schema['properties']
extra_properties = extra_properties - current_schema.schema['properties'].keys
end

if current_schema.schema['patternProperties']
current_schema.schema['patternProperties'].each_key do |key|
r = Regexp.new(key)
extras_clone = extra_properties.clone
extras_clone.each do |prop|
if r.match(prop)
extra_properties = extra_properties - [prop]
end
if current_schema.schema['patternProperties']
current_schema.schema['patternProperties'].each_key do |key|
r = Regexp.new(key)
extras_clone = extra_properties.clone
extras_clone.each do |prop|
if r.match(prop)
extra_properties = extra_properties - [prop]
end
end
end
end

if schemas= current_schema.schema['extends']
schemas = [schemas] if !schemas.is_a?(Array)
schemas.each do |schema_value|
temp_uri,extended_schema= JSON::Schema::ExtendsAttribute.get_extended_uri_and_schema(schema_value, current_schema, validator)
if extended_schema
extra_properties= remove_valid_properties(extra_properties, extended_schema, validator)
end
if schemas = current_schema.schema['extends']
schemas = [schemas] if !schemas.is_a?(Array)
schemas.each do |schema_value|
_, extended_schema = JSON::Schema::ExtendsAttribute.get_extended_uri_and_schema(schema_value, current_schema, validator)
if extended_schema
extra_properties = remove_valid_properties(extra_properties, extended_schema, validator)
end
end
end

extra_properties
extra_properties
end

end
Expand Down
6 changes: 2 additions & 4 deletions lib/json-schema/attributes/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,9 @@ def self.validate(current_schema, data, fragments, processor, validator, options

# Lookup Schema type of given class instance
def self.type_of_data(data)
type, klass = TYPE_CLASS_MAPPINGS.map { |k,v| [k,v] }.sort_by { |i|
k,v = i
type, _ = TYPE_CLASS_MAPPINGS.map { |k,v| [k,v] }.sort_by { |(_, v)|
-Array(v).map { |klass| klass.ancestors.size }.max
}.find { |i|
k,v = i
}.find { |(_, v)|
Array(v).any? { |klass| data.kind_of?(klass) }
}
type
Expand Down
108 changes: 108 additions & 0 deletions test/support/array_validation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
module ArrayValidation
module ItemsTests
def test_items_single_schema
schema = { 'items' => { 'type' => 'string' } }

assert_valid schema, []
assert_valid schema, ['a']
assert_valid schema, ['a', 'b']

refute_valid schema, [1]
refute_valid schema, ['a', 1]

# other types are disregarded
assert_valid schema, {'a' => 'foo'}
end

def test_items_multiple_schemas
schema = {
'items' => [
{ 'type' => 'string' },
{ 'type' => 'integer' }
]
}

assert_valid schema, ['b', 1]
assert_valid schema, ['b', 1, nil]
refute_valid schema, [1, 'b']
refute_valid schema, []
end

def test_minitems
schema = { 'minItems' => 1 }

assert_valid schema, [1]
assert_valid schema, [1, 2]
refute_valid schema, []

# other types are disregarded
assert_valid schema, 5
end

def test_maxitems
schema = { 'maxItems' => 1 }

assert_valid schema, []
assert_valid schema, [1]
refute_valid schema, [1, 2]

# other types are disregarded
assert_valid schema, 5
end
end

module AdditionalItemsTests
def test_additional_items_false
schema = {
'items' => [
{ 'type' => 'integer' },
{ 'type' => 'string' }
],
'additionalItems' => false
}

assert_valid schema, [1, 'string']
refute_valid schema, [1, 'string', 2]
refute_valid schema, ['string', 1]
end

def test_additional_items_schema
schema = {
'items' => [
{ 'type' => 'integer' },
{ 'type' => 'string' }
],
'additionalItems' => { 'type' => 'integer' }
}

assert_valid schema, [1, 'string']
assert_valid schema, [1, 'string', 2]
refute_valid schema, [1, 'string', 'string']
end
end

module UniqueItemsTests
def test_unique_items
schema = { 'uniqueItems' => true }

assert_valid schema, [nil, 5]
refute_valid schema, [nil, nil]

assert_valid schema, [true, false]
refute_valid schema, [true, true]

assert_valid schema, [4, 4.1]
refute_valid schema, [4, 4]

assert_valid schema, ['a', 'ab']
refute_valid schema, ['a', 'a']

assert_valid schema, [[1], [2]]
refute_valid schema, [[1], [1]]

assert_valid schema, [{'b' => 1}, {'c' => 2}]
assert_valid schema, [{'b' => 1}, {'c' => 1}]
refute_valid schema, [{'b' => 1}, {'b' => 1}]
end
end
end
93 changes: 93 additions & 0 deletions test/support/number_validation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
module NumberValidation
module MinMaxTests
def test_minimum
schema = {
'properties' => {
'a' => { 'minimum' => 5 }
}
}

assert_valid schema, {'a' => 5}
assert_valid schema, {'a' => 6}

refute_valid schema, {'a' => 4}
refute_valid schema, {'a' => 4.99999}

# other types are disregarded
assert_valid schema, {'a' => 'str'}
end

def test_exclusive_minimum
schema = {
'properties' => {
'a' => { 'minimum' => 5 }.merge(exclusive_minimum)
}
}

assert_valid schema, {'a' => 6}
assert_valid schema, {'a' => 5.0001}
refute_valid schema, {'a' => 5}
end

def test_maximum
schema = {
'properties' => {
'a' => { 'maximum' => 5 }
}
}

assert_valid schema, {'a' => 4}
assert_valid schema, {'a' => 5}

refute_valid schema, {'a' => 6}
refute_valid schema, {'a' => 5.0001}
end

def test_exclusive_maximum
schema = {
'properties' => {
'a' => { 'maximum' => 5 }.merge(exclusive_maximum)
}
}

assert_valid schema, {'a' => 4}
assert_valid schema, {'a' => 4.99999}
refute_valid schema, {'a' => 5}
end
end

# draft3 introduced `divisibleBy`, renamed to `multipleOf` in draft4.
# Favor the newer name, but the behavior should be identical.
module MultipleOfTests
def multiple_of
'multipleOf'
end

def test_multiple_of
schema = {
'properties' => {
'a' => { multiple_of => 1.1 }
}
}

assert_valid schema, {'a' => 0}

assert_valid schema, {'a' => 2.2}
refute_valid schema, {'a' => 3.4}

# other types are disregarded
assert_valid schema, {'a' => 'hi'}
end

def test_multiple_of_zero
schema = {
'properties' => {
'a' => { multiple_of => 0 }
}
}

refute_valid schema, {'a' => 5}
refute_valid schema, {'a' => 0}
end
end
end
68 changes: 68 additions & 0 deletions test/support/object_validation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module ObjectValidation
module AdditionalPropertiesTests
def test_additional_properties_false
schema = {
'properties' => {
'a' => { 'type' => 'integer' }
},
'additionalProperties' => false
}

assert_valid schema, {'a' => 1}
refute_valid schema, {'a' => 1, 'b' => 2}
end

def test_additional_properties_schema
schema = {
'properties' => {
'a' => { 'type' => 'integer' }
},
'additionalProperties' => { 'type' => 'string' }
}

assert_valid schema, {'a' => 1}
assert_valid schema, {'a' => 1, 'b' => 'hi'}
refute_valid schema, {'a' => 1, 'b' => 2}
end
end

module PatternPropertiesTests
def test_pattern_properties
schema = {
'patternProperties' => {
"\\d+ taco" => { 'type' => 'integer' }
}
}

assert_valid schema, {'1 taco' => 1, '20 taco' => 20}
assert_valid schema, {'foo' => true, '1 taco' => 1}
refute_valid schema, {'1 taco' => 'yum'}
end

def test_pattern_properties_additional_properties_false
schema = {
'patternProperties' => {
"\\d+ taco" => { 'type' => 'integer' }
},
'additionalProperties' => false
}

assert_valid schema, {'1 taco' => 1}
refute_valid schema, {'1 taco' => 'yum'}
refute_valid schema, {'1 taco' => 1, 'foo' => true}
end

def test_pattern_properties_additional_properties_schema
schema = {
'patternProperties' => {
"\\d+ taco" => { 'type' => 'integer' }
},
'additionalProperties' => { 'type' => 'string' }
}

assert_valid schema, {'1 taco' => 1}
assert_valid schema, {'1 taco' => 1, 'foo' => 'bar'}
refute_valid schema, {'1 taco' => 1, 'foo' => 2}
end
end
end
Loading

0 comments on commit fef4526

Please sign in to comment.