from io import BytesIO from unittest.mock import AsyncMock import pytest import pytest_asyncio from src.backend.logic.reporting.report import main from tests_backend.mock_data import WHITE_PIXEL class TestNewReport: sample_data = main.PlaceholderDataSRAT( label_name="test_label", sr_isrcs_unique_total_count=100, sr_isrcs_unique_flagged=frozenset(["isrc1", "isrc2", "isrc3"]), sr_isrcs_unique_corrected=frozenset(["isrc1"]), sr_isrcs_unique_flagged_yt_ownership_conflict=frozenset(["isrc2"]), sr_isrcs_unique_flagged_mrr_ownership_conflict=frozenset(["isrc3"]), sr_isrcs_unique_flagged_ineligible_track=frozenset(["isrc1"]), sr_isrcs_unique_corrected_yt_ownership_updated=frozenset(["isrc1"]), sr_isrcs_unique_corrected_reference_reactivated=frozenset(["isrc2"]), sr_isrcs_unique_corrected_match_policy_updated=frozenset(["isrc3"]), sr_added_territory_rights_count=10, sr_actionable_conflict_count=10, sr_actionable_attached_conflict_count=10, srugc_avg_daily_views=1000, srugc_match_count=100, srugc_tracks_with_ugc_match_pct=0.5, at_isrcs_total_count=100, at_isrcs_flagged=("isrc1", "isrc2", "isrc3"), at_isrcs_corrected=("isrc1",), at_isrcs_flagged_yt_incorrect_channel=("isrc2",), at_isrcs_flagged_yt_ownership_conflict=("isrc2",), at_isrcs_corrected_incorrect_channel_fixed=("isrc1",), at_isrcs_corrected_yt_ownership_updated=("isrc1",), at_redelivered_count=10, at_remapped_count=10, ) sample_image_data = main.ImageData( top_artist_image=BytesIO(WHITE_PIXEL), ) @pytest_asyncio.fixture async def new_report(self): """Test new_report function.""" def _(*args, **kwargs): return main.new_report( self.sample_data, self.sample_image_data, *args, **kwargs ) return _ @pytest.mark.asyncio async def test_new_report_returns(self, new_report): """Test new_report function returned value.""" assert isinstance(await new_report(), BytesIO) @pytest.mark.asyncio @pytest.mark.parametrize("as_pdf", [True, False]) async def test_new_report_as_pdf(self, mocker, new_report, as_pdf): """Test new_report function output format.""" pdf_convert_spy = mocker.spy(main.pdf, "convert_pptx_to_pdf") report = await new_report(as_pdf=as_pdf) assert isinstance(report, BytesIO) assert pdf_convert_spy.called == as_pdf @pytest.mark.asyncio @pytest.mark.parametrize( "output_path,save_expected", [ (None, False), ("", True), ("test", True), ], ) async def test_new_report_save_to_disk( self, mocker, new_report, output_path, save_expected ): """Test new_report function save to disk.""" mocker_save_to_disk = mocker.patch.object( main, "save_to_disk", spec_set=main.save_to_disk, new_callable=AsyncMock, ) await new_report(output_path=output_path) assert mocker_save_to_disk.called == save_expected