| 8 | |
| 9 | |
| 10 | def run_common_cache_tests(subtests, cache): |
| 11 | bank = "fnord/kevin/stuart" |
| 12 | # ^^^^ This bank can be just fnord, or fnord/foo, or any mildly reasonable |
| 13 | # or possibly unreasonably nested names. |
| 14 | # |
| 15 | # No. Seriously. Try import string; bank = '/'.join(string.ascii_letters) |
| 16 | # - it works! |
| 17 | # import string; bank = "/".join(string.ascii_letters) |
| 18 | good_key = "roscivs" |
| 19 | bad_key = "monkey" |
| 20 | |
| 21 | with subtests.test("non-existent bank should be empty on cache start"): |
| 22 | assert not cache.contains(bank=bank) |
| 23 | assert cache.list(bank=bank) == [] |
| 24 | |
| 25 | with subtests.test("after storing key in bank it should be in cache list"): |
| 26 | cache.store(bank=bank, key=good_key, data=b"\x01\x04\x05fnordy data") |
| 27 | assert cache.list(bank) == [good_key] |
| 28 | |
| 29 | with subtests.test("after storing value, it should be fetchable"): |
| 30 | expected_data = "trombone pleasantry" |
| 31 | cache.store(bank=bank, key=good_key, data=expected_data) |
| 32 | assert cache.fetch(bank=bank, key=good_key) == expected_data |
| 33 | |
| 34 | with subtests.test("bad key should still be absent from cache"): |
| 35 | assert cache.fetch(bank=bank, key=bad_key) == {} |
| 36 | |
| 37 | with subtests.test("storing new value should update it"): |
| 38 | # Double check that the data was still the old stuff |
| 39 | old_data = expected_data |
| 40 | assert cache.fetch(bank=bank, key=good_key) == old_data |
| 41 | new_data = "stromboli" |
| 42 | cache.store(bank=bank, key=good_key, data=new_data) |
| 43 | assert cache.fetch(bank=bank, key=good_key) == new_data |
| 44 | |
| 45 | with subtests.test("storing complex object works"): |
| 46 | new_thing = { |
| 47 | "some": "data", |
| 48 | 42: "wheee", |
| 49 | "some other": {"sub": {"objects": "here"}}, |
| 50 | } |
| 51 | |
| 52 | cache.store(bank=bank, key=good_key, data=new_thing) |
| 53 | actual_thing = cache.fetch(bank=bank, key=good_key) |
| 54 | if isinstance(cache, salt.cache.MemCache): |
| 55 | # MemCache should actually store the object - everything else |
| 56 | # should create a copy of it. |
| 57 | assert actual_thing is new_thing |
| 58 | else: |
| 59 | assert actual_thing is not new_thing |
| 60 | assert actual_thing == new_thing |
| 61 | |
| 62 | with subtests.test("contains returns true if key in bank"): |
| 63 | assert cache.contains(bank=bank, key=good_key) |
| 64 | |
| 65 | with subtests.test("contains returns true if bank exists and key is None"): |
| 66 | assert cache.contains(bank=bank, key=None) |
| 67 | |