|
| 1 | +import * as fs from 'fs-extra'; |
| 2 | +import { DiskCacheServiceImpl } from '../disk-cache.service'; |
| 3 | + |
| 4 | +describe('DiskCacheService tests', () => { |
| 5 | + const filepath = '/tmp/dsa2d'; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + fs.writeJsonSync(filepath, {}); |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + fs.removeSync(filepath); |
| 13 | + }); |
| 14 | + |
| 15 | + test('set - primitive', async () => { |
| 16 | + const cacheBefore = await fs.readJson(filepath); |
| 17 | + |
| 18 | + const cacheService = new DiskCacheServiceImpl({ |
| 19 | + filepath, |
| 20 | + }); |
| 21 | + |
| 22 | + await cacheService.set('key', 42); |
| 23 | + |
| 24 | + const cacheAfter = await fs.readJson(filepath); |
| 25 | + |
| 26 | + expect(cacheBefore.key).toEqual(undefined); |
| 27 | + expect(cacheAfter.key).toEqual(42); |
| 28 | + }); |
| 29 | + |
| 30 | + test('set - object', async () => { |
| 31 | + const cacheBefore = await fs.readJson(filepath); |
| 32 | + |
| 33 | + const cacheService = new DiskCacheServiceImpl({ |
| 34 | + filepath, |
| 35 | + }); |
| 36 | + |
| 37 | + await cacheService.set('key', { value: 42 }); |
| 38 | + |
| 39 | + const cacheAfter = await fs.readJson(filepath); |
| 40 | + |
| 41 | + expect(cacheBefore.key).toEqual(undefined); |
| 42 | + expect(cacheAfter.key).toEqual({ value: 42 }); |
| 43 | + }); |
| 44 | + |
| 45 | + test('get - primitive', async () => { |
| 46 | + await fs.writeJson(filepath, { key: 42 }); |
| 47 | + |
| 48 | + const cacheService = new DiskCacheServiceImpl({ |
| 49 | + filepath, |
| 50 | + }); |
| 51 | + |
| 52 | + const value = await cacheService.get('key'); |
| 53 | + |
| 54 | + expect(value).toEqual(42); |
| 55 | + }); |
| 56 | + |
| 57 | + test('get - object', async () => { |
| 58 | + await fs.writeJson(filepath, { key: { value: 42 } }); |
| 59 | + |
| 60 | + const cacheService = new DiskCacheServiceImpl({ |
| 61 | + filepath, |
| 62 | + }); |
| 63 | + |
| 64 | + const value = await cacheService.get('key'); |
| 65 | + |
| 66 | + expect(value).toEqual({ value: 42 }); |
| 67 | + }); |
| 68 | + |
| 69 | + test('get - key not exists', async () => { |
| 70 | + const cacheService = new DiskCacheServiceImpl({ |
| 71 | + filepath, |
| 72 | + }); |
| 73 | + |
| 74 | + const value = await cacheService.get('key'); |
| 75 | + |
| 76 | + expect(value).toEqual(undefined); |
| 77 | + }); |
| 78 | + |
| 79 | + test('remove - primitive', async () => { |
| 80 | + await fs.writeJson(filepath, { key: 42 }); |
| 81 | + |
| 82 | + const cacheBefore = await fs.readJson(filepath); |
| 83 | + |
| 84 | + const cacheService = new DiskCacheServiceImpl({ |
| 85 | + filepath, |
| 86 | + }); |
| 87 | + |
| 88 | + await cacheService.remove('key'); |
| 89 | + |
| 90 | + const cacheAfter = await fs.readJson(filepath); |
| 91 | + |
| 92 | + expect(cacheBefore.key).toEqual(42); |
| 93 | + expect(cacheAfter.key).toEqual(undefined); |
| 94 | + }); |
| 95 | + |
| 96 | + test('remove - object', async () => { |
| 97 | + await fs.writeJson(filepath, { key: { value: 42 } }); |
| 98 | + |
| 99 | + const cacheBefore = await fs.readJson(filepath); |
| 100 | + |
| 101 | + const cacheService = new DiskCacheServiceImpl({ |
| 102 | + filepath, |
| 103 | + }); |
| 104 | + |
| 105 | + await cacheService.remove('key'); |
| 106 | + |
| 107 | + const cacheAfter = await fs.readJson(filepath); |
| 108 | + |
| 109 | + expect(cacheBefore.key).toEqual({ value: 42 }); |
| 110 | + expect(cacheAfter.key).toEqual(undefined); |
| 111 | + }); |
| 112 | + |
| 113 | + test('remove - key not exists', async () => { |
| 114 | + const cacheBefore = await fs.readJson(filepath); |
| 115 | + |
| 116 | + const cacheService = new DiskCacheServiceImpl({ |
| 117 | + filepath, |
| 118 | + }); |
| 119 | + |
| 120 | + await cacheService.remove('non-existant-key'); |
| 121 | + |
| 122 | + const cacheAfter = await fs.readJson(filepath); |
| 123 | + |
| 124 | + expect(cacheBefore.key).toEqual(undefined); |
| 125 | + expect(cacheAfter.key).toEqual(undefined); |
| 126 | + }); |
| 127 | + |
| 128 | + test('clear', async () => { |
| 129 | + await fs.writeJson(filepath, { key: { value: 42 } }); |
| 130 | + |
| 131 | + const cacheBefore = await fs.readJson(filepath); |
| 132 | + |
| 133 | + const cacheService = new DiskCacheServiceImpl({ |
| 134 | + filepath, |
| 135 | + }); |
| 136 | + |
| 137 | + await cacheService.clear(); |
| 138 | + |
| 139 | + const cacheAfter = await fs.readJson(filepath); |
| 140 | + |
| 141 | + expect(cacheBefore).toEqual({ key: { value: 42 } }); |
| 142 | + expect(cacheAfter).toEqual({}); |
| 143 | + }); |
| 144 | + |
| 145 | + test('read cache', async () => { |
| 146 | + const cacheService = new DiskCacheServiceImpl({ |
| 147 | + filepath, |
| 148 | + }); |
| 149 | + |
| 150 | + expect(await cacheService.readCache()).toEqual({}); |
| 151 | + |
| 152 | + await cacheService.set('key', 42); |
| 153 | + expect(await cacheService.readCache()).toEqual({ key: 42 }); |
| 154 | + |
| 155 | + await cacheService.set('foo', 'bar'); |
| 156 | + expect(await cacheService.readCache()).toEqual({ key: 42, foo: 'bar' }); |
| 157 | + |
| 158 | + await cacheService.clear(); |
| 159 | + expect(await cacheService.readCache()).toEqual({}); |
| 160 | + }); |
| 161 | + |
| 162 | + test('write cache', async () => { |
| 163 | + const cacheService = new DiskCacheServiceImpl({ |
| 164 | + filepath, |
| 165 | + }); |
| 166 | + |
| 167 | + expect(await cacheService.readCache()).toEqual({}); |
| 168 | + |
| 169 | + await cacheService.writeCache({ key: 42, foo: 'bar' }); |
| 170 | + |
| 171 | + expect(await cacheService.readCache()).toEqual({ key: 42, foo: 'bar' }); |
| 172 | + }); |
| 173 | +}); |
0 commit comments