-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #SB-16090 merge: Merge pull request #231 from Ajoymaity/release…
…-2.7.0 Issue #SB-16090 test: Unit test for Profile export handler
- Loading branch information
Showing
6 changed files
with
296 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
55 changes: 55 additions & 0 deletions
55
src/profile/handler/export/generate-profile-export-telemetry.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters