@@ -15,6 +15,7 @@ import { CommandExecutionResult } from 'src/modules/workbench/models/command-exe
15
15
import { CommandExecutionStatus } from 'src/modules/cli/dto/cli.dto' ;
16
16
import { BadRequestException , InternalServerErrorException } from '@nestjs/common' ;
17
17
import ERROR_MESSAGES from 'src/constants/error-messages' ;
18
+ import { CreateCommandExecutionsDto } from 'src/modules/workbench/dto/create-command-executions.dto' ;
18
19
import { WorkbenchAnalyticsService } from './services/workbench-analytics/workbench-analytics.service' ;
19
20
20
21
const mockClientOptions : IFindRedisClientInstanceByOptions = {
@@ -31,6 +32,13 @@ const mockCreateCommandExecutionDto: CreateCommandExecutionDto = {
31
32
role : ClusterNodeRole . All ,
32
33
mode : RunQueryMode . ASCII ,
33
34
} ;
35
+ const mockCreateCommandExecutionsDto : CreateCommandExecutionsDto = {
36
+ commands : [
37
+ mockCreateCommandExecutionDto . command ,
38
+ mockCreateCommandExecutionDto . command ,
39
+ ] ,
40
+ ...mockCreateCommandExecutionDto ,
41
+ } ;
34
42
35
43
const mockCommandExecutionResults : CommandExecutionResult [ ] = [
36
44
new CommandExecutionResult ( {
@@ -43,16 +51,20 @@ const mockCommandExecutionResults: CommandExecutionResult[] = [
43
51
} ,
44
52
} ) ,
45
53
] ;
46
- const mockCommandExecution : CommandExecution = new CommandExecution ( {
54
+ const mockCommandExecutionToRun : CommandExecution = new CommandExecution ( {
47
55
...mockCreateCommandExecutionDto ,
48
56
databaseId : mockStandaloneDatabaseEntity . id ,
57
+ } ) ;
58
+
59
+ const mockCommandExecution : CommandExecution = new CommandExecution ( {
60
+ ...mockCommandExecutionToRun ,
49
61
id : uuidv4 ( ) ,
50
62
createdAt : new Date ( ) ,
51
63
result : mockCommandExecutionResults ,
52
64
} ) ;
53
65
54
66
const mockCommandExecutionProvider = ( ) => ( {
55
- create : jest . fn ( ) ,
67
+ createMany : jest . fn ( ) ,
56
68
getList : jest . fn ( ) ,
57
69
getOne : jest . fn ( ) ,
58
70
delete : jest . fn ( ) ,
@@ -91,13 +103,8 @@ describe('WorkbenchService', () => {
91
103
92
104
describe ( 'createCommandExecution' , ( ) => {
93
105
it ( 'should successfully execute command and save it' , async ( ) => {
94
- workbenchCommandsExecutor . sendCommand . mockResolvedValueOnce ( mockCommandExecutionResults ) ;
95
- commandExecutionProvider . create . mockResolvedValueOnce ( mockCommandExecution ) ;
96
-
97
- const result = await service . createCommandExecution ( mockClientOptions , mockCreateCommandExecutionDto ) ;
98
-
99
- expect ( result ) . toBeInstanceOf ( CommandExecution ) ;
100
- expect ( result ) . toEqual ( mockCommandExecution ) ;
106
+ expect ( await service . createCommandExecution ( mockClientOptions , mockCreateCommandExecutionDto ) )
107
+ . toEqual ( mockCommandExecutionToRun ) ;
101
108
} ) ;
102
109
it ( 'should save result as unsupported command message' , async ( ) => {
103
110
workbenchCommandsExecutor . sendCommand . mockResolvedValueOnce ( mockCommandExecutionResults ) ;
@@ -108,9 +115,7 @@ describe('WorkbenchService', () => {
108
115
mode : RunQueryMode . ASCII ,
109
116
} ;
110
117
111
- await service . createCommandExecution ( mockClientOptions , dto ) ;
112
-
113
- expect ( commandExecutionProvider . create ) . toHaveBeenCalledWith ( {
118
+ expect ( await service . createCommandExecution ( mockClientOptions , dto ) ) . toEqual ( {
114
119
...dto ,
115
120
databaseId : mockClientOptions . instanceId ,
116
121
result : [
@@ -137,18 +142,35 @@ describe('WorkbenchService', () => {
137
142
expect ( e ) . toBeInstanceOf ( BadRequestException ) ;
138
143
}
139
144
} ) ;
140
- it ( 'should throw an error from command execution provider (create)' , async ( ) => {
141
- workbenchCommandsExecutor . sendCommand . mockResolvedValueOnce ( mockCommandExecutionResults ) ;
142
- commandExecutionProvider . create . mockRejectedValueOnce ( new InternalServerErrorException ( 'db error' ) ) ;
145
+ } ) ;
143
146
144
- const dto = {
145
- ...mockCommandExecutionResults ,
146
- command : 'scan 0' ,
147
- mode : RunQueryMode . ASCII ,
148
- } ;
147
+ describe ( 'createCommandExecutions' , ( ) => {
148
+ it ( 'should successfully execute commands and save them' , async ( ) => {
149
+ workbenchCommandsExecutor . sendCommand . mockResolvedValueOnce (
150
+ [ mockCommandExecutionResults , mockCommandExecutionResults ] ,
151
+ ) ;
152
+ commandExecutionProvider . createMany . mockResolvedValueOnce ( [ mockCommandExecution , mockCommandExecution ] ) ;
153
+
154
+ const result = await service . createCommandExecutions ( mockClientOptions , mockCreateCommandExecutionsDto ) ;
155
+
156
+ expect ( result ) . toEqual ( [ mockCommandExecution , mockCommandExecution ] ) ;
157
+ } ) ;
158
+ it ( 'should throw an error when command execution failed' , async ( ) => {
159
+ workbenchCommandsExecutor . sendCommand . mockRejectedValueOnce ( new BadRequestException ( 'error' ) ) ;
149
160
150
161
try {
151
- await service . createCommandExecution ( mockClientOptions , dto ) ;
162
+ await service . createCommandExecutions ( mockClientOptions , mockCreateCommandExecutionsDto ) ;
163
+ fail ( ) ;
164
+ } catch ( e ) {
165
+ expect ( e ) . toBeInstanceOf ( BadRequestException ) ;
166
+ }
167
+ } ) ;
168
+ it ( 'should throw an error from command execution provider (create)' , async ( ) => {
169
+ workbenchCommandsExecutor . sendCommand . mockResolvedValueOnce ( [ mockCommandExecutionResults ] ) ;
170
+ commandExecutionProvider . createMany . mockRejectedValueOnce ( new InternalServerErrorException ( 'db error' ) ) ;
171
+
172
+ try {
173
+ await service . createCommandExecutions ( mockClientOptions , mockCreateCommandExecutionsDto ) ;
152
174
fail ( ) ;
153
175
} catch ( e ) {
154
176
expect ( e ) . toBeInstanceOf ( InternalServerErrorException ) ;
0 commit comments