-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brajesh Kumar
authored and
Brajesh Kumar
committed
Nov 1, 2023
1 parent
a87ab10
commit 5b0bbfb
Showing
4 changed files
with
72 additions
and
43 deletions.
There are no files selected for viewing
89 changes: 58 additions & 31 deletions
89
src/app/core/duplicate-records/duplicate-records.service.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 |
---|---|---|
@@ -1,70 +1,97 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { DuplicateRecordsService } from './duplicate-records.service'; | ||
import { DuplicateRecordService } from './duplicate-records.service'; | ||
import { EntityMapperService } from '../entity/entity-mapper/entity-mapper.service'; | ||
import { DatabaseEntity, EntityRegistry } from '../entity/database-entity.decorator'; | ||
import { EntitySchemaService } from '../entity/schema/entity-schema.service'; | ||
import { DatabaseEntity, entityRegistry, EntityRegistry } from '../entity/database-entity.decorator'; | ||
import { Database } from '../database/database'; | ||
import { SessionService } from '../session/session-service/session.service'; | ||
import { Entity } from '../entity/model/entity'; | ||
import { DatabaseField } from '../entity/database-field.decorator'; | ||
import { CoreModule } from '../core.module'; | ||
import { ComponentRegistry } from 'app/dynamic-components'; | ||
import { UpdateMetadata } from '../entity/model/update-metadata'; | ||
|
||
describe('DuplicateRecordsService', () => { | ||
let service: DuplicateRecordsService; | ||
let service: DuplicateRecordService; | ||
let entityMapperService: EntityMapperService; | ||
let entityTypes: EntityRegistry; | ||
let entitySchemaService: EntitySchemaService; | ||
let database: Database; | ||
let sessionService: SessionService; | ||
let database: Database; | ||
|
||
@DatabaseEntity("DuplicateTestEntity") | ||
class DuplicateTestEntity extends Entity { | ||
@DatabaseField() name: String; | ||
@DatabaseField() boolProperty: boolean; | ||
@DatabaseField() created: UpdateMetadata; | ||
@DatabaseField() updated: UpdateMetadata; | ||
@DatabaseField() inactive: boolean; | ||
} | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [CoreModule], | ||
providers: [ | ||
DuplicateRecordsService, | ||
DuplicateRecordService, | ||
Database, | ||
EntityMapperService, | ||
EntityRegistry, | ||
EntitySchemaService, | ||
SessionService, | ||
{ provide: Database, useValue: {} }, | ||
], | ||
{ provide: EntityRegistry, useValue: entityRegistry }, | ||
ComponentRegistry,] | ||
}); | ||
service = TestBed.inject(DuplicateRecordsService); | ||
service = TestBed.inject(DuplicateRecordService); | ||
entityMapperService = TestBed.inject(EntityMapperService); | ||
entityTypes = TestBed.inject(EntityRegistry); | ||
entitySchemaService = TestBed.inject(EntitySchemaService); | ||
database = TestBed.inject(Database); | ||
sessionService = TestBed.inject(SessionService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it('should transform data correctly', () => { | ||
const duplicateTest = new DuplicateTestEntity(); | ||
duplicateTest.name = "TestName" | ||
duplicateTest.boolProperty = true; | ||
|
||
const schemaName = DuplicateTestEntity.ENTITY_TYPE; | ||
const originalData = [ | ||
{ | ||
record: duplicateTest | ||
} | ||
]; | ||
|
||
const transformedData = service.transformData(originalData, schemaName); | ||
expect(transformedData[0]).toBeInstanceOf(Entity); | ||
expect(transformedData[0]._id).toBeDefined(); | ||
expect(transformedData[0]._id).not.toBe(duplicateTest['_id']); | ||
expect(transformedData[0].name).toMatch(/^Copy of /); | ||
expect(transformedData[0].boolProperty).toBe(true); | ||
}); | ||
|
||
it('should duplicate record and save data', async () => { | ||
@DatabaseEntity("DuplicateTestEntity") | ||
class DuplicateTestEntity extends Entity { | ||
@DatabaseField() string: String; | ||
@DatabaseField() boolProperty: boolean; | ||
@DatabaseEntity("SaveTestEntity") | ||
class SaveTestEntity extends Entity { | ||
@DatabaseField() name: String; | ||
@DatabaseField() boolProperty: boolean; | ||
@DatabaseField() created: UpdateMetadata; | ||
@DatabaseField() updated: UpdateMetadata; | ||
@DatabaseField() inactive: boolean; | ||
} | ||
|
||
const duplicateTestEntity = new DuplicateTestEntity(); | ||
duplicateTestEntity.string = "TestName" | ||
const duplicateTestEntity = new SaveTestEntity(); | ||
duplicateTestEntity.name = "TestName" | ||
duplicateTestEntity.boolProperty = true; | ||
duplicateTestEntity.inactive = false; | ||
|
||
const schemaName =DuplicateTestEntity.ENTITY_TYPE; | ||
|
||
const originalData = [ | ||
{ | ||
record: duplicateTestEntity | ||
} | ||
]; | ||
|
||
const transformDataSpy = spyOn(service, 'transformData').and.callThrough(); | ||
const saveAllSpy = spyOn(entityMapperService, 'saveAll'); | ||
|
||
const data = transformDataSpy(originalData, schemaName) | ||
|
||
await service.getDataforDuplicate(originalData, schemaName); | ||
await service.duplicateRecord(originalData, schemaName); | ||
expect(transformDataSpy).toHaveBeenCalledWith(originalData, schemaName); | ||
expect(saveAllSpy).toHaveBeenCalledWith(data); | ||
expect(saveAllSpy).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
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
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