Skip to content

Commit 31d2d06

Browse files
author
arthosofteq
committed
add tests
1 parent 4f1dd94 commit 31d2d06

File tree

10 files changed

+315
-4
lines changed

10 files changed

+315
-4
lines changed

redisinsight/api/src/__mocks__/analytics.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ export const mockCliAnalyticsService = () => ({
77
sendCommandErrorEvent: jest.fn(),
88
sendClusterCommandExecutedEvent: jest.fn(),
99
sendConnectionErrorEvent: jest.fn(),
10+
sendIndexInfoEvent: jest.fn(),
1011
});
1112

1213
export const mockWorkbenchAnalyticsService = () => ({
1314
sendCommandExecutedEvents: jest.fn(),
1415
sendCommandExecutedEvent: jest.fn(),
1516
sendCommandDeletedEvent: jest.fn(),
17+
sendIndexInfoEvent: jest.fn(),
1618
});
1719

1820
export const mockSettingsAnalyticsService = () => ({

redisinsight/api/src/__mocks__/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './encryption';
55
export * from './errors';
66
// export * from './redis-databases';
77
export * from './redis-info';
8+
export * from './redis-rs';
89
export * from './app-settings';
910
export * from './analytics';
1011
export * from './profiler';
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
import { isArray, isString } from 'lodash';
2+
3+
export const mockRedisFtInfoReply = [
4+
'index_name',
5+
'idx:bicycle',
6+
'index_options',
7+
[],
8+
'index_definition',
9+
[
10+
'key_type',
11+
'HASH',
12+
'prefixes',
13+
['bicycle:'],
14+
'default_score',
15+
'1',
16+
],
17+
'attributes',
18+
[
19+
[
20+
'identifier',
21+
'$.brand',
22+
'attribute',
23+
'brand',
24+
'type',
25+
'TEXT',
26+
'WEIGHT',
27+
'1',
28+
],
29+
[
30+
'identifier', '$.model',
31+
'attribute', 'model',
32+
'type', 'TEXT',
33+
'WEIGHT', '1',
34+
'NOSTEM',
35+
],
36+
[
37+
'identifier',
38+
'$.description',
39+
'attribute',
40+
'description',
41+
'type',
42+
'TEXT',
43+
'WEIGHT',
44+
'1',
45+
],
46+
[
47+
'identifier',
48+
'$.price',
49+
'attribute',
50+
'price',
51+
'type',
52+
'NUMERIC',
53+
'NOINDEX',
54+
],
55+
[
56+
'identifier',
57+
'$.condition',
58+
'attribute',
59+
'condition',
60+
'type',
61+
'TAG',
62+
'SEPARATOR',
63+
',',
64+
'CASESENSITIVE',
65+
'SORTABLE',
66+
],
67+
],
68+
'num_docs',
69+
'0',
70+
'max_doc_id',
71+
'0',
72+
'num_terms',
73+
'0',
74+
'num_records',
75+
'0',
76+
'inverted_sz_mb',
77+
'0',
78+
'vector_index_sz_mb',
79+
'0',
80+
'total_inverted_index_blocks',
81+
'0',
82+
'offset_vectors_sz_mb',
83+
'0',
84+
'doc_table_size_mb',
85+
'0',
86+
'sortable_values_size_mb',
87+
'0',
88+
'key_table_size_mb',
89+
'0',
90+
'records_per_doc_avg',
91+
'-nan',
92+
'bytes_per_record_avg',
93+
'-nan',
94+
'offsets_per_term_avg',
95+
'-nan',
96+
'offset_bits_per_record_avg',
97+
'-nan',
98+
'hash_indexing_failures',
99+
'0',
100+
'indexing',
101+
'0',
102+
'percent_indexed',
103+
'1',
104+
'gc_stats',
105+
[
106+
'bytes_collected',
107+
'0',
108+
'total_ms_run',
109+
'0',
110+
'total_cycles',
111+
'0',
112+
'average_cycle_time_ms',
113+
'-nan',
114+
'last_run_time_ms',
115+
'0',
116+
'gc_numeric_trees_missed',
117+
'0',
118+
'gc_blocks_denied',
119+
'0',
120+
],
121+
'cursor_stats',
122+
[
123+
'global_idle',
124+
0,
125+
'global_total',
126+
0,
127+
'index_capacity',
128+
128,
129+
'index_total',
130+
0,
131+
],
132+
'dialect_stats',
133+
[
134+
'dialect_1',
135+
'0',
136+
'dialect_2',
137+
'0',
138+
]
139+
];
140+
141+
export const mockFtInfoAnalyticsData = {
142+
attributes: [
143+
{
144+
type: 'TEXT',
145+
weight: '1',
146+
},
147+
{
148+
nostem: true,
149+
type: 'TEXT',
150+
weight: '1',
151+
},
152+
{
153+
type: 'TEXT',
154+
weight: '1',
155+
},
156+
{
157+
noindex: true,
158+
type: 'NUMERIC',
159+
},
160+
{
161+
casesensitive: true,
162+
sortable: true,
163+
type: 'TAG',
164+
},
165+
],
166+
default_score: '1',
167+
key_type: 'HASH',
168+
max_doc_id: '0',
169+
num_docs: '0',
170+
num_records: '0',
171+
num_terms: '0',
172+
dialect_stats: {
173+
dialect_1: '0',
174+
dialect_2: '0',
175+
}
176+
};
177+
178+
type InfoReplyRaw = string | number | InfoReplyRaw[];
179+
export const replyToBuffer = (input: InfoReplyRaw[]) => {
180+
if (isArray(input)) {
181+
return input.map(replyToBuffer);
182+
}
183+
184+
return isString(input) ? Buffer.from(input) : input;
185+
};

redisinsight/api/src/modules/cli/services/cli-analytics/cli-analytics.service.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ describe('CliAnalyticsService', () => {
8484
});
8585
});
8686

