Skip to content

Commit e7b8caf

Browse files
author
arthosofteq
authored
Merge pull request #561 from RedisInsight/add-utests
add unit tests + fix minor bug
2 parents f036d89 + b96b0ad commit e7b8caf

File tree

8 files changed

+485
-177
lines changed

8 files changed

+485
-177
lines changed

redisinsight/api/src/__mocks__/profiler.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ import { MockType } from 'src/__mocks__/common';
88
import { TelemetryEvents } from 'src/constants';
99
import * as MockedSocket from 'socket.io-mock';
1010
import { IMonitorData } from 'src/modules/profiler/interfaces/monitor-data.interface';
11+
import { LogFileProvider } from 'src/modules/profiler/providers/log-file.provider';
12+
import { MonitorSettings } from 'src/modules/profiler/models/monitor-settings';
1113

1214
export const mockMonitorDataItemEmitted = {
1315
time: '14239988881.12341',
16+
args: ['set', 'foo', 'bar'],
1417
source: '127.0.0.1',
1518
database: 0,
16-
args: ['set', 'foo', 'bar'],
1719
};
1820

1921
export const mockMonitorDataItem: IMonitorData = {
2022
...mockMonitorDataItemEmitted,
21-
shardOptions: null,
23+
shardOptions: undefined,
2224
};
2325

2426
export const mockSocket = new MockedSocket();
@@ -88,7 +90,20 @@ export const mockLogFile: LogFile = new LogFile('instanceid', testLogFileId, moc
8890
mockLogFile['getWriteStream'] = jest.fn();
8991
mockLogFile['addProfilerClient'] = jest.fn();
9092
mockLogFile['removeProfilerClient'] = jest.fn();
93+
mockLogFile['setAlias'] = jest.fn();
9194
mockLogFile['destroy'] = jest.fn();
9295

93-
export const mockProfilerClientId = 'profiler-client-id';
94-
export const mockProfilerClient: ProfilerClient = new ProfilerClient(mockProfilerClientId, mockSocket);
96+
export const mockProfilerClient: ProfilerClient = new ProfilerClient(mockSocket.id, mockSocket);
97+
mockProfilerClient['handleOnData'] = jest.fn();
98+
mockProfilerClient['handleOnDisconnect'] = jest.fn();
99+
100+
export const mockMonitorSettings: MonitorSettings = {
101+
logFileId: testLogFileId,
102+
};
103+
104+
export const mockLogFileProvider: MockType<LogFileProvider> = {
105+
getOrCreate: jest.fn(),
106+
get: jest.fn(),
107+
getDownloadData: jest.fn(),
108+
onModuleDestroy: jest.fn(),
109+
};

redisinsight/api/src/modules/profiler/models/log-file.spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ describe('LogFile', () => {
1616

1717
beforeEach(() => {
1818
logFile = new LogFile(mockLogFile.instanceId, mockLogFile.id, mockProfilerAnalyticsEvents);
19-
jest.resetAllMocks();
2019
});
2120

2221
it('Initialization', () => {
@@ -98,9 +97,9 @@ describe('LogFile', () => {
9897
expect(fs.existsSync(logFile['filePath'])).toEqual(false);
9998
const stream = logFile.getWriteStream();
10099
expect(stream['_writableState'].ended).toEqual(false);
101-
stream.write('somedata');
102-
await new Promise((r) => setTimeout(r, 500));
103-
expect(fs.existsSync(logFile['filePath'])).toEqual(true);
100+
stream.write('somedata', () => {
101+
expect(fs.existsSync(logFile['filePath'])).toEqual(true);
102+
});
104103
expect(logFile['writeStream']).toEqual(stream);
105104
await logFile.destroy();
106105
expect(logFile['writeStream']).toEqual(null);

redisinsight/api/src/modules/profiler/models/log-file.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export class LogFile {
4343
*/
4444
getWriteStream(): WriteStream {
4545
if (!this.writeStream) {
46+
fs.ensureFileSync(this.filePath);
4647
this.writeStream = fs.createWriteStream(this.filePath, { flags: 'a' });
4748
}
4849
this.writeStream.on('error', () => {});
@@ -54,6 +55,7 @@ export class LogFile {
5455
* Used to download file using http server
5556
*/
5657
getReadStream(): ReadStream {
58+
fs.ensureFileSync(this.filePath);
5759
const stream = fs.createReadStream(this.filePath);
5860
stream.once('end', () => {
5961
stream.destroy();

redisinsight/api/src/modules/profiler/models/profiler.client.spec.ts

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -57,53 +57,3 @@ describe('ProfilerClient', () => {
5757
expect(mockLogEmitter.removeProfilerClient).toHaveBeenCalledWith(profilerClient.id);
5858
});
5959
});
60-
61-
// import { WsException } from '@nestjs/websockets';
62-
// import * as MockedSocket from 'socket.io-mock';
63-
// import ERROR_MESSAGES from 'src/constants/error-messages';
64-
// import { MonitorGatewayServerEvents } from 'src/modules/profiler/constants/events';
65-
// import { ProfilerClient } from './profiler.client';
66-
// import { IOnDatePayload } from '../interfaces/client-monitor-observer.interface';
67-
//
68-
// describe('ClientMonitorObserver', () => {
69-
// let socketClient;
70-
//
71-
// beforeEach(() => {
72-
// socketClient = new MockedSocket();
73-
// socketClient.id = '123';
74-
// socketClient.emit = jest.fn();
75-
// });
76-
//
77-
// it.only('should be defined', () => {
78-
// const client = new ProfilerClient(socketClient.id, socketClient);
79-
//
80-
// expect(client.id).toEqual(socketClient.id);
81-
// });
82-
// it.only('should emit event on monitorData', async () => {
83-
// const client = new ProfilerClient(socketClient.id, socketClient);
84-
// const monitorData = {
85-
// // unix timestamp
86-
// time: `${(new Date()).getTime() / 1000}`,
87-
// source: '127.0.0.1:58612',
88-
// database: 0,
89-
// args: ['set', 'foo', 'bar'],
90-
// };
91-
// const payload: IOnDatePayload = { ...monitorData, shardOptions: { host: '127.0.0.1', port: 6379 } };
92-
//
93-
// client.handleOnData(payload);
94-
//
95-
// await new Promise((r) => setTimeout(r, 500));
96-
//
97-
// expect(socketClient.emit).toHaveBeenCalledWith(MonitorGatewayServerEvents.Data, [monitorData]);
98-
// });
99-
// it.only('should emit exception event', () => {
100-
// const client = new ProfilerClient(socketClient.id, socketClient);
101-
//
102-
// client.handleOnDisconnect();
103-
//
104-
// expect(socketClient.emit).toHaveBeenCalledWith(
105-
// MonitorGatewayServerEvents.Exception,
106-
// new WsException(ERROR_MESSAGES.NO_CONNECTION_TO_REDIS_DB),
107-
// );
108-
// });
109-
// });

0 commit comments

Comments
 (0)