-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1341 from fedspendingtransparency/stg
Mid-Sprint 67 Production Deployment
- Loading branch information
Showing
42 changed files
with
883 additions
and
92 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
usaspending_api/accounts/tests/unit/test_account_download_filter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import pytest | ||
|
||
from model_mommy import mommy | ||
|
||
from usaspending_api.accounts.models import AppropriationAccountBalances | ||
from usaspending_api.accounts.v2.filters.account_download import account_download_filter | ||
from usaspending_api.awards.models import FinancialAccountsByAwards | ||
from usaspending_api.financial_activities.models import FinancialAccountsByProgramActivityObjectClass | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_fyq_filter(): | ||
""" Ensure the fiscal year and quarter filter is working """ | ||
# Create TAS models | ||
tas1 = mommy.make('accounts.TreasuryAppropriationAccount') | ||
tas2 = mommy.make('accounts.TreasuryAppropriationAccount') | ||
|
||
# Create file A models | ||
mommy.make('accounts.AppropriationAccountBalances', treasury_account_identifier=tas1, | ||
reporting_period_start='1699-10-01', reporting_period_end='1699-12-31') | ||
mommy.make('accounts.AppropriationAccountBalances', treasury_account_identifier=tas2, | ||
reporting_period_start='1700-01-01', reporting_period_end='1700-03-31') | ||
|
||
queryset = account_download_filter('account_balances', AppropriationAccountBalances, { | ||
'fy': 1700, | ||
'quarter': 1 | ||
}) | ||
assert queryset.count() == 1 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_federal_account_filter(): | ||
""" Ensure the fiscal year and quarter filter is working """ | ||
# Create FederalAccount models | ||
fed_acct1 = mommy.make('accounts.FederalAccount') | ||
fed_acct2 = mommy.make('accounts.FederalAccount') | ||
|
||
# Create TAS models | ||
tas1 = mommy.make('accounts.TreasuryAppropriationAccount', federal_account=fed_acct1) | ||
tas2 = mommy.make('accounts.TreasuryAppropriationAccount', federal_account=fed_acct2) | ||
|
||
# Create file A models | ||
mommy.make('accounts.AppropriationAccountBalances', treasury_account_identifier=tas1, | ||
reporting_period_start='1699-10-01', reporting_period_end='1699-12-31') | ||
mommy.make('accounts.AppropriationAccountBalances', treasury_account_identifier=tas2, | ||
reporting_period_start='1699-10-01', reporting_period_end='1699-12-31') | ||
|
||
queryset = account_download_filter('account_balances', AppropriationAccountBalances, { | ||
'federal_account': fed_acct1.id, | ||
'fy': 1700, | ||
'quarter': 1 | ||
}) | ||
assert queryset.count() == 1 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_cgac_agency_filter(): | ||
""" Ensure the CGAC agency filter is working """ | ||
# Create TAS models | ||
tas1 = mommy.make('accounts.TreasuryAppropriationAccount', agency_id='NOT') | ||
tas2 = mommy.make('accounts.TreasuryAppropriationAccount', agency_id='CGC') | ||
|
||
# Create file B models | ||
mommy.make('financial_activities.FinancialAccountsByProgramActivityObjectClass', | ||
treasury_account_id=tas1.treasury_account_identifier, reporting_period_start='1699-10-01', | ||
reporting_period_end='1699-12-31') | ||
mommy.make('financial_activities.FinancialAccountsByProgramActivityObjectClass', | ||
treasury_account_id=tas2.treasury_account_identifier, reporting_period_start='1699-10-01', | ||
reporting_period_end='1699-12-31') | ||
|
||
# Create ToptierAgency models | ||
mommy.make('references.ToptierAgency', toptier_agency_id=-9999, cgac_code='CGC') | ||
mommy.make('references.ToptierAgency', toptier_agency_id=-9998, cgac_code='NOT') | ||
|
||
# Filter by ToptierAgency (CGAC) | ||
queryset = account_download_filter('program_activity_object_class', FinancialAccountsByProgramActivityObjectClass, { | ||
'agency': '-9999', | ||
'fy': 1700, | ||
'quarter': 1 | ||
}) | ||
assert queryset.count() == 1 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_frec_agency_filter(): | ||
""" Ensure the FREC agency filter is working """ | ||
# Create TAS models | ||
tas1 = mommy.make('accounts.TreasuryAppropriationAccount', agency_id='CGC', fr_entity_code='FAKE') | ||
tas2 = mommy.make('accounts.TreasuryAppropriationAccount', agency_id='CGC', fr_entity_code='FREC') | ||
|
||
# Create file C models | ||
mommy.make('awards.FinancialAccountsByAwards', treasury_account_id=tas1.treasury_account_identifier, | ||
reporting_period_start='1699-10-01', reporting_period_end='1699-12-31') | ||
mommy.make('awards.FinancialAccountsByAwards', treasury_account_id=tas2.treasury_account_identifier, | ||
reporting_period_start='1699-10-01', reporting_period_end='1699-12-31') | ||
|
||
# Create ToptierAgency models | ||
mommy.make('references.ToptierAgency', toptier_agency_id=-9999, cgac_code='FREC') | ||
mommy.make('references.ToptierAgency', toptier_agency_id=-9998, cgac_code='FAKE') | ||
mommy.make('references.ToptierAgency', toptier_agency_id=-9997, cgac_code='CGC') | ||
|
||
# Filter by ToptierAgency (FREC) | ||
queryset = account_download_filter('award_financial', FinancialAccountsByAwards, { | ||
'agency': '-9999', | ||
'fy': 1700, | ||
'quarter': 1 | ||
}) | ||
assert queryset.count() == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
usaspending_api/awards/migrations/0033_add_indexes_to_financial_accounts_by_awards.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.4 on 2018-07-17 16:16 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('awards', '0032_auto_20180614_1929'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunSQL(sql="CREATE INDEX faba_fain_and_uri ON financial_accounts_by_awards(fain, uri, award_id) " | ||
"WHERE fain IS NOT NULL AND uri IS NOT NULL AND award_id IS NULL;", | ||
reverse_sql="DROP INDEX faba_fain_and_uri;"), | ||
migrations.RunSQL(sql="CREATE INDEX faba_fain ON financial_accounts_by_awards(fain, uri, award_id) WHERE fain " | ||
"IS NOT NULL AND uri IS NULL AND award_id IS NULL;", | ||
reverse_sql="DROP INDEX faba_fain;"), | ||
migrations.RunSQL(sql="CREATE INDEX faba_uri ON financial_accounts_by_awards(fain, uri, award_id) WHERE fain " | ||
"IS NULL AND uri IS NOT NULL AND award_id IS NULL;", | ||
reverse_sql="DROP INDEX faba_uri;"), | ||
migrations.RunSQL(sql="CREATE INDEX faba_piid ON financial_accounts_by_awards(piid, award_id) WHERE piid IS " | ||
"NOT NULL AND award_id IS NULL;", | ||
reverse_sql="DROP INDEX faba_piid;"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
usaspending_api/broker/management/sql/load_historic_duns.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
DROP TABLE IF EXISTS historic_parent_duns_new; | ||
|
||
CREATE TABLE historic_parent_duns_new AS ( | ||
SELECT | ||
broker_historic_duns.awardee_or_recipient_uniqu AS awardee_or_recipient_uniqu, | ||
broker_historic_duns.legal_business_name AS legal_business_name, | ||
broker_historic_duns.ultimate_parent_unique_ide AS ultimate_parent_unique_ide, | ||
broker_historic_duns.ultimate_parent_legal_enti AS ultimate_parent_legal_enti, | ||
broker_historic_duns.duns_id AS broker_historic_duns_id, | ||
broker_historic_duns.year AS year | ||
FROM | ||
dblink ('broker_server', '( | ||
SELECT | ||
hduns.awardee_or_recipient_uniqu, | ||
hduns.legal_business_name, | ||
hduns.ultimate_parent_unique_ide, | ||
hduns.ultimate_parent_legal_enti, | ||
hduns.duns_id, | ||
hduns.year | ||
FROM | ||
historic_parent_duns as hduns)') AS broker_historic_duns | ||
( | ||
awardee_or_recipient_uniqu text, | ||
legal_business_name text, | ||
ultimate_parent_unique_ide text, | ||
ultimate_parent_legal_enti text, | ||
duns_id text, | ||
year int | ||
) | ||
); | ||
CREATE INDEX historic_parent_duns_awardee_idx_new ON historic_parent_duns_new USING btree (awardee_or_recipient_uniqu); | ||
CREATE INDEX historic_parent_duns_year_idx_new ON historic_parent_duns_new USING btree (year); | ||
|
||
BEGIN; | ||
DROP TABLE IF EXISTS historic_parent_duns CASCADE; | ||
ALTER TABLE historic_parent_duns_new RENAME TO historic_parent_duns; | ||
ALTER INDEX historic_parent_duns_awardee_idx_new RENAME TO historic_parent_duns_awardee_idx; | ||
ALTER INDEX historic_parent_duns_year_idx_new RENAME TO historic_parent_duns_year_idx; | ||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from datetime import datetime | ||
import logging | ||
|
||
from django.db import connection | ||
|
||
from usaspending_api.common.exceptions import InvalidParameterException | ||
from usaspending_api.common.helpers.sql_helpers import read_sql_file | ||
|
||
|
||
logger = logging.getLogger('console') | ||
ETL_SQL_FILE_PATH = 'usaspending_api/etl/management/sql/' | ||
|
||
|
||
def get_unlinked_count(file_name): | ||
|
||
file_path = ETL_SQL_FILE_PATH + file_name | ||
sql_commands = read_sql_file(file_path=file_path) | ||
|
||
if len(sql_commands) != 1: | ||
raise InvalidParameterException('Invalid number of commands in specified file. File should contain 1 SQL ' | ||
'command to get the count of unlinked records.') | ||
|
||
with connection.cursor() as cursor: | ||
cursor.execute(sql_commands[0]) | ||
result = cursor.fetchall()[0][0] | ||
|
||
return int(result) | ||
|
||
|
||
def update_c_to_d_linkages(type): | ||
logger.info('Starting File C to D linkage updates for %s records' % type) | ||
|
||
if type.lower() == 'contract': | ||
file_names = ['update_file_c_linkages_piid.sql'] | ||
unlinked_count_file_name = 'check_contract_file_c_linkages.sql' | ||
elif type.lower() == 'assistance': | ||
file_names = ['update_file_c_linkages_fain.sql', 'update_file_c_linkages_uri.sql', | ||
'update_file_c_linkages_fain_and_uri.sql'] | ||
unlinked_count_file_name = 'check_assistance_file_c_linkages.sql' | ||
else: | ||
raise InvalidParameterException('Invalid type provided to process C to D linkages.') | ||
|
||
file_paths = [ETL_SQL_FILE_PATH + file_name for file_name in file_names] | ||
|
||
starting_unlinked_count = get_unlinked_count(file_name=unlinked_count_file_name) | ||
logger.info('Current count of unlinked %s records: %s' % (type, str(starting_unlinked_count))) | ||
|
||
total_start = datetime.now() | ||
for file_name in file_paths: | ||
start = datetime.now() | ||
logger.info('Running %s' % file_name) | ||
sql_commands = read_sql_file(file_path=file_name) | ||
for command in sql_commands: | ||
with connection.cursor() as cursor: | ||
cursor.execute(command) | ||
logger.info('Finished %s in %s seconds' % (file_name, str(datetime.now() - start))) | ||
|
||
ending_unlinked_count = get_unlinked_count(file_name=unlinked_count_file_name) | ||
logger.info('Count of unlinked %s records after updates: %s' % (type, str(ending_unlinked_count))) | ||
|
||
logger.info('Finished all queries in %s seconds' % str(datetime.now() - total_start)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import os | ||
import logging | ||
|
||
from usaspending_api.common.exceptions import InvalidParameterException | ||
|
||
|
||
logger = logging.getLogger('console') | ||
|
||
|
||
def read_sql_file(file_path): | ||
# Read in SQL file and extract commands into a list | ||
_, file_extension = os.path.splitext(file_path) | ||
|
||
if file_extension != '.sql': | ||
raise InvalidParameterException("Invalid file provided. A file with extension '.sql' is required.") | ||
|
||
# Open and read the file as a single buffer | ||
fd = open(file_path, 'r') | ||
sql_file = fd.read() | ||
fd.close() | ||
|
||
# all SQL commands (split on ';') and trimmed for whitespaces | ||
return [command.strip() for command in sql_file.split(';') if command] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.