Persistent Storage General Testing.
(tmpdir)
| 81 | |
| 82 | |
| 83 | def test_disabled_persistent_storage(tmpdir): |
| 84 | """Persistent Storage General Testing.""" |
| 85 | # Create ourselves an attachment object set in Memory Mode only |
| 86 | pc = PersistentStore( |
| 87 | namespace="abc", path=str(tmpdir), mode=PersistentStoreMode.MEMORY |
| 88 | ) |
| 89 | assert pc.read() is None |
| 90 | assert pc.read("mykey") is None |
| 91 | with pytest.raises(AttributeError): |
| 92 | # Invalid key specified |
| 93 | pc.read("!invalid") |
| 94 | assert pc.write("data") is False |
| 95 | assert pc.get("key") is None |
| 96 | assert pc.set("key", "value") |
| 97 | assert pc.get("key") == "value" |
| 98 | |
| 99 | assert pc.set("key2", "value") |
| 100 | pc.clear("key", "key-not-previously-set") |
| 101 | assert pc.get("key2") == "value" |
| 102 | assert pc.get("key") is None |
| 103 | |
| 104 | # Set it again |
| 105 | assert pc.set("key", "another-value") |
| 106 | # Clears all |
| 107 | pc.clear() |
| 108 | assert pc.get("key2") is None |
| 109 | assert pc.get("key") is None |
| 110 | # A second call to clear on an already empty cache set |
| 111 | pc.clear() |
| 112 | |
| 113 | # No dirty flag is set as ther is nothing to write to disk |
| 114 | pc.set("not-persistent", "value", persistent=False) |
| 115 | del pc["not-persistent"] |
| 116 | with pytest.raises(KeyError): |
| 117 | # Can't delete it twice |
| 118 | del pc["not-persistent"] |
| 119 | |
| 120 | # A Persistent key |
| 121 | pc.set("persistent", "value") |
| 122 | # Removes it and sets/clears the dirty flag |
| 123 | del pc["persistent"] |
| 124 | |
| 125 | # After all of the above, nothing was done to the directory |
| 126 | assert len(os.listdir(str(tmpdir))) == 0 |
| 127 | |
| 128 | with pytest.raises(AttributeError): |
| 129 | # invalid persistent store specified |
| 130 | PersistentStore(namespace="abc", path=str(tmpdir), mode="garbage") |
| 131 | |
| 132 | |
| 133 | def test_persistent_storage_init(tmpdir): |