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)
Esempio n. 2
0
  def test_one_of_type_generator(self):
    # options
    data = fake(schema.one_of(schema.boolean, schema.number(1), schema.number(0)))
    self.assertIn(data, (True, False, 1, 0))

    # overriding
    data = fake(schema.one_of(schema.string('true'), schema.string('false')), 'true')
    self.assertEqual(data, 'true')
    def test_it_validates_options_with_nullable_type(self):
        option1 = schema.object({'id': schema.integer})
        option2 = schema.object({'id': schema.string.numeric})
        option3 = schema.null

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

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

        self.assertValidationFails(False,
                                   schema.one_of(option1, option2, option3))
        self.assertValidationFails(0, schema.one_of(option1, option2, option3))
        self.assertValidationFails('', schema.one_of(option1, option2,
                                                     option3))
        self.assertValidationFails([], schema.one_of(option1, option2,
                                                     option3))
        self.assertValidationFails({}, schema.one_of(option1, option2,
                                                     option3))
    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.one_of(option1, option2).nullable)
            self.assertValidationPasses({'id': 42},
                                        schema.one_of(option1,
                                                      option2).nullable)
            self.assertValidationPasses({'id': '42'},
                                        schema.one_of(option1,
                                                      option2).nullable)

            self.assertValidationFails(
                False,
                schema.one_of(option1, option2).nullable)
            self.assertValidationFails(
                0,
                schema.one_of(option1, option2).nullable)
            self.assertValidationFails(
                '',
                schema.one_of(option1, option2).nullable)
            self.assertValidationFails([],
                                       schema.one_of(option1,
                                                     option2).nullable)
            self.assertValidationFails({},
                                       schema.one_of(option1,
                                                     option2).nullable)
  def test_it_validates_nullable(self):
    option1 = schema.object({'id': schema.integer})
    option2 = schema.object({'id': schema.string.numeric})

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

    self.assertValidationFails(False, schema.one_of(option1, option2).nullable)
    self.assertValidationFails(0,     schema.one_of(option1, option2).nullable)
    self.assertValidationFails('',    schema.one_of(option1, option2).nullable)
    self.assertValidationFails([],    schema.one_of(option1, option2).nullable)
    self.assertValidationFails({},    schema.one_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.object({'deleted': schema.boolean})

    self.assertValidationPasses({'id': 42},         schema.one_of(option1, option2))
    self.assertValidationPasses({'id': '42'},       schema.one_of(option1, option2))
    self.assertValidationPasses({'id': 42},         schema.one_of(option1, option3))
    self.assertValidationPasses({'deleted': False}, schema.one_of(option1, option3))

    self.assertValidationFails({'id': None},                 schema.one_of(option1, option2))
    self.assertValidationFails({'id': 42, 'deleted': False}, schema.one_of(option1, option3))
    def test_it_validates_options(self):
        option1 = schema.object({'id': schema.integer})
        option2 = schema.object({'id': schema.string.numeric})
        option3 = schema.object({'deleted': schema.boolean})

        self.assertValidationPasses({'id': 42},
                                    schema.one_of(option1, option2))
        self.assertValidationPasses({'id': '42'},
                                    schema.one_of(option1, option2))
        self.assertValidationPasses({'id': 42},
                                    schema.one_of(option1, option3))
        self.assertValidationPasses({'deleted': False},
                                    schema.one_of(option1, option3))

        self.assertValidationFails({'id': None},
                                   schema.one_of(option1, option2))
        self.assertValidationFails({
            'id': 42,
            'deleted': False
        }, schema.one_of(option1, option3))
  def test_one_of_type_representation(self):
    self.assertRepr(schema.one_of(schema.integer, schema.string.numeric),
                   'schema.one_of(schema.integer, schema.string.numeric)')

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