From dba4ddf39688e965a10fed7b4c4faf9212a03a7e Mon Sep 17 00:00:00 2001 From: Ajoymaity Date: Tue, 7 Jan 2020 20:20:41 +0530 Subject: [PATCH] Issue #SB-16090 test: Unit test for profile import handler --- .../generate-profile-import-telemetry.spec.ts | 52 +++++++++++++++++++ .../import/transport-assesments.spec.ts | 44 ++++++++++++++++ .../transport-framework-n-channel.spec.ts | 43 +++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 src/profile/handler/import/generate-profile-import-telemetry.spec.ts create mode 100644 src/profile/handler/import/transport-assesments.spec.ts create mode 100644 src/profile/handler/import/transport-framework-n-channel.spec.ts diff --git a/src/profile/handler/import/generate-profile-import-telemetry.spec.ts b/src/profile/handler/import/generate-profile-import-telemetry.spec.ts new file mode 100644 index 000000000..0cb920f93 --- /dev/null +++ b/src/profile/handler/import/generate-profile-import-telemetry.spec.ts @@ -0,0 +1,52 @@ +import { GenerateProfileImportTelemetry } from './generate-profile-import-telemetry'; +import { DbService, TelemetryService, ImportTelemetryContext } from '../../..'; +import { of } from 'rxjs'; +import { TelemetryLogger } from '../../../telemetry/util/telemetry-logger'; + +jest.mock('../../../telemetry/util/telemetry-logger'); + +describe('GenerateProfileImportTelemetry', () => { + let generateProfileImportTelemetry: GenerateProfileImportTelemetry; + const mockDbService: Partial = {}; + const mockTelemetryService: Partial = { + share: jest.fn(() => of(undefined)) + }; + (TelemetryLogger as any)['log'] = mockTelemetryService; + + beforeAll(() => { + generateProfileImportTelemetry = new GenerateProfileImportTelemetry( + mockDbService as DbService + ); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should be create a instance of GenerateProfileImportTelemetry', () => { + expect(generateProfileImportTelemetry).toBeTruthy(); + }); + + it('should generate import 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 + generateProfileImportTelemetry.execute(request).then(() => { + // assert + expect(mockDbService.read).toHaveBeenCalled(); + expect(mockTelemetryService.share).toHaveBeenCalled(); + done(); + }).catch((e) => { + console.error(e); + fail(e); + }); + }); +}); diff --git a/src/profile/handler/import/transport-assesments.spec.ts b/src/profile/handler/import/transport-assesments.spec.ts new file mode 100644 index 000000000..e7d3666e1 --- /dev/null +++ b/src/profile/handler/import/transport-assesments.spec.ts @@ -0,0 +1,44 @@ +import {TransportAssesments} from './transport-assesments'; +import { DbService, ImportTelemetryContext } from '../../..'; +import { of } from 'rxjs'; + +describe('TransportAssesments', () => { + let transportAssesments: TransportAssesments; + const mockDbService: Partial = {}; + + beforeAll(() => { + transportAssesments = new TransportAssesments( + mockDbService as DbService + ); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should be create a instance of transportAssesments', () => { + expect(transportAssesments).toBeTruthy(); + }); + + it('should delete unwanted assessment and save lerner assessment', (done) => { + // arrange + const request: ImportTelemetryContext = { + sourceDBFilePath: 'src/db/path', + metadata: { 'index': 1 } + }; + mockDbService.read = jest.fn(() => of([{uid: 'sample-uid'}])); + mockDbService.execute = jest.fn(() => of({})); + mockDbService.insert = jest.fn(() => of(2)); + mockDbService.update = jest.fn(() => of(2)); + // act + transportAssesments.execute(request).then(() => { + // assert + expect(mockDbService.read).toHaveBeenCalled(); + expect(mockDbService.execute).toHaveBeenCalled(); + expect(mockDbService.insert).toHaveBeenCalled(); + expect(mockDbService.update).toHaveBeenCalled(); + + done(); + }); + }); +}); diff --git a/src/profile/handler/import/transport-framework-n-channel.spec.ts b/src/profile/handler/import/transport-framework-n-channel.spec.ts new file mode 100644 index 000000000..fd3d5a9ca --- /dev/null +++ b/src/profile/handler/import/transport-framework-n-channel.spec.ts @@ -0,0 +1,43 @@ +import { TransportFrameworkNChannel } from './transport-framework-n-channel'; +import { DbService, ImportTelemetryContext } from '../../..'; +import { of } from 'rxjs'; + +describe('TransportFrameworkNChannel', () => { + let transportFrameworkNChannel: TransportFrameworkNChannel; + const mockDbService: Partial = {}; + + beforeAll(() => { + transportFrameworkNChannel = new TransportFrameworkNChannel( + mockDbService as DbService + ); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should be create a instace of transportFrameworkNChannel', () => { + expect(transportFrameworkNChannel).toBeTruthy(); + }); + + it('should save NoSqlEntry To Db', (done) => { + // arrange + const request: ImportTelemetryContext = { + sourceDBFilePath: 'src/db/path', + metadata: { 'index': 1 } + }; + mockDbService.read = jest.fn(() => of([{ + key: 'sample-key', + value: 'sample-value', + _id: 'sample-id', + uid: 'uid', + gid: 'gid' + }])); + // act + transportFrameworkNChannel.execute(request).then(() => { + // assert + expect(mockDbService.read).toHaveBeenCalled(); + done(); + }); + }); +});