def test_query_update_returns_number_of_affected_rows(self):
        objects = [MockModel(foo=1), MockModel(foo=1), MockModel(foo=2)]
        qs = MockSet(*objects, model=create_model('foo', 'bar'))
        count = qs.filter(foo=1).update(bar=2)

        assert count == len(objects) - 1, count
def test_recipient_overview(client, mock_matviews_qs, monkeypatch):
    """ Testing a simple example of the endpoint as a whole """
    r_id = "00077a9a-5a70-8919-fd19-330762af6b84-C"
    recipient_hash = r_id[:-2]

    # Mock Transactions
    mock_transactions = []
    for category, transaction in TEST_SUMMARY_TRANSACTIONS.items():
        transaction["recipient_hash"] = recipient_hash
        mock_transactions.append(MockModel(**transaction))
    add_to_mock_objects(mock_matviews_qs, mock_transactions)

    # Mock Recipient Profiles
    for recipient_id, recipient_profile in TEST_RECIPIENT_PROFILES.items():
        recipient_profile_copy = recipient_profile.copy()
        if recipient_id == r_id:
            recipient_profile_copy["last_12_months"] = 100
            recipient_profile_copy["last_12_months_count"] = 1
        mommy.make(RecipientProfile, **recipient_profile_copy)

    # Mock Recipient Lookups
    for recipient_hash, recipient_lookup in TEST_RECIPIENT_LOOKUPS.items():
        mommy.make(RecipientLookup, **recipient_lookup)

    # Mock DUNS - should add `category-business`
    for duns, duns_dict in TEST_DUNS.items():
        test_duns_model = duns_dict.copy()
        country_code = test_duns_model["country_code"]
        mommy.make(DUNS, **test_duns_model)
        mommy.make(RefCountryCode, **TEST_REF_COUNTRY_CODE[country_code])

    # Mock Legal Entity
    expected_business_cat = ["expected", "business", "cat"]
    mommy.make(LegalEntity,
               recipient_name="PARENT RECIPIENT",
               recipient_unique_id="000000001")

    utm_objects = Mock()
    utm_objects.filter().order_by().values().first.return_value = {
        "business_categories": expected_business_cat
    }
    monkeypatch.setattr(
        "usaspending_api.awards.models_matviews.UniversalTransactionView.objects",
        utm_objects)

    resp = client.get(recipient_overview_endpoint(r_id))
    assert resp.status_code == status.HTTP_200_OK
    expected = {
        "name":
        "PARENT RECIPIENT",
        "duns":
        "000000001",
        "recipient_id":
        "00077a9a-5a70-8919-fd19-330762af6b84-C",
        "recipient_level":
        "C",
        "parent_name":
        "PARENT RECIPIENT",
        "parent_duns":
        "000000001",
        "parent_id":
        "00077a9a-5a70-8919-fd19-330762af6b84-P",
        "parents": [{
            "parent_duns": "000000001",
            "parent_id": "00077a9a-5a70-8919-fd19-330762af6b84-P",
            "parent_name": "PARENT RECIPIENT",
        }],
        "business_types":
        sorted(["expected", "business", "cat"] + ["category_business"]),
        "location": {
            "address_line1": "PARENT ADDRESS LINE 1",
            "address_line2": "PARENT ADDRESS LINE 2",
            "address_line3": None,
            "county_name": None,
            "city_name": "PARENT CITY",
            "congressional_code": "PARENT CONGRESSIONAL DISTRICT",
            "country_code": "PARENT COUNTRY CODE",
            "country_name": "PARENT COUNTRY NAME",
            "state_code": "PARENT STATE",
            "zip": "PARENT ZIP",
            "zip4": "PARENT ZIP4",
            "foreign_province": None,
            "foreign_postal_code": None,
        },
        "total_transaction_amount":
        100,
        "total_transactions":
        1,
    }
    # testing for equality-only, order unnecessary
    resp.data["business_types"] = sorted(resp.data["business_types"])
    assert resp.data == expected
def test_child_recipient_success(client, mock_matviews_qs):
    """ Testing successfull child recipient calls """
    child1_id = "00077a9a-5a70-8919-fd19-330762af6b84-C"
    child1_hash = child1_id[:-2]
    parent_child1_name = "PARENT RECIPIENT"
    parent_child1_duns = "000000001"
    child2_id = "392052ae-92ab-f3f4-d9fa-b57f45b7750b-C"
    child2_hash = child2_id[:-2]
    child2_name = "CHILD RECIPIENT"
    child2_duns = "000000002"
    transaction_hash_map = {
        "latest": {
            "hash": child1_hash,
            "duns": parent_child1_duns,
            "name": parent_child1_name,
            "parent_duns": parent_child1_duns,
        },
        "FY2016": {
            "hash": child2_hash,
            "duns": child2_duns,
            "name": child2_name,
            "parent_duns": parent_child1_duns
        },
        # Making sure the children total only applies to transactions where it listed the parent
        # Not all transactions of that child in general
        "FY2008": {
            "hash": child2_hash,
            "duns": child2_duns,
            "name": child2_name,
            "parent_duns": "000000009"
        },
    }

    # Mock Recipient Profiles
    for recipient_id, recipient_profile in TEST_RECIPIENT_PROFILES.items():
        mommy.make(RecipientProfile, **recipient_profile)

    # Mock Recipient Lookups
    for recipient_hash, recipient_lookup in TEST_RECIPIENT_LOOKUPS.items():
        mommy.make(RecipientLookup, **recipient_lookup)

    # load transactions for each child and parent (making sure it's excluded)
    mock_transactions = []
    for category, transaction in TEST_SUMMARY_TRANSACTIONS.items():
        transaction["recipient_hash"] = transaction_hash_map[category]["hash"]
        transaction["recipient_unique_id"] = transaction_hash_map[category][
            "duns"]
        transaction["recipient_name"] = transaction_hash_map[category]["name"]
        transaction["parent_recipient_unique_id"] = transaction_hash_map[
            category]["parent_duns"]
        mock_transactions.append(MockModel(**transaction))
    add_to_mock_objects(mock_matviews_qs, mock_transactions)

    # Ignoring nonexistent child duns - 000000005
    child1_object = {
        "recipient_id": child1_id,
        "name": "PARENT RECIPIENT",
        "duns": parent_child1_duns,
        "amount": 100,
        "state_province": "PARENT STATE",
    }
    child2_object = {
        "recipient_id": child2_id,
        "name": "CHILD RECIPIENT",
        "duns": child2_duns,
        "amount": 50,
        "state_province": "CHILD STATE",
    }
    expected = [child1_object, child2_object]
    resp = client.get(recipient_children_endpoint(parent_child1_duns, "all"))
    assert resp.status_code == status.HTTP_200_OK
    # testing for equality-only, order unnecessary
    assert sorted(resp.data, key=lambda key: key["recipient_id"]) == expected