87+
describe('sendCliClientCreatedEvent', () => {
88+
it('should emit CliIndexInfoSubmitted event', () => {
89+
service.sendIndexInfoEvent(databaseId, mockCustomData);
90+
91+
expect(sendEventMethod).toHaveBeenCalledWith(
92+
TelemetryEvents.CliIndexInfoSubmitted,
93+
{
94+
databaseId,
95+
...mockCustomData,
96+
},
97+
);
98+
});
99+
it('should not fail and should not emit when there is no data', () => {
100+
service.sendIndexInfoEvent(databaseId, null);
101+
102+
expect(sendEventMethod).not.toHaveBeenCalled();
103+
});
104+
});
105+
87106
describe('sendCliClientCreatedEvent', () => {
88107
it('should emit CliClientCreated event', () => {
89108
service.sendClientCreatedEvent(databaseId, mockCustomData);

redisinsight/api/src/modules/cli/services/cli-analytics/cli-analytics.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,12 @@ export class CliAnalyticsService extends CommandTelemetryBaseService {
7878

7979
sendIndexInfoEvent(
8080
databaseId: string,
81-
additionalData: object = {},
81+
additionalData: object,
8282
): void {
83+
if (!additionalData) {
84+
return;
85+
}
86+
8387
try {
8488
this.sendEvent(
8589
TelemetryEvents.CliIndexInfoSubmitted,

redisinsight/api/src/modules/cli/services/cli-business/cli-business.service.spec.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
mockCliClientMetadata,
1414
mockDatabaseClientFactory,
1515
mockStandaloneRedisClient,
16-
mockClusterRedisClient,
16+
mockClusterRedisClient, mockRedisFtInfoReply, mockFtInfoAnalyticsData,
1717
} from 'src/__mocks__';
1818
import {
1919
CommandExecutionStatus,
@@ -232,6 +232,34 @@ describe('CliBusinessService', () => {
232232
});
233233

234234
describe('sendCommand', () => {
235+
it('should successfully execute ft.info command', async () => {
236+
const dto: SendCommandDto = { command: 'ft.info idx' };
237+
const formatSpy = jest.spyOn(rawFormatter, 'format');
238+
const mockResult: SendCommandResponse = {
239+
response: mockRedisFtInfoReply,
240+
status: CommandExecutionStatus.Success,
241+
};
242+
when(standaloneClient.sendCommand)
243+
.calledWith(['ft.info', 'idx'], expect.anything())
244+
.mockReturnValue(mockRedisFtInfoReply);
245+
246+
const result = await service.sendCommand(mockCliClientMetadata, dto);
247+
248+
expect(result).toEqual(mockResult);
249+
expect(formatSpy).toHaveBeenCalled();
250+
expect(analyticsService.sendCommandExecutedEvent).toHaveBeenCalledWith(
251+
mockCliClientMetadata.databaseId,
252+
{
253+
command: 'ft.info',
254+
outputFormat: CliOutputFormatterTypes.Raw,
255+
},
256+
);
257+
expect(analyticsService.sendIndexInfoEvent).toHaveBeenCalledWith(
258+
mockCliClientMetadata.databaseId,
259+
mockFtInfoAnalyticsData,
260+
);
261+
});
262+
235263
it('should successfully execute command (RAW format)', async () => {
236264
const dto: SendCommandDto = { command: mockMemoryUsageCommand };
237265
const formatSpy = jest.spyOn(rawFormatter, 'format');

redisinsight/api/src/modules/workbench/providers/workbench-commands.executor.spec.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Test, TestingModule } from '@nestjs/testing';
22
import { get } from 'lodash';
33
import {
4-
mockDatabaseClientFactory,
4+
mockDatabaseClientFactory, mockFtInfoAnalyticsData, mockRedisFtInfoReply,
55
mockStandaloneRedisClient,
66
mockWorkbenchAnalyticsService,
77
mockWorkbenchClientMetadata,
@@ -74,6 +74,37 @@ describe('WorkbenchCommandsExecutor', () => {
7474

7575
describe('sendCommand', () => {
7676
describe('sendCommandForStandalone', () => {
77+
it('should successfully send ft.info', async () => {
78+
client.sendCommand.mockResolvedValueOnce(mockRedisFtInfoReply);
79+
80+
const result = await service.sendCommand(client, {
81+
command: 'ft.info idx',
82+
mode: RunQueryMode.Raw,
83+
});
84+
85+
expect(result).toEqual([{
86+
response: mockRedisFtInfoReply,
87+
status: mockCommandExecutionResult.status,
88+
}]);
89+
90+
expect(mockAnalyticsService.sendCommandExecutedEvents).toHaveBeenCalledWith(
91+
mockWorkbenchClientMetadata.databaseId,
92+
[
93+
{
94+
response: mockRedisFtInfoReply,
95+
status: CommandExecutionStatus.Success,
96+
},
97+
],
98+
{
99+
command: 'ft.info',
100+
rawMode: true,
101+
},
102+
);
103+
expect(mockAnalyticsService.sendIndexInfoEvent).toHaveBeenCalledWith(
104+
mockWorkbenchClientMetadata.databaseId,
105+
mockFtInfoAnalyticsData,
106+
);
107+
});
77108
it('should successfully send command for standalone', async () => {
78109
client.sendCommand.mockResolvedValueOnce('OK');
79110

redisinsight/api/src/modules/workbench/services/workbench-analytics/workbench-analytics.service.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,32 @@ describe('WorkbenchAnalyticsService', () => {
8282
});
8383
});
8484

85+
describe('sendIndexInfoEvent', () => {
86+
it('should emit index info event', async () => {
87+
service.sendIndexInfoEvent(
88+
instanceId,
89+
{
90+
any: 'fields',
91+
},
92+
);
93+
94+
expect(sendEventMethod).toHaveBeenCalledWith(
95+
TelemetryEvents.WorkbenchIndexInfoSubmitted,
96+
{
97+
databaseId: instanceId,
98+
any: 'fields',
99+
},
100+
);
101+
});
102+
it('should not fail and should not emit when no data to send', async () => {
103+
service.sendIndexInfoEvent(
104+
instanceId,
105+
null,
106+
);
107+
108+
expect(sendEventMethod).not.toHaveBeenCalled();
109+
});
110+
});
85111
describe('sendCommandExecutedEvents', () => {
86112
it('should emit multiple events', async () => {
87113
await service.sendCommandExecutedEvents(

redisinsight/api/src/modules/workbench/services/workbench-analytics/workbench-analytics.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ export class WorkbenchAnalyticsService extends CommandTelemetryBaseService {
2323

2424
sendIndexInfoEvent(
2525
databaseId: string,
26-
additionalData: object = {},
26+
additionalData: object,
2727
): void {
28+
if (!additionalData) {
29+
return;
30+
}
31+
2832
try {
2933
this.sendEvent(
3034
TelemetryEvents.WorkbenchIndexInfoSubmitted,

0 commit comments

Comments
 (0)