"""Tests for report run model.""" from datetime import datetime, timezone from decimal import Decimal from uuid import UUID import pytest from collaborator.models.rds import report_run_persister from tests.testutils import db, report_fixtures @db.test_schema_default_seed @pytest.mark.parametrize( [ "collaborators", "period_ids", "period_name", "report_run_name", "migrated_to_abacus", "requestor_identity_uuid", "notification_email", "number_format", "currency", ], [ ( [ { "id": 1, "name": "Test", "performance_rights": 1, "vendor_id": 24601, "dp_enabled_date": None, }, { "id": 2, "name": "Person", "performance_rights": 1, "vendor_id": 24601, "dp_enabled_date": None, }, ], "4,5,6", "Q2 2005", "My great report run", False, "a9ead030-209d-48c1-929e-5f4da58e5f2e", "myguy@sonymusic-pde.com", "us", "USD", ) ], ) def test_create_report_run( mocker, collaborators, period_ids, period_name, report_run_name, migrated_to_abacus, requestor_identity_uuid, notification_email, number_format, currency, ): """Test creating reports.""" request_datetime = datetime(2019, 10, 18, 15, 50, 12, tzinfo=timezone.utc) mock_datetime = mocker.patch.object(report_run_persister, "datetime") mock_datetime.now = mocker.MagicMock(return_value=request_datetime) mock_uuid = mocker.patch.object(report_run_persister, "uuid") mock_uuid.uuid4 = mocker.MagicMock( return_value=UUID("969cf84b-14c7-48bb-940a-a5a2fa1ee101") ) mock_datetime = mocker.patch.object(report_run_persister, "datetime") mock_datetime.now = mocker.MagicMock(return_value=request_datetime) created_report_run, created_reports = ( report_run_persister.ReportRunPersister.create_report_run( name=report_run_name, collaborators=collaborators, period_ids=period_ids, period_name=period_name, migrated_to_abacus=migrated_to_abacus, requestor_identity_uuid=requestor_identity_uuid, notification_email=notification_email, number_format=number_format, currency=currency, ) ) assert created_report_run.to_dict() == report_fixtures.created_report_run.to_dict() assert [report.to_dict() for report in created_reports] == [ report.to_dict() for report in report_fixtures.created_reports ] @db.test_schema_default_seed def test_get_report_runs_by_ids(): """Test getting report runs by IDs.""" report_run_ids = [1, 2] report_runs = report_run_persister.ReportRunPersister.get_report_runs_by_ids( report_run_ids=report_run_ids ) assert [report_run.to_dict() for report_run in report_runs] == [ { "id": 1, "report_run_uuid": "abcd-1234", "report_run_name": "Test Run", "period_ids": "1,2,3", "period_name": "Sometime", "file_format": "xls", "requested_datetime": "2019-09-08T07:06:05", "source": "LEGACY", "requestor_identity_uuid": None, "notification_email": None, "number_format": None, "trigger_type": "AUTO", }, { "id": 2, "report_run_uuid": "1234-abcd", "report_run_name": "Test Run 2", "period_ids": "1,2,3", "period_name": "Sometime", "file_format": "xls", "requested_datetime": "2019-10-09T08:07:06", "source": "LEGACY", "requestor_identity_uuid": None, "notification_email": None, "number_format": None, "trigger_type": "MANUAL", }, ] @db.test_schema_default_seed @pytest.mark.parametrize( "params, expected", [ [ {"account_id": 24601, "report_run_id": None}, [ { "vendor_id": 24601, "report_run_id": 2, "amount": None, "currency": None, "total_count": 1, "error_count": 0, "requested_count": 1, "generated_count": 0, "transaction_count": 0, }, { "vendor_id": 24601, "report_run_id": 1, "amount": Decimal("1234.000000"), "currency": "NOK", "total_count": 1, "error_count": 0, "requested_count": 0, "generated_count": 1, "transaction_count": 1, }, ], ], [ {"account_id": None, "report_run_id": 1}, [ { "vendor_id": 120594, "report_run_id": 1, "amount": Decimal("3702.000000"), "currency": "NOK", "total_count": 3, "error_count": 0, "requested_count": 0, "generated_count": 3, "transaction_count": 1, }, { "vendor_id": 24601, "report_run_id": 1, "amount": Decimal("1234.000000"), "currency": "NOK", "total_count": 1, "error_count": 0, "requested_count": 0, "generated_count": 1, "transaction_count": 1, }, ], ], ], ) def test_get_report_run_participations( params, expected, ): """Test getting reports run participations.""" report_runs, total_count = ( report_run_persister.ReportRunPersister.get_report_run_participations( **params, sort_direction=None, sort_key=None, limit=None, offset=None, ) ) assert total_count == len(expected) assert [report_run._mapping for report_run in report_runs] == expected