コード例 #1
0
    def test_banana_fruit(self):
        """Test GmFruit"""

        # make an instance of GmFruit, a composed schema anyOf model
        # banana test
        fruit = GmFruit(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(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
        })

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

        with self.assertRaises(AttributeError):
            invalid_variable = 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': [
                apple.Apple,
                banana.Banana,
            ],
            'allOf': [],
            'oneOf': [],
        })
        # 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, banana_instance],
                'length_cm': [fruit, banana_instance],
            })
        self.assertEqual(fruit._additional_properties_model_instances, [fruit])
コード例 #2
0
    def test_combo_fruit(self):
        # including input parameters for both anyOf instances works
        cultivar = 'banaple'
        color = 'orange'
        fruit = GmFruit(color=color,
                        cultivar=cultivar,
                        length_cm=self.length_cm)
        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)
        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)

        # 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
            elif composed_instance.__class__ == banana.Banana:
                banana_instance = composed_instance
        self.assertEqual(fruit._composed_instances,
                         [apple_instance, 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, apple_instance, banana_instance],
                'length_cm': [fruit, apple_instance, banana_instance],
                'cultivar': [fruit, apple_instance, banana_instance],
            })
        self.assertEqual(fruit._additional_properties_model_instances, [fruit])
コード例 #3
0
    def test_set_addprop_attributes(self):
        # setting a value that doesn't exist works because additional_properties_type allows any type
        other_fruit = GmFruit(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
            })
コード例 #4
0
 def test_anyof_composition_gm_fruit_validate(self):
     im = InstantiationMetadata()
     path_to_schemas = GmFruit._validate(frozendict(
         cultivar='GoldenDelicious', lengthCm=Decimal(10)),
                                         _instantiation_metadata=im)
     assert path_to_schemas == {
         ('args[0]', ): set([GmFruit, Apple, Banana, frozendict]),
         ('args[0]', 'cultivar'): set([Apple.cultivar, AnyTypeSchema, str]),
         ('args[0]', 'lengthCm'):
         set([AnyTypeSchema, NumberSchema, Decimal]),
     }
コード例 #5
0
 def test_anyof_composition_gm_fruit_validate(self):
     vm = ValidationMetadata()
     path_to_schemas = GmFruit._validate(
         frozendict(cultivar="GoldenDelicious", lengthCm=Decimal(10)),
         validation_metadata=vm,
     )
     assert path_to_schemas == {
         ("args[0]", ): set([GmFruit, Apple, Banana, frozendict]),
         ("args[0]", "cultivar"): set([Apple.cultivar, AnyTypeSchema, str]),
         ("args[0]", "lengthCm"):
         set([AnyTypeSchema, NumberSchema, Decimal]),
     }
コード例 #6
0
    def test_apple_fruit(self):
        # apple test
        color = 'red'
        cultivar = 'golden delicious'
        origin = 'California'
        fruit = GmFruit(color=color, cultivar=cultivar, origin=origin)
        # 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)

        self.assertEqual(fruit.origin, origin)
        self.assertEqual(fruit['origin'], origin)
        self.assertEqual(getattr(fruit, 'origin'), origin)

        # check the dict representation
        self.assertEqual(fruit.to_dict(), {
            'color': color,
            'cultivar': cultivar,
            'origin': origin,
        })

        # 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],
                'origin': [fruit, apple_instance],
            })
        self.assertEqual(fruit._additional_properties_model_instances, [fruit])
コード例 #7
0
    def testGmFruit(self):
        """Test GmFruit"""

        # make an instance of GmFruit, a composed schema anyOf model
        # banana test
        length_cm = 20.3
        color = 'yellow'
        fruit = GmFruit(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(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'
        # 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.assertTrue(getattr(fruit, 'cultivar', 'some value'), 'some value')

        with self.assertRaises(AttributeError):
            invalid_variable = 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': [
                    apple.Apple,
                    banana.Banana,
                ],
                'allOf': [],
                'oneOf': [],
            }
        )
        # 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 = GmFruit(
                color=color,
                length_cm=length_cm,
                unknown_property='some value'
            )

        # including input parameters for both anyOf instances works
        cultivar = 'banaple'
        color = 'orange'
        fruit = GmFruit(
            color=color,
            cultivar=cultivar,
            length_cm=length_cm
        )
        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)
        self.assertEqual(fruit.length_cm, length_cm)
        self.assertEqual(fruit['length_cm'], length_cm)
        self.assertEqual(getattr(fruit, 'length_cm'), length_cm)

        # 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
            elif composed_instance.__class__ == banana.Banana:
                banana_instance = composed_instance
        self.assertEqual(
            fruit._composed_instances,
            [apple_instance, 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, apple_instance],
                'origin': [fruit, apple_instance],
            }
        )

        # make an instance of GmFruit, a composed schema anyOf model
        # apple test
        color = 'red'
        cultivar = 'golden delicious'
        origin = 'California'
        fruit = GmFruit(color=color, cultivar=cultivar, origin=origin)
        # 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)

        self.assertEqual(fruit.origin, origin)
        self.assertEqual(fruit['origin'], origin)
        self.assertEqual(getattr(fruit, 'origin'), origin)

        # check the dict representation
        self.assertEqual(
            fruit.to_dict(),
            {
                'color': color,
                'cultivar': cultivar,
                'origin': origin,
            }
        )

        # 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, []
        )
コード例 #8
0
    def testGmFruit(self):
        """Test GmFruit"""

        # make an instance of GmFruit, a composed schema anyOf model
        # banana test
        length_cm = 20.3
        color = 'yellow'
        cultivar = 'banaple'
        fruit = GmFruit(lengthCm=length_cm, color=color, cultivar=cultivar)
        assert isinstance(fruit, GmFruit)
        assert isinstance(fruit, banana.Banana)
        assert isinstance(fruit, apple.Apple)
        assert isinstance(fruit, frozendict)
        # check its properties
        self.assertEqual(fruit.lengthCm, length_cm)
        self.assertEqual(fruit['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,
            'cultivar': cultivar
        })

        with self.assertRaises(KeyError):
            invalid_variable = fruit['origin']
        # with getattr
        self.assertTrue(getattr(fruit, 'origin', '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': [
                    apple.Apple,
                    banana.Banana,
                ],
                'allOf': [],
                'oneOf': [],
                'not': None
            })

        # including extra parameters works
        fruit = GmFruit(color=color,
                        length_cm=length_cm,
                        cultivar=cultivar,
                        unknown_property='some value')

        # including input parameters for both anyOf instances works
        color = 'orange'
        color_stored = b'orange'
        fruit = GmFruit(color=color, cultivar=cultivar, length_cm=length_cm)
        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)
        self.assertEqual(fruit.length_cm, length_cm)
        self.assertEqual(fruit['length_cm'], length_cm)
        self.assertEqual(getattr(fruit, 'length_cm'), length_cm)

        # make an instance of GmFruit, a composed schema anyOf model
        # apple test
        color = 'red'
        cultivar = 'golden delicious'
        origin = 'California'
        fruit = GmFruit(color=color, cultivar=cultivar, origin=origin)
        # 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)

        self.assertEqual(fruit.origin, origin)
        self.assertEqual(fruit['origin'], origin)
        self.assertEqual(getattr(fruit, 'origin'), origin)

        # check the dict representation
        self.assertEqual(fruit, {
            'color': color,
            'cultivar': cultivar,
            'origin': origin,
        })