Example #4
0
    def test_query_length2(self):
        q = MockSet(MockModel(), MockModel())

        n = len(q)

        self.assertEqual(2, n)
def test_recipient_overview(client, mock_matviews_qs):
    """ Testing a simple example of the endpoint as a whole """
    r_id = '00077a9a-5a70-8919-fd19-330762af6b84-C'
    recipient_hash = r_id[:-2]

    # Mock Transactions
    mock_transactions = []
    for category, transaction in TEST_SUMMARY_TRANSACTIONS.items():
        transaction['recipient_hash'] = recipient_hash
        mock_transactions.append(MockModel(**transaction))
    add_to_mock_objects(mock_matviews_qs, mock_transactions)

    # Mock Recipient Profiles
    for recipient_id, recipient_profile in TEST_RECIPIENT_PROFILES.items():
        recipient_profile_copy = recipient_profile.copy()
        if recipient_id == r_id:
            recipient_profile_copy['last_12_months'] = 100
            recipient_profile_copy['last_12_months_count'] = 1
        mommy.make(RecipientProfile, **recipient_profile_copy)

    # Mock Recipient Lookups
    for recipient_hash, recipient_lookup in TEST_RECIPIENT_LOOKUPS.items():
        mommy.make(RecipientLookup, **recipient_lookup)

    # Mock DUNS - should add `category-business`
    for duns, duns_dict in TEST_DUNS.items():
        test_duns_model = duns_dict.copy()
        country_code = test_duns_model['country_code']
        mommy.make(DUNS, **test_duns_model)
        mommy.make(RefCountryCode, **TEST_REF_COUNTRY_CODE[country_code])

    # Mock Legal Entity
    expected_business_cat = ['expected', 'business', 'cat']
    mommy.make(LegalEntity,
               business_categories=expected_business_cat,
               recipient_name='PARENT RECIPIENT',
               recipient_unique_id='000000001')

    resp = client.get(recipient_overview_endpoint(r_id))
    assert resp.status_code == status.HTTP_200_OK
    expected = {
        'name':
        'PARENT RECIPIENT',
        'duns':
        '000000001',
        'recipient_id':
        '00077a9a-5a70-8919-fd19-330762af6b84-C',
        'recipient_level':
        'C',
        'parent_name':
        'PARENT RECIPIENT',
        'parent_duns':
        '000000001',
        'parent_id':
        '00077a9a-5a70-8919-fd19-330762af6b84-P',
        'business_types':
        sorted(['expected', 'business', 'cat'] + ['category_business']),
        'location': {
            'address_line1': 'PARENT ADDRESS LINE 1',
            'address_line2': 'PARENT ADDRESS LINE 2',
            'address_line3': None,
            'county_name': None,
            'city_name': 'PARENT CITY',
            'congressional_code': 'PARENT CONGRESSIONAL DISTRICT',
            'country_code': 'PARENT COUNTRY CODE',
            'country_name': 'PARENT COUNTRY NAME',
            'state_code': 'PARENT STATE',
            'zip': 'PARENT ZIP',
            'zip4': 'PARENT ZIP4',
            'foreign_province': None,
            'foreign_postal_code': None
        },
        'total_transaction_amount':
        100,
        'total_transactions':
        1
    }
    # testing for equality-only, order unnecessary
    resp.data['business_types'] = sorted(resp.data['business_types'])
    assert resp.data == expected
Example #6
0
 def test_query_values_list_raises_type_error_when_kwargs_other_than_flat_specified(
         self):
     qs = MockSet(MockModel(foo=1), MockModel(foo=2))
     self.assertRaises(TypeError, qs.values_list, arg='value')
Example #7
0
 def test_query_values_list_raises_type_error_if_flat_and_named_are_true(
         self):
     qs = MockSet(MockModel(foo=1), MockModel(foo=2))
     self.assertRaises(TypeError, qs.values_list, flat=True, named=True)
 def test_valid_user_id(self):
     qs = MockSet(MockModel(username='******'))
     self.assertEqual(validate_user_id(qs, 'test_id'), 'test_id')
