Skip to content

Commit

Permalink
Issue #SB-16090 merge: Merge pull request #231 from Ajoymaity/release…
Browse files Browse the repository at this point in the history
…-2.7.0

Issue #SB-16090 test: Unit test for Profile export handler
  • Loading branch information
AmiableAnil authored Jan 7, 2020
2 parents c6d75e9 + 8194a2d commit 69feaba
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 1 deletion.
82 changes: 82 additions & 0 deletions src/profile/handler/export/clean-up-export-file.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { CleanupExportedFile } from './clean-up-exported-file';
import { DbService, ErrorCode } from '../../..';
import { FileService } from '../../../util/file/def/file-service';
import { ExportProfileContext } from '../../def/export-profile-context';
import { of } from 'rxjs';

describe('CleanupExportedFile', () => {
let cleanupExportedFile: CleanupExportedFile;
const mockFileService: Partial<FileService> = {};
const mockDbService: Partial<DbService> = {};

beforeAll(() => {
cleanupExportedFile = new CleanupExportedFile(
mockDbService as DbService,
mockFileService as FileService
);
});

beforeEach(() => {
jest.clearAllMocks();
});

it('should be create a instance of cleanupExportedFile', () => {
// arrange
// act
// assert
expect(cleanupExportedFile).toBeTruthy();
});

it('should return all table details and remove table', (done) => {
// arrange
const request: ExportProfileContext = {
userIds: ['sample-user-id', 'sample-user-id'],
groupIds: ['group-1', 'group-2'],
destinationFolder: 'dest/folder/file',
destinationDBFilePath: 'dest/db/file/path',
size: '32MB'
};
mockDbService.execute = jest.fn(() => of([{ tableName: 'sample-table', name: 'db' }]));
mockDbService.read = jest.fn(() => of([{}]));
mockDbService.insert = jest.fn(() => of(2));
mockFileService.getMetaData = jest.fn(() => Promise.resolve({ size: 4 }));
mockFileService.removeFile = jest.fn(() => Promise.resolve({ success: true }));
// act
cleanupExportedFile.execute(request).then(() => {
expect(mockDbService.execute).toHaveBeenCalled();
expect(mockDbService.read).toHaveBeenCalled();
expect(mockDbService.insert).toHaveBeenCalled();
expect(mockFileService.getMetaData).toHaveBeenCalled();
expect(mockFileService.removeFile).toHaveBeenCalled();
done();
}).catch((e) => {
console.error(e);
fail(e);
});
});

it('should return errorMesg for for failed removeFile', (done) => {
// arrange
const request: ExportProfileContext = {
userIds: [],
groupIds: [],
destinationFolder: '',
destinationDBFilePath: '',
size: '32MB'
};
mockDbService.execute = jest.fn(() => of([{ tableName: 'sample-table', name: 'db' }]));
mockDbService.read = jest.fn(() => of([{}]));
mockDbService.insert = jest.fn(() => of(2));
mockFileService.getMetaData = jest.fn(() => Promise.resolve({ size: 4 }));
mockFileService.removeFile = jest.fn(() => Promise.reject({ errorMesg: '' }));
// act
cleanupExportedFile.execute(request).catch((e) => {
expect(mockDbService.execute).toHaveBeenCalled();
expect(mockDbService.insert).toHaveBeenCalled();
expect(mockFileService.getMetaData).toHaveBeenCalled();
expect(mockFileService.removeFile).toHaveBeenCalled();
expect(e._errorMesg).toBe(ErrorCode.EXPORT_FAILED);
done();
});
});
});
39 changes: 39 additions & 0 deletions src/profile/handler/export/copy-database.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { CopyDatabase } from './copy-database';
import { DbService } from '../../..';
import { ExportProfileContext } from '../../def/export-profile-context';
import { of } from 'rxjs';

describe('CopyDatabase', () => {
let copyDatabase: CopyDatabase;
const mockDbService: Partial<DbService> = {};

beforeAll(() => {
copyDatabase = new CopyDatabase(
mockDbService as DbService
);
});

beforeEach(() => {
jest.clearAllMocks();
});

it('should be create a instance of CopyDatabase', () => {
expect(copyDatabase).toBeTruthy();
});

it('should copy file from destination file path', () => {
// arrange
const request: ExportProfileContext = {
userIds: ['sample-user-id'],
groupIds: ['group-1', 'group-2'],
destinationFolder: 'dest/folder/file',
destinationDBFilePath: 'dest/db/file/path',
size: '32MB'
};
mockDbService.copyDatabase = jest.fn(() => of({}));
// act
copyDatabase.execute(request);
// assert
expect(mockDbService.copyDatabase).toHaveBeenCalled();
});
});
77 changes: 77 additions & 0 deletions src/profile/handler/export/create-metadata.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { CreateMetaData } from './create-metadata';
import { DbService, DeviceInfo } from '../../..';
import { FileService } from '../../../util/file/def/file-service';
import { ExportProfileContext } from '../../def/export-profile-context';
import { of } from 'rxjs';

