Beispiel #1
0
    def test_that_it_works(self) -> None:
        another_cls = mapry.Class(name='another_cls',
                                  plural='',
                                  description='',
                                  ref='')

        embed = mapry.Embed(name='some_embed', description='', ref='')
        embed.properties = {
            "another_ref":
            dummy_property(name='another_ref',
                           a_type=another_cls,
                           composite=embed)
        }

        cls = mapry.Class(name='some_class', plural='', description='', ref='')

        cls.properties = {
            "some_primitive":
            dummy_property(name="some_primitive",
                           a_type=mapry.Path(),
                           composite=cls),
            "some_ref":
            dummy_property(name="some_ref", a_type=cls, composite=cls),
            "some_embed":
            dummy_property(name="some_embed", a_type=embed, composite=cls)
        }

        self.assertListEqual([another_cls, cls], mapry.references(a_type=cls))
Beispiel #2
0
    def test_value_needs_type(self) -> None:
        type_definition = mapry.Path()

        self.assertTrue(
            mapry.needs_type(a_type=type_definition, query=mapry.Path))

        self.assertFalse(
            mapry.needs_type(a_type=type_definition, query=mapry.Date))
Beispiel #3
0
    def test_iterate_over_types(self) -> None:
        embed_path = mapry.Path()
        embed_array = mapry.Array(values=embed_path)
        embed = mapry.Embed(name='some_embed', description='', ref='E#')
        embed.properties = {
            "some_embed_property":
            dummy_property(name="some_embed_property",
                           a_type=embed_array,
                           composite=embed)
        }

        cls_path = mapry.Path()
        cls = mapry.Class(name='some_class',
                          plural='',
                          description='',
                          ref='C#')
        cls.properties = {
            "some_class_property":
            dummy_property(name="some_class_property",
                           a_type=cls_path,
                           composite=cls)
        }

        graph_path = mapry.Path()
        graph = mapry.Graph()
        graph.ref = 'G#'
        graph.properties = {
            "some_graph_property":
            dummy_property(name="some_graph_property",
                           a_type=graph_path,
                           composite=graph),
        }
        graph.classes = {cls.name: cls}
        graph.embeds = {embed.name: embed}

        # yapf: disable
        self.assertListEqual(
            [(cls_path, 'C#/some_class_property'),
             (embed_array, 'E#/some_embed_property'),
             (embed_path, 'E#/some_embed_property/values'),
             (graph_path, 'G#/some_graph_property')],
            list(mapry.iterate_over_types(graph=graph)))
Beispiel #4
0
    def test_embed_needs_type(self) -> None:
        embed = mapry.Embed(name='some_embed', description='', ref='')

        embed.properties = {
            'some_property':
            dummy_property(name='some_property',
                           a_type=mapry.Path(),
                           composite=embed)
        }

        self.assertTrue(mapry.needs_type(a_type=embed, query=mapry.Path))
        self.assertFalse(mapry.needs_type(a_type=embed, query=mapry.Date))
Beispiel #5
0
    def test_class_needs_type(self) -> None:
        cls = mapry.Class(name='some_class', plural='', description='', ref='')
        cls.name = 'some_class'
        cls.properties = {
            'some_property':
            dummy_property(name='some_property',
                           a_type=mapry.Path(),
                           composite=cls)
        }

        self.assertTrue(mapry.needs_type(a_type=cls, query=mapry.Path))
        self.assertFalse(mapry.needs_type(a_type=cls, query=mapry.Date))
Beispiel #6
0
    def test_graph_needs_type(self) -> None:
        # Property of an object graph
        graph = mapry.Graph()
        graph.properties = {
            "some_property":
            dummy_property(name='some_property',
                           a_type=mapry.Path(),
                           composite=graph)
        }

        self.assertTrue(mapry.needs_type(a_type=graph, query=mapry.Path))
        self.assertFalse(mapry.needs_type(a_type=graph, query=mapry.Date))

        # Recursively needs type by a property of a class
        cls = mapry.Class(name='some_class', plural='', description='', ref='')
        cls.properties = {
            'some_property':
            dummy_property(name='some_property',
                           a_type=mapry.Path(),
                           composite=cls)
        }

        graph = mapry.Graph()
        graph.classes = {cls.name: cls}
        self.assertTrue(mapry.needs_type(a_type=graph, query=mapry.Path))
        self.assertFalse(mapry.needs_type(a_type=graph, query=mapry.Date))

        # Recursively needs type by a property of an embeddable structure
        embed = mapry.Embed(name='some_embed', description='', ref='')
        embed.properties = {
            'some_property':
            dummy_property(name='some_property',
                           a_type=mapry.Path(),
                           composite=embed)
        }

        graph = mapry.Graph()
        graph.embeds = {embed.name: embed}
        self.assertTrue(mapry.needs_type(a_type=graph, query=mapry.Path))
        self.assertFalse(mapry.needs_type(a_type=graph, query=mapry.Date))
