def test_aggregate_fy_with_traversal(monkeypatch, aggregate_models, model,
                                     request_data, expected):
    request = Mock()
    request.query_params = {}
    request.data = request_data
    a = AggregateQuerysetMixin()
    a.get_queryset = lambda: model.objects.all()
    agg = a.aggregate(request=request)

    def itemsorter(a):
        if a['item'] is None:
            return 0
        return a['item']

    agg_list = [a for a in agg]
    if 'order' not in request_data:
        # this isn't an 'order by' request, (i.e., we're not testing
        # the result order), so sort the actual and expected results
        # to ensure a good comparison
        agg_list.sort(key=itemsorter)
        expected.sort(key=itemsorter)
        # agg_list.sort(key=itemgetter('item'))
        # expected.sort(key=itemgetter('item'))
        #
    assert agg_list == expected
def test_agg_fields(monkeypatch, aggregate_models):
    """Test length and field names of aggregate query result."""
    request = Mock()
    request.query_params = {}
    request.data = {'field': 'total_obligation', 'group': 'type'}
    a = AggregateQuerysetMixin()
    a.get_queryset = lambda: Award.objects.all()
    agg = a.aggregate(request=request)

    # Test number of returned recrods
    assert agg.count() == 3

    # Query should return two field names: 'item' and 'aggregate'
    fields = agg.first().keys()
    assert len(fields) == 2
    assert 'aggregate' in fields
    assert 'item' in fields
def test_aggregate(monkeypatch, aggregate_models, model, request_data, result):
    request = Mock()
    request.query_params = {}
    request.data = request_data
    a = AggregateQuerysetMixin()
    a.get_queryset = lambda: model.objects.all()
    agg = a.aggregate(request=request)

    agg_list = [a for a in agg]
    if 'order' not in request_data:
        # this isn't an 'order by' request, (i.e., we're not testing
        # the result order), so sort the actual and expected results
        # to ensure a good comparison
        agg_list.sort(key=itemgetter('item'))
        result.sort(key=itemgetter('item'))

    assert agg_list == result