Skip to content

Commit c908b0a

Browse files
author
Michael Pegios
committed
add further tests for test_catalog
* Tests saving and loading functionality * Edge cases testing None strings/objects
1 parent e615efb commit c908b0a

1 file changed

Lines changed: 56 additions & 1 deletion

File tree

packages/data/tests/test_catalog.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414

1515
import pyearthtools
1616
from pyearthtools.data import catalog
17+
from pyearthtools.utils.initialisation import imports
1718
from collections import namedtuple
1819
import pytest
1920
import io
21+
from types import ModuleType
2022

2123

2224
def test_get_name():
23-
25+
"""
26+
Test the test_get_name functionality
27+
"""
2428
result = catalog.get_name("testname")
2529
assert result == "testname"
2630

@@ -103,3 +107,54 @@ def mockEntry():
103107
cat.remove("TestEntryKey")
104108
with pytest.raises(KeyError):
105109
popped = cat.remove("TestEntryKey")
110+
111+
112+
def test_CatalogEntry_Nones():
113+
class mockEntry:
114+
def __init__(self, x, kwarg_1):
115+
self.item_class = None
116+
self.x = x
117+
self.kwarg_1 = kwarg_1
118+
119+
# Test item_class with "String" instance
120+
ce = catalog.CatalogEntry("None", args=["None"], name=["None"], kwargs={"my_kwarg": "None"})
121+
assert ce.item_class is None # Test item_class set to None
122+
123+
ce = catalog.CatalogEntry("pytest", args=["None"], name=["None"], kwargs={"my_kwarg": "None"})
124+
assert isinstance(ce.item_class, ModuleType) # Test item_class dynamic import work
125+
126+
ce = catalog.CatalogEntry(mockEntry, args=["None"], name=["None"], kwargs={"my_kwarg": "None"})
127+
args = ce.to_dict()["args"]
128+
kwargs = ce.to_dict()["kwargs"]
129+
assert args[0] is None # Test str to None conversion for args
130+
assert kwargs["my_kwarg"] is None # Test str to None conversion for kwargs
131+
132+
133+
def test_CatalogEntry_Save_and_Load(monkeypatch, tmpdir):
134+
tmp_path = tmpdir.mkdir("sub").join("cat.tmp")
135+
tmp_path = tmp_path.strpath
136+
137+
class mock_callable:
138+
def __init__(self, _x):
139+
return None
140+
141+
def mock_callable(self):
142+
return "Class has a method with the same name for dynamic importing save/load!"
143+
144+
test_mock_callable = mock_callable('x')
145+
test_mock_callable.mock_callable()
146+
147+
monkeypatch.setattr(imports, "dynamic_import", mock_callable)
148+
ce = catalog.CatalogEntry(mock_callable, args=["X"])
149+
cat = catalog.Catalog(catalog_name="smart_test_catalog", entries={"foo": ce})
150+
cat.save(tmp_path)
151+
loaded_cat = cat.load(tmp_path)
152+
153+
cat_dict = cat.to_dict()
154+
loaded_cat_dict = loaded_cat.to_dict()
155+
156+
first_cat_key = list(cat_dict.keys())[0]
157+
first_loaded_cat_key = list(loaded_cat_dict.keys())[0]
158+
159+
# Assert that we were able to load in the same data as what was saved...
160+
assert loaded_cat_dict[first_loaded_cat_key]["args"] == cat_dict[first_cat_key]["args"]

0 commit comments

Comments
 (0)