Example #9
0
def test_spending_by_award_both_zip_filter(client, mock_matviews_qs):
    """ Test that filtering by both kinds of zips works"""
    mock_model_1 = MockModel(recipient_location_zip5="00501",
                             recipient_location_country_code='USA',
                             pop_zip5='00001',
                             pop_country_code='USA',
                             award_id=1,
                             piid=None,
                             fain='abc',
                             uri=None,
                             type='B',
                             pulled_from="AWARD")
    mock_model_2 = MockModel(recipient_location_zip5="00502",
                             recipient_location_country_code='USA',
                             pop_zip5='00002',
                             pop_country_code='USA',
                             award_id=2,
                             piid=None,
                             fain='abd',
                             uri=None,
                             type='B',
                             pulled_from="AWARD")
    mock_model_3 = MockModel(recipient_location_zip5="00503",
                             recipient_location_country_code='USA',
                             pop_zip5='00003',
                             pop_country_code='USA',
                             award_id=3,
                             piid=None,
                             fain='abe',
                             uri=None,
                             type='B',
                             pulled_from="AWARD")
    add_to_mock_objects(mock_matviews_qs,
                        [mock_model_1, mock_model_2, mock_model_3])

    # test simple, single pair of zips that both match
    resp = client.post('/api/v2/search/spending_by_award/',
                       content_type='application/json',
                       data=json.dumps({
                           "fields": ["Place of Performance Zip5"],
                           "filters": {
                               "award_type_codes": ["A", "B", "C", "D"],
                               "recipient_locations": [{
                                   "country": "USA",
                                   "zip": "00501"
                               }],
                               "place_of_performance_locations": [{
                                   "country":
                                   "USA",
                                   "zip":
                                   "00001"
                               }]
                           }
                       }))
    assert len(resp.data['results']) == 1
    assert resp.data['results'][0] == {
        'internal_id': 1,
        'Place of Performance Zip5': '00001'
    }

    # test simple, single pair of zips that don't match
    resp = client.post('/api/v2/search/spending_by_award/',
                       content_type='application/json',
                       data=json.dumps({
                           "fields": ["Place of Performance Zip5"],
                           "filters": {
                               "award_type_codes": ["A", "B", "C", "D"],
                               "recipient_locations": [{
                                   "country": "USA",
                                   "zip": "00501"
                               }],
                               "place_of_performance_locations": [{
                                   "country":
                                   "USA",
                                   "zip":
                                   "00002"
                               }]
                           }
                       }))
    assert len(resp.data['results']) == 0

    # test 2 pairs (only one pair can be made from this)
    resp = client.post('/api/v2/search/spending_by_award/',
                       content_type='application/json',
                       data=json.dumps({
                           "fields": ["Place of Performance Zip5"],
                           "filters": {
                               "award_type_codes": ["A", "B", "C", "D"],
                               "recipient_locations": [{
                                   "country": "USA",
                                   "zip": "00501"
                               }, {
                                   "country": "USA",
                                   "zip": "00502"
                               }],
                               "place_of_performance_locations": [{
                                   "country":
                                   "USA",
                                   "zip":
                                   "00001"
                               }, {
                                   "country":
                                   "USA",
                                   "zip":
                                   "00003"
                               }]
                           }
                       }))
    assert len(resp.data['results']) == 1
    assert resp.data['results'][0] == {
        'internal_id': 1,
        'Place of Performance Zip5': '00001'
    }
def test_category_recipient_parent_duns_subawards(mock_matviews_qs,
                                                  mock_recipients):
    mock_recipient_1 = MockModel(recipient_unique_id="00UOP00",
                                 legal_entity_id=1)
    mock_recipient_2 = MockModel(recipient_unique_id="1234JD4321",
                                 legal_entity_id=2)
    mock_model_1 = MockModel(recipient_name="University of Pawnee",
                             parent_recipient_unique_id="00UOP00",
                             amount=1)
    mock_model_2 = MockModel(recipient_name="University of Pawnee",
                             parent_recipient_unique_id="00UOP00",
                             amount=1)
    mock_model_3 = MockModel(recipient_name="John Doe",
                             parent_recipient_unique_id="1234JD4321",
                             amount=1)
    mock_model_4 = MockModel(recipient_name="John Doe",
                             parent_recipient_unique_id="1234JD4321",
                             amount=10)

    add_to_mock_objects(
        mock_matviews_qs,
        [mock_model_1, mock_model_2, mock_model_3, mock_model_4])
    add_to_mock_objects(mock_recipients, [mock_recipient_1, mock_recipient_2])

    test_payload = {
        "category": "recipient_parent_duns",
        "subawards": True,
        "page": 1,
        "limit": 50
    }

    spending_by_category_logic = BusinessLogic(test_payload).results()

    expected_response = {
        "category":
        "recipient_parent_duns",
        "limit":
        50,
        "page_metadata": {
            "page": 1,
            "next": None,
            "previous": None,
            "hasNext": False,
            "hasPrevious": False
        },
        "results": [
            {
                "amount": 11,
                "name": "John Doe",
                "code": "1234JD4321",
                "id": 2
            },
            {
                "amount": 2,
                "name": "University of Pawnee",
                "code": "00UOP00",
                "id": 1
            },
        ],
    }

    assert expected_response == spending_by_category_logic
