def test_obtain_recipient_totals_year():
    """ Testing recipient totals with different year values """
    # Testing with specific child
    recipient_id = "392052ae-92ab-f3f4-d9fa-b57f45b7750b-C"
    recipient_hash = recipient_id[:-2]

    # load all of the transactions
    transaction_fpds_recipient_data = {
        "awardee_or_recipient_legal": "CHILD RECIPIENT",
        "awardee_or_recipient_uniqu": "000000002",
        "ultimate_parent_unique_ide": "000000001",
    }
    create_transaction_test_data(
        TEST_SUMMARY_TRANSACTION_NORMALIZED.values(),
        [transaction_fpds_recipient_data] *
        len(TEST_SUMMARY_TRANSACTION_NORMALIZED),
    )

    # load recipient lookup
    create_recipient_lookup_test_data(TEST_RECIPIENT_LOOKUPS[recipient_hash])

    # For latest transaction's we're pulling from recipient_profile
    associated_recipient_profile = TEST_RECIPIENT_PROFILES[recipient_id].copy()
    associated_recipient_profile["last_12_months"] = 100
    associated_recipient_profile["last_12_months_count"] = 1
    mommy.make("recipient.RecipientProfile", **associated_recipient_profile)

    # Latest
    expected_total = 100
    expected_count = 1
    results = recipients.obtain_recipient_totals(recipient_id,
                                                 year="latest",
                                                 subawards=False)
    assert results[0]["total"] == expected_total
    assert results[0]["count"] == expected_count

    # All
    expected_total = 350
    expected_count = 3
    results = recipients.obtain_recipient_totals(recipient_id,
                                                 year="all",
                                                 subawards=False)
    assert results[0]["total"] == expected_total
    assert results[0]["count"] == expected_count

    # FY2016
    expected_total = 50
    expected_count = 1
    results = recipients.obtain_recipient_totals(recipient_id,
                                                 year="2016",
                                                 subawards=False)
    assert results[0]["total"] == expected_total
    assert results[0]["count"] == expected_count
def test_obtain_recipient_totals_year(monkeypatch,
                                      elasticsearch_transaction_index):
    """ Testing recipient totals with different year values """
    # Testing with specific child
    recipient_id = "392052ae-92ab-f3f4-d9fa-b57f45b7750b-C"
    recipient_hash = recipient_id[:-2]

    # load all of the transactions
    transaction_recipient_data = {
        "awardee_or_recipient_legal": "CHILD RECIPIENT",
        "awardee_or_recipient_uniqu": "000000002",
        "ultimate_parent_unique_ide": "000000001",
    }
    create_transaction_test_data([transaction_recipient_data] *
                                 len(TEST_SUMMARY_TRANSACTION_RECIPIENT))

    # load recipient lookup
    create_recipient_lookup_test_data(TEST_RECIPIENT_LOOKUPS[recipient_hash])

    # For latest transaction's we're pulling from recipient_profile
    associated_recipient_profile = TEST_RECIPIENT_PROFILES[recipient_id].copy()
    associated_recipient_profile["last_12_months"] = 100
    associated_recipient_profile["last_12_months_count"] = 1
    mommy.make("recipient.RecipientProfile", **associated_recipient_profile)

    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)

    # Latest
    results = recipients.obtain_recipient_totals(recipient_id, year="latest")
    assert results[0]["total_obligation_amount"] == 100
    assert results[0]["total_obligation_count"] == 2
    assert results[0]["total_face_value_loan_amount"] == 1000
    assert results[0]["total_face_value_loan_count"] == 1

    # All
    results = recipients.obtain_recipient_totals(recipient_id, year="all")
    assert results[0]["total_obligation_amount"] == 350
    assert results[0]["total_obligation_count"] == 6
    assert results[0]["total_face_value_loan_amount"] == 3500
    assert results[0]["total_face_value_loan_count"] == 3

    # FY2016
    results = recipients.obtain_recipient_totals(recipient_id, year="2016")
    assert results[0]["total_obligation_amount"] == 50
    assert results[0]["total_obligation_count"] == 2
    assert results[0]["total_face_value_loan_amount"] == 500
    assert results[0]["total_face_value_loan_count"] == 1
