Ejemplo n.º 1
0
    def test_regex_constraint(self):
        """
        Test regex pattern validation.
        """

        # Test with valid regex pattern.
        inst = petstore_api.Apple(cultivar="Akane")
        assert isinstance(inst, petstore_api.Apple)

        inst = petstore_api.Apple(origin="cHiLe")
        assert isinstance(inst, petstore_api.Apple)

        # Test with invalid regex pattern.
        err_msg = (
            "Invalid value for `{}`, must match regular expression `{}`$")
        with self.assertRaisesRegexp(petstore_api.ApiValueError,
                                     err_msg.format("cultivar", "[^`]*")):
            inst = petstore_api.Apple(cultivar="!@#%@$#Akane")

        err_msg = (
            "Invalid value for `{}`, must match regular expression `{}` with flags"
        )
        with self.assertRaisesRegexp(petstore_api.ApiValueError,
                                     err_msg.format("origin", "[^`]*")):
            inst = petstore_api.Apple(origin="!@#%@$#Chile")
Ejemplo n.º 2
0
    def testFruitNullValue(self):
        # Since 'apple' is nullable, validate we can create an apple with the 'null' value.
        apple = petstore_api.Apple(None)
        self.assertIsNone(apple)

        # 'banana' is not nullable.
        with self.assertRaises(petstore_api.ApiTypeError):
            banana = petstore_api.Banana(None)

        # Since 'fruit' has oneOf 'apple', 'banana' and 'apple' is nullable,
        # validate we can create a fruit with the 'null' value.
        fruit = petstore_api.Fruit(None)
        self.assertIsNone(fruit)
 
        # Redo the same thing, this time passing a null Apple to the Fruit constructor.
        fruit = petstore_api.Fruit(petstore_api.Apple(None))
        self.assertIsNone(fruit)