Example #11
0
def test_spending_by_award_subawards(client, mock_matviews_qs):

    mock_model_0 = MockModel(
        fain="",
        prime_award_type="IDV_A",
        award_ts_vector="",
        subaward_number="EP-W-13-028-0",
        award_type="procurement",
        recipient_name="Frodo Baggins",
        action_date="2013-10-01",
        amount=125000,
        awarding_toptier_agency_name="Environmental Protection Agency",
        awarding_subtier_agency_name="Environmental Protection Agency",
        piid="EPW13028",
        prime_recipient_name="Frodo Baggins")

    mock_model_1 = MockModel(
        fain="",
        prime_award_type="IDV_B",
        award_ts_vector="",
        subaward_number="EP-W-13-028-1",
        award_type="procurement",
        recipient_name="Samwise Gamgee",
        action_date="2013-09-01",
        amount=102432,
        awarding_toptier_agency_name="Environmental Protection Agency",
        awarding_subtier_agency_name="Environmental Protection Agency",
        piid="EPW13028",
        prime_recipient_name="Samwise Gamgee")

    mock_model_2 = MockModel(
        fain="",
        prime_award_type="IDV_C",
        award_ts_vector="",
        subaward_number="EP-W-13-028-2",
        award_type="procurement",
        recipient_name="Legolas Greenleaf",
        action_date="2013-09-01",
        amount=10,
        awarding_toptier_agency_name="Environmental Protection Agency",
        awarding_subtier_agency_name="Environmental Protection Agency",
        piid="EPW13028",
        prime_recipient_name="Legolas Greenleaf")

    mock_model_3 = MockModel(
        fain="",
        prime_award_type="IDV_D",
        award_ts_vector="",
        subaward_number="EP-W-13-028-3",
        award_type="procurement",
        recipient_name="Gandalf",
        action_date="2013-10-01",
        amount=125000,
        awarding_toptier_agency_name="Environmental Protection Agency",
        awarding_subtier_agency_name="Environmental Protection Agency",
        piid="EPW13028",
        prime_recipient_name="Gandalf")

    mock_model_4 = MockModel(
        fain="",
        prime_award_type="IDV_E",
        award_ts_vector="",
        subaward_number="EP-W-13-028-4",
        award_type="procurement",
        recipient_name="Radagast",
        action_date="2013-10-01",
        amount=125000,
        awarding_toptier_agency_name="Environmental Protection Agency",
        awarding_subtier_agency_name="Environmental Protection Agency",
        piid="EPW13028",
        prime_recipient_name="Radagast")

    mock_model_5 = MockModel(
        fain="",
        prime_award_type="IDV_B_A",
        award_ts_vector="",
        subaward_number="EP-W-13-028-5",
        award_type="procurement",
        recipient_name="Tom Bombadil",
        action_date="2013-10-01",
        amount=125000,
        awarding_toptier_agency_name="Environmental Protection Agency",
        awarding_subtier_agency_name="Environmental Protection Agency",
        piid="EPW13028",
        prime_recipient_name="Tom Bombadil")

    mock_model_6 = MockModel(
        fain="",
        prime_award_type="IDV_B_B",
        award_ts_vector="",
        subaward_number="EP-W-13-028-6",
        award_type="procurement",
        recipient_name="Tom Bombadil",
        action_date="2013-10-01",
        amount=125000,
        awarding_toptier_agency_name="Environmental Protection Agency",
        awarding_subtier_agency_name="Environmental Protection Agency",
        piid="EPW13028",
        prime_recipient_name="Tom Bombadil")

    mock_model_7 = MockModel(
        fain="",
        prime_award_type="IDV_B_C",
        award_ts_vector="",
        subaward_number="EP-W-13-028-7",
        award_type="procurement",
        recipient_name="Sauron",
        action_date="2013-10-01",
        amount=125000,
        awarding_toptier_agency_name="Environmental Protection Agency",
        awarding_subtier_agency_name="Environmental Protection Agency",
        piid="EPW13028",
        prime_recipient_name="Sauron")

    add_to_mock_objects(mock_matviews_qs, [
        mock_model_0, mock_model_1, mock_model_2, mock_model_3, mock_model_4,
        mock_model_5, mock_model_6, mock_model_7
    ])
    resp = client.post('/api/v2/search/spending_by_award',
                       content_type='application/json',
                       data=json.dumps({
                           "filters": {
                               "award_type_codes": [
                                   "IDV_A", "IDV_B", "IDV_B_A", "IDV_B_B",
                                   "IDV_B_C", "IDV_C", "IDV_D", "IDV_E"
                               ]
                           },
                           "fields": [
                               "Sub-Award ID", "Sub-Awardee Name",
                               "Sub-Award Date", "Sub-Award Amount",
                               "Awarding Agency", "Awarding Sub Agency",
                               "Prime Award ID", "Prime Recipient Name"
                           ],
                           "subawards":
                           True
                       }))
    assert resp.status_code == status.HTTP_200_OK
    assert len(resp.data['results']) == 8

    resp = client.post('/api/v2/search/spending_by_award',
                       content_type='application/json',
                       data=json.dumps({
                           "filters": {
                               "award_type_codes": ["IDV_A"]
                           },
                           "fields": ["Sub-Award ID"],
                           "subawards": True
                       }))
    assert resp.status_code == status.HTTP_200_OK
    assert len(resp.data['results']) == 1

    resp = client.post('/api/v2/search/spending_by_award',
                       content_type='application/json',
                       data=json.dumps({
                           "filters": {
                               "award_type_codes": ["IDV_B"]
                           },
                           "fields": ["Sub-Award ID"],
                           "subawards": True
                       }))
    assert resp.status_code == status.HTTP_200_OK
    assert len(resp.data['results']) == 1

    resp = client.post('/api/v2/search/spending_by_award',
                       content_type='application/json',
                       data=json.dumps({
                           "filters": {
                               "award_type_codes": ["IDV_C"]
                           },
                           "fields": ["Sub-Award ID"],
                           "subawards": True
                       }))
    assert resp.status_code == status.HTTP_200_OK
    assert len(resp.data['results']) == 1

    resp = client.post('/api/v2/search/spending_by_award',
                       content_type='application/json',
                       data=json.dumps({
                           "filters": {
                               "award_type_codes": ["IDV_D"]
                           },
                           "fields": ["Sub-Award ID"],
                           "subawards": True
                       }))
    assert resp.status_code == status.HTTP_200_OK
    assert len(resp.data['results']) == 1

    resp = client.post('/api/v2/search/spending_by_award',
                       content_type='application/json',
                       data=json.dumps({
                           "filters": {
                               "award_type_codes": ["IDV_E"]
                           },
                           "fields": ["Sub-Award ID"],
                           "subawards": True
                       }))
    assert resp.status_code == status.HTTP_200_OK
    assert len(resp.data['results']) == 1

    resp = client.post('/api/v2/search/spending_by_award',
                       content_type='application/json',
                       data=json.dumps({
                           "filters": {
                               "award_type_codes": ["IDV_B_A"]
                           },
                           "fields": ["Sub-Award ID"],
                           "subawards": True
                       }))
    assert resp.status_code == status.HTTP_200_OK
    assert len(resp.data['results']) == 1

    resp = client.post('/api/v2/search/spending_by_award',
                       content_type='application/json',
                       data=json.dumps({
                           "filters": {
                               "award_type_codes": ["IDV_B_B"]
                           },
                           "fields": ["Sub-Award ID"],
                           "subawards": True
                       }))
    assert resp.status_code == status.HTTP_200_OK
    assert len(resp.data['results']) == 1

    resp = client.post('/api/v2/search/spending_by_award',
                       content_type='application/json',
                       data=json.dumps({
                           "filters": {
                               "award_type_codes": ["IDV_B_C"]
                           },
                           "fields": ["Sub-Award ID"],
                           "subawards": True
                       }))
    assert resp.status_code == status.HTTP_200_OK
    assert len(resp.data['results']) == 1
