예제 #1
0
    def test_invalid_inputs(self):
        # including extra parameters raises an exception
        with self.assertRaises(petstore_api.ApiValueError):
            fruit = FruitReq(
                length_cm=self.length_cm,
                unknown_property='some value'
            )

        # including input parameters for two oneOf instances raise an exception
        with self.assertRaises(petstore_api.ApiValueError):
            fruit = FruitReq(
                length_cm=self.length_cm,
                cultivar='granny smith'
            )
예제 #2
0
    def test_FruitReq_apple(self):
        """Test FruitReq"""

        # apple test
        cultivar = 'golden delicious'
        fruit = FruitReq(cultivar=cultivar)
        # check its properties
        self.assertEqual(fruit.cultivar, cultivar)
        self.assertEqual(fruit['cultivar'], cultivar)
        self.assertEqual(getattr(fruit, 'cultivar'), cultivar)
        # check the dict representation
        self.assertEqual(
            fruit.to_dict(),
            {
                'cultivar': cultivar
            }
        )

        # model._composed_instances is a list of the instances that were
        # made from the anyOf/allOf/OneOf classes in model._composed_schemas
        for composed_instance in fruit._composed_instances:
            if composed_instance.__class__ == apple_req.AppleReq:
                apple_instance = composed_instance
        self.assertEqual(
            fruit._composed_instances,
            [apple_instance]
        )
        # model._var_name_to_model_instances maps the variable name to the
        # model instances which store that variable
        self.assertEqual(
            fruit._var_name_to_model_instances,
            {
                'cultivar': [fruit, apple_instance],
            }
        )
        self.assertEqual(
            fruit._additional_properties_model_instances, [fruit]
        )
예제 #3
0
    def test_fruit_access_errors(self):
        fruit = FruitReq(length_cm=self.length_cm)

        # setting a value that doesn't exist raises an exception
        # with a key
        with self.assertRaises(AttributeError):
            fruit['invalid_variable'] = 'some value'
        # with setattr
        with self.assertRaises(AttributeError):
            setattr(fruit, 'invalid_variable', 'some value')

        # getting a value that doesn't exist raises an exception
        # with a key
        with self.assertRaises(AttributeError):
            invalid_variable = fruit['cultivar']

        with self.assertRaises(AttributeError):
            getattr(fruit, 'cultivar')
예제 #4
0
    def testFruitReq(self):
        """Test FruitReq"""

        # make an instance of Fruit, a composed schema oneOf model
        # banana test
        length_cm = 20.3
        fruit = FruitReq(lengthCm=length_cm)
        # check its properties
        self.assertEqual(fruit.lengthCm, length_cm)
        self.assertEqual(fruit['lengthCm'], length_cm)
        self.assertEqual(getattr(fruit, 'lengthCm'), length_cm)
        # check the dict representation
        self.assertEqual(fruit, {
            'lengthCm': length_cm,
        })
        # setting values after instance creation is not allowed
        with self.assertRaises(TypeError):
            fruit['lengthCm'] = 'some value'

        # setting values after instance creation is not allowed
        with self.assertRaises(AttributeError):
            setattr(fruit, 'lengthCm', 'some value')

        # getting a value that doesn't exist raises an exception
        # with a key
        with self.assertRaises(KeyError):
            invalid_variable = fruit['cultivar']

        # with getattr
        self.assertEqual(getattr(fruit, 'cultivar', 'some value'),
                         'some value')

        with self.assertRaises(AttributeError):
            getattr(fruit, 'cultivar')

        # make sure that the ModelComposed class properties are correct
        # model._composed_schemas stores the anyOf/allOf/oneOf info
        self.assertEqual(
            fruit._composed_schemas, {
                'anyOf': [],
                'allOf': [],
                'oneOf': [
                    NoneSchema,
                    apple_req.AppleReq,
                    banana_req.BananaReq,
                ],
                'not': None
            })

        # including extra parameters raises an exception
        with self.assertRaises(petstore_api.ApiValueError):
            fruit = FruitReq(length_cm=length_cm,
                             unknown_property='some value')

        # including input parameters for two oneOf instances raise an exception
        with self.assertRaises(petstore_api.ApiValueError):
            fruit = FruitReq(length_cm=length_cm, cultivar='granny smith')

        # make an instance of Fruit, a composed schema oneOf model
        # apple test
        cultivar = 'golden delicious'
        fruit = FruitReq(cultivar=cultivar)
        # check its properties
        self.assertEqual(fruit.cultivar, cultivar)
        self.assertEqual(fruit['cultivar'], cultivar)
        self.assertEqual(getattr(fruit, 'cultivar'), cultivar)
        # check the dict representation
        self.assertEqual(fruit, {'cultivar': cultivar})

        # we can pass in None
        fruit = FruitReq(None)
        assert isinstance(fruit, Singleton)
        assert isinstance(fruit, FruitReq)
        assert isinstance(fruit, NoneSchema)
        assert fruit.is_none() is True
