|
| 1 | +import IORedis, { Command } from 'ioredis'; |
| 2 | +import { when } from 'jest-when'; |
| 3 | +import { getTotal } from 'src/modules/database/utils/database.total.util'; |
| 4 | + |
| 5 | +const nodeClient = Object.create(IORedis.prototype); |
| 6 | +nodeClient.sendCommand = jest.fn(); |
| 7 | + |
| 8 | +const mockRedisKeyspaceInfoResponse: string = '# Keyspace\r\ndb0:keys=2,expires=0,avg_ttl=0\r\n'; |
| 9 | +const mockRedisKeyspaceInfoResponseNoKeyspaceData: string = '# Keyspace\r\n \r\n'; |
| 10 | + |
| 11 | +describe('getTotalFromInfo', () => { |
| 12 | + it('Should return total from dbsize', () => { |
| 13 | + when(nodeClient.sendCommand) |
| 14 | + .calledWith(jasmine.objectContaining({ name: 'dbsize' })) |
| 15 | + .mockResolvedValue('1'); |
| 16 | + return getTotal(nodeClient).then((total) => { |
| 17 | + expect(total).toBe(1); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + it('Should return total from info when dbsize execute with error', () => { |
| 22 | + when(nodeClient.sendCommand) |
| 23 | + .calledWith(jasmine.objectContaining({ name: 'dbsize' })) |
| 24 | + .mockRejectedValue('some error'); |
| 25 | + when(nodeClient.sendCommand) |
| 26 | + .calledWith(jasmine.objectContaining({ name: 'info' })) |
| 27 | + .mockResolvedValue(mockRedisKeyspaceInfoResponse); |
| 28 | + return getTotal(nodeClient).then((total) => { |
| 29 | + expect(total).toBe(2); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('Should return 0', () => { |
| 34 | + when(nodeClient.sendCommand) |
| 35 | + .calledWith(jasmine.objectContaining({ name: 'dbsize' })) |
| 36 | + .mockRejectedValue('some error'); |
| 37 | + when(nodeClient.sendCommand) |
| 38 | + .calledWith(jasmine.objectContaining({ name: 'info' })) |
| 39 | + .mockResolvedValue(mockRedisKeyspaceInfoResponseNoKeyspaceData); |
| 40 | + return getTotal(nodeClient).then((total) => { |
| 41 | + expect(total).toBe(0); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('Should return -1 when dbsize and info execute with errors', () => { |
| 46 | + when(nodeClient.sendCommand) |
| 47 | + .calledWith(jasmine.objectContaining({ name: 'dbsize' })) |
| 48 | + .mockRejectedValue('some error'); |
| 49 | + when(nodeClient.sendCommand) |
| 50 | + .calledWith(jasmine.objectContaining({ name: 'info' })) |
| 51 | + .mockRejectedValue('some error'); |
| 52 | + return getTotal(nodeClient).then((total) => { |
| 53 | + expect(total).toBe(-1); |
| 54 | + }); |
| 55 | + }); |
| 56 | +}); |
0 commit comments