def test_category_recipient_duns_awards(mock_matviews_qs):
    # recipient_hash = SELECT MD5(UPPER(CONCAT('<duns>','<recipient_name>')))::uuid;
    mock_model_1 = MockModel(
        recipient_hash="59f9a646-cd1c-cbdc-63dd-1020fac59336",
        generated_pragmatic_obligation=1)
    mock_model_2 = MockModel(
        recipient_hash="59f9a646-cd1c-cbdc-63dd-1020fac59336",
        generated_pragmatic_obligation=1)
    mock_model_3 = MockModel(
        recipient_hash="3725ba78-a607-7ab4-1cf6-2a08207bac3c",
        generated_pragmatic_obligation=1)
    mock_model_4 = MockModel(
        recipient_hash="3725ba78-a607-7ab4-1cf6-2a08207bac3c",
        generated_pragmatic_obligation=10)
    mock_model_5 = MockModel(
        recipient_hash="18569a71-3b0a-1586-50a9-cbb8bb070136",
        generated_pragmatic_obligation=15)

    mommy.make(
        "recipient.RecipientLookup",
        recipient_hash="3725ba78-a607-7ab4-1cf6-2a08207bac3c",
        legal_business_name="John Doe",
        duns="1234JD4321",
    )
    mommy.make(
        "recipient.RecipientLookup",
        recipient_hash="59f9a646-cd1c-cbdc-63dd-1020fac59336",
        legal_business_name="University of Pawnee",
        duns="00UOP00",
    )
    mommy.make(
        "recipient.RecipientLookup",
        recipient_hash="18569a71-3b0a-1586-50a9-cbb8bb070136",
        legal_business_name="MULTIPLE RECIPIENTS",
        duns=None,
    )
    add_to_mock_objects(
        mock_matviews_qs,
        [mock_model_1, mock_model_2, mock_model_3, mock_model_4, mock_model_5])

    test_payload = {
        "category": "recipient_duns",
        "subawards": False,
        "page": 1,
        "limit": 50
    }

    spending_by_category_logic = BusinessLogic(test_payload).results()

    expected_response = {
        "category":
        "recipient_duns",
        "limit":
        50,
        "page_metadata": {
            "page": 1,
            "next": None,
            "previous": None,
            "hasNext": False,
            "hasPrevious": False
        },
        "results": [
            {
                "amount": 15,
                "name": "MULTIPLE RECIPIENTS",
                "code": None,
                "id": None
            },
            {
                "amount": 11,
                "name": "John Doe",
                "code": "1234JD4321",
                "id": None
            },
            {
                "amount": 2,
                "name": "University of Pawnee",
                "code": "00UOP00",
                "id": None
            },
        ],
    }

    assert expected_response == spending_by_category_logic
def test_category_federal_accounts_subawards(mock_matviews_qs,
                                             mock_federal_account, mock_tas,
                                             mock_award,
                                             mock_financial_account,
                                             mock_transaction):
    fa = MockModel(id=10,
                   agency_identifier='020',
                   main_account_code='0001',
                   account_title='Test Federal Account')
    add_to_mock_objects(mock_federal_account, [fa])

    tas = MockModel(treasury_account_identifier=2, federal_account_id=10)
    add_to_mock_objects(mock_tas, [tas])

    award = MockModel(id=3)
    add_to_mock_objects(mock_award, [award])

    fs = MockModel(financial_accounts_by_awards_id=4,
                   submission_id=3,
                   treasury_account=tas,
                   award=award)
    add_to_mock_objects(mock_financial_account, [fs])

    award.financial_set = MockSet(fs)
    t1 = MockModel(award=award, id=5)
    t2 = MockModel(award=award, id=6)
    add_to_mock_objects(mock_transaction, [t1, t2])

    mock_model_1 = MockModel(transaction=t1,
                             recipient_hash='00000-00000-00000-00000-00000',
                             parent_recipient_unique_id='000000',
                             amount=1)
    mock_model_2 = MockModel(transaction=t2,
                             recipient_hash='00000-00000-00000-00000-00000',
                             parent_recipient_unique_id='000000',
                             amount=1)
    add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_2])

    test_payload = {
        'category': 'federal_account',
        'filters': {
            'recipient_id': '00000-00000-00000-00000-00000-C',
        },
        'subawards': True,
        'page': 1,
        'limit': 50
    }

    spending_by_category_logic = BusinessLogic(test_payload).results()

    expected_response = {
        'category':
        'federal_account',
        'limit':
        50,
        'page_metadata': {
            'page': 1,
            'next': None,
            'previous': None,
            'hasNext': False,
            'hasPrevious': False
        },
        'results': [{
            'amount': 2,
            'code': '020-0001',
            'name': 'Test Federal Account',
            'id': 10
        }]
    }

    assert expected_response == spending_by_category_logic
def test_child_recipient_success(client, mock_matviews_qs):
    """ Testing successfull child recipient calls """
    child1_id = '00077a9a-5a70-8919-fd19-330762af6b84-C'
    child1_hash = child1_id[:-2]
    parent_child1_name = 'PARENT RECIPIENT'
    parent_child1_duns = '000000001'
    child2_id = '392052ae-92ab-f3f4-d9fa-b57f45b7750b-C'
    child2_hash = child2_id[:-2]
    child2_name = 'CHILD RECIPIENT'
    child2_duns = '000000002'
    transaction_hash_map = {
        'latest': {
            'hash': child1_hash,
            'duns': parent_child1_duns,
            'name': parent_child1_name,
            'parent_duns': parent_child1_duns
        },
        'FY2016': {
            'hash': child2_hash,
            'duns': child2_duns,
            'name': child2_name,
            'parent_duns': parent_child1_duns
        },
        # Making sure the children total only applies to transactions where it listed the parent
        # Not all transactions of that child in general
        'FY2008': {
            'hash': child2_hash,
            'duns': child2_duns,
            'name': child2_name,
            'parent_duns': '000000009'
        }
    }

    # Mock Recipient Profiles
    for recipient_id, recipient_profile in TEST_RECIPIENT_PROFILES.items():
        mommy.make(RecipientProfile, **recipient_profile)

    # Mock Recipient Lookups
    for recipient_hash, recipient_lookup in TEST_RECIPIENT_LOOKUPS.items():
        mommy.make(RecipientLookup, **recipient_lookup)

    # load transactions for each child and parent (making sure it's excluded)
    mock_transactions = []
    for category, transaction in TEST_SUMMARY_TRANSACTIONS.items():
        transaction['recipient_hash'] = transaction_hash_map[category]['hash']
        transaction['recipient_unique_id'] = transaction_hash_map[category][
            'duns']
        transaction['recipient_name'] = transaction_hash_map[category]['name']
        transaction['parent_recipient_unique_id'] = transaction_hash_map[
            category]['parent_duns']
        mock_transactions.append(MockModel(**transaction))
    add_to_mock_objects(mock_matviews_qs, mock_transactions)

    # Ignoring nonexistent child duns - 000000005
    child1_object = {
        'recipient_id': child1_id,
        'name': 'PARENT RECIPIENT',
        'duns': parent_child1_duns,
        'amount': 100,
        'state_province': 'PARENT STATE'
    }
    child2_object = {
        'recipient_id': child2_id,
        'name': 'CHILD RECIPIENT',
        'duns': child2_duns,
        'amount': 50,
        'state_province': 'CHILD STATE'
    }
    expected = [child1_object, child2_object]
    resp = client.get(recipient_children_endpoint(parent_child1_duns, 'all'))
    assert resp.status_code == status.HTTP_200_OK
    # testing for equality-only, order unnecessary
    assert sorted(resp.data, key=lambda key: key['recipient_id']) == expected
