コード例 #1
0
    def test_regex_constraint(self):
        """
        Test regex pattern validation.
        """

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

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

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

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

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

        # Since 'fruit' has oneOf 'apple', 'banana' and 'apple' is nullable,
        # validate we can create a fruit with the 'null' value.
        fruit = Fruit(None)
        self.assertIsNone(fruit)

        # Redo the same thing, this time passing a null Apple to the Fruit constructor.
        fruit = Fruit(apple.Apple(None))
        self.assertIsNone(fruit)
コード例 #3
0
    def test_regex_constraint(self):
        """
        Test regex pattern validation.
        """
        from petstore_api.model import apple

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

        inst = apple.Apple(cultivar="Golden Delicious", origin="cHiLe")
        assert isinstance(inst, apple.Apple)

        # Test with invalid regex pattern.
        err_regex = r"Invalid value `.+?`, must match regular expression `.+?` at \('args\[0\]', 'cultivar'\)"
        with self.assertRaisesRegex(petstore_api.ApiValueError, err_regex):
            inst = apple.Apple(cultivar="!@#%@$#Akane")

        err_regex = r"Invalid value `.+?`, must match regular expression `.+?` at \('args\[0\]', 'origin'\)"
        with self.assertRaisesRegex(petstore_api.ApiValueError, err_regex):
            inst = apple.Apple(cultivar="Golden Delicious",
                               origin="!@#%@$#Chile")
コード例 #4
0
    def testFruitNullValue(self):
        # Since 'apple' is nullable, validate we can create an apple with the 'null' value.
        fruit = apple.Apple(None)
        assert isinstance(fruit, Singleton)
        assert isinstance(fruit, apple.Apple)
        assert fruit.is_none() is True

        # 'banana' is not nullable.
        # TODO cast this into ApiTypeError?
        with self.assertRaises(TypeError):
            banana.Banana(None)

        # Since 'fruit' has oneOf 'apple', 'banana' and 'apple' is nullable,
        # validate we can create a fruit with the 'null' value.
        fruit = Fruit(None)
        assert isinstance(fruit, Singleton)
        assert isinstance(fruit, apple.Apple)
        assert isinstance(fruit, Fruit)
        assert fruit.is_none() is True