describe('CreateMetaData', () => {
let createMetaData: CreateMetaData;
const mockDbService: Partial<DbService> = {};
const mockFileService: Partial<FileService> = {};
const mockDeviceInfo: Partial<DeviceInfo> = {};

beforeAll(() => {
createMetaData = new CreateMetaData(
mockDbService as DbService,
mockFileService as FileService,
mockDeviceInfo as DeviceInfo
);
});

beforeEach(() => {
jest.clearAllMocks();
});

it('should create a instance of CreateMetaData', () => {
expect(createMetaData).toBeTruthy();
});

it('should generate metadata and populate metadata', (done) => {
// arrange
const request: ExportProfileContext = {
userIds: ['sample-user-id'],
groupIds: ['group-1', 'group-2'],
destinationFolder: 'dest/folder/file',
destinationDBFilePath: 'dest/db/file/path',
size: '32MB'
};
mockDeviceInfo.getDeviceID = jest.fn(() => 'device-id');
mockDbService.open = jest.fn(() => Promise.resolve(undefined));
mockDbService.execute = jest.fn(() => of({}));
mockDbService.insert = jest.fn(() => of(1));
// act
createMetaData.execute(request).then(() => {
// assert
expect(mockDeviceInfo.getDeviceID).toHaveBeenCalled();
expect(mockDbService.open).toHaveBeenCalledWith(request.destinationDBFilePath);
expect(mockDbService.execute).toHaveBeenCalled();
expect(mockDbService.insert).toHaveBeenCalled();
done();
}).catch((e) => {
console.error(e);
fail(e);
});
});

it('should generate metadata and populate metadata for error part', (done) => {
// arrange
const request: ExportProfileContext = {
userIds: ['sample-user-id'],
groupIds: ['group-1', 'group-2'],
destinationFolder: 'dest/folder/file',
destinationDBFilePath: 'dest/db/file/path',
size: '32MB'
};
mockDeviceInfo.getDeviceID = jest.fn(() => 'device-id');
mockDbService.open = jest.fn(() => Promise.reject(undefined));
mockDbService.execute = jest.fn(() => of({}));
mockDbService.insert = jest.fn(() => of(1));
// act
createMetaData.execute(request).catch((e) => {
// assert
expect(mockDeviceInfo.getDeviceID).toHaveBeenCalled();
expect(mockDbService.open).toHaveBeenCalledWith(request.destinationDBFilePath);
done();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {GenerateProfileExportTelemetry} from './generate-profile-export-telemetry';
import { DbService, ImportTelemetryContext } from '../../..';
import { of } from 'rxjs';
import { TelemetryLogger } from '../../../telemetry/util/telemetry-logger';
import { share } from 'rxjs/operators';
import { ProfileServiceImpl } from '../../impl/profile-service-impl';
import { TelemetryService } from '../../../telemetry';

jest.mock('../../../telemetry/util/telemetry-logger');

describe('GenerateProfileExportTelemetry', () => {
let generateProfileExportTelemetry: GenerateProfileExportTelemetry;
const mockDbService: Partial<DbService> = {};
const mockTelemetryService: Partial<TelemetryService> = {
share: jest.fn(() => of(undefined))
};
(TelemetryLogger as any)['log'] = mockTelemetryService;

beforeAll(() => {
generateProfileExportTelemetry = new GenerateProfileExportTelemetry(
mockDbService as DbService
);
});

beforeEach(() => {
jest.clearAllMocks();
});

it('should create a instance of GenerateProfileExportTelemetry', () => {
expect(generateProfileExportTelemetry).toBeTruthy();
});

it('should generate export profile telemetry', (done) => {
// arrange
const request: ImportTelemetryContext = {
sourceDBFilePath: 'src/db/path',
metadata: { 'index': 1 }
};
mockDbService.read = jest.fn(() => of([{
imported_id: 'sample-imported_id',
device_id: 'sample-device_id',
count: 'no-of-count'
}]));
// act
generateProfileExportTelemetry.execute(request).then(() => {
// assert
expect(mockDbService.read).toHaveBeenCalled();
expect(mockTelemetryService.share).toHaveBeenCalled();
done();
}).catch((e) => {
console.error(e);
fail(e);
});
});
});
42 changes: 42 additions & 0 deletions src/profile/handler/export/get-epar-file-path.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {GetEparFilePath} from './get-epar-file-path';
import { FileService } from '../../../util/file/def/file-service';
import { ExportProfileContext } from '../../def/export-profile-context';

describe('GetEparFilePath', () => {
let getEparFilePath: GetEparFilePath;
const mockFileService: Partial<FileService> = {};
Date.now = jest.fn(() => 1487076708000);

beforeAll(() => {
getEparFilePath = new GetEparFilePath(
mockFileService as FileService
);
});

beforeEach(() => {
jest.clearAllMocks();
});

it('should be create a instance of GetEparFilePath', () => {
expect(getEparFilePath).toBeTruthy();
});

it('should create a directory and a file', (done) => {
// arrange
const request: ExportProfileContext = {
userIds: ['sample-user-id', 'sample-user-id'],
groupIds: ['group-1', 'group-2'],
destinationFolder: 'dest/folder/file',
destinationDBFilePath: 'dest/db/file/path',
size: '32MB'
};
mockFileService.createDir = jest.fn(() => Promise.resolve({}));
mockFileService.createFile = jest.fn(() => Promise.resolve({}));
// act
getEparFilePath.execute(request).then(() => {
// assert
expect(mockFileService.createDir).toHaveBeenCalled();
done();
});
});
});
2 changes: 1 addition & 1 deletion src/profile/handler/export/get-epar-file-path.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Response} from '../../../api';
import {FileService} from '../../../util/file/def/file-service';
import {ExportProfileContext} from '../../def/export-profile-context';
import dayjs from 'dayjs';
import * as dayjs from 'dayjs';

export class GetEparFilePath {
constructor(private fileService: FileService) {
Expand Down

0 comments on commit 69feaba

Please sign in to comment.