Example #15
0
 def test_query_values_list_raises_not_implemented_if_no_fields_specified(
         self):
     qs = MockSet(MockModel(foo=1), MockModel(foo=2))
     self.assertRaises(NotImplementedError, qs.values_list)
Example #16
0
def test_spending_by_award_foreign_filter(client, mock_matviews_qs):
    """ Verify that foreign country filter is returning the correct results """
    mock_model_0 = MockModel(award_id=0,
                             piid=None,
                             fain='aaa',
                             uri=None,
                             type='B',
                             pulled_from="AWARD",
                             recipient_location_country_name="UNITED STATES",
                             recipient_location_country_code="USA")
    mock_model_1 = MockModel(award_id=1,
                             piid=None,
                             fain='abc',
                             uri=None,
                             type='B',
                             pulled_from="AWARD",
                             recipient_location_country_name="",
                             recipient_location_country_code="USA")
    mock_model_2 = MockModel(award_id=2,
                             piid=None,
                             fain='abd',
                             uri=None,
                             type='B',
                             pulled_from="AWARD",
                             recipient_location_country_name="UNITED STATES",
                             recipient_location_country_code="")
    mock_model_3 = MockModel(award_id=3,
                             piid=None,
                             fain='abe',
                             uri=None,
                             type='B',
                             pulled_from="AWARD",
                             recipient_location_country_name="Gibraltar",
                             recipient_location_country_code="GIB")

    add_to_mock_objects(
        mock_matviews_qs,
        [mock_model_0, mock_model_1, mock_model_2, mock_model_3])
    # add_to_mock_objects(mock_matviews_qs, [mock_model_1, mock_model_3])

    resp = client.post(
        '/api/v2/search/spending_by_award/',
        content_type='application/json',
        data=json.dumps({
            "filters": {
                "award_type_codes": ["A", "B", "C", "D"],
                # "recipient_locations": [{"country": "USA"}]
                "recipient_scope": "domestic"
            },
            "fields": ["Award ID"]
        }))
    # Three results are returned when searching for "USA"-based recipients
    # e.g. "USA"; "UNITED STATES"; "USA" and "UNITED STATES";
    assert len(resp.data['results']) == 3

    resp = client.post('/api/v2/search/spending_by_award/',
                       content_type='application/json',
                       data=json.dumps({
                           "filters": {
                               "award_type_codes": ["A", "B", "C", "D"],
                               "recipient_scope": "foreign"
                           },
                           "fields": ["Award ID"],
                       }))
    # One result is returned when searching for "Foreign" recipients
    assert len(resp.data['results']) == 1
Example #17
0
class MockFormset(object):
    queryset = MockSet(*[MockModel(name=str(i)) for i in range(0, 100)])
    per_page = 20
    page_param = 'mock'
Example #18
0
def test_spending_by_award_pop_zip_filter(client, mock_matviews_qs):
    """ Test that filtering by pop zips works"""
    mock_model_1 = MockModel(pop_zip5="00501",
                             pop_country_code='USA',
                             award_id=1,
                             piid=None,
                             fain='abc',
                             uri=None,
                             type='B',
                             pulled_from="AWARD")
    mock_model_2 = MockModel(pop_zip5="00502",
                             pop_country_code='USA',
                             award_id=2,
                             piid=None,
                             fain='abd',
                             uri=None,
                             type='B',
                             pulled_from="AWARD")
    mock_model_3 = MockModel(pop_zip5="00503",
                             pop_country_code='USA',
                             award_id=3,
                             piid=None,
                             fain='abe',
                             uri=None,
                             type='B',
                             pulled_from="AWARD")
    add_to_mock_objects(mock_matviews_qs,
                        [mock_model_1, mock_model_2, mock_model_3])

    # test simple, single zip
    resp = client.post('/api/v2/search/spending_by_award/',
                       content_type='application/json',
                       data=json.dumps({
                           "fields": ["Place of Performance Zip5"],
                           "filters": {
                               "award_type_codes": ["A", "B", "C", "D"],
                               "place_of_performance_locations": [{
                                   "country":
                                   "USA",
                                   "zip":
                                   "00501"
                               }]
                           }
                       }))
    assert len(resp.data['results']) == 1
    assert resp.data['results'][0] == {
        'internal_id': 1,
        'Place of Performance Zip5': '00501'
    }

    # test that adding a zip that has no results doesn't remove the results from the first zip
    resp = client.post('/api/v2/search/spending_by_award/',
                       content_type='application/json',
                       data=json.dumps({
                           "fields": ["Place of Performance Zip5"],
                           "filters": {
                               "award_type_codes": ["A", "B", "C", "D"],
                               "place_of_performance_locations": [{
                                   "country":
                                   "USA",
                                   "zip":
                                   "00501"
                               }, {
                                   "country":
                                   "USA",
                                   "zip":
                                   "10000"
                               }]
                           }
                       }))
    assert len(resp.data['results']) == 1
    assert resp.data['results'][0] == {
        'internal_id': 1,
        'Place of Performance Zip5': '00501'
    }

    # test that we get 2 results with 2 valid zips
    resp = client.post('/api/v2/search/spending_by_award/',
                       content_type='application/json',
                       data=json.dumps({
                           "fields": ["Place of Performance Zip5"],
                           "filters": {
                               "award_type_codes": ["A", "B", "C", "D"],
                               "place_of_performance_locations": [{
                                   "country":
                                   "USA",
                                   "zip":
                                   "00501"
                               }, {
                                   "country":
                                   "USA",
                                   "zip":
                                   "00502"
                               }]
                           }
                       }))
    possible_results = ({
        'internal_id': 1,
        'Place of Performance Zip5': '00501'
    }, {
        'internal_id': 2,
        'Place of Performance Zip5': '00502'
    })
    assert len(resp.data['results']) == 2
    assert resp.data['results'][0] in possible_results
    assert resp.data['results'][1] in possible_results
    # Just to make sure it isn't returning the same thing twice somehow
    assert resp.data['results'][0] != resp.data['results'][1]
