示例#1
0
def test_many_full_nested_query():
    unfiltered = [{
        'a': 1,
        'b': {
            'nest': True,
            'second': 13,
        },
        'c': 'testing',
    }, {
        'a': 5,
        'b': {
            'nest': False,
            'second': 4,
        },
        'c': 'another',
    }]

    expected = [{
        'b': {
            'nest': True,
            'second': 13,
        },
        'c': 'testing',
    }, {
        'b': {
            'nest': False,
            'second': 4,
        },
        'c': 'another',
    }]

    assert query(unfiltered, ['b', 'c']) == expected
示例#2
0
def test_single_multi_nested_list_query():
    unfiltered = {
        'unwanted': 6,
        'b': [{
            'nest': True,
            'other': 'another',
        }, {
            'nest': False,
            'other': 'the other',
        }],
        'c': 'testing',
    }

    expected = {
        'b': [{
            'nest': True,
            'other': 'another',
        }, {
            'nest': False,
            'other': 'the other',
        }],
        'c': 'testing',
    }

    assert query(unfiltered, ['b.nest', 'b.other', 'c']) == expected
示例#3
0
def test_many_multi_nested_query():
    unfiltered = [{
        'unwanted': 6,
        'b': {
            'nest': True,
            'other': 'another',
        },
        'c': 'testing',
    }, {
        'unwanted': 23,
        'b': {
            'nest': False,
            'other': 'the other',
        },
        'c': 'another',
    }]

    expected = [{
        'b': {
            'nest': True,
            'other': 'another',
        },
        'c': 'testing',
    }, {
        'b': {
            'nest': False,
            'other': 'the other',
        },
        'c': 'another',
    }]

    assert query(unfiltered, ['b.nest', 'b.other', 'c']) == expected
示例#4
0
def test_many_select_nested_query():
    unfiltered = [{
        'a': 1,
        'b': {
            'nest': True,
            'unwanted': 7,
        },
        'c': 'testing',
    }, {
        'a': 5,
        'b': {
            'nest': False,
            'unwanted': 91,
        },
        'c': 'another',
    }]

    expected = [{
        'b': {
            'nest': True,
        },
        'c': 'testing',
    }, {
        'b': {
            'nest': False,
        },
        'c': 'another',
    }]

    assert query(unfiltered, ['b.nest', 'c']) == expected
示例#5
0
def test_single_flat_query():
    unfiltered = {
        'a': 1,
        'b': True,
        'c': 'testing',
    }

    expected = {'a': 1, 'c': 'testing'}

    assert query(unfiltered, ['a', 'c']) == expected
示例#6
0
def test_non_existent_fields_query():
    unfiltered = {
        'a': 1,
        'b': True,
        'c': 'testing',
    }

    expected = {'a': 1, 'c': 'testing'}

    assert query(unfiltered, ['a', 'c', 'd']) == expected
示例#7
0
def test_many_flat_query():
    unfiltered = [{
        'a': 1,
        'b': True,
        'c': 'testing',
    }, {
        'a': 5,
        'b': False,
        'c': 'another',
    }]

    expected = [{'a': 1, 'c': 'testing'}, {'a': 5, 'c': 'another'}]

    assert query(unfiltered, ['a', 'c']) == expected
示例#8
0
def test_deep_nested_query():
    unfiltered = {
        'a': {
            'b': {
                'c': {
                    'value': 5,
                    'another': 6
                },
                'irrelevant_c': 10,
            },
            'irrelevant_b': 10,
        },
        'irrelevant_a': 10,
    }

    expected = {'a': {'b': {'c': {'value': 5}}}}
    assert query(unfiltered, ['a.b.c.value']) == expected
示例#9
0
def test_single_select_nested_query():
    unfiltered = {
        'a': 1,
        'b': {
            'nest': True,
            'unwanted': 7,
        },
        'c': 'testing',
    }

    expected = {
        'b': {
            'nest': True,
        },
        'c': 'testing',
    }

    assert query(unfiltered, ['b.nest', 'c']) == expected
示例#10
0
def test_single_full_nested_query():
    unfiltered = {
        'a': 1,
        'b': {
            'nest': True,
            'second': 13,
        },
        'c': 'testing',
    }

    expected = {
        'b': {
            'nest': True,
            'second': 13,
        },
        'c': 'testing',
    }

    assert query(unfiltered, ['b', 'c']) == expected
示例#11
0
    def middleware(request):
        fields_param = request.GET.get('fields')

        response = get_response(request)

        if 200 <= response.status_code < 300:
            if not fields_param or fields_param == '*':
                query_fields = None
            else:
                query_fields = fields_param.split(',')

            if query_fields is not None:
                filtered_data = query(response.data, query_fields)

                # re-render the response
                response.data = filtered_data
                response._is_rendered = False
                response.render()

        return response