Skip to content

Commit 2d25aec

Browse files
#RI-2509-add tests
1 parent 5aa8b74 commit 2d25aec

File tree

16 files changed

+118
-77
lines changed

16 files changed

+118
-77
lines changed

redisinsight/api/src/modules/workbench/models/command-execution-result.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ export class CommandExecutionResult {
2525
})
2626
response: any;
2727

28-
@ApiPropertyOptional({
29-
type: Boolean,
30-
description: 'Result did not stored in db',
31-
})
32-
isNotStored?: boolean;
33-
3428
@ApiPropertyOptional({
3529
type: () => ClusterNode,
3630
description: 'Redis Cluster Node info',

redisinsight/api/src/modules/workbench/models/command-execution.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ export class CommandExecution {
7070
@Expose()
7171
result: CommandExecutionResult[];
7272

73+
@ApiPropertyOptional({
74+
type: Boolean,
75+
description: 'Result did not stored in db',
76+
})
77+
@Expose()
78+
isNotStored?: boolean;
79+
7380
@ApiPropertyOptional({
7481
description: 'Nodes roles where command was executed',
7582
default: ClusterNodeRole.All,

redisinsight/api/src/modules/workbench/plugins.service.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ClusterNodeRole,
88
CreateCommandExecutionDto,
99
RunQueryMode,
10+
ResultsMode,
1011
} from 'src/modules/workbench/dto/create-command-execution.dto';
1112
import { CommandExecutionResult } from 'src/modules/workbench/models/command-execution-result';
1213
import { CommandExecutionStatus } from 'src/modules/cli/dto/cli.dto';
@@ -34,6 +35,7 @@ const mockCreateCommandExecutionDto: CreateCommandExecutionDto = {
3435
},
3536
role: ClusterNodeRole.All,
3637
mode: RunQueryMode.ASCII,
38+
resultsMode: ResultsMode.Default,
3739
};
3840

3941
const mockCommandExecutionResults: CommandExecutionResult[] = [

redisinsight/api/src/modules/workbench/providers/command-execution.provider.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ export class CommandExecutionProvider {
3131
*/
3232
async createMany(commandExecutions: Partial<CommandExecution>[]): Promise<CommandExecution[]> {
3333
// todo: limit by 30 max to insert
34-
let entities = await Promise.all(commandExecutions.map(async (commandExecution) => {
34+
let notStoredIndexes = {}
35+
let entities = await Promise.all(commandExecutions.map(async (commandExecution, idx) => {
3536
const entity = plainToClass(CommandExecutionEntity, commandExecution);
3637

3738
// Do not store command execution result that exceeded limitation
38-
if (JSON.stringify(entity.result).length > WORKBENCH_CONFIG.maxResultSize) {
39-
// eslint-disable-next-line no-param-reassign
40-
commandExecution.result[0].isNotStored = true;
39+
// if (JSON.stringify(entity.result).length > WORKBENCH_CONFIG.maxResultSize) {
40+
if (JSON.stringify(entity.result).length > 1000) {
41+
notStoredIndexes[idx] = true;
4142
entity.result = JSON.stringify([
4243
{
4344
status: CommandExecutionStatus.Success,
@@ -60,10 +61,11 @@ export class CommandExecutionProvider {
6061
result: commandExecutions[idx].result,
6162
nodeOptions: commandExecutions[idx].nodeOptions,
6263
summary: commandExecutions[idx].summary,
64+
isNotStored: notStoredIndexes[idx] || false,
6365
},
6466
)),
6567
);
66-
68+
this.logger.log(response[0].isNotStored)
6769
// cleanup history and ignore error if any
6870
try {
6971
await this.cleanupDatabaseHistory(entities[0].databaseId);

redisinsight/api/src/modules/workbench/workbench.service.spec.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ const mockCreateCommandExecutionDto: CreateCommandExecutionDto = {
3535
mode: RunQueryMode.ASCII,
3636
resultsMode: ResultsMode.Default,
3737
};
38+
39+
const mockCommands = ["set 1 1", "get 1"];
40+
41+
const mockCreateCommandExecutionDtoWithGroupMode: CreateCommandExecutionsDto = {
42+
commands: mockCommands,
43+
nodeOptions: {
44+
host: '127.0.0.1',
45+
port: 7002,
46+
enableRedirection: true,
47+
},
48+
role: ClusterNodeRole.All,
49+
mode: RunQueryMode.ASCII,
50+
resultsMode: ResultsMode.GroupMode,
51+
};
52+
3853
const mockCreateCommandExecutionsDto: CreateCommandExecutionsDto = {
3954
commands: [
4055
mockCreateCommandExecutionDto.command,
@@ -66,6 +81,21 @@ const mockCommandExecution: CommandExecution = new CommandExecution({
6681
result: mockCommandExecutionResults,
6782
});
6883

84+
const mockSendCommandResultSuccess = { response: "1", status: "success" };
85+
const mockSendCommandResultFail = { response: "error", status: "fail" };
86+
87+
const mockCommandExecutionWithGroupMode = {
88+
mode: "ASCII",
89+
commands: mockCommands,
90+
resultsMode: "GROUP_MODE",
91+
databaseId: "d05043d0 - 0d12- 4ce1-9ca3 - 30c6d7e391ea",
92+
summary: { "total": 2, "success": 1, "fail": 1 },
93+
command: "set 1 1\r\nget 1",
94+
result: [{
95+
"status": "success", "response": [{ "response": "OK", "status": "success", "command": "set 1 1" }, { "response": "error", "status": "fail", "command": "get 1" }]
96+
}]
97+
}
98+
6999
const mockCommandExecutionProvider = () => ({
70100
createMany: jest.fn(),
71101
getList: jest.fn(),
@@ -158,6 +188,41 @@ describe('WorkbenchService', () => {
158188

159189
expect(result).toEqual([mockCommandExecution, mockCommandExecution]);
160190
});
191+
192+
it('should successfully execute commands and save in group mode view', async () => {
193+
when(workbenchCommandsExecutor.sendCommand)
194+
.calledWith(mockClientOptions, expect.anything())
195+
.mockResolvedValue([mockSendCommandResultSuccess]);
196+
197+
commandExecutionProvider.createMany.mockResolvedValueOnce([mockCommandExecutionWithGroupMode]);
198+
199+
const result = await service.createCommandExecutions(
200+
mockClientOptions,
201+
mockCreateCommandExecutionDtoWithGroupMode,
202+
);
203+
204+
expect(result).toEqual([mockCommandExecutionWithGroupMode]);
205+
});
206+
207+
it('should successfully execute commands with error and save summary', async () => {
208+
when(workbenchCommandsExecutor.sendCommand)
209+
.calledWith(mockClientOptions, {...mockCreateCommandExecutionDtoWithGroupMode, command: mockCommands[0]})
210+
.mockResolvedValue([mockSendCommandResultSuccess]);
211+
212+
when(workbenchCommandsExecutor.sendCommand)
213+
.calledWith(mockClientOptions, {...mockCreateCommandExecutionDtoWithGroupMode, command: mockCommands[1]})
214+
.mockResolvedValue([mockSendCommandResultFail]);
215+
216+
commandExecutionProvider.createMany.mockResolvedValueOnce([mockCommandExecutionWithGroupMode]);
217+
218+
const result = await service.createCommandExecutions(
219+
mockClientOptions,
220+
mockCreateCommandExecutionDtoWithGroupMode,
221+
);
222+
223+
expect(result).toEqual([mockCommandExecutionWithGroupMode]);
224+
});
225+
161226
it('should throw an error when command execution failed', async () => {
162227
workbenchCommandsExecutor.sendCommand.mockRejectedValueOnce(new BadRequestException('error'));
163228

redisinsight/ui/src/components/query-card/QueryCard.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { sendEventTelemetry, TelemetryEvent } from 'uiSrc/telemetry'
1717
import { toggleOpenWBResult } from 'uiSrc/slices/workbench/wb-results'
1818

1919
import QueryCardHeader from './QueryCardHeader'
20-
import QueryCardCliResult from './QueryCardCliResultWrapper'
20+
import QueryCardCliResultWrapper from './QueryCardCliResultWrapper'
2121
import QueryCardCliPlugin from './QueryCardCliPlugin'
2222
import QueryCardCommonResult, { CommonErrorResponse } from './QueryCardCommonResult'
2323

@@ -35,6 +35,7 @@ export interface Props {
3535
summary?: ResultsSummary
3636
createdAt?: Date
3737
loading?: boolean
38+
isNotStored?: boolean
3839
onQueryDelete: () => void
3940
onQueryReRun: () => void
4041
onQueryOpen: () => void
@@ -67,6 +68,7 @@ const QueryCard = (props: Props) => {
6768
onQueryReRun,
6869
loading,
6970
emptyCommand,
71+
isNotStored,
7072
} = props
7173

7274
const { visualizations = [] } = useSelector(appPluginsSelector)
@@ -175,11 +177,12 @@ const QueryCard = (props: Props) => {
175177
: (
176178
<>
177179
{resultsMode === ResultsMode.GroupMode && (
178-
<QueryCardCliResult
180+
<QueryCardCliResultWrapper
179181
loading={loading}
180182
query={command}
181183
resultsMode={resultsMode}
182184
result={result}
185+
isNotStored={isNotStored}
183186
data-testid="group-mode-card"
184187
/>
185188
)}
@@ -203,11 +206,12 @@ const QueryCard = (props: Props) => {
203206
</>
204207
)}
205208
{(viewTypeSelected === WBQueryType.Text) && (
206-
<QueryCardCliResult
209+
<QueryCardCliResultWrapper
207210
loading={loading}
208211
query={command}
209212
resultsMode={resultsMode}
210213
result={result}
214+
isNotStored={isNotStored}
211215
/>
212216
)}
213217
</>

redisinsight/ui/src/components/query-card/QueryCardCliDefaultResult/QueryCardCliDefaultResult.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import { instance, mock } from 'ts-mockito'
33
import { render } from 'uiSrc/utils/test-utils'
4-
import QueryCardCliDefaultResult, { Props, resultTestId } from './QueryCardCliDefaultResult'
4+
import QueryCardCliDefaultResult, { Props } from './QueryCardCliDefaultResult'
55

66
const mockedProps = mock<Props>()
77

@@ -20,7 +20,7 @@ describe('QueryCardCliDefaultResult', () => {
2020
<QueryCardCliDefaultResult {...instance(mockedProps)} result={mockResult} />
2121
)
2222

23-
const resultEl = queryByTestId(resultTestId)
23+
const resultEl = queryByTestId('query-cli-group-result')
2424

2525
expect(resultEl).toHaveTextContent('(nil)')
2626
})

redisinsight/ui/src/components/query-card/QueryCardCliDefaultResult/QueryCardCliDefaultResult.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ export interface Props {
88
result: Maybe<CommandExecutionResult[]>
99
}
1010

11-
export const resultTestId = 'query-cli-group-result'
12-
1311
const QueryCardCliGroupResult = (props: Props) => {
1412
const { result = [], query } = props
1513

1614
return (
17-
<div data-testid={resultTestId}>
15+
<div data-testid="query-cli-group-result">
1816
{result?.map(({ response, status }) =>
1917
cliParseTextResponse(response || '(nil)', query, status, CliPrefix.QueryCard))}
2018
</div>

redisinsight/ui/src/components/query-card/QueryCardCliGroupResult/QueryCardCliGroupResult.spec.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ describe('QueryCardCliGroupResult', () => {
1515
})
1616

1717
it('Should render result when result is undefined', () => {
18-
const mockResult = undefined
19-
20-
expect(render(<QueryCardCliGroupResult result={mockResult} />)).toBeTruthy()
18+
expect(render(<QueryCardCliGroupResult />)).toBeTruthy()
2119
})
2220
})

redisinsight/ui/src/components/query-card/QueryCardCliGroupResult/QueryCardCliGroupResult.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CommandExecutionResult } from 'uiSrc/slices/interfaces'
44
import { cliParseCommandsGroupResult, Maybe } from 'uiSrc/utils'
55

66
export interface Props {
7-
result: Maybe<CommandExecutionResult[]>
7+
result?: Maybe<CommandExecutionResult[]>
88
}
99

1010
const QueryCardCliGroupResult = (props: Props) => {

0 commit comments

Comments
 (0)