Repository: adbar/simplemma

Issue:
Tests: remove pickled data before exiting
Dictionary pickling currently generates a superfluous `zz.plzma` file which is not removed after running the tests.

Discussion:

Solution patch:
diff --git a/training/dictionary_pickler.py b/training/dictionary_pickler.py
index a1eed9f..1e3a22d 100644
--- a/training/dictionary_pickler.py
+++ b/training/dictionary_pickler.py
@@ -133,6 +133,16 @@ def _load_dict(
     return _read_dict(filepath, langcode, silent)
 
 
+def _determine_pickle_path(langcode: str = "en", in_place: bool = False) -> str:
+    filename = f"strategies/dictionaries/data/{langcode}.plzma"
+    directory = (
+        Path(simplemma.__file__).parent
+        if in_place
+        else Path(__file__).parent.parent / "simplemma"
+    )
+    return str(directory / filename)
+
+
 def _pickle_dict(
     langcode: str = "en",
     listpath: str = "lists",
@@ -144,13 +154,7 @@ def _pickle_dict(
     if langcode not in ("lt", "sw"):
         mydict = dict(sorted(mydict.items(), key=itemgetter(1)))
     if filepath is None:
-        filename = f"strategies/dictionaries/data/{langcode}.plzma"
-        directory = (
-            Path(simplemma.__file__).parent
-            if in_place
-            else Path(__file__).parent.parent / "simplemma"
-        )
-        filepath = str(directory / filename)
+        filepath = _determine_pickle_path(langcode, in_place)
     with lzma.open(filepath, "wb") as filehandle:  # , filters=my_filters, preset=9
         pickle.dump(mydict, filehandle, protocol=4)
     LOGGER.debug("%s %s", langcode, len(mydict))


Test patch:
diff --git a/tests/test_dictionary_pickler.py b/tests/test_dictionary_pickler.py
index 333484c..c53b967 100644
--- a/tests/test_dictionary_pickler.py
+++ b/tests/test_dictionary_pickler.py
@@ -39,3 +39,7 @@ def test_logic() -> None:
     os_handle, temp_outputfile = tempfile.mkstemp(suffix=".pkl", text=True)
     dictionary_pickler._pickle_dict("zz", listpath, temp_outputfile)
     dictionary_pickler._pickle_dict("zz", listpath, in_place=True)
+
+    # remove pickle file
+    filepath = dictionary_pickler._determine_pickle_path("zz")
+    os.remove(filepath)