def test_obtain_recipient_totals_year(mock_matviews_qs):
    """ Testing recipient totals with different year values """
    # Testing with specific child
    recipient_id = "392052ae-92ab-f3f4-d9fa-b57f45b7750b-C"
    recipient_hash = recipient_id[:-2]
    # load all of the transactions
    mock_transactions = []
    for category, transaction in TEST_SUMMARY_TRANSACTIONS.items():
        transaction["recipient_hash"] = recipient_hash
        transaction["parent_recipient_unique_id"] = "000000009"
        mock_transactions.append(MockModel(**transaction))
    add_to_mock_objects(mock_matviews_qs, mock_transactions)

    # For latest transaction's we're pulling from recipient_profile
    associated_recipient_profile = TEST_RECIPIENT_PROFILES[recipient_id].copy()
    associated_recipient_profile["last_12_months"] = 100
    associated_recipient_profile["last_12_months_count"] = 1
    mommy.make(RecipientProfile, **associated_recipient_profile)

    # Latest
    expected_total = 100
    expected_count = 1
    results = recipients.obtain_recipient_totals(recipient_id,
                                                 year="latest",
                                                 subawards=False)
    assert results[0]["total"] == expected_total
    assert results[0]["count"] == expected_count

    # All
    expected_total = 350
    expected_count = 3
    results = recipients.obtain_recipient_totals(recipient_id,
                                                 year="all",
                                                 subawards=False)
    assert results[0]["total"] == expected_total
    assert results[0]["count"] == expected_count

    # FY2016
    expected_total = 50
    expected_count = 1
    results = recipients.obtain_recipient_totals(recipient_id,
                                                 year="2016",
                                                 subawards=False)
    assert results[0]["total"] == expected_total
    assert results[0]["count"] == expected_count
def test_obtain_recipient_totals_year(mock_matviews_qs):
    """ Testing recipient totals with different year values """
    # Testing with specific child
    recipient_id = '392052ae-92ab-f3f4-d9fa-b57f45b7750b-C'
    recipient_hash = recipient_id[:-2]
    # load all of the transactions
    mock_transactions = []
    for category, transaction in TEST_SUMMARY_TRANSACTIONS.items():
        transaction['recipient_hash'] = recipient_hash
        transaction['parent_recipient_unique_id'] = '000000009'
        mock_transactions.append(MockModel(**transaction))
    add_to_mock_objects(mock_matviews_qs, mock_transactions)

    # For latest transaction's we're pulling from recipient_profile
    associated_recipient_profile = TEST_RECIPIENT_PROFILES[recipient_id].copy()
    associated_recipient_profile['last_12_months'] = 100
    associated_recipient_profile['last_12_months_count'] = 1
    mommy.make(RecipientProfile, **associated_recipient_profile)

    # Latest
    expected_total = 100
    expected_count = 1
    results = recipients.obtain_recipient_totals(recipient_id, year='latest', subawards=False)
    assert results[0]['total'] == expected_total
    assert results[0]['count'] == expected_count

    # All
    expected_total = 350
    expected_count = 3
    results = recipients.obtain_recipient_totals(recipient_id, year='all', subawards=False)
    assert results[0]['total'] == expected_total
    assert results[0]['count'] == expected_count

    # FY2016
    expected_total = 50
    expected_count = 1
    results = recipients.obtain_recipient_totals(recipient_id, year='2016', subawards=False)
    assert results[0]['total'] == expected_total
    assert results[0]['count'] == expected_count
def test_obtain_recipient_totals_parent(mock_matviews_qs, ):
    """ Testing recipient totals with parent child relationships """
    # Testing with specific parent/child ids
    parent_id = "00077a9a-5a70-8919-fd19-330762af6b84-P"
    child1_id = "00077a9a-5a70-8919-fd19-330762af6b84-C"
    child1_hash = child1_id[:-2]
    parent_child1_duns = "000000001"
    child2_id = "392052ae-92ab-f3f4-d9fa-b57f45b7750b-C"
    child2_hash = child2_id[:-2]
    other_id = "00002940-fdbe-3fc5-9252-d46c0ae8758c-R"
    transaction_hash_map = {
        "latest": {
            "hash": child1_hash,
            "parent_duns": parent_child1_duns
        },
        "FY2016": {
            "hash": child2_hash,
            "parent_duns": parent_child1_duns
        },
        "FY2008": {
            "hash": other_id,
            "parent_duns": None
        },
    }

    # load recipient profiles
    for recipient_id, recipient_profile in TEST_RECIPIENT_PROFILES.items():
        mommy.make(RecipientProfile, **recipient_profile)

    # 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["parent_recipient_unique_id"] = transaction_hash_map[
            category]["parent_duns"]
        mock_transactions.append(MockModel(**transaction))
    add_to_mock_objects(mock_matviews_qs, mock_transactions)

    expected_total = 150
    expected_count = 2
    results = recipients.obtain_recipient_totals(parent_id,
                                                 year="all",
                                                 subawards=False)
    assert results[0]["total"] == expected_total
    assert results[0]["count"] == expected_count