Example #19
0
 def test_query_values_list_raises_type_error_when_flat_specified_with_multiple_fields(
         self):
     qs = MockSet(MockModel(foo=1, bar=1), MockModel(foo=2, bar=2))
     self.assertRaises(TypeError, qs.values_list, 'foo', 'bar', flat=True)
Example #20
0
def awards_over_different_date_ranges_with_different_counts(mock_matviews_qs):
    award_category_list_and_counts = {
        "contracts": 5,
        "direct_payments": 8,
        "grants": 16,
        "idvs": 10,
        "loans": 9,
        "other_financial_assistance": 14,
    }

    # The date ranges for the different awards are setup to cover possible intersection points by the
    # different date ranges being searched. The comments on each line specify where the date ranges are
    # suppose to overlap the searched for date ranges. The search for date ranges are:
    #    - {"start_date": "2015-01-01", "end_date": "2015-12-31"}
    #    - {"start_date": "2017-02-01", "end_date": "2017-11-30"}
    date_range_list = [
        # Intersect only one of the date ranges searched for
        {"date_signed": datetime(2014, 1, 1), "action_date": datetime(2014, 5, 1)},  # Before both
        {"date_signed": datetime(2014, 3, 1), "action_date": datetime(2015, 4, 15)},  # Beginning of first
        {"date_signed": datetime(2015, 2, 1), "action_date": datetime(2015, 7, 1)},  # Middle of first
        {"date_signed": datetime(2015, 2, 1), "action_date": datetime(2015, 4, 17)},
        {"date_signed": datetime(2014, 12, 1), "action_date": datetime(2016, 1, 1)},  # All of first
        {"date_signed": datetime(2015, 11, 1), "action_date": datetime(2016, 3, 1)},  # End of first
        {"date_signed": datetime(2016, 2, 23), "action_date": datetime(2016, 7, 19)},  # Between both
        {"date_signed": datetime(2016, 11, 26), "action_date": datetime(2017, 3, 1)},  # Beginning of second
        {"date_signed": datetime(2017, 5, 1), "action_date": datetime(2017, 7, 1)},  # Middle of second
        {"date_signed": datetime(2017, 1, 1), "action_date": datetime(2017, 12, 1)},  # All of second
        {"date_signed": datetime(2017, 9, 1), "action_date": datetime(2017, 12, 17)},  # End of second
        {"date_signed": datetime(2018, 2, 1), "action_date": datetime(2018, 7, 1)},  # After both
        # Intersect both date ranges searched for
        {"date_signed": datetime(2014, 12, 1), "action_date": datetime(2017, 12, 5)},  # Completely both
        {"date_signed": datetime(2015, 7, 1), "action_date": datetime(2017, 5, 1)},  # Partially both
        {"date_signed": datetime(2014, 10, 3), "action_date": datetime(2017, 4, 8)},  # All first; partial second
        {"date_signed": datetime(2015, 8, 1), "action_date": datetime(2018, 1, 2)},  # Partial first; all second
    ]

    award_id = 0
    mock_model_list = []

    for award_category, count in award_category_list_and_counts.items():
        for date_range in date_range_list[:count]:
            award_id += 1
            award_type_list = all_award_types_mappings[award_category]
            award_type = award_type_list[award_id % len(award_type_list)]

            mock_model_list.append(
                MockModel(
                    award_ts_vector="",
                    type=award_type,
                    category=award_category,
                    type_of_contract_pricing="",
                    naics_code=str(9000 + award_id),
                    cfda_number=str(900 + award_id),
                    pulled_from=None,
                    uri=None,
                    piid="abcdefg{}".format(award_id),
                    fain=None,
                    award_id=award_id,
                    awarding_toptier_agency_name="Department of {}".format(award_id),
                    awarding_toptier_agency_abbreviation="DOP",
                    generated_pragmatic_obligation=10,
                    date_signed=date_range["date_signed"],
                    action_date=date_range["action_date"],
                    counts=1,
                )
            )

    add_to_mock_objects(mock_matviews_qs, mock_model_list)
Example #21
0
 def test_query_values_raises_attribute_error_when_field_is_not_in_meta_concrete_fields(
         self):
     qs = MockSet(MockModel(foo=1), MockModel(foo=2))
     self.assertRaises(FieldError, qs.values, 'bar')
