def test_it_validates_value(self): self.assertValidationPasses(True, schema.boolean(True)) self.assertValidationPasses(False, schema.boolean(False)) self.assertValidationFails(None, schema.boolean(False)) self.assertValidationFails(True, schema.boolean(False)) self.assertValidationFails(False, schema.boolean(True))
def test_it_validates_nullable(self): self.assertValidationPasses(None, schema.boolean(True).nullable) self.assertValidationPasses(False, schema.boolean(False).nullable) self.assertValidationPasses(True, schema.boolean.nullable) self.assertValidationFails(0, schema.boolean.nullable) self.assertValidationFails('', schema.boolean.nullable) self.assertValidationFails([], schema.boolean.nullable) self.assertValidationFails({}, schema.boolean.nullable)
def test_it_validates_all_occurrences(self): array_schema = schema.array.contains_all( [schema.boolean(True), schema.boolean(False)]) self.assertValidationPasses([False, True], array_schema) self.assertValidationPasses([False, True, True, None], array_schema) self.assertValidationFails([False, False], array_schema) self.assertValidationFails([], array_schema)
def test_it_validates_nullable(self): with warnings.catch_warnings(): warnings.simplefilter('ignore') self.assertValidationPasses(None, schema.boolean(True).nullable) self.assertValidationPasses(False, schema.boolean(False).nullable) self.assertValidationPasses(True, schema.boolean.nullable) self.assertValidationFails(0, schema.boolean.nullable) self.assertValidationFails('', schema.boolean.nullable) self.assertValidationFails([], schema.boolean.nullable) self.assertValidationFails({}, schema.boolean.nullable)
def test_it_validates_keys(self): self.assertValidationPasses({}, schema.object({})) self.assertValidationPasses( { 'string': 'banana', 'number': 42, 'array': [0, 1], 'empty_object': {}, 'object': { 'boolean': True, 'object': { 'nothing': None } } }, schema.object({ 'string': schema.string('banana'), 'number': schema.number(42), 'array': schema.array([schema.number(0), schema.number(1)]), 'empty_object': schema.object({}), 'object': schema.object({ 'boolean': schema.boolean(True), 'object': schema.object({ 'nothing': schema.null }) }) }) ) self.assertValidationFails(None, schema.object({})) self.assertValidationFails({}, schema.object({'id': schema.integer})) self.assertValidationFails({'id': '1'}, schema.object({'id': schema.integer}))
def test_one_of_type_substitution(self): self.assertSchemaCloned(schema.one_of(schema.boolean(False), schema.array), False) integer_or_numeric = schema.one_of(schema.integer, schema.string.numeric) value = '1234' self.assertIsInstance(integer_or_numeric % value, schema.types.String) self.assertSchemaHasValue(integer_or_numeric % value, value)
def test_boolean_type_generator(self): # type data = fake(schema.boolean) self.assertIn(data, [True, False]) # overriding data = fake(schema.boolean(False), True) self.assertTrue(data) # example data = fake(schema.boolean.example(True)) self.assertTrue(data)
def test_it_validates_options(self): option1 = schema.object({'id': schema.integer}) option2 = schema.object({'id': schema.string.numeric}) option3 = schema.boolean(False) option4 = schema.object({'deleted': schema.boolean}) self.assertValidationPasses({'id': 42}, schema.any_of(option1, option2)) self.assertValidationPasses({'id': '42'}, schema.any_of(option1, option2)) self.assertValidationPasses({'id': 42}, schema.any_of(option1, option3)) self.assertValidationPasses(False, schema.any_of(option1, option3)) self.assertValidationPasses({'id': 42, 'deleted': False}, schema.any_of(option1, option4)) self.assertValidationFails({}, schema.any_of(option1, option2)) self.assertValidationFails({'id': None}, schema.any_of(option1, option2))
def test_it_validates_options(self): option1 = schema.object({'id': schema.integer}) option2 = schema.object({'id': schema.string.numeric}) option3 = schema.boolean(False) option4 = schema.object({'deleted': schema.boolean}) self.assertValidationPasses({'id': 42}, schema.any_of(option1, option2)) self.assertValidationPasses({'id': '42'}, schema.any_of(option1, option2)) self.assertValidationPasses({'id': 42}, schema.any_of(option1, option3)) self.assertValidationPasses(False, schema.any_of(option1, option3)) self.assertValidationPasses({ 'id': 42, 'deleted': False }, schema.any_of(option1, option4)) self.assertValidationFails({}, schema.any_of(option1, option2)) self.assertValidationFails({'id': None}, schema.any_of(option1, option2))
def test_boolean_type_representation(self): self.assertRepr(schema.boolean, 'schema.boolean') self.assertRepr(schema.boolean(True), 'schema.boolean(True)') self.assertRepr(schema.boolean.nullable, 'schema.boolean.nullable')