diff options
| author | Cody Logan <cody@lokken.dev> | 2023-10-23 12:08:08 -0700 |
|---|---|---|
| committer | Cody Logan <cody@lokken.dev> | 2023-10-23 12:32:05 -0700 |
| commit | daf4a17b53ed59ffa4b130ce9bedd4f42ef1bfdb (patch) | |
| tree | 7163641d5f71ba9e7a5d8d905cfc742c0f804fe1 | |
| parent | 775fb9e663d1704302cbc2329e9538c0c4353bf5 (diff) | |
| download | wikiget-daf4a17b53ed59ffa4b130ce9bedd4f42ef1bfdb.tar.gz wikiget-daf4a17b53ed59ffa4b130ce9bedd4f42ef1bfdb.zip | |
Reorganize tests using fixtures
| -rw-r--r-- | tests/test_file_class.py | 37 | ||||
| -rw-r--r-- | tests/test_parse.py | 53 | ||||
| -rw-r--r-- | tests/test_validations.py | 191 |
3 files changed, 172 insertions, 109 deletions
diff --git a/tests/test_file_class.py b/tests/test_file_class.py index dd30207..1b76566 100644 --- a/tests/test_file_class.py +++ b/tests/test_file_class.py @@ -15,23 +15,36 @@ # You should have received a copy of the GNU General Public License # along with Wikiget. If not, see <https://www.gnu.org/licenses/>. +import pytest + from wikiget import DEFAULT_SITE from wikiget.file import File -def test_file_with_name_only(): - file = File("foobar.jpg") - assert file.name == "foobar.jpg" - assert file.dest == file.name - assert file.site == DEFAULT_SITE +class TestFileClass: + @pytest.fixture(scope="class") + def file_with_name(self) -> File: + return File("foobar.jpg") + + def test_file_with_name(self, file_with_name: File) -> None: + assert file_with_name.name == "foobar.jpg" + + def test_file_with_name_dest(self, file_with_name: File) -> None: + assert file_with_name.dest == file_with_name.name + + def test_file_with_name_site(self, file_with_name: File) -> None: + assert file_with_name.site == DEFAULT_SITE + @pytest.fixture(scope="class") + def file_with_name_and_dest(self) -> File: + return File("foobar.jpg", dest="bazqux.jpg") -def test_file_with_name_and_dest(): - file = File("foobar.jpg", dest="bazqux.jpg") - assert file.dest == "bazqux.jpg" - assert file.dest != file.name + def test_file_with_name_and_dest(self, file_with_name_and_dest: File) -> None: + assert file_with_name_and_dest.dest == "bazqux.jpg" + def test_name_and_dest_are_different(self, file_with_name_and_dest: File) -> None: + assert file_with_name_and_dest.dest != file_with_name_and_dest.name -def test_file_with_name_and_site(): - file = File("foobar.jpg", site="en.wikipedia.org") - assert file.site == "en.wikipedia.org" + def test_file_with_name_and_site(self) -> None: + file = File("foobar.jpg", site="en.wikipedia.org") + assert file.site == "en.wikipedia.org" diff --git a/tests/test_parse.py b/tests/test_parse.py index 757b361..a04b30f 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -18,38 +18,51 @@ import pytest from wikiget.exceptions import ParseError +from wikiget.file import File from wikiget.parse import get_dest from wikiget.wikiget import construct_parser class TestGetDest: - parser = construct_parser() + @pytest.fixture(scope="class") + def file_with_filename(self) -> File: + args = construct_parser().parse_args(["File:Example.jpg"]) + return get_dest(args.FILE, args) - def test_get_dest_with_filename(self): - args = self.parser.parse_args(["File:Example.jpg"]) - file = get_dest(args.FILE, args) - assert file.name == "Example.jpg" - assert file.dest == "Example.jpg" - assert file.site == "commons.wikimedia.org" + def test_get_dest_name_with_filename(self, file_with_filename: File) -> None: + assert file_with_filename.name == "Example.jpg" - def test_get_dest_with_url(self): - args = self.parser.parse_args( - [ - "https://en.wikipedia.org/wiki/File:Example.jpg", - ] + def test_get_dest_with_filename(self, file_with_filename: File) -> None: + assert file_with_filename.dest == "Example.jpg" + + def test_get_dest_site_with_filename(self, file_with_filename: File) -> None: + assert file_with_filename.site == "commons.wikimedia.org" + + @pytest.fixture(scope="class") + def file_with_url(self) -> File: + args = construct_parser().parse_args( + ["https://en.wikipedia.org/wiki/File:Example.jpg"] ) - file = get_dest(args.FILE, args) - assert file.name == "Example.jpg" - assert file.dest == "Example.jpg" - assert file.site == "en.wikipedia.org" + return get_dest(args.FILE, args) + + def test_get_dest_name_with_url(self, file_with_url: File) -> None: + assert file_with_url.name == "Example.jpg" + + def test_get_dest_with_url(self, file_with_url: File) -> None: + assert file_with_url.dest == "Example.jpg" + + def test_get_dest_site_with_url(self, file_with_url: File) -> None: + assert file_with_url.site == "en.wikipedia.org" - def test_get_dest_with_bad_filename(self): - args = self.parser.parse_args(["Example.jpg"]) + def test_get_dest_with_bad_filename(self) -> None: + args = construct_parser().parse_args(["Example.jpg"]) with pytest.raises(ParseError): _ = get_dest(args.FILE, args) - def test_get_dest_with_different_site(self, caplog: pytest.LogCaptureFixture): - args = self.parser.parse_args( + def test_get_dest_with_different_site( + self, caplog: pytest.LogCaptureFixture + ) -> None: + args = construct_parser().parse_args( [ "https://commons.wikimedia.org/wiki/File:Example.jpg", "--site", diff --git a/tests/test_validations.py b/tests/test_validations.py index 9d70f6e..30e59a3 100644 --- a/tests/test_validations.py +++ b/tests/test_validations.py @@ -15,88 +15,125 @@ # You should have received a copy of the GNU General Public License # along with Wikiget. If not, see <https://www.gnu.org/licenses/>. +from pathlib import Path +from re import Match +from typing import Optional + +import pytest + from wikiget.validations import valid_file, valid_site, verify_hash -def test_invalid_site_input(): - """ - Invalid site strings should not return regex match objects. - """ - invalid_input = [ - "example.com", - "vim.wikia.com", - "en.wikipedia.com", - "en.wikimpedia.org", - ] - for i in invalid_input: - site_match = valid_site(i) - assert site_match is None - - -def test_valid_site_input(): - """ - Valid site strings should return regex match objects. - """ - valid_input = [ - "en.wikipedia.org", - "commons.wikimedia.org", - "de.wikipedia.org", - "meta.wikimedia.org", - ] - for i in valid_input: - site_match = valid_site(i) - assert site_match is not None - - -def test_file_regex(): - """ - File regex should return a match object with match groups corresponding - to the file prefix and name. - """ - i = "File:Example.jpg" - file_match = valid_file(i) - assert file_match is not None - assert file_match.group(0) == "File:Example.jpg" # entire match - assert file_match.group(1) == "File:" # first group - assert file_match.group(2) == "Example.jpg" # second group - - -def test_invalid_file_input(): - """ - Invalid file strings should not return regex match objects. - """ - invalid_input = ["file:example", "example.jpg", "Foo Bar.gif", "Fil:Example.jpg"] - for i in invalid_input: - file_match = valid_file(i) - assert file_match is None - - -def test_valid_file_input(): - """ - Valid file strings should return regex match objects. - """ - valid_input = [ - "Image:example.jpg", - "file:example.jpg", - "File:example.file-01.jpg", - "FILE:FOO.BMP", - "File:ß handwritten sample.gif", - "File:A (1).jpeg", - ] - for i in valid_input: - file_match = valid_file(i) +class TestSiteInput: + @pytest.fixture( + scope="class", + params=[ + "example.com", + "vim.wikia.com", + "en.wikipedia.com", + "en.wikimpedia.org", + ], + ) + def invalid_input(self, request: pytest.FixtureRequest) -> Optional[Match]: + return valid_site(request.param) + + @pytest.fixture( + scope="class", + params=[ + "en.wikipedia.org", + "commons.wikimedia.org", + "de.wikipedia.org", + "meta.wikimedia.org", + ], + ) + def valid_input(self, request: pytest.FixtureRequest) -> Optional[Match]: + return valid_site(request.param) + + def test_invalid_site_input(self, invalid_input: None) -> None: + """ + Invalid site strings should not return regex match objects. + """ + assert invalid_input is None + + def test_valid_site_input(self, valid_input: Match) -> None: + """ + Valid site strings should return regex match objects. + """ + assert valid_input is not None + + +class TestFileRegex: + @pytest.fixture(scope="class") + def file_match(self) -> Optional[Match]: + """ + File regex should return a match object with match groups corresponding + to the file prefix and name. + """ + return valid_file("File:Example.jpg") + + def test_file_match_exists(self, file_match: Match) -> None: assert file_match is not None + def test_file_match_entire_match(self, file_match: Match) -> None: + assert file_match.group(0) == "File:Example.jpg" + + def test_file_match_first_group(self, file_match: Match) -> None: + assert file_match.group(1) == "File:" + + def test_file_match_second_group(self, file_match: Match) -> None: + assert file_match.group(2) == "Example.jpg" + + +class TestFileInput: + @pytest.fixture( + scope="class", + params=[ + "file:example", + "example.jpg", + "Foo Bar.gif", + "Fil:Example.jpg", + ], + ) + def invalid_input(self, request: pytest.FixtureRequest) -> Optional[Match]: + return valid_file(request.param) + + @pytest.fixture( + scope="class", + params=[ + "Image:example.jpg", + "file:example.jpg", + "File:example.file-01.jpg", + "FILE:FOO.BMP", + "File:ß handwritten sample.gif", + "File:A (1).jpeg", + ], + ) + def valid_input(self, request: pytest.FixtureRequest) -> Optional[Match]: + return valid_file(request.param) + + def test_invalid_file_input(self, invalid_input: None) -> None: + """ + Invalid file strings should not return regex match objects. + """ + assert invalid_input is None + + def test_valid_file_input(self, valid_input: Match) -> None: + """ + Valid file strings should return regex match objects. + """ + assert valid_input is not None + -def test_verify_hash(tmp_path): - """ - Confirm that verify_hash returns the proper SHA1 hash. - """ - file_name = "testfile" - file_contents = "foobar" - file_sha1 = "8843d7f92416211de9ebb963ff4ce28125932878" +class TestVerifyHash: + 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" - tmp_file = tmp_path / file_name - tmp_file.write_text(file_contents) + tmp_file = tmp_path / file_name + tmp_file.write_text(file_contents) - assert verify_hash(tmp_file) == file_sha1 + assert verify_hash(str(tmp_file)) == file_sha1 |