Example #22
0
class DoesTokenExistTestCase(TestCase):
    # Sets up some fake bookings
    fake_bookings = MockSet(
        MockModel(setid='LIVE-17-18',
                  finishtime='16:00',
                  id=8869,
                  weeknumber=12.0,
                  starttime='13:00',
                  bookabletype='CB',
                  title='Some topic',
                  finishdatetime=datetime.datetime(2017, 11, 14, 16, 0),
                  descrip='8 x rooms needed',
                  slotid=1662773,
                  sitename='IOE - Bedford Way, 20',
                  phone=None,
                  siteid='162',
                  roomname='IOE - Bedford Way (20) - 790',
                  condisplayname='Some Lecturer',
                  startdatetime=datetime.datetime(2017, 11, 14, 13, 0),
                  roomid='790',
                  bookingid=None),
        MockModel(setid='LIVE-17-18',
                  finishtime='12:30',
                  id=13692,
                  weeknumber=21.0,
                  starttime='11:00',
                  bookabletype='CB',
                  title='Another topic',
                  finishdatetime=datetime.datetime(2018, 1, 19, 12, 30),
                  descrip=None,
                  slotid=1673854,
                  sitename='IOE - Bedford Way, 20',
                  phone=None,
                  siteid='162',
                  roomname='Some other room',
                  condisplayname='Some other lecturer',
                  startdatetime=datetime.datetime(2018, 1, 19, 11, 0),
                  roomid='418',
                  bookingid=None))
    booking_objects = unittest.mock.patch(
        'roombookings.models.Booking.objects', fake_bookings)
    bookinga_objects = unittest.mock.patch(
        'roombookings.models.BookingA.objects', fake_bookings)
    bookingb_objects = unittest.mock.patch(
        'roombookings.models.BookingB.objects', fake_bookings)

    fake_locks = MockSet(MockModel(a=True, b=False))

    lock_objects = unittest.mock.patch('timetable.models.Lock.objects',
                                       fake_locks)

    def setUp(self):
        self.factory = APIRequestFactory()

        # General temporary token for tests
        self.token = get_temp_token()

        # A valid token to use later
        self.valid_token = get_temp_token()

        # Standard Token data
        self.user_ = User.objects.create(cn="test", employee_id=7357)
        self.app = App.objects.create(user=self.user_, name="An App")

    @booking_objects
    @bookinga_objects
    @bookingb_objects
    @lock_objects
    def test_no_token_provided(self):
        request = self.factory.get('/a/random/path')
        response = get_bookings(request)
        content = json.loads(response.content.decode())

        self.assertEqual(response.status_code, 400)
        self.assertFalse(content["ok"])
        self.assertEqual(content["error"], "No token provided.")

    @booking_objects
    @bookinga_objects
    @bookingb_objects
    @lock_objects
    def test_invalid_token_provided(self):
        request = self.factory.get('/a/random/path', {'token': 'uclapi'})
        response = get_bookings(request)

        content = json.loads(response.content.decode())
        self.assertEqual(response.status_code, 400)
        self.assertFalse(content["ok"])
        self.assertEqual(content["error"], "Token is invalid.")

    @booking_objects
    @bookinga_objects
    @bookingb_objects
    @lock_objects
    def test_invalid_temp_token_provided(self):
        request = self.factory.get('/a/random/path',
                                   {'token': 'uclapi-temp-invalid-token'})
        response = get_bookings(request)

        content = json.loads(response.content.decode())
        self.assertEqual(response.status_code, 400)
        self.assertFalse(content["ok"])
        self.assertEqual(content["error"],
                         "Temporary token is either invalid or expired.")

    @booking_objects
    @bookinga_objects
    @bookingb_objects
    @lock_objects
    def test_temp_token_wrong_path(self):
        request = self.factory.get('/a/path', {'token': self.token})
        response = get_bookings(request)

        content = json.loads(response.content.decode())
        self.assertEqual(response.status_code, 400)
        self.assertFalse(content["ok"])
        self.assertEqual(content["error"],
                         "Temporary token can only be used for /bookings.")

    @booking_objects
    @bookinga_objects
    @bookingb_objects
    @lock_objects
    def test_temp_token_page_token_provided(self):
        request = self.factory.get('/roombookings/bookings', {
            'token': self.token,
            'page_token': 'next_page_comes_here'
        })
        response = get_bookings(request)

        content = json.loads(response.content.decode())
        self.assertEqual(response.status_code, 400)
        self.assertFalse(content["ok"])
        self.assertEqual(content["error"],
                         "Temporary token can only return one booking.")

    @booking_objects
    @bookinga_objects
    @bookingb_objects
    @lock_objects
    def test_temp_token_overused(self):
        request = self.factory.get('/roombookings/bookings',
                                   {'token': self.token})
        for _ in repeat(None, 11):
            response = get_bookings(request)

        content = json.loads(response.content.decode())
        self.assertEqual(response.status_code, 429)
        self.assertFalse(content["ok"])
        self.assertEqual("You have been throttled. Please try again in ",
                         content["error"][:45])

    @booking_objects
    @bookinga_objects
    @bookingb_objects
    @lock_objects
    def test_temp_token_valid(self):
        request = self.factory.get('/roombookings/bookings',
                                   {'token': self.valid_token})
        response = get_bookings(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(request.GET['results_per_page'], 1)

    @booking_objects
    @bookinga_objects
    @bookingb_objects
    @lock_objects
    def test_normal_token_valid(self):
        request = self.factory.get('/roombookings/bookings',
                                   {'token': self.app.api_token})
        response = get_bookings(request)

        self.assertEqual(response.status_code, 200)
Example #23
0
 def test_query_model_repr_returns_mock_name(self):
     model = MockModel(mock_name='model_name')
     assert repr(model) == model.mock_name
def test_category_recipient_duns_subawards(mock_matviews_qs, mock_recipients):
    mock_recipient_1 = MockModel(recipient_unique_id='00UOP00',
                                 legal_entity_id=1)
    mock_recipient_2 = MockModel(recipient_unique_id='1234JD4321',
                                 legal_entity_id=2)
    mock_recipient_3 = MockModel(recipient_name='MULTIPLE RECIPIENTS',
                                 recipient_unique_id=None,
                                 legal_entity_id=3)
    mock_model_1 = MockModel(recipient_name='University of Pawnee',
                             recipient_unique_id='00UOP00',
                             amount=1)
    mock_model_2 = MockModel(recipient_name='University of Pawnee',
                             recipient_unique_id='00UOP00',
                             amount=1)
    mock_model_3 = MockModel(recipient_name='John Doe',
                             recipient_unique_id='1234JD4321',
                             amount=1)
    mock_model_4 = MockModel(recipient_name='John Doe',
                             recipient_unique_id='1234JD4321',
                             amount=10)
    mock_model_5 = MockModel(recipient_name='MULTIPLE RECIPIENTS',
                             recipient_unique_id=None,
                             amount=15)

    add_to_mock_objects(
        mock_matviews_qs,
        [mock_model_1, mock_model_2, mock_model_3, mock_model_4, mock_model_5])
    add_to_mock_objects(mock_recipients,
                        [mock_recipient_1, mock_recipient_2, mock_recipient_3])

    test_payload = {
        'category': 'recipient_duns',
        'subawards': True,
        'page': 1,
        'limit': 50
    }

    spending_by_category_logic = BusinessLogic(test_payload).results()

    expected_response = {
        'category':
        'recipient_duns',
        'limit':
        50,
        'page_metadata': {
            'page': 1,
            'next': None,
            'previous': None,
            'hasNext': False,
            'hasPrevious': False
        },
        'results': [{
            'amount': 15,
            'name': 'MULTIPLE RECIPIENTS',
            'code': None,
            'id': 3
        }, {
            'amount': 11,
            'name': 'John Doe',
            'code': '1234JD4321',
            'id': 2
        }, {
            'amount': 2,
            'name': 'University of Pawnee',
            'code': '00UOP00',
            'id': 1
        }]
    }

    assert expected_response == spending_by_category_logic