Example #1
0
    def test_array(self) -> None:
        cls = mapry.Class(name='some_class', plural='', description='', ref='')

        cls.properties = {
            "friends":
            mapry.Property(ref='',
                           name='friends',
                           a_type=mapry.Array(values=cls),
                           description='',
                           json='',
                           optional=False,
                           composite=cls)
        }

        self.assertListEqual([cls], mapry.references(a_type=cls))
Example #2
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)))
Example #3
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))
Example #4
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))