From 605085c95fe5ab6af337aefc723794f78cf3dfd7 Mon Sep 17 00:00:00 2001 From: Cody Logan Date: Tue, 14 Nov 2023 16:44:34 -0800 Subject: Use fixtures to create test files This reduces the number of temporary folders and files created during testing. Additionally, an actual JPEG is created for a couple tests instead of using random text for the contents. --- tests/conftest.py | 73 ++++++++++++++++++++++++++++++++++++++++------- tests/test_dl.py | 12 +++----- tests/test_logging.py | 4 +-- tests/test_parse.py | 28 +++++++----------- tests/test_validations.py | 15 ++++------ 5 files changed, 86 insertions(+), 46 deletions(-) (limited to 'tests') diff --git a/tests/conftest.py b/tests/conftest.py index 8b1fea9..3eb99cd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -25,15 +25,68 @@ import requests_mock as rm from wikiget.file import File +# 2x2 JPEG +TEST_FILE_BYTES = ( + b"\xff\xd8\xff\xdb\x00C\x00\x03\x02\x02\x02\x02\x02\x03\x02\x02\x02\x03\x03\x03\x03" + b"\x04\x06\x04\x04\x04\x04\x04\x08\x06\x06\x05\x06\t\x08\n\n\t\x08\t\t\n\x0c\x0f" + b"\x0c\n\x0b\x0e\x0b\t\t\r\x11\r\x0e\x0f\x10\x10\x11\x10\n\x0c\x12\x13\x12\x10\x13" + b"\x0f\x10\x10\x10\xff\xc0\x00\x0b\x08\x00\x02\x00\x02\x01\x01\x11\x00\xff\xc4\x00" + b"\x14\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\t\xff" + b"\xc4\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + b"\x00\xff\xda\x00\x08\x01\x01\x00\x00?\x00T\xdf\xff\xd9" +) + + +@pytest.fixture(autouse=True, scope="session") +def _chdir_to_tmp_dir(tmp_path_factory: pytest.TempPathFactory) -> None: + """Change to the base temporary directory before running tests. + + :param tmp_path_factory: temporary path generator + :type tmp_path_factory: pytest.TempPathFactory + """ + chdir(tmp_path_factory.getbasetemp()) + + +@pytest.fixture(scope="session") +def batch_file(tmp_path_factory: pytest.TempPathFactory) -> Path: + """Create a temporary batch file for testing. + + :param tmp_path_factory: temporary path generator + :type tmp_path_factory: pytest.TempPathFactory + :return: test batch file + :rtype: pathlib.Path + """ + tmp_file = tmp_path_factory.getbasetemp() / "batch.txt" + tmp_file.write_text("File:Foo.jpg\nFile:Bar.jpg\nFile:Baz.jpg\n") + return tmp_file + + +@pytest.fixture(scope="session") +def batch_file_with_comment(tmp_path_factory: pytest.TempPathFactory) -> Path: + """Create a temporary batch file with comments for testing. + + :param tmp_path_factory: temporary path generator + :type tmp_path_factory: pytest.TempPathFactory + :return: test batch file + :rtype: pathlib.Path + """ + tmp_file = tmp_path_factory.getbasetemp() / "batch_with_comment.txt" + tmp_file.write_text("File:Foo.jpg\n\n#File:Bar.jpg\nFile:Baz.jpg\n") + return tmp_file + -@pytest.fixture(autouse=True) -def _chdir_to_tmp_dir(tmp_path: Path) -> None: - """Change to a temporary directory before running tests. +@pytest.fixture(scope="session") +def test_file(tmp_path_factory: pytest.TempPathFactory) -> Path: + """Create a fake downloaded file with known contents. - :param tmp_path: temporary path object - :type tmp_path: Path + :param tmp_path_factory: temporary path generator + :type tmp_path_factory: pytest.TempPathFactory + :return: test file + :rtype: pathlib.Path """ - chdir(tmp_path) + tmp_file = tmp_path_factory.getbasetemp() / "Testfile.jpg" + tmp_file.write_bytes(TEST_FILE_BYTES) + return tmp_file @pytest.fixture() @@ -44,7 +97,7 @@ def file_with_name() -> File: the same value and its site property to the program's default site. :return: File object created using a filename - :rtype: File + :rtype: wikiget.file.File """ return File("foobar.jpg") @@ -54,7 +107,7 @@ def file_with_name_and_dest() -> File: """Create a test File with a name and destination. :return: File object created with name and dest - :rtype: File + :rtype: wikiget.file.File """ return File(name="foobar.jpg", dest="bazqux.jpg") @@ -64,9 +117,9 @@ def _mock_get(requests_mock: rm.Mocker) -> None: """Fake the download request for the true URL of File:Example.jpg. :param requests_mock: a requests_mock Mocker object - :type requests_mock: rm.Mocker + :type requests_mock: requests_mock.Mocker """ requests_mock.get( "https://upload.wikimedia.org/wikipedia/commons/a/a9/Example.jpg", - text="data", + content=TEST_FILE_BYTES, ) diff --git a/tests/test_dl.py b/tests/test_dl.py index d7d5d77..cb5f0b6 100644 --- a/tests/test_dl.py +++ b/tests/test_dl.py @@ -54,15 +54,13 @@ class TestPrepDownload: assert file == expected_file - def test_prep_download_with_existing_file(self, tmp_path: Path) -> None: + def test_prep_download_with_existing_file(self, test_file: Path) -> None: """Test that an exception is raised when the download file already exists. Attempting to download a file with the same destination name as an existing file should raise a FileExistsError. """ - tmp_file = tmp_path / "File:Example.jpg" - tmp_file.write_text("nothing") - args = parse_args(["File:Example.jpg", "-o", str(tmp_file)]) + args = parse_args(["File:Example.jpg", "-o", str(test_file)]) with pytest.raises(FileExistsError): _ = prep_download(args.FILE, args) @@ -301,15 +299,13 @@ class TestDownload: """Define tests related to wikiget.dl.download.""" @pytest.fixture() - def mock_file(self, tmp_path: Path) -> File: + def mock_file(self) -> File: """Create a mock File object to test against. - :param tmp_path: temporary path object - :type tmp_path: Path :return: mock File object :rtype: File """ - file = File(name="Example.jpg", dest=str(tmp_path / "Example.jpg")) + file = File(name="Example.jpg") file.image = Mock() file.image.exists = True file.image.imageinfo = { diff --git a/tests/test_logging.py b/tests/test_logging.py index 2fd95cd..8d58cdf 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -39,9 +39,9 @@ class TestLogging: adapter.warning("test log") assert "[Example.jpg] test log" in caplog.text - def test_file_logging(self, tmp_path: Path) -> None: + def test_file_logging(self) -> None: """Logging to a file should create the file in the specified location.""" - logfile_location = tmp_path / "test.log" + logfile_location = Path("test.log") args = parse_args(["File:Example.jpg", "-l", str(logfile_location)]) configure_logging(args.verbose, args.logfile, quiet=args.quiet) assert logfile_location.is_file() diff --git a/tests/test_parse.py b/tests/test_parse.py index fbbd1b7..4f74ac9 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -126,20 +126,18 @@ class TestReadBatchFile: """Define tests related to wikiget.parse.read_batch_file.""" @pytest.fixture() - def dl_dict(self, tmp_path: Path) -> dict[int, str]: + def dl_dict(self, batch_file: Path) -> dict[int, str]: """Create and process a test batch file with three lines. - :param tmp_path: temporary path object - :type tmp_path: Path + :param batch_file: test batch file + :type batch_file: Path :return: dictionary representation of the input file :rtype: dict[int, str] """ - tmp_file = tmp_path / "batch.txt" - tmp_file.write_text("File:Foo.jpg\nFile:Bar.jpg\nFile:Baz.jpg\n") - return read_batch_file(str(tmp_file)) + return read_batch_file(str(batch_file)) def test_batch_file_log( - self, caplog: pytest.LogCaptureFixture, tmp_path: Path + self, caplog: pytest.LogCaptureFixture, batch_file: Path ) -> None: """Test that reading a batch file creates an info log message. @@ -147,10 +145,8 @@ class TestReadBatchFile: of the batch file. """ caplog.set_level(logging.INFO) - tmp_file = tmp_path / "batch.txt" - tmp_file.write_text("File:Foo.jpg\n") - _ = read_batch_file(str(tmp_file)) - assert f"Using file '{tmp_file}' for batch download" in caplog.text + _ = read_batch_file(str(batch_file)) + assert f"Using file '{batch_file}' for batch download" in caplog.text def test_batch_file_length(self, dl_dict: dict[int, str]) -> None: """Test that the batch dict has the same number of lines as the batch file.""" @@ -205,19 +201,17 @@ class TestReadBatchFile: assert dl_dict_stdin == expected_list @pytest.fixture() - def dl_dict_with_comment(self, tmp_path: Path) -> dict[int, str]: + def dl_dict_with_comment(self, batch_file_with_comment: Path) -> dict[int, str]: """Create and process a test batch file with four lines. In addition to filenames, one line is commented out and another line is blank. - :param tmp_path: temporary path object - :type tmp_path: Path + :param batch_file_with_comment: test batch file + :type batch_file_with_comment: Path :return: dictionary representation of the input file :rtype: dict[int, str] """ - tmp_file = tmp_path / "batch.txt" - tmp_file.write_text("File:Foo.jpg\n\n#File:Bar.jpg\nFile:Baz.jpg\n") - return read_batch_file(str(tmp_file)) + return read_batch_file(str(batch_file_with_comment)) def test_batch_file_with_comment_length( self, dl_dict_with_comment: dict[int, str] diff --git a/tests/test_validations.py b/tests/test_validations.py index 161d102..aa4fdd0 100644 --- a/tests/test_validations.py +++ b/tests/test_validations.py @@ -163,14 +163,11 @@ class TestFileInput: class TestVerifyHash: """Define tests related to wikiget.validations.verify_hash.""" - def test_verify_hash(self, tmp_path: Path) -> None: - """Confirm that verify_hash returns the proper SHA1 hash.""" - file_name = "testfile" - file_contents = "foobar" - file_sha1 = "8843d7f92416211de9ebb963ff4ce28125932878" + def test_verify_hash(self, test_file: Path) -> None: + """Confirm that verify_hash returns the proper SHA1 hash. - # create a temporary file with known contents - tmp_file = tmp_path / file_name - tmp_file.write_text(file_contents) + The test file used here is generated by a fixture in conftest.py. + """ + expected_sha1 = "cd19c009a30ca9b68045415a3a0838e64f3c2443" - assert verify_hash(str(tmp_file)) == file_sha1 + assert verify_hash(str(test_file)) == expected_sha1 -- cgit v1.2.3