Skip to content

Commit

Permalink
improved Path usage in test cases #51
Browse files Browse the repository at this point in the history
  • Loading branch information
bensteUEM committed May 7, 2024
1 parent 03694d8 commit e687d67
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
15 changes: 8 additions & 7 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,25 +276,26 @@ def test_download_missing_online_songs(self) -> None:
"""
# 1. prepare
songs_temp = []
dirname = "testData/"
dirprefix = "TEST"
test_dir = Path("testData/")

sample1_id = 762

sample2_id = 1113
sample2_name = "002 Er ist die rechte Freudensonn.sng"
test2path = dirname + "/EG Lieder/" + sample2_name
test2path = test_dir / "EG Lieder" / sample2_name

Path.unlink(test2path, missing_ok=True)
Path(test2path).unlink(missing_ok=True)

# 2. read all songs from known folders in testData
for key, value in SNG_DEFAULTS.KnownFolderWithPrefix.items():
dirname = "./testData/" + key
if not Path(dirname).exists():
folder = test_dir / key
if not folder.exists():
continue
dirprefix = value
songs_temp.extend(
parse_sng_from_directory(directory=dirname, songbook_prefix=dirprefix)
parse_sng_from_directory(
directory=str(folder), songbook_prefix=dirprefix
)
)

df_sng_test = pd.DataFrame(songs_temp, columns=["SngFile"])
Expand Down
26 changes: 13 additions & 13 deletions tests/test_sng_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,28 @@ def test_isoutf8(self) -> None:
4. Parses an utf-8 file without BOM
5. Parsing iso file writes utf8 and checks if output file has BOM
"""
path = "testData/ISO-UTF8/"
iso_file_path = path + "Herr du wollest uns bereiten_iso.sng"
path = Path("testData/ISO-UTF8/")
iso_file_path = path / "Herr du wollest uns bereiten_iso.sng"
iso2utf_file_name = "Herr du wollest uns bereiten_iso2utf.sng"
iso2utf_file_path = path + iso2utf_file_name
utf_file_path = path + "Herr du wollest uns bereiten_ct_utf8.sng"
no_bom_utf_file_path = path + "Herr du wollest uns bereiten_noBOM_utf8.sng"
iso2utf_file_path = path / iso2utf_file_name
utf_file_path = path / "Herr du wollest uns bereiten_ct_utf8.sng"
no_bom_utf_file_path = path / "Herr du wollest uns bereiten_noBOM_utf8.sng"

# Part 1
with Path(iso_file_path).open(encoding="iso-8859-1") as file_iso_as_iso:
with iso_file_path.open(encoding="iso-8859-1") as file_iso_as_iso:
text = file_iso_as_iso.read()
self.assertEqual("#", text[0], "ISO file read with correct ISO encoding")

with Path(iso_file_path).open(
encoding="utf-8"
) as file_iso_as_utf, self.assertRaises(UnicodeDecodeError) as cm:
with iso_file_path.open(encoding="utf-8") as file_iso_as_utf, self.assertRaises(
UnicodeDecodeError
) as cm:
text = file_iso_as_utf.read()

with Path(utf_file_path).open(encoding="iso-8859-1") as file_utf_as_iso:
with utf_file_path.open(encoding="iso-8859-1") as file_utf_as_iso:
text = file_utf_as_iso.read()
self.assertEqual("", text[0:3], "UTF8 file read with wrong encoding")

with Path(utf_file_path).open(encoding="utf-8") as file_utf_as_utf:
with utf_file_path.open(encoding="utf-8") as file_utf_as_utf:
text = file_utf_as_utf.read()
self.assertEqual(
"\ufeff", text[0], "UTF8 file read with correct encoding including BOM"
Expand Down Expand Up @@ -162,12 +162,12 @@ def test_isoutf8(self) -> None:
sng.filename = iso2utf_file_name
sng.write_file()

with Path(iso2utf_file_path).open(encoding="utf-8") as file_iso2utf:
with iso2utf_file_path.open(encoding="utf-8") as file_iso2utf:
text = file_iso2utf.read()
self.assertEqual(
"\ufeff", text[0], "UTF8 file read with correct encoding including BOM"
)
Path.unlink(iso2utf_file_path)
iso2utf_file_path.unlink()


if __name__ == "__main__":
Expand Down

0 comments on commit e687d67

Please sign in to comment.