|
14 | 14 |
|
15 | 15 | import pyearthtools |
16 | 16 | from pyearthtools.data import catalog |
| 17 | +from pyearthtools.utils.initialisation import imports |
17 | 18 | from collections import namedtuple |
18 | 19 | import pytest |
19 | 20 | import io |
| 21 | +from types import ModuleType |
20 | 22 |
|
21 | 23 |
|
22 | 24 | def test_get_name(): |
23 | | - |
| 25 | + """ |
| 26 | + Test the test_get_name functionality |
| 27 | + """ |
24 | 28 | result = catalog.get_name("testname") |
25 | 29 | assert result == "testname" |
26 | 30 |
|
@@ -103,3 +107,54 @@ def mockEntry(): |
103 | 107 | cat.remove("TestEntryKey") |
104 | 108 | with pytest.raises(KeyError): |
105 | 109 | 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