def test_any_of_type_substitution(self):
    self.assertSchemaCloned(schema.any_of(schema.boolean(False), schema.array), False)

    integer_or_numeric = schema.any_of(schema.integer, schema.string.numeric)
    value = '1234'
    self.assertIsInstance(integer_or_numeric % value, schema.types.String)
    self.assertSchemaHasValue(integer_or_numeric % value, value)
Exemplo n.º 2
0
  def test_any_of_type_generator(self):
    # options
    data = fake(schema.any_of(schema.number(1), schema.number(2)))
    self.assertIn(data, (1, 2))

    # overriding
    data = fake(schema.any_of(schema.integer, schema.string.numeric), '1')
    self.assertEqual(data, '1')
Exemplo n.º 3
0
    def test_it_validates_options_with_null_type(self):
        option1 = schema.object({'id': schema.integer})
        option2 = schema.object({'id': schema.string.numeric})
        option3 = schema.null

        self.assertValidationPasses(None,
                                    schema.any_of(option1, option2, option3))
        self.assertValidationPasses({'id': 42},
                                    schema.any_of(option1, option2, option3))
        self.assertValidationPasses({'id': '42'},
                                    schema.any_of(option1, option2, option3))
  def test_it_validates_nullable(self):
    option1 = schema.object({'id': schema.integer})
    option2 = schema.object({'id': schema.string.numeric})

    self.assertValidationPasses(None,         schema.any_of(option1, option2).nullable)
    self.assertValidationPasses({'id': 42},   schema.any_of(option1, option2).nullable)
    self.assertValidationPasses({'id': '42'}, schema.any_of(option1, option2).nullable)

    self.assertValidationFails(False, schema.any_of(option1, option2).nullable)
    self.assertValidationFails(0,     schema.any_of(option1, option2).nullable)
    self.assertValidationFails('',    schema.any_of(option1, option2).nullable)
    self.assertValidationFails([],    schema.any_of(option1, option2).nullable)
    self.assertValidationFails({},    schema.any_of(option1, option2).nullable)
  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))
Exemplo n.º 6
0
    def test_it_validates_nullable(self):
        with warnings.catch_warnings():
            warnings.simplefilter('ignore')

            option1 = schema.object({'id': schema.integer})
            option2 = schema.object({'id': schema.string.numeric})

            self.assertValidationPasses(
                None,
                schema.any_of(option1, option2).nullable)
            self.assertValidationPasses({'id': 42},
                                        schema.any_of(option1,
                                                      option2).nullable)
            self.assertValidationPasses({'id': '42'},
                                        schema.any_of(option1,
                                                      option2).nullable)

            self.assertValidationPasses(
                None,
                schema.any_of(option1, option2, schema.null).nullable)

            self.assertValidationFails(
                False,
                schema.any_of(option1, option2).nullable)
            self.assertValidationFails(
                0,
                schema.any_of(option1, option2).nullable)
            self.assertValidationFails(
                '',
                schema.any_of(option1, option2).nullable)
            self.assertValidationFails([],
                                       schema.any_of(option1,
                                                     option2).nullable)
            self.assertValidationFails({},
                                       schema.any_of(option1,
                                                     option2).nullable)
Exemplo n.º 7
0
    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_any_of_type_representation(self):
    self.assertRepr(schema.any_of(schema.integer, schema.string.numeric),
                   'schema.any_of(schema.integer, schema.string.numeric)')

    self.assertRepr(schema.any_of(schema.integer(0), schema.integer(1)).nullable,
                   'schema.any_of(schema.integer(0), schema.integer(1)).nullable')