|
| 1 | +const { expect } = require("chai"); |
| 2 | +const { cachedKeysStore } = require("../../../utils/cache"); |
| 3 | + |
| 4 | +describe("cachedKeysStore", function () { |
| 5 | + let keyStore; |
| 6 | + const modelKey = "modelKey"; |
| 7 | + beforeEach(function () { |
| 8 | + keyStore = cachedKeysStore(); |
| 9 | + }); |
| 10 | + |
| 11 | + describe("getCachedKeys", function () { |
| 12 | + it("should return an array of cached keys for a model key", function () { |
| 13 | + const cachedKeys = new Set(["key1", "key2", "key3"]); |
| 14 | + keyStore.addCachedKey(modelKey, "key1"); |
| 15 | + keyStore.addCachedKey(modelKey, "key2"); |
| 16 | + keyStore.addCachedKey(modelKey, "key3"); |
| 17 | + |
| 18 | + const result = keyStore.getCachedKeys(modelKey); |
| 19 | + |
| 20 | + expect(result).to.be.an("set"); |
| 21 | + expect(result).to.be.deep.equal(cachedKeys); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + describe("addCachedKey", function () { |
| 26 | + it("should add a cached key to the key store", function () { |
| 27 | + const cachedKey = "key1"; |
| 28 | + |
| 29 | + keyStore.addCachedKey(modelKey, cachedKey); |
| 30 | + |
| 31 | + const result = keyStore.getCachedKeys(modelKey); |
| 32 | + expect(result).to.deep.equal(new Set([cachedKey])); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe("removeModelKey", function () { |
| 37 | + it("should remove the model key and its associated cached keys from the key store", function () { |
| 38 | + keyStore.addCachedKey(modelKey, "key1"); |
| 39 | + keyStore.addCachedKey(modelKey, "key2"); |
| 40 | + keyStore.addCachedKey(modelKey, "key3"); |
| 41 | + |
| 42 | + keyStore.removeModelKey(modelKey); |
| 43 | + |
| 44 | + const result = keyStore.getCachedKeys(modelKey); |
| 45 | + expect(result).to.be.an("set"); |
| 46 | + expect(result).to.deep.equal(new Set()); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe("removeCachedKey", function () { |
| 51 | + it("should remove a cached key from the key store for a given model key", function () { |
| 52 | + const cachedKey = "key1"; |
| 53 | + keyStore.addCachedKey(modelKey, cachedKey); |
| 54 | + |
| 55 | + keyStore.removeCachedKey(modelKey, cachedKey); |
| 56 | + |
| 57 | + const result = keyStore.getCachedKeys(modelKey); |
| 58 | + expect(result).to.be.an("set"); |
| 59 | + expect(result).to.deep.equal(new Set()); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should not remove other cached keys for the same model key", function () { |
| 63 | + keyStore.addCachedKey(modelKey, "key1"); |
| 64 | + keyStore.addCachedKey(modelKey, "key2"); |
| 65 | + keyStore.addCachedKey(modelKey, "key3"); |
| 66 | + |
| 67 | + keyStore.removeCachedKey(modelKey, "key2"); |
| 68 | + |
| 69 | + const result = keyStore.getCachedKeys(modelKey); |
| 70 | + expect(result).to.deep.equal(new Set(["key1", "key3"])); |
| 71 | + }); |
| 72 | + }); |
| 73 | +}); |
0 commit comments