Exemplo n.º 1
0
def test_dict_to_camelcase(monkeypatch):
    before_each_response_test(monkeypatch)

    assert (dict_to_camelcase({}) == {})

    adict = {"camel_case1": "camel_case"}
    assert (dict_to_camelcase(adict) == {'camelCase1': 'camel_case'})

    adict = {"camel_case1": {"camel_case2": "camel_case"}}
    assert (dict_to_camelcase(adict) == {
        'camelCase1': {
            'camelCase2': 'camel_case'
        }
    })

    adict = {
        "camel_case1": {
            "k": "v",
            "camel_case2": {
                "camel_case3": "camel_case",
                "x": "y"
            },
            "a": "b",
            "c": {
                "camel_case4": "e"
            }
        }
    }
    assert (dict_to_camelcase(adict) == {
        'camelCase1': {
            'k': 'v',
            'camelCase2': {
                'camelCase3': 'camel_case',
                'x': 'y'
            },
            'a': 'b',
            'c': {
                'camelCase4': 'e'
            },
        }
    })

    adict = {"camel_case1": [{"camel_case2": "camel_case"}]}
    assert (dict_to_camelcase(adict) == {
        'camelCase1': [{
            'camelCase2': 'camel_case'
        }]
    })

    adict = {"camel_case1": [[{"camel_case2": "camel_case"}]]}
    assert (dict_to_camelcase(adict) == {
        'camelCase1': [[{
            'camelCase2': 'camel_case'
        }]]
    })
Exemplo n.º 2
0
def hal_response(data, links=None):
    """HAL JSON response

    Converts the specified data and links to a HAL JSON document

    :param data: any data object
    :param links: any links to related data, eg next and previous page
    :return:
    """
    self = request.path
    if len(request.args):
        self += f'?{urllib.parse.urlencode(request.args)}'
    links = links or {}
    links['self'] = self

    response = dict_to_camelcase({
        '_links': {key: {'href': href} for key, href in links.items()},
        **data
    })

    return json.dumps(response, cls=APIGobTypeJSONEncoder), 200, {'Content-Type': 'application/json'}
Exemplo n.º 3
0
    def __iter__(self):
        """Main method. Use class as iterator.

        Loops through database result rows (as passed in the constructor), collects all result rows belonging to the
        same object (entity) and merges these rows back into one object with nested relations.

        :return:
        """
        self.evaluation_order, self.root_relation = self._determine_relation_evaluation_order()
        self.requested_sourcevalues = self._get_requested_sourcevalues()

        collected_rows = []
        for row in self.rows:
            # Collect entities with the same FIELD.GOBID in collected_rows. These entities together represent
            # one entity, but were returned from the database as multiple rows as a result of joins.
            # When all entities with the same GOBID are collected, self.build_entity() is called to merge the rows
            # back into one entity.
            row = self._publish_meta_fields(dict(row))

            row = dict_to_camelcase(row)
            built_entity = None

            if self.last_id is not None and row[FIELD.GOBID] != self.last_id:
                # Build entity when all rows of same GOBID are collected
                built_entity = self._build_entity(collected_rows)
                collected_rows = []

            collected_rows.append(row)
            self.last_id = row[FIELD.GOBID]

            if built_entity:
                yield stream_response(built_entity) + "\n"

        # Return last entity in pipeline
        built_entity = self._build_entity(collected_rows)

        if built_entity:
            yield stream_response(built_entity) + "\n"
Exemplo n.º 4
0
 def resolve_attribute(obj, info, **kwargs):
     value = getattr(obj, name)
     return dict_to_camelcase(value)
Exemplo n.º 5
0
def stream_response(data):
    response = dict_to_camelcase(data)
    return json.dumps(response, cls=APIGobTypeJSONEncoder)
Exemplo n.º 6
0
 def resolve_attribute(obj, info, **kwargs):
     value = getattr(obj, name)
     if isinstance(value, list):
         return [dict_to_camelcase(d) for d in value]
     return dict_to_camelcase(value)