Skip to content

Commit 7cde6e6

Browse files
#RI-3704-fix tests
1 parent 62010de commit 7cde6e6

19 files changed

+104
-433
lines changed

redisinsight/api/src/modules/browser/services/keys-business/scanner/strategies/cluster.strategy.spec.ts

Lines changed: 47 additions & 280 deletions
Large diffs are not rendered by default.

redisinsight/api/src/modules/browser/services/keys-business/scanner/strategies/standalone.strategy.spec.ts

Lines changed: 24 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { IFindRedisClientInstanceByOptions } from 'src/modules/redis/redis.servi
1515
import { IGetNodeKeysResult } from 'src/modules/browser/services/keys-business/scanner/scanner.interface';
1616
import IORedis from 'ioredis';
1717
import { SettingsService } from 'src/modules/settings/settings.service';
18+
import * as Utils from 'src/modules/database/utils/database.total.util';
1819
import { StandaloneStrategy } from './standalone.strategy';
1920

2021
const REDIS_SCAN_CONFIG = config.get('redis_scan');
@@ -38,10 +39,10 @@ const mockNodeEmptyResult: IGetNodeKeysResult = {
3839
keys: [],
3940
};
4041

41-
const mockRedisKeyspaceInfoResponse_1: string = '# Keyspace\r\ndb0:keys=1,expires=0,avg_ttl=0\r\n';
42-
const mockRedisKeyspaceInfoResponse_2: string = '# Keyspace\r\ndb0:keys=1000000,expires=0,avg_ttl=0\r\n';
43-
const mockRedisKeyspaceInfoResponse_3: string = '# Keyspace\r\n \r\n';
44-
const mockRedisKeyspaceInfoResponse_4: string = '# Keyspace\r\ndb0:keys=10,expires=0,avg_ttl=0\r\n';
42+
const mockGetTotalResponse_1: number = 1;
43+
const mockGetTotalResponse_2: number = 1000000;
44+
const mockGetTotalResponse_3: number = 0;
45+
const mockGetTotalResponse_4: number = 10;
4546

