Skip to content

Commit

Permalink
Change some logic & add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lakesare committed Jul 29, 2021
1 parent 2f88974 commit a26533a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
16 changes: 7 additions & 9 deletions sciunit/models/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,15 @@ def backend_run(self) -> Any:
return self.cache_to_results(self._results)
if self.use_disk_cache and self.get_disk_cache(key):
return self.cache_to_results(self._results)
results = self.results_to_cache(self._backend_run())
results = self._backend_run()
if self.use_memory_cache:
self.set_memory_cache(results, key)
self.set_memory_cache(self.results_to_cache(results), key)
if self.use_disk_cache:
self.set_disk_cache(results, key)
self.set_disk_cache(self.results_to_cache(results), key)
return results

def cache_to_results(self, cache: Any) -> Any:
"""A method you should override if you have to make some adjustments
to the simulation results after they've been restored from the cache.
"""A method to convert cache to some hypothetical Results object.
Args:
cache (Any): An object returned from .get_memory_cache() or .get_disk_cache().
Expand All @@ -181,15 +180,14 @@ def cache_to_results(self, cache: Any) -> Any:
return cache

def results_to_cache(self, results: Any) -> Any:
"""A method you should override if you have to make some adjustments
to the way your simulation results are stored in cache.
"""A method to convert the results from your model run
into storable cache object (usually a simple dictionary or an array).
Args:
results (Any): An object returned from your ._backend_run().
Returns:
Any: The results in the format that's good for storing in cache
(usually a simple dictionary or an array).
Any: The results in the format that's good for storing in cache.
"""
return results

Expand Down
20 changes: 20 additions & 0 deletions sciunit/unit_test/backend_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,26 @@ def _backend_run(self) -> str:
backend.set_memory_cache("value2")
backend.backend_run()

def test_backend_cache_to_results(self):
myModel = Model()
class MyBackend(Backend):
def cache_to_results(self, cache):
return { "color": "red" }

def results_to_cache(self, results):
return { "color": "blue" }

def _backend_run(self):
return { "color": "white" }

backend = MyBackend()
backend.model = myModel
backend.init_backend(use_disk_cache=False, use_memory_cache=True)
# On first run we get the original object
self.assertEqual(backend.backend_run(), { "color": "white" })
# And on consequent runs we get the object recovered from the cache
self.assertEqual(backend.backend_run(), { "color": "red" })
self.assertEqual(backend.backend_run(), { "color": "red" })

if __name__ == "__main__":
unittest.main()

0 comments on commit a26533a

Please sign in to comment.