예제 #5
0
    def testFruitReq(self):
        """Test FruitReq"""

        # make an instance of Fruit, a composed schema oneOf model
        # banana test
        length_cm = 20.3
        fruit = FruitReq(length_cm=length_cm)
        # check its properties
        self.assertEqual(fruit.length_cm, length_cm)
        self.assertEqual(fruit['length_cm'], length_cm)
        self.assertEqual(getattr(fruit, 'length_cm'), length_cm)
        # check the dict representation
        self.assertEqual(
            fruit.to_dict(),
            {
                'length_cm': length_cm,
            }
        )
        # setting a value that doesn't exist raises an exception
        # with a key
        with self.assertRaises(AttributeError):
            fruit['invalid_variable'] = 'some value'
        # with setattr
        with self.assertRaises(AttributeError):
            setattr(fruit, 'invalid_variable', 'some value')

        # getting a value that doesn't exist raises an exception
        # with a key
        with self.assertRaises(AttributeError):
            invalid_variable = fruit['cultivar']
        # with getattr
        self.assertEqual(getattr(fruit, 'cultivar', 'some value'), 'some value')

        with self.assertRaises(AttributeError):
            getattr(fruit, 'cultivar')

        # make sure that the ModelComposed class properties are correct
        # model._composed_schemas stores the anyOf/allOf/oneOf info
        self.assertEqual(
            fruit._composed_schemas,
            {
                'anyOf': [],
                'allOf': [],
                'oneOf': [
                    apple_req.AppleReq,
                    banana_req.BananaReq,
                    type(None),
                ],
            }
        )
        # model._composed_instances is a list of the instances that were
        # made from the anyOf/allOf/OneOf classes in model._composed_schemas
        for composed_instance in fruit._composed_instances:
            if composed_instance.__class__ == banana_req.BananaReq:
                banana_instance = composed_instance
        self.assertEqual(
            fruit._composed_instances,
            [banana_instance]
        )
        # model._var_name_to_model_instances maps the variable name to the
        # model instances which store that variable
        self.assertEqual(
            fruit._var_name_to_model_instances,
            {
                'length_cm': [fruit, banana_instance],
                'cultivar': [fruit],
                'mealy': [fruit],
                'sweet': [fruit, banana_instance],
            }
        )
        # model._additional_properties_model_instances stores a list of
        # models which have the property additional_properties_type != None
        self.assertEqual(
            fruit._additional_properties_model_instances, []
        )

        # if we modify one of the properties owned by multiple
        # model_instances we get an exception when we try to access that
        # property because the retrieved values are not all the same
        banana_instance.length_cm = 4.56
        with self.assertRaises(petstore_api.ApiValueError):
            some_length_cm = fruit.length_cm

        # including extra parameters raises an exception
        with self.assertRaises(petstore_api.ApiValueError):
            fruit = FruitReq(
                length_cm=length_cm,
                unknown_property='some value'
            )

        # including input parameters for two oneOf instances raise an exception
        with self.assertRaises(petstore_api.ApiValueError):
            fruit = FruitReq(
                length_cm=length_cm,
                cultivar='granny smith'
            )

        # make an instance of Fruit, a composed schema oneOf model
        # apple test
        cultivar = 'golden delicious'
        fruit = FruitReq(cultivar=cultivar)
        # check its properties
        self.assertEqual(fruit.cultivar, cultivar)
        self.assertEqual(fruit['cultivar'], cultivar)
        self.assertEqual(getattr(fruit, 'cultivar'), cultivar)
        # check the dict representation
        self.assertEqual(
            fruit.to_dict(),
            {
                'cultivar': cultivar
            }
        )

        # model._composed_instances is a list of the instances that were
        # made from the anyOf/allOf/OneOf classes in model._composed_schemas
        for composed_instance in fruit._composed_instances:
            if composed_instance.__class__ == apple_req.AppleReq:
                apple_instance = composed_instance
        self.assertEqual(
            fruit._composed_instances,
            [apple_instance]
        )
        # model._var_name_to_model_instances maps the variable name to the
        # model instances which store that variable
        self.assertEqual(
            fruit._var_name_to_model_instances,
            {
                'length_cm': [fruit],
                'cultivar': [fruit, apple_instance],
                'mealy': [fruit, apple_instance],
                'sweet': [fruit],
            }
        )
        # model._additional_properties_model_instances stores a list of
        # models which have the property additional_properties_type != None
        self.assertEqual(
            fruit._additional_properties_model_instances, []
        )

        # we can pass in None
        fruit = FruitReq(None)
        assert fruit is None