Beispiel #7
0
def _recurse_type_from_mapping(
        mapping: Mapping[str, Any], classes: Mapping[str, mapry.Class],
        embeds: Mapping[str, mapry.Embed]) -> mapry.Type:
    """
    Parse recursively the type definition from the given mapping.

    :param mapping: to be parsed
    :param classes: pointer table to the classes
    :param embeds: pointer table to the embeddable structures
    :return: parsed type definition
    """
    # pylint: disable=too-many-return-statements
    # pylint: disable=too-many-branches
    type_identifier = mapping['type']

    if type_identifier == 'boolean':
        return mapry.Boolean()

    if type_identifier == 'integer':
        return mapry.Integer(
            minimum=int(mapping['minimum']) if 'minimum' in mapping else None,
            exclusive_minimum=bool(mapping['exclusive_minimum'])
            if 'exclusive_minimum' in mapping else False,
            maximum=int(mapping['maximum']) if 'maximum' in mapping else None,
            exclusive_maximum=bool(mapping['exclusive_maximum'])
            if 'exclusive_maximum' in mapping else False)

    if type_identifier == 'float':
        return mapry.Float(
            minimum=int(mapping['minimum']) if 'minimum' in mapping else None,
            exclusive_minimum=bool(mapping['exclusive_minimum'])
            if 'exclusive_minimum' in mapping else False,
            maximum=int(mapping['maximum']) if 'maximum' in mapping else None,
            exclusive_maximum=bool(mapping['exclusive_maximum'])
            if 'exclusive_maximum' in mapping else False)

    if type_identifier == 'string':
        return mapry.String(
            pattern=re.compile(mapping['pattern']) if 'pattern' in
            mapping else None)

    if type_identifier == 'path':
        return mapry.Path(
            pattern=re.compile(mapping['pattern']) if 'pattern' in
            mapping else None)

    if type_identifier == 'date':
        return mapry.Date(fmt=mapping.get('format', None))

    if type_identifier == 'time':
        return mapry.Time(fmt=mapping.get('format', None))

    if type_identifier == 'datetime':
        return mapry.Datetime(fmt=mapping.get('format', None))

    if type_identifier == 'time_zone':
        return mapry.TimeZone()

    if type_identifier == 'duration':
        return mapry.Duration()

    if type_identifier == 'array':
        return mapry.Array(
            values=_recurse_type_from_mapping(
                mapping=mapping['values'], classes=classes, embeds=embeds),
            minimum_size=int(mapping['minimum_size'])
            if 'minimum_size' in mapping else None,
            maximum_size=int(mapping['maximum_size'])
            if 'maximum_size' in mapping else None)

    if type_identifier == 'map':
        return mapry.Map(
            values=_recurse_type_from_mapping(
                mapping=mapping['values'], classes=classes, embeds=embeds))

    if type_identifier in classes:
        return classes[type_identifier]

    if type_identifier in embeds:
        return embeds[type_identifier]

    raise NotImplementedError(
        "Unhandled type identifier: {!r}".format(type_identifier))
Beispiel #8
0
    def test_map_needs_type(self) -> None:
        a_map = mapry.Map(values=mapry.Path())

        self.assertTrue(mapry.needs_type(a_type=a_map, query=mapry.Path))
        self.assertFalse(mapry.needs_type(a_type=a_map, query=mapry.Date))
Beispiel #9
0
    def test_array_needs_type(self) -> None:
        array = mapry.Array(values=mapry.Path(), minimum_size=None)

        self.assertTrue(mapry.needs_type(a_type=array, query=mapry.Path))
        self.assertFalse(mapry.needs_type(a_type=array, query=mapry.Date))