def test_obtain_recipient_totals_parent(mock_matviews_qs, ):
    """ Testing recipient totals with parent child relationships """
    # Testing with specific parent/child ids
    parent_id = '00077a9a-5a70-8919-fd19-330762af6b84-P'
    child1_id = '00077a9a-5a70-8919-fd19-330762af6b84-C'
    child1_hash = child1_id[:-2]
    parent_child1_duns = '000000001'
    child2_id = '392052ae-92ab-f3f4-d9fa-b57f45b7750b-C'
    child2_hash = child2_id[:-2]
    other_id = '00002940-fdbe-3fc5-9252-d46c0ae8758c-R'
    transaction_hash_map = {
        'latest': {
            'hash': child1_hash,
            'parent_duns': parent_child1_duns
        },
        'FY2016': {
            'hash': child2_hash,
            'parent_duns': parent_child1_duns,
        },
        'FY2008': {
            'hash': other_id,
            'parent_duns': None
        }
    }

    # load recipient profiles
    for recipient_id, recipient_profile in TEST_RECIPIENT_PROFILES.items():
        mommy.make(RecipientProfile, **recipient_profile)

    # 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['parent_recipient_unique_id'] = transaction_hash_map[
            category]['parent_duns']
        mock_transactions.append(MockModel(**transaction))
    add_to_mock_objects(mock_matviews_qs, mock_transactions)

    expected_total = 150
    expected_count = 2
    results = recipients.obtain_recipient_totals(parent_id,
                                                 year='all',
                                                 subawards=False)
    assert results[0]['total'] == expected_total
    assert results[0]['count'] == expected_count
def test_obtain_recipient_totals_parent(mock_matviews_qs, ):
    """ Testing recipient totals with parent child relationships """
    # Testing with specific parent/child ids
    parent_id = '00077a9a-5a70-8919-fd19-330762af6b84-P'
    child1_id = '00077a9a-5a70-8919-fd19-330762af6b84-C'
    child1_hash = child1_id[:-2]
    parent_child1_duns = '000000001'
    child2_id = '392052ae-92ab-f3f4-d9fa-b57f45b7750b-C'
    child2_hash = child2_id[:-2]
    other_id = '00002940-fdbe-3fc5-9252-d46c0ae8758c-R'
    transaction_hash_map = {
        'latest': {
            'hash': child1_hash,
            'parent_duns': parent_child1_duns
        },
        'FY2016': {
            'hash': child2_hash,
            'parent_duns': parent_child1_duns,
        },
        'FY2008': {
            'hash': other_id,
            'parent_duns': None
        }
    }

    # load recipient profiles
    for recipient_id, recipient_profile in TEST_RECIPIENT_PROFILES.items():
        mommy.make(RecipientProfile, **recipient_profile)

    # 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['parent_recipient_unique_id'] = transaction_hash_map[category]['parent_duns']
        mock_transactions.append(MockModel(**transaction))
    add_to_mock_objects(mock_matviews_qs, mock_transactions)

    expected_total = 150
    expected_count = 2
    results = recipients.obtain_recipient_totals(parent_id, year='all', subawards=False)
    assert results[0]['total'] == expected_total
    assert results[0]['count'] == expected_count
def test_obtain_recipient_totals_parent(monkeypatch, elasticsearch_transaction_index):
    """ Testing recipient totals with parent child relationships """
    # Testing with specific parent/child ids
    parent_id = "00077a9a-5a70-8919-fd19-330762af6b84-P"

    # load recipient profiles
    for recipient_id, recipient_profile in TEST_RECIPIENT_PROFILES.items():
        mommy.make("recipient.RecipientProfile", **recipient_profile)

    # load transactions for each child and parent (making sure it's excluded)
    create_transaction_test_data()

    # load recipient lookup
    create_recipient_lookup_test_data(*TEST_RECIPIENT_LOOKUPS.values())

    setup_elasticsearch_test(monkeypatch, elasticsearch_transaction_index)

    results = recipients.obtain_recipient_totals(parent_id, year="all")
    assert results[0]["total_obligation_amount"] == 150
    assert results[0]["total_obligation_count"] == 4
    assert results[0]["total_face_value_loan_amount"] == 1500
    assert results[0]["total_face_value_loan_count"] == 2
def test_obtain_recipient_totals_parent():
    """ Testing recipient totals with parent child relationships """
    # Testing with specific parent/child ids
    parent_id = "00077a9a-5a70-8919-fd19-330762af6b84-P"

    # load recipient profiles
    for recipient_id, recipient_profile in TEST_RECIPIENT_PROFILES.items():
        mommy.make("recipient.RecipientProfile", **recipient_profile)

    # load transactions for each child and parent (making sure it's excluded)
    create_transaction_test_data(TEST_SUMMARY_TRANSACTION_NORMALIZED.values(),
                                 TEST_SUMMARY_TRANSACTION_FPDS.values())

    # load recipient lookup
    create_recipient_lookup_test_data(*TEST_RECIPIENT_LOOKUPS.values())

    expected_total = 150
    expected_count = 2
    results = recipients.obtain_recipient_totals(parent_id,
                                                 year="all",
                                                 subawards=False)
    assert results[0]["total"] == expected_total
    assert results[0]["count"] == expected_count