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/test_parse.py | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) (limited to 'tests/test_parse.py') 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] -- cgit v1.2.3 From c459e3bb9553e31224348b055aaaec3a567613bb Mon Sep 17 00:00:00 2001 From: Cody Logan Date: Tue, 14 Nov 2023 16:56:15 -0800 Subject: Docstring type fixes --- tests/test_parse.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests/test_parse.py') diff --git a/tests/test_parse.py b/tests/test_parse.py index 4f74ac9..fb824d4 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -46,7 +46,7 @@ class TestGetDest: to the same value as the filename and the default site will be used. :return: a File object created using a filename - :rtype: File + :rtype: wikiget.file.File """ args = parse_args(["File:Example.jpg"]) return get_dest(args.FILE, args) @@ -77,7 +77,7 @@ class TestGetDest: filename and site parsed from the URL. :return: a File object created using a URL - :rtype: File + :rtype: wikiget.file.File """ args = parse_args(["https://en.wikipedia.org/wiki/File:Example.jpg"]) return get_dest(args.FILE, args) @@ -130,7 +130,7 @@ class TestReadBatchFile: """Create and process a test batch file with three lines. :param batch_file: test batch file - :type batch_file: Path + :type batch_file: pathlib.Path :return: dictionary representation of the input file :rtype: dict[int, str] """ @@ -207,7 +207,7 @@ class TestReadBatchFile: In addition to filenames, one line is commented out and another line is blank. :param batch_file_with_comment: test batch file - :type batch_file_with_comment: Path + :type batch_file_with_comment: pathlib.Path :return: dictionary representation of the input file :rtype: dict[int, str] """ -- cgit v1.2.3