This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
forked from HHS/simpler-grants-gov
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test for export_opportunity_data_task
- Loading branch information
1 parent
f56bd36
commit e45969b
Showing
3 changed files
with
86 additions
and
8 deletions.
There are no files selected for viewing
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
72 changes: 72 additions & 0 deletions
72
api/tests/src/task/opportunities/test_export_opportunity_data_task.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,72 @@ | ||
import csv | ||
import json | ||
|
||
import pytest | ||
|
||
import src.util.file_util as file_util | ||
from src.api.opportunities_v1.opportunity_schemas import OpportunityV1Schema | ||
from src.task.opportunities.export_opportunity_data_task import ( | ||
ExportOpportunityDataConfig, | ||
ExportOpportunityDataTask, | ||
) | ||
from tests.conftest import BaseTestClass | ||
from tests.src.db.models.factories import OpportunityFactory | ||
|
||
|
||
class TestExportOpportunityDataTask(BaseTestClass): | ||
@pytest.fixture | ||
def export_opportunity_data_task(self, db_session, mock_s3_bucket): | ||
config = ExportOpportunityDataConfig(file_path=f"s3://{mock_s3_bucket}/") | ||
return ExportOpportunityDataTask(db_session, config) | ||
|
||
def test_export_opportunity_data_task( | ||
self, | ||
db_session, | ||
truncate_opportunities, | ||
enable_factory_create, | ||
export_opportunity_data_task, | ||
): | ||
# Create 25 opportunities we will load | ||
opportunities = [] | ||
opportunities.extend(OpportunityFactory.create_batch(size=6, is_posted_summary=True)) | ||
opportunities.extend(OpportunityFactory.create_batch(size=3, is_forecasted_summary=True)) | ||
opportunities.extend(OpportunityFactory.create_batch(size=2, is_closed_summary=True)) | ||
opportunities.extend( | ||
OpportunityFactory.create_batch(size=8, is_archived_non_forecast_summary=True) | ||
) | ||
opportunities.extend( | ||
OpportunityFactory.create_batch(size=6, is_archived_forecast_summary=True) | ||
) | ||
|
||
export_opportunity_data_task.run() | ||
|
||
# Verify some metrics first | ||
# Make sure the opportunities we have created matches the number | ||
# That get exported | ||
assert ( | ||
len(opportunities) | ||
== export_opportunity_data_task.metrics[ | ||
export_opportunity_data_task.Metrics.RECORDS_EXPORTED | ||
] | ||
) | ||
|
||
# Verify csv file contents | ||
with file_util.open_stream(export_opportunity_data_task.csv_file, "r") as infile: | ||
reader = csv.DictReader(infile) | ||
assert set([opp.opportunity_id for opp in opportunities]) == set( | ||
[int(record["opportunity_id"]) for record in reader] | ||
) | ||
|
||
with file_util.open_stream(export_opportunity_data_task.json_file, "r") as infile: | ||
# Parse JSON File | ||
print("JSON OPPORTUNITIES:") | ||
json_opportunities = json.load(infile) | ||
|
||
assert set([opp.opportunity_id for opp in opportunities]) == set( | ||
[int(record["opportunity_id"]) for record in json_opportunities["opportunities"]] | ||
) | ||
|
||
schema = OpportunityV1Schema(many=True) | ||
|
||
errors = schema.validate(json_opportunities["opportunities"]) | ||
assert len(errors) == 0 |