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_aggregate(monkeypatch, aggregate_models, model, request_data, result): request = Mock() request.query_params = {} request.data = request_data a = AggregateQuerysetMixin() agg = a.aggregate(request=request, queryset=model.objects.all()) 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
def test_aggregate_fy_and_type(monkeypatch, aggregate_models, model, request_data, expected): request = Mock() request.query_params = {} request.data = request_data a = AggregateQuerysetMixin() agg = a.aggregate(request=request, queryset=model.objects.all()) 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('type', 'item')) expected.sort(key=itemgetter('type', '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() created, a.req = RequestCatalog.get_or_create_from_request(request) agg = a.aggregate(request=request, queryset=model.objects.all()) 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
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', 'show_nulls': True} a = AggregateQuerysetMixin() agg = a.aggregate(request=request, queryset=Award.objects.all()) # Test number of returned recrods assert agg.count() == 3 # Query should return three field names: 'item' - legacy and deprecated, 'aggregate', and 'type' fields = agg.first().keys() assert len(fields) == 3 assert 'aggregate' in fields assert 'item' in fields assert 'type' in fields
def test_aggregate_nulls(monkeypatch, aggregate_models_with_nulls): def itemsorter(a): if a["aggregate"] is None: return 0 return a["aggregate"] assert TransactionNormalized.objects.count() == 5 # Ensure defaults don't return values where all group fields are null request = Mock() request.query_params = {} request.data = { "field": "federal_action_obligation", "group": "assistance_data__cfda_number" } a = AggregateQuerysetMixin() agg = a.aggregate(request=request, queryset=TransactionNormalized.objects.all()) agg_list = [a for a in agg] assert len(agg_list) == 1 assert agg_list[0]["aggregate"] == 10.0 request.data = { "field": "federal_action_obligation", "group": ["assistance_data__cfda_number", "contract_data__naics"], } agg = a.aggregate(request=request, queryset=TransactionNormalized.objects.all()) agg_list = [a for a in agg] agg_list.sort(key=itemsorter) assert len(agg_list) == 2 assert agg_list[0]["aggregate"] == 10.0 assert agg_list[1]["aggregate"] == 30.0 # Allow null aggregate fileds request.data = { "field": "federal_action_obligation", "group": ["assistance_data__cfda_number", "contract_data__naics"], "show_null_aggregates": True, } agg = a.aggregate(request=request, queryset=TransactionNormalized.objects.all()) agg_list = [a for a in agg] agg_list.sort(key=itemsorter) assert len(agg_list) == 3 assert agg_list[0]["aggregate"] is None assert agg_list[1]["aggregate"] == 10.0 assert agg_list[2]["aggregate"] == 30.0 # Allow null groups fields request.data = { "field": "federal_action_obligation", "group": "assistance_data__cfda_number", "show_null_groups": True, } agg = a.aggregate(request=request, queryset=TransactionNormalized.objects.all()) agg_list = [a for a in agg] agg_list.sort(key=itemsorter) assert len(agg_list) == 2 assert agg_list[0]["aggregate"] == 10.0 assert agg_list[1]["aggregate"] == 30.0 # Allow null aggregate fields and null groups request.data = { "field": "federal_action_obligation", "group": "contract_data__naics", "show_null_aggregates": True, "show_null_groups": True, } agg = a.aggregate(request=request, queryset=TransactionNormalized.objects.all()) agg_list = [a for a in agg] agg_list.sort(key=itemsorter) assert len(agg_list) == 3 assert agg_list[0]["aggregate"] is None assert agg_list[1]["aggregate"] == 10.0 assert agg_list[2]["aggregate"] == 30.0 # Allow null aggregate fields and groups (using show_nulls to trigger both) request.data = { "field": "federal_action_obligation", "group": "contract_data__naics", "show_nulls": True } agg = a.aggregate(request=request, queryset=TransactionNormalized.objects.all()) agg_list = [a for a in agg] agg_list.sort(key=itemsorter) assert len(agg_list) == 3 assert agg_list[0]["aggregate"] is None assert agg_list[1]["aggregate"] == 10.0 assert agg_list[2]["aggregate"] == 30.0
def test_aggregate_nulls(monkeypatch, aggregate_models_with_nulls): def itemsorter(a): if a['aggregate'] is None: return 0 return a['aggregate'] assert TransactionNormalized.objects.count() == 5 # Ensure defaults don't return values where all group fields are null request = Mock() request.query_params = {} request.data = {"field": "federal_action_obligation", "group": "assistance_data__cfda_number"} a = AggregateQuerysetMixin() agg = a.aggregate(request=request, queryset=TransactionNormalized.objects.all()) agg_list = [a for a in agg] assert len(agg_list) == 1 assert agg_list[0]["aggregate"] == 10.0 request.data = {"field": "federal_action_obligation", "group": ["assistance_data__cfda_number", "contract_data__naics"]} agg = a.aggregate(request=request, queryset=TransactionNormalized.objects.all()) agg_list = [a for a in agg] agg_list.sort(key=itemsorter) assert len(agg_list) == 2 assert agg_list[0]["aggregate"] == 10.0 assert agg_list[1]["aggregate"] == 30.0 # Allow null aggregate fileds request.data = {"field": "federal_action_obligation", "group": ["assistance_data__cfda_number", "contract_data__naics"], "show_null_aggregates": True} agg = a.aggregate(request=request, queryset=TransactionNormalized.objects.all()) agg_list = [a for a in agg] agg_list.sort(key=itemsorter) assert len(agg_list) == 3 assert agg_list[0]["aggregate"] is None assert agg_list[1]["aggregate"] == 10.0 assert agg_list[2]["aggregate"] == 30.0 # Allow null groups fields request.data = {"field": "federal_action_obligation", "group": "assistance_data__cfda_number", "show_null_groups": True} agg = a.aggregate(request=request, queryset=TransactionNormalized.objects.all()) agg_list = [a for a in agg] agg_list.sort(key=itemsorter) assert len(agg_list) == 2 assert agg_list[0]["aggregate"] == 10.0 assert agg_list[1]["aggregate"] == 30.0 # Allow null aggregate fields and null groups request.data = {"field": "federal_action_obligation", "group": "contract_data__naics", "show_null_aggregates": True, "show_null_groups": True} agg = a.aggregate(request=request, queryset=TransactionNormalized.objects.all()) agg_list = [a for a in agg] agg_list.sort(key=itemsorter) assert len(agg_list) == 3 assert agg_list[0]["aggregate"] is None assert agg_list[1]["aggregate"] == 10.0 assert agg_list[2]["aggregate"] == 30.0 # Allow null aggregate fields and groups (using show_nulls to trigger both) request.data = {"field": "federal_action_obligation", "group": "contract_data__naics", "show_nulls": True} agg = a.aggregate(request=request, queryset=TransactionNormalized.objects.all()) agg_list = [a for a in agg] agg_list.sort(key=itemsorter) assert len(agg_list) == 3 assert agg_list[0]["aggregate"] is None assert agg_list[1]["aggregate"] == 10.0 assert agg_list[2]["aggregate"] == 30.0