def test_cache_loc(self): """ Change the default cache location """ from cachejar.jar import CacheFactory test_dir = os.path.join(self.datadir, 'cache') make_and_clear_directory(test_dir) local_factory = CacheFactory(test_dir) jar = local_factory.cachejar(self.appid) o1 = TestObj() o1.foo = "bagels" jar.update(self.datafilename, o1, TestObj) o2 = TestObj() o2.foo = "cheese" jar.update(self.datafilename2, o2, TestObj) ot1 = jar.object_for(self.datafilename, TestObj) ot2 = jar.object_for(self.datafilename2, TestObj) self.assertEqual(o1, ot1) self.assertEqual(o2, ot2) self.assertIsNone( cachejar.jar(self.appid).object_for(self.datafilename, TestObj)) self.assertIsNone( cachejar.jar(self.appid).object_for(self.datafilename2, TestObj)) local_factory.clear(self.appid, remove_completely=True) cachejar.factory.clear(self.appid, remove_completely=True)
def test_singleton(self): """ Make sure identity is preserved across imports """ from cachejar import factory as fact2 cachejar.jar(self.appid).update(self.datafilename, TestObj(), TestObj, "DUPTEST") o = fact2.cachejar(self.appid).object_for(self.datafilename, TestObj, "DUPTEST") self.assertIsNotNone(o) self.assertEqual("penguin", o.s)
def process_input(fname: str, forward: bool) -> MyClass: my_obj = cachejar.jar(appid).object_for(fname, MyClass, forward) if not my_obj: print(f"Processing {fname}") my_obj = MyClass(fname, forward) cachejar.jar(appid).update(fname, MyClass(fname, forward), MyClass, forward) else: print(f"Using cached image for {fname}") return my_obj
def test_pickled_file(self): """ Basic functional tests """ o1 = TestObj() # Reference the cachejar jar = cachejar.jar(self.appid) # Add the same object to the jar twice jar.update(self.datafilename, o1, TestObj) cachejar.jar(self.appid).update(self.datafilename, o1, TestObj) self.assertEqual(1, self.num_data_files()) # Pull the object out of the jar o2 = jar.object_for(self.datafilename, TestObj) self.assertIsNotNone(o2) self.assertEqual(o1, o2) # Change the data file signature Path(self.datafilename).touch() self.assertIsNone(jar.object_for(self.datafilename, TestObj)) # The chaange in signature will flush the cache self.assertEqual(0, self.num_data_files()) # Create an object with 'dependent' parameters o1 = TestObj('baseball', 17) jar.update(self.datafilename, o1, TestObj, 'baseball', 17) o2 = jar.object_for(self.datafilename, TestObj, 'baseball', 17) self.assertEqual(o1, o2) self.assertEqual(1, self.num_data_files()) # Create a second object with different parameters self.assertIsNone( jar.object_for(self.datafilename, TestObj, 'robin', -173)) o3 = TestObj('robin', -173) # Add same parameters, two different data files jar.update(self.datafilename, o3, TestObj, 'robin', -173) jar.update(self.datafilename2, o3, TestObj, 'robin', -173) o4 = jar.object_for(self.datafilename2, TestObj, 'robin', -173) self.assertEqual(o3, o4) self.assertEqual(3, self.num_data_files()) # Clean everything for a single file, and obj id jar.clean(self.datafilename, TestObj, 'robin', -173) self.assertEqual(2, self.num_data_files()) # Clean everything for a whole file jar.clean(self.datafilename) self.assertEqual(1, self.num_data_files()) # Clean everything jar.clean() self.assertEqual(0, self.num_data_files())
def test_example(self): appid = 'example_notebbook' url = "http://hl7.org/fhir/fhir.ttl" # Connect to the cachejar for this application jar = cachejar.jar(appid) # Remove any existing data jar.clear() # Invoke the same operation twice outf = StringIO() with redirect_stdout(outf): for _ in range(1, 3): obj = jar.object_for(url, ByteSum) if obj: print("Retrived from cache") else: print("Not cached - computing md5") obj = ByteSum(url) jar.update(url, obj, ByteSum) print(f"MD5 for {url} = {obj.hash}") self.assertEqual( """Not cached - computing md5 MD5 for http://hl7.org/fhir/fhir.ttl = 8ce6c3545b7a238f0091abe05dbcb7dd Retrived from cache MD5 for http://hl7.org/fhir/fhir.ttl = 8ce6c3545b7a238f0091abe05dbcb7dd """, outf.getvalue())
def test_disabled(self): outf = StringIO() with redirect_stdout(outf): jar = cachejar.jar('someapp') print(f"Factory: {cachejar.factory.disabled}") print(f"Jar: {jar.disabled}\n") cachejar.factory.disabled = True print(f"Factory: {cachejar.factory.disabled}") print(f"Jar: {jar.disabled}\n") jar.disabled = True print(f"Factory: {cachejar.factory.disabled}") print(f"Jar: {jar.disabled}\n") cachejar.factory.disabled = False print(f"Factory: {cachejar.factory.disabled}") print(f"Jar: {jar.disabled}\n") self.assertEqual( """Factory: False Jar: False Factory: True Jar: True Factory: True Jar: True Factory: False Jar: True """, outf.getvalue())
def test_reload_file(self): """ Make sure that saving and restoring the index behaves correctly. JSON doesn't deal well with tuples... """ o1 = TestObj() # Reference the cachejar jar = cachejar.jar(self.appid) # Add the same object to the jar twice jar.update(self.datafilename, o1, TestObj) o2 = jar.object_for(self.datafilename, TestObj) self.assertEqual(o1, o2) jar._load_index() o3 = jar.object_for(self.datafilename, TestObj) self.assertEqual(o1, o3)
def test_multi_applications(self): """ Make sure that multi applications can use the same cache """ o1 = TestObj("a", 1) o2 = TestObj("b", 2) cachejar.jar(self.appid).update(self.datafilename, o1, TestObj) cachejar.jar(self.appid2).update(self.datafilename, o2, TestObj) self.assertEqual(1, self.num_data_files()) o3 = cachejar.jar(self.appid).object_for(self.datafilename, TestObj) o4 = cachejar.jar(self.appid2).object_for(self.datafilename, TestObj) self.assertEqual(o1, o3) self.assertEqual(o2, o4)
def test_kw_parms(self): o1 = TestObj('abc', 123) o2 = TestObj('def', 456) o3 = TestObj('ghi', 789) jar = cachejar.jar(self.appid) jar.update(self.datafilename, o1, TestObj, 'abc', 123, test=False) jar.update(self.datafilename, o2, TestObj, 'abc', 123, test=True) jar.update(self.datafilename, o3, TestObj, 'abc', test=True) ot1 = jar.object_for(self.datafilename, TestObj, 'abc', 123, test=False) ot2 = jar.object_for(self.datafilename, TestObj, 'abc', 123, test=True) ot3 = jar.object_for(self.datafilename, TestObj, 'abc', test=True) self.assertEqual(o1, ot1) self.assertEqual(o2, ot2) self.assertEqual(o3, ot3)
def test_urls(self): """ Test application against URLs """ solid_url = "http://hl7.org/fhir/fhir.ttl" o1 = TestObj() o1.url = solid_url changing_url = "https://www.nytimes.com/" o2 = TestObj() o2.url = changing_url cachejar.factory.clear(self.appid) jar = cachejar.jar(self.appid) jar.update(solid_url, o1, TestObj) jar.update(changing_url, o2, TestObj) ot1 = jar.object_for(solid_url, TestObj) self.assertIsNotNone(ot1) # Note: this doesn't really test what we think. We need to find a URL somewhere whose update data # continuously changes ot2 = jar.object_for(changing_url + 'z', TestObj) self.assertIsNone(ot2)
def setUp(self): super().setUp() self.jar = cachejar.jar(self.appid)
def testdocumentation(self): __version__ = "1.3.0" appid = __name__ + __version__ cachejar.factory.clear(appid) def process_input(fname: str, forward: bool) -> MyClass: my_obj = cachejar.jar(appid).object_for(fname, MyClass, forward) if not my_obj: print(f"Processing {fname}") my_obj = MyClass(fname, forward) cachejar.jar(appid).update(fname, MyClass(fname, forward), MyClass, forward) else: print(f"Using cached image for {fname}") return my_obj outf = StringIO() with redirect_stdout(outf): print( process_input("https://github.com/hsolbrig/cachejar", forward=True).fname) print( process_input("https://github.com/hsolbrig/cachejar", forward=True).fname) print( process_input("https://github.com/hsolbrig/cachejar", forward=False).fname) print( process_input("https://github.com/hsolbrig/cachejar", forward=False).fname) self.assertEqual( """Processing https://github.com/hsolbrig/cachejar https://github.com/hsolbrig/cachejar Using cached image for https://github.com/hsolbrig/cachejar https://github.com/hsolbrig/cachejar Processing https://github.com/hsolbrig/cachejar rajehcac/girblosh/moc.buhtig//:sptth Using cached image for https://github.com/hsolbrig/cachejar rajehcac/girblosh/moc.buhtig//:sptth """, outf.getvalue()) from cachejar.jar import CacheFactory outf = StringIO() with redirect_stdout(outf): print(cachejar.factory.cache_root) _ = cachejar.jar('someapp') # Create a local cache print(cachejar.factory.cache_directory('someapp')) local_factory = CacheFactory('/tmp/caches') print(local_factory.cache_root) _ = local_factory.cachejar('someapp') print(local_factory.cache_directory('someapp')) # You can do this, but once done it holds for the life of the application (and Jupyter has # a life that extends well beyond this cell) # cachejar.factory = local_factory self.assertEqual( """/Users/mrf7578/.cachejar /Users/mrf7578/.cachejar/someapp /tmp/caches /tmp/caches/someapp """, outf.getvalue()) outf = StringIO() with redirect_stdout(outf): print(cachejar.factory.cache_directory('someapp')) cachejar.factory.clear('someapp') # Remove all contents print(cachejar.factory.cache_directory('someapp')) cachejar.factory.clear( 'someapp', remove_completely=True) # Remove all knowledge print(cachejar.factory.cache_directory('someapp')) self.assertEqual( """/Users/mrf7578/.cachejar/someapp /Users/mrf7578/.cachejar/someapp None """, outf.getvalue())
def setUp(self): cachejar.jar(self.appid).clear()
def test_basic_clear(self): """ Catch a bit of code that wasn't caught elsewhere """ o1 = TestObj() jar = cachejar.jar(self.appid) jar.update(self.datafilename, o1, TestObj) cachejar.jar(self.appid).clear()