예제 #6
0
    def test_FruitReq_banana(self):
        """Test FruitReq"""

        # make an instance of Fruit, a composed schema oneOf model
        # banana test
        fruit = FruitReq(length_cm=self.length_cm)
        # check its properties
        self.assertEqual(fruit.length_cm, self.length_cm)
        self.assertEqual(fruit['length_cm'], self.length_cm)
        self.assertEqual(getattr(fruit, 'length_cm'), self.length_cm)
        # check the dict representation
        self.assertEqual(
            fruit.to_dict(),
            {
                'length_cm': self.length_cm,
            }
        )

        # with getattr
        self.assertEqual(getattr(fruit, 'cultivar', 'some value'), 'some value')

        # make sure that the ModelComposed class properties are correct
        # model._composed_schemas stores the anyOf/allOf/oneOf info
        self.assertEqual(
            fruit._composed_schemas,
            {
                'anyOf': [],
                'allOf': [],
                'oneOf': [
                    apple_req.AppleReq,
                    banana_req.BananaReq,
                    type(None),
                ],
            }
        )
        # model._composed_instances is a list of the instances that were
        # made from the anyOf/allOf/OneOf classes in model._composed_schemas
        for composed_instance in fruit._composed_instances:
            if composed_instance.__class__ == banana_req.BananaReq:
                banana_instance = composed_instance
        self.assertEqual(
            fruit._composed_instances,
            [banana_instance]
        )
        # model._var_name_to_model_instances maps the variable name to the
        # model instances which store that variable
        self.assertEqual(
            fruit._var_name_to_model_instances,
            {
                'length_cm': [fruit, banana_instance],
            }
        )
        self.assertEqual(
            fruit._additional_properties_model_instances, [fruit]
        )

        # if we modify one of the properties owned by multiple
        # model_instances we get an exception when we try to access that
        # property because the retrieved values are not all the same
        banana_instance.length_cm = 4.56
        with self.assertRaises(petstore_api.ApiValueError):
            some_length_cm = fruit.length_cm
예제 #7
0
 def test_null_fruit(self):
     # we can pass in None
     fruit = FruitReq(None)
     assert fruit is None