def test_apple_fruit(self):

        # make an instance of Fruit, a composed schema oneOf model
        # apple test
        color = 'red'
        cultivar = 'golden delicious'
        fruit = Fruit(color=color, cultivar=cultivar)
        # check its properties
        self.assertEqual(fruit.color, color)
        self.assertEqual(fruit['color'], color)
        self.assertEqual(getattr(fruit, 'color'), color)
        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(), {
            'color': color,
            '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.Apple:
                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, {
                'color': [fruit, apple_instance],
                'cultivar': [fruit, apple_instance],
            })
        self.assertEqual(fruit._additional_properties_model_instances, [fruit])
 def test_fruit_with_additional_props(self):
     # including extra parameters works because the oneOf models include additionalProperties
     some_value = 'some_value'
     some_fruit = Fruit(color=self.color,
                        length_cm=self.length_cm,
                        unknown_property=some_value)
     assert some_fruit['unknown_property'] == some_value
Exemplo n.º 3
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)
 def test_fruit_with_invalid_input_type(self):
     """
     color must be a str, color's str type is only defined at the Fruit level
     Banana + Apple would allow color to be assigned with any type of value
     """
     invalid_value = 1
     with self.assertRaises(petstore_api.ApiTypeError):
         fruit = Fruit(color=invalid_value, length_cm=self.length_cm)
    def test_fruit_assigning_additional_props_in_client(self):
        # setting a value that doesn't exist works because additional_properties_type allows any type
        other_fruit = Fruit(length_cm=self.length_cm, color=self.color)
        blah = 'blah'
        other_fruit['a'] = blah
        assert other_fruit.a == blah

        # with setattr
        setattr(other_fruit, 'b', blah)
        assert other_fruit.b == blah

        self.assertEqual(
            other_fruit.to_dict(), {
                'a': 'blah',
                'b': 'blah',
                'length_cm': self.length_cm,
                'color': self.color
            })
    def test_fruit_attribute_access(self):
        fruit = Fruit(length_cm=self.length_cm, color=self.color)

        # Assert that we can call the builtin hasattr() function.
        # hasattr should return False for non-existent attribute.
        # Internally hasattr catches the AttributeError exception.
        self.assertFalse(hasattr(fruit, 'invalid_variable'))

        # Assert that we can call the builtin hasattr() function.
        # hasattr should return True for existent attribute.
        self.assertTrue(hasattr(fruit, 'color'))

        # with getattr
        # Per Python doc, if the named attribute does not exist,
        # default is returned if provided.
        self.assertEqual(getattr(fruit, 'cultivar', 'some value'),
                         'some value')
        self.assertEqual(fruit.get('cultivar'), None)
        self.assertEqual(fruit.get('cultivar', 'some value'), 'some value')
Exemplo n.º 7
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
    def test_fruit_access_errors(self):
        fruit = Fruit(length_cm=self.length_cm, color=self.color)

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

        # Per Python doc, if the named attribute does not exist,
        # default is returned if provided, otherwise AttributeError is raised.
        with self.assertRaises(AttributeError):
            getattr(fruit, 'cultivar')
    def test_banana_fruit(self):
        """Test Fruit"""

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

        # 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.Apple,
                banana.Banana,
            ],
        })
        # model._composed_instances is a list of the instances that were
        # made from the anyOf/allOf/OneOf classes in model._composed_schemas
        self.assertEqual(len(fruit._composed_instances), 1)
        for composed_instance in fruit._composed_instances:
            if composed_instance.__class__ == banana.Banana:
                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
        print(fruit._var_name_to_model_instances)
        self.assertEqual(
            fruit._var_name_to_model_instances, {
                'color': [fruit, banana_instance],
                '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

        # including input parameters for two oneOf instances raise an exception
        with self.assertRaises(petstore_api.ApiValueError):
            fruit = Fruit(length_cm=self.length_cm, cultivar='granny smith')
Exemplo n.º 10
0
    def testFruit(self):
        """Test Fruit"""

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

        # Assert that we can call the builtin hasattr() function.
        # hasattr should return False for non-existent attribute.
        # Internally hasattr catches the AttributeError exception.
        self.assertFalse(hasattr(fruit, 'invalid_variable'))

        # Assert that we can call the builtin hasattr() function.
        # hasattr should return True for existent attribute.
        self.assertTrue(hasattr(fruit, 'color'))

        # 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
        # Per Python doc, if the named attribute does not exist,
        # default is returned if provided.
        self.assertEqual(getattr(fruit, 'cultivar', 'some value'),
                         'some value')
        self.assertEqual(fruit.get('cultivar'), None)
        self.assertEqual(fruit.get('cultivar', 'some value'), 'some value')

        # Per Python doc, if the named attribute does not exist,
        # default is returned if provided, otherwise AttributeError is raised.
        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.Apple,
                banana.Banana,
            ],
        })
        # 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.Banana:
                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, {
                'color': [fruit],
                'length_cm': [fruit, banana_instance],
                'cultivar': [fruit],
                'origin': [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, [])

        # 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 = Fruit(color=color,
                          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 = Fruit(length_cm=length_cm, cultivar='granny smith')

        # make an instance of Fruit, a composed schema oneOf model
        # apple test
        color = 'red'
        cultivar = 'golden delicious'
        fruit = Fruit(color=color, cultivar=cultivar)
        # check its properties
        self.assertEqual(fruit.color, color)
        self.assertEqual(fruit['color'], color)
        self.assertEqual(getattr(fruit, 'color'), color)
        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(), {
            'color': color,
            '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.Apple:
                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, {
                'color': [fruit],
                'length_cm': [fruit],
                'cultivar': [fruit, apple_instance],
                'origin': [fruit, apple_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, [])
Exemplo n.º 11
0
    def testFruit(self):
        """Test Fruit"""

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

        # Assert that we can call the builtin hasattr() function.
        # hasattr should return False for non-existent attribute.
        # Internally hasattr catches the AttributeError exception.
        self.assertFalse(hasattr(fruit, 'invalid_variable'))

        # Assert that we can call the builtin hasattr() function.
        # hasattr should return True for existent attribute.
        self.assertTrue(hasattr(fruit, 'color'))

        # getting a value that doesn't exist raises an exception
        # with a key
        with self.assertRaises(KeyError):
            fruit['cultivar']
        # with getattr
        # Per Python doc, if the named attribute does not exist,
        # default is returned if provided.
        self.assertEqual(getattr(fruit, 'cultivar', 'some value'),
                         'some value')
        self.assertEqual(fruit.get('cultivar'), None)
        self.assertEqual(fruit.get('cultivar', 'some value'), 'some value')

        # Per Python doc, if the named attribute does not exist,
        # default is returned if provided, otherwise AttributeError is raised.
        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.Apple,
                    banana.Banana,
                ],
                'not': None
            })
        """
        including extra parameters does not raise an exception
        because objects support additional properties by default
        """
        kwargs = dict(
            color=color,
            lengthCm=length_cm,
            additional_string='some value',
            additional_date='2021-01-02',
        )

        fruit = Fruit._from_openapi_data(**kwargs)
        self.assertEqual(fruit, kwargs)

        fruit = Fruit(**kwargs)
        self.assertEqual(fruit, kwargs)

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

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