def test_write_once_persistent_dict_storage_and_lookup(in_mem_cache_size): try: tmpdir = tempfile.mkdtemp() pdict = WriteOncePersistentDict("pytools-test", container_dir=tmpdir, in_mem_cache_size=in_mem_cache_size) # check lookup pdict[0] = 1 assert pdict[0] == 1 # do two lookups to test the cache assert pdict[0] == 1 # check updating with pytest.raises(ReadOnlyEntryError): pdict[0] = 2 # check not found with pytest.raises(NoSuchEntryError): pdict.fetch(1) # check store_if_not_present pdict.store_if_not_present(0, 2) assert pdict[0] == 1 pdict.store_if_not_present(1, 1) assert pdict[1] == 1 finally: shutil.rmtree(tmpdir)
def test_write_once_persistent_dict_storage_and_lookup(in_mem_cache_size): try: tmpdir = tempfile.mkdtemp() pdict = WriteOncePersistentDict( "pytools-test", container_dir=tmpdir, in_mem_cache_size=in_mem_cache_size) # check lookup pdict[0] = 1 assert pdict[0] == 1 # do two lookups to test the cache assert pdict[0] == 1 # check updating with pytest.raises(ReadOnlyEntryError): pdict[0] = 2 # check not found with pytest.raises(NoSuchEntryError): pdict.fetch(1) # check store_if_not_present pdict.store_if_not_present(0, 2) assert pdict[0] == 1 pdict.store_if_not_present(1, 1) assert pdict[1] == 1 finally: shutil.rmtree(tmpdir)
def test_write_once_persistent_dict_clear(): try: tmpdir = tempfile.mkdtemp() pdict = WriteOncePersistentDict("pytools-test", container_dir=tmpdir) pdict[0] = 1 pdict.fetch(0) pdict.clear() with pytest.raises(NoSuchEntryError): pdict.fetch(0) finally: shutil.rmtree(tmpdir)
def test_write_once_persistent_dict_cache_collisions(): try: tmpdir = tempfile.mkdtemp() pdict = WriteOncePersistentDict("pytools-test", container_dir=tmpdir) key1 = PDictTestingKeyOrValue(1, hash_key=0) key2 = PDictTestingKeyOrValue(2, hash_key=0) pdict[key1] = 1 # check lookup with pytest.warns(CollisionWarning): with pytest.raises(NoSuchEntryError): pdict.fetch(key2) # check update with pytest.raises(ReadOnlyEntryError): pdict[key2] = 1 # check store_if_not_present pdict.store_if_not_present(key2, 2) assert pdict[key1] == 1 finally: shutil.rmtree(tmpdir)
def test_write_once_persistent_dict_lru_policy(): try: tmpdir = tempfile.mkdtemp() pdict = WriteOncePersistentDict("pytools-test", container_dir=tmpdir, in_mem_cache_size=3) pdict[1] = PDictTestingKeyOrValue(1) pdict[2] = PDictTestingKeyOrValue(2) pdict[3] = PDictTestingKeyOrValue(3) pdict[4] = PDictTestingKeyOrValue(4) val1 = pdict.fetch(1) assert pdict.fetch(1) is val1 pdict.fetch(2) assert pdict.fetch(1) is val1 pdict.fetch(2) pdict.fetch(3) assert pdict.fetch(1) is val1 pdict.fetch(2) pdict.fetch(3) pdict.fetch(2) assert pdict.fetch(1) is val1 pdict.fetch(2) pdict.fetch(3) pdict.fetch(4) assert pdict.fetch(1) is not val1 finally: shutil.rmtree(tmpdir)
def test_write_once_persistent_dict_lru_policy(): try: tmpdir = tempfile.mkdtemp() pdict = WriteOncePersistentDict( "pytools-test", container_dir=tmpdir, in_mem_cache_size=3) pdict[1] = PDictTestingKeyOrValue(1) pdict[2] = PDictTestingKeyOrValue(2) pdict[3] = PDictTestingKeyOrValue(3) pdict[4] = PDictTestingKeyOrValue(4) val1 = pdict.fetch(1) assert pdict.fetch(1) is val1 pdict.fetch(2) assert pdict.fetch(1) is val1 pdict.fetch(2) pdict.fetch(3) assert pdict.fetch(1) is val1 pdict.fetch(2) pdict.fetch(3) pdict.fetch(2) assert pdict.fetch(1) is val1 pdict.fetch(2) pdict.fetch(3) pdict.fetch(4) assert pdict.fetch(1) is not val1 finally: shutil.rmtree(tmpdir)