4647
let strategy;
4748
let browserTool;
@@ -72,6 +73,7 @@ describe('Standalone Scanner Strategy', () => {
7273
const getKeysDto: GetKeysDto = { cursor: '0', count: 15, keysInfo: true };
7374
it('should return appropriate value with filter by type', async () => {
7475
const args = { ...getKeysDto, type: 'string', match: 'pattern*' };
76+
jest.spyOn(Utils, 'getTotal').mockResolvedValue(mockGetTotalResponse_1);
7577

7678
when(browserTool.execCommand)
7779
.calledWith(
@@ -81,14 +83,6 @@ describe('Standalone Scanner Strategy', () => {
8183
null,
8284
)
8385
.mockResolvedValue([0, [getKeyInfoResponse.name]]);
84-
when(browserTool.execCommand)
85-
.calledWith(
86-
mockClientOptions,
87-
BrowserToolKeysCommands.InfoKeyspace,
88-
expect.anything(),
89-
'utf8',
90-
)
91-
.mockResolvedValue(mockRedisKeyspaceInfoResponse_1);
9286

9387
strategy.getKeysInfo = jest.fn().mockResolvedValue([getKeyInfoResponse]);
9488

@@ -104,7 +98,7 @@ describe('Standalone Scanner Strategy', () => {
10498
]);
10599
expect(strategy.getKeysInfo).toHaveBeenCalled();
106100
expect(browserTool.execCommand).toHaveBeenNthCalledWith(
107-
2,
101+
1,
108102
mockClientOptions,
109103
BrowserToolKeysCommands.Scan,
110104
['0', 'MATCH', args.match, 'COUNT', args.count, 'TYPE', args.type],
@@ -115,6 +109,7 @@ describe('Standalone Scanner Strategy', () => {
115109
const args = {
116110
...getKeysDto, type: 'string', match: 'pattern*', keysInfo: false,
117111
};
112+
jest.spyOn(Utils, 'getTotal').mockResolvedValue(mockGetTotalResponse_1);
118113

119114
when(browserTool.execCommand)
120115
.calledWith(
@@ -124,14 +119,6 @@ describe('Standalone Scanner Strategy', () => {
124119
null,
125120
)
126121
.mockResolvedValue([0, [getKeyInfoResponse.name]]);
127-
when(browserTool.execCommand)
128-
.calledWith(
129-
mockClientOptions,
130-
BrowserToolKeysCommands.InfoKeyspace,
131-
expect.anything(),
132-
'utf8',
133-
)
134-
.mockResolvedValue(mockRedisKeyspaceInfoResponse_1);
135122

136123
strategy.getKeysInfo = jest.fn();
137124

@@ -148,6 +135,8 @@ describe('Standalone Scanner Strategy', () => {
148135
]);
149136
});
150137
it('should call scan 3 times and return appropriate value', async () => {
138+
jest.spyOn(Utils, 'getTotal').mockResolvedValue(mockGetTotalResponse_2);
139+
151140
when(browserTool.execCommand)
152141
.calledWith(mockClientOptions, BrowserToolKeysCommands.Scan, [
153142
'0',
@@ -175,14 +164,6 @@ describe('Standalone Scanner Strategy', () => {
175164
getKeysDto.count,
176165
], null)
177166
.mockResolvedValue(['0', new Array(3).fill(getKeyInfoResponse.name)]);
178-
when(browserTool.execCommand)
179-
.calledWith(
180-
mockClientOptions,
181-
BrowserToolKeysCommands.InfoKeyspace,
182-
expect.anything(),
183-
'utf8',
184-
)
185-
.mockResolvedValue(mockRedisKeyspaceInfoResponse_2);
186167

187168
strategy.getKeysInfo = jest
188169
.fn()
@@ -199,37 +180,32 @@ describe('Standalone Scanner Strategy', () => {
199180
},
200181
]);
201182
expect(strategy.getKeysInfo).toHaveBeenCalled();
202-
expect(browserTool.execCommand).toBeCalledTimes(4);
183+
expect(browserTool.execCommand).toBeCalledTimes(3);
203184
expect(browserTool.execCommand).toHaveBeenNthCalledWith(
204185
1,
205186
mockClientOptions,
206-
BrowserToolKeysCommands.InfoKeyspace,
207-
expect.anything(),
208-
'utf8',
209-
);
210-
expect(browserTool.execCommand).toHaveBeenNthCalledWith(
211-
2,
212-
mockClientOptions,
213187
BrowserToolKeysCommands.Scan,
214188
['0', 'MATCH', '*', 'COUNT', getKeysDto.count],
215189
null,
216190
);
217191
expect(browserTool.execCommand).toHaveBeenNthCalledWith(
218-
3,
192+
2,
219193
mockClientOptions,
220194
BrowserToolKeysCommands.Scan,
221195
['1', 'MATCH', '*', 'COUNT', getKeysDto.count],
222196
null,
223197
);
224198
expect(browserTool.execCommand).toHaveBeenNthCalledWith(
225-
4,
199+
3,
226200
mockClientOptions,
227201
BrowserToolKeysCommands.Scan,
228202
['2', 'MATCH', '*', 'COUNT', getKeysDto.count],
229203
null,
230204
);
231205
});
232206
it('should call scan N times until threshold exceeds', async () => {
207+
jest.spyOn(Utils, 'getTotal').mockResolvedValue(mockGetTotalResponse_2);
208+
233209
when(browserTool.execCommand)
234210
.calledWith(
235211
mockClientOptions,
@@ -238,14 +214,6 @@ describe('Standalone Scanner Strategy', () => {
238214
null,
239215
)
240216
.mockResolvedValue(['1', []]);
241-
when(browserTool.execCommand)
242-
.calledWith(
243-
mockClientOptions,
244-
BrowserToolKeysCommands.InfoKeyspace,
245-
expect.anything(),
246-
'utf8',
247-
)
248-
.mockResolvedValue(mockRedisKeyspaceInfoResponse_2);
249217

250218
strategy.getKeysInfo = jest.fn().mockResolvedValue([]);
251219

@@ -264,23 +232,9 @@ describe('Standalone Scanner Strategy', () => {
264232
},
265233
]);
266234
expect(strategy.getKeysInfo).toHaveBeenCalledTimes(0);
267-
expect(browserTool.execCommand).toHaveBeenNthCalledWith(
268-
1,
269-
mockClientOptions,
270-
BrowserToolKeysCommands.InfoKeyspace,
271-
expect.anything(),
272-
'utf8',
273-
);
274235
});
275236
it('should not call scan when total is 0', async () => {
276-
when(browserTool.execCommand)
277-
.calledWith(
278-
mockClientOptions,
279-
BrowserToolKeysCommands.InfoKeyspace,
280-
expect.anything(),
281-
'utf8',
282-
)
283-
.mockResolvedValue(mockRedisKeyspaceInfoResponse_3);
237+
jest.spyOn(Utils, 'getTotal').mockResolvedValue(mockGetTotalResponse_3);
284238

285239
strategy.getKeysInfo = jest.fn().mockResolvedValue([]);
286240

@@ -291,24 +245,12 @@ describe('Standalone Scanner Strategy', () => {
291245
...mockNodeEmptyResult,
292246
},
293247
]);
294-
expect(browserTool.execCommand).toBeCalledTimes(1);
295-
expect(browserTool.execCommand).toHaveBeenLastCalledWith(
296-
mockClientOptions,
297-
BrowserToolKeysCommands.InfoKeyspace,
298-
expect.anything(),
299-
'utf8',
300-
);
248+
expect(browserTool.execCommand).toBeCalledTimes(0);
301249
expect(strategy.getKeysInfo).toBeCalledTimes(0);
302250
});
303251
it('should call scan with required args', async () => {
304-
when(browserTool.execCommand)
305-
.calledWith(
306-
mockClientOptions,
307-
BrowserToolKeysCommands.InfoKeyspace,
308-
expect.anything(),
309-
'utf8',
310-
)
311-
.mockResolvedValue(mockRedisKeyspaceInfoResponse_3);
252+
jest.spyOn(Utils, 'getTotal').mockResolvedValue(mockGetTotalResponse_3);
253+
312254
strategy.getKeysInfo = jest.fn().mockResolvedValue([]);
313255
strategy.scan = jest.fn().mockResolvedValue(undefined);
314256

@@ -327,30 +269,8 @@ describe('Standalone Scanner Strategy', () => {
327269
expect(result).toEqual([mockNodeEmptyResult]);
328270
expect(strategy.getKeysInfo).toBeCalledTimes(0);
329271
});
330-
it('should throw error on Info Keyspace command', async () => {
331-
const replyError: ReplyError = {
332-
...mockRedisNoPermError,
333-
command: 'info keyspace',
334-
};
335-
when(browserTool.execCommand)
336-
.calledWith(mockClientOptions, BrowserToolKeysCommands.InfoKeyspace, [], 'utf8')
337-
.mockRejectedValue(replyError);
338-
try {
339-
await strategy.getKeys(mockClientOptions, getKeysDto);
340-
fail('Should throw an error');
341-
} catch (err) {
342-
expect(err.message).toEqual(replyError.message);
343-
}
344-
});
345272
it('should throw error on scan command', async () => {
346-
when(browserTool.execCommand)
347-
.calledWith(
348-
mockClientOptions,
349-
BrowserToolKeysCommands.InfoKeyspace,
350-
expect.anything(),
351-
'utf8',
352-
)
353-
.mockResolvedValue(mockRedisKeyspaceInfoResponse_1);
273+
jest.spyOn(Utils, 'getTotal').mockResolvedValue(mockGetTotalResponse_1);
354274

355275
const replyError: ReplyError = {
356276
...mockRedisNoPermError,
@@ -374,14 +294,8 @@ describe('Standalone Scanner Strategy', () => {
374294
});
375295
describe('get keys by glob patter', () => {
376296
beforeEach(async () => {
377-
when(browserTool.execCommand)
378-
.calledWith(
379-
mockClientOptions,
380-
BrowserToolKeysCommands.InfoKeyspace,
381-
expect.anything(),
382-
'utf8',
383-
)
384-
.mockResolvedValue(mockRedisKeyspaceInfoResponse_4);
297+
jest.spyOn(Utils, 'getTotal').mockResolvedValue(mockGetTotalResponse_4);
298+
385299
strategy.scan = jest.fn();
386300
});
387301
it("should call scan when math contains '?' glob", async () => {
@@ -449,14 +363,8 @@ describe('Standalone Scanner Strategy', () => {
449363
const key = getKeyInfoResponse.name;
450364
const total = 10;
451365
beforeEach(async () => {
452-
when(browserTool.execCommand)
453-
.calledWith(
454-
mockClientOptions,
455-
BrowserToolKeysCommands.InfoKeyspace,
456-
[],
457-
'utf8',
458-
)
459-
.mockResolvedValue(mockRedisKeyspaceInfoResponse_4);
366+
jest.spyOn(Utils, 'getTotal').mockResolvedValue(mockGetTotalResponse_4);
367+
460368
strategy.scan = jest.fn();
461369
});
462370
it('should find exact key when match is not glob patter', async () => {

redisinsight/api/src/modules/bulk-actions/bulk-actions.gateway.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
Logger, UseFilters, UsePipes, ValidationPipe,
1313
} from '@nestjs/common';
1414
import config from 'src/utils/config';
15-
import { BulkActionsServerEvents } from 'src/modules/bulk-actions/contants';
15+
import { BulkActionsServerEvents } from 'src/modules/bulk-actions/constants';
1616
import { CreateBulkActionDto } from 'src/modules/bulk-actions/dto/create-bulk-action.dto';
1717
import { BulkActionsService } from 'src/modules/bulk-actions/bulk-actions.service';
1818
import { AckWsExceptionFilter } from 'src/modules/pub-sub/filters/ack-ws-exception.filter';

redisinsight/api/src/modules/bulk-actions/bulk-actions.service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from 'src/__mocks__';
77
import { BulkActionsProvider } from 'src/modules/bulk-actions/providers/bulk-actions.provider';
88
import { RedisDataType } from 'src/modules/browser/dto';
9-
import { BulkActionType } from 'src/modules/bulk-actions/contants';
9+
import { BulkActionType } from 'src/modules/bulk-actions/constants';
1010
import { CreateBulkActionDto } from 'src/modules/bulk-actions/dto/create-bulk-action.dto';
1111
import { BulkActionFilter } from 'src/modules/bulk-actions/models/bulk-action-filter';
1212
import { BulkAction } from 'src/modules/bulk-actions/models/bulk-action';
File renamed without changes.

redisinsight/api/src/modules/bulk-actions/dto/create-bulk-action.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BulkActionFilter } from 'src/modules/bulk-actions/models/bulk-action-filter';
2-
import { BulkActionType } from 'src/modules/bulk-actions/contants';
2+
import { BulkActionType } from 'src/modules/bulk-actions/constants';
33
import { IsEnum, IsNotEmpty, IsString } from 'class-validator';
44
import { Type } from 'class-transformer';
55
import { BulkActionIdDto } from 'src/modules/bulk-actions/dto/bulk-action-id.dto';

redisinsight/api/src/modules/bulk-actions/interfaces/bulk-action-overview.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BulkActionStatus, BulkActionType } from 'src/modules/bulk-actions/contants';
1+
import { BulkActionStatus, BulkActionType } from 'src/modules/bulk-actions/constants';
22
import { IBulkActionProgressOverview } from './bulk-action-progress-overview.interface';
33
import { IBulkActionSummaryOverview } from './bulk-action-summary-overview.interface';
44
import { IBulkActionFilterOverview } from './bulk-action-filter-overview.interface';

redisinsight/api/src/modules/bulk-actions/interfaces/bulk-action.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BulkActionStatus } from 'src/modules/bulk-actions/contants';
1+
import { BulkActionStatus } from 'src/modules/bulk-actions/constants';
22
import { BulkActionFilter } from 'src/modules/bulk-actions/models/bulk-action-filter';
33
import { Socket } from 'socket.io';
44

redisinsight/api/src/modules/bulk-actions/models/bulk-action.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import {
77
DeleteBulkActionSimpleRunner,
88
} from 'src/modules/bulk-actions/models/runners/simple/delete.bulk-action.simple.runner';
99
import { BulkAction } from 'src/modules/bulk-actions/models/bulk-action';
10-
import { BulkActionStatus, BulkActionType } from 'src/modules/bulk-actions/contants';
10+
import { BulkActionStatus, BulkActionType } from 'src/modules/bulk-actions/constants';
1111
import { BulkActionFilter } from 'src/modules/bulk-actions/models/bulk-action-filter';
1212
import { BulkActionProgress } from 'src/modules/bulk-actions/models/bulk-action-progress';
1313
import { BulkActionSummary } from 'src/modules/bulk-actions/models/bulk-action-summary';
14+
import * as Utils from 'src/modules/database/utils/database.total.util';
1415

1516
const mockExec = jest.fn();
1617

@@ -43,7 +44,7 @@ const mockKeyBuffer = Buffer.from(mockKey);
4344
const mockRESPError = 'Reply Error: NOPERM for delete.';
4445
const mockRESPErrorBuffer = Buffer.from(mockRESPError);
4546

46-
const mockRedisKeyspaceInfoResponse_1: string = '# Keyspace\r\ndb0:keys=10000,expires=0,avg_ttl=0\r\n';
47+
const mockGetTotalResponse_1: number = 10000;
4748

4849
const generateErrors = (amount: number, raw = true): any => (
4950
new Array(amount).fill(1)
@@ -89,8 +90,7 @@ describe('AbstractBulkActionSimpleRunner', () => {
8990

9091
describe('prepare', () => {
9192
beforeEach(() => {
92-
nodeClient.sendCommand.mockResolvedValue(mockRedisKeyspaceInfoResponse_1);
93-
clusterClient.sendCommand.mockResolvedValue(mockRedisKeyspaceInfoResponse_1);
93+
jest.spyOn(Utils, 'getTotal').mockResolvedValue(mockGetTotalResponse_1);
9494
clusterClient.nodes = jest.fn().mockReturnValue([nodeClient, nodeClient]);
9595
});
9696

redisinsight/api/src/modules/bulk-actions/models/bulk-action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as IORedis from 'ioredis';
22
import { debounce } from 'lodash';
3-
import { BulkActionStatus, BulkActionType } from 'src/modules/bulk-actions/contants';
3+
import { BulkActionStatus, BulkActionType } from 'src/modules/bulk-actions/constants';
44
import { BulkActionFilter } from 'src/modules/bulk-actions/models/bulk-action-filter';
55
import { Socket } from 'socket.io';
66
import { Logger } from '@nestjs/common';

0 commit comments

Comments
 (0)