import os from io import BytesIO from unittest.mock import Mock, create_autospec import pytest from src.backend.logic.reporting.report import io class TestSaveToDisk: """Test save_to_disk function.""" @pytest.fixture def run_save_pdf(self, save_to_disk): out_path = "" def _(output_path): nonlocal out_path out_path = output_path return save_to_disk(as_pdf=True, output_path=output_path) yield _ if out_path and os.path.exists(out_path): os.remove(out_path) @pytest.fixture def save_to_disk(self): def _(**kwargs): return io.save_to_disk( **( dict( as_pdf=False, buffer=BytesIO(), output_path="", source=Mock(spec_set=io.presentation.Presentation), ) | kwargs ) ) return _ @pytest.mark.parametrize( "output_path,exception_expected", [ (None, True), ("", True), ("test", True), ("test.pptx", True), ("test.pdf", False), ("test.PDf", False), ], ) def test_save_to_disk_as_pdf(self, output_path, exception_expected, run_save_pdf): """Test _save_to_disk function.""" if exception_expected: with pytest.raises(ValueError): run_save_pdf(output_path) else: run_save_pdf(output_path) assert os.path.exists(output_path) @pytest.mark.parametrize( "output_path,exception_expected", [ (None, True), ("", True), ("test", True), ("test.pptx", False), ("test.PPTx", False), ("test.pdf", True), ], ) def test_save_to_disk_as_pptx(self, save_to_disk, output_path, exception_expected): """Test _save_to_disk function.""" mock_source = create_autospec(io.presentation.Presentation) def func(): return save_to_disk( output_path=output_path, source=mock_source, ) if exception_expected: with pytest.raises(ValueError): func() else: func() assert mock_source.save.called