|
| 1 | +import { TestBed } from "@angular/core/testing"; |
| 2 | +import { |
| 3 | + mockEntityMapperProvider, |
| 4 | + MockEntityMapperService, |
| 5 | +} from "../../core/entity/entity-mapper/mock-entity-mapper-service"; |
| 6 | +import { EntityMapperService } from "../../core/entity/entity-mapper/entity-mapper.service"; |
| 7 | +import { CoreTestingModule } from "../../utils/core-testing.module"; |
| 8 | +import { TestEntity } from "../../utils/test-utils/TestEntity"; |
| 9 | +import { DuplicateDetectionService } from "./duplicate-detection.service"; |
| 10 | + |
| 11 | +describe("DuplicateDetectionService", () => { |
| 12 | + let service: DuplicateDetectionService; |
| 13 | + let entityMapper: MockEntityMapperService; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + TestBed.configureTestingModule({ |
| 17 | + imports: [CoreTestingModule], |
| 18 | + providers: [...mockEntityMapperProvider()], |
| 19 | + }); |
| 20 | + service = TestBed.inject(DuplicateDetectionService); |
| 21 | + entityMapper = TestBed.inject( |
| 22 | + EntityMapperService, |
| 23 | + ) as MockEntityMapperService; |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(() => vi.restoreAllMocks()); |
| 27 | + |
| 28 | + it("should return a pair for two entities matching on a single field (case-insensitive)", async () => { |
| 29 | + const a = TestEntity.create({ name: "Alice" }); |
| 30 | + const b = TestEntity.create({ name: "alice" }); |
| 31 | + entityMapper.addAll([a, b]); |
| 32 | + |
| 33 | + const result = await service.findDuplicates(TestEntity, ["name"]); |
| 34 | + |
| 35 | + expect(result).toHaveLength(1); |
| 36 | + expect(result[0].record).toBe(a); |
| 37 | + expect(result[0].possibleDuplicate).toBe(b); |
| 38 | + }); |
| 39 | + |
| 40 | + it("should return a pair when names differ only by Unicode whitespace (non-breaking space)", async () => { |
| 41 | + const a = TestEntity.create({ name: "Alice\u00A0Smith" }); |
| 42 | + const b = TestEntity.create({ name: "Alice Smith" }); |
| 43 | + entityMapper.addAll([a, b]); |
| 44 | + |
| 45 | + const result = await service.findDuplicates(TestEntity, ["name"]); |
| 46 | + |
| 47 | + expect(result).toHaveLength(1); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should return a pair only when ALL selected fields match", async () => { |
| 51 | + const a = TestEntity.create({ name: "Alice", other: "X" }); |
| 52 | + const b = TestEntity.create({ name: "alice", other: "X" }); |
| 53 | + const c = TestEntity.create({ name: "alice", other: "Y" }); |
| 54 | + entityMapper.addAll([a, b, c]); |
| 55 | + |
| 56 | + const result = await service.findDuplicates(TestEntity, ["name", "other"]); |
| 57 | + |
| 58 | + expect(result).toHaveLength(1); |
| 59 | + expect(result[0].record).toBe(a); |
| 60 | + expect(result[0].possibleDuplicate).toBe(b); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should not return a pair when field values differ", async () => { |
| 64 | + const a = TestEntity.create({ name: "Alice" }); |
| 65 | + const b = TestEntity.create({ name: "Bob" }); |
| 66 | + entityMapper.addAll([a, b]); |
| 67 | + |
| 68 | + const result = await service.findDuplicates(TestEntity, ["name"]); |
| 69 | + |
| 70 | + expect(result).toHaveLength(0); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should not return a pair when a field value is empty on either entity", async () => { |
| 74 | + const a = TestEntity.create({ name: "" }); |
| 75 | + const b = TestEntity.create({ name: "" }); |
| 76 | + entityMapper.addAll([a, b]); |
| 77 | + |
| 78 | + const result = await service.findDuplicates(TestEntity, ["name"]); |
| 79 | + |
| 80 | + expect(result).toHaveLength(0); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should not return a pair when a field value is null or undefined", async () => { |
| 84 | + const a = TestEntity.create({}); |
| 85 | + const b = TestEntity.create({}); |
| 86 | + entityMapper.addAll([a, b]); |
| 87 | + |
| 88 | + const result = await service.findDuplicates(TestEntity, ["name"]); |
| 89 | + |
| 90 | + expect(result).toHaveLength(0); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should return only 1 pair for 3 mutually matching entities (no duplicate rows)", async () => { |
| 94 | + const a = TestEntity.create({ name: "Alice" }); |
| 95 | + const b = TestEntity.create({ name: "alice" }); |
| 96 | + const c = TestEntity.create({ name: "ALICE" }); |
| 97 | + entityMapper.addAll([a, b, c]); |
| 98 | + |
| 99 | + const result = await service.findDuplicates(TestEntity, ["name"]); |
| 100 | + |
| 101 | + expect(result).toHaveLength(1); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should return empty array when there are no entities", async () => { |
| 105 | + const result = await service.findDuplicates(TestEntity, ["name"]); |
| 106 | + |
| 107 | + expect(result).toHaveLength(0); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should return empty array when no fields are selected", async () => { |
| 111 | + const a = TestEntity.create({ name: "Alice" }); |
| 112 | + const b = TestEntity.create({ name: "Alice" }); |
| 113 | + entityMapper.addAll([a, b]); |
| 114 | + |
| 115 | + const result = await service.findDuplicates(TestEntity, []); |
| 116 | + |
| 117 | + expect(result).toHaveLength(0); |
| 118 | + }); |
| 119 | + |
| 120 | + it("should not treat object-valued fields as duplicates", async () => { |
| 121 | + const a = TestEntity.create({ name: "Alice" }); |
| 122 | + const b = TestEntity.create({ name: "Alice" }); |
| 123 | + (a as unknown as Record<string, unknown>)["metadata"] = { key: "A" }; |
| 124 | + (b as unknown as Record<string, unknown>)["metadata"] = { key: "B" }; |
| 125 | + entityMapper.addAll([a, b]); |
| 126 | + |
| 127 | + const result = await service.findDuplicates(TestEntity, ["metadata"]); |
| 128 | + |
| 129 | + expect(result).toHaveLength(0); |
| 130 | + }); |
| 131 | + |
| 132 | + it("should match configurable-enum-like objects by id", async () => { |
| 133 | + const a = TestEntity.create({ name: "Alice" }); |
| 134 | + const b = TestEntity.create({ name: "Bob" }); |
| 135 | + (a as unknown as Record<string, unknown>)["center"] = { |
| 136 | + id: "barabazar", |
| 137 | + label: "Barabazar", |
| 138 | + }; |
| 139 | + (b as unknown as Record<string, unknown>)["center"] = { |
| 140 | + id: "barabazar", |
| 141 | + label: "Bara Bazar", |
| 142 | + }; |
| 143 | + entityMapper.addAll([a, b]); |
| 144 | + |
| 145 | + const result = await service.findDuplicates(TestEntity, ["center"]); |
| 146 | + |
| 147 | + expect(result).toHaveLength(1); |
| 148 | + expect(result[0].record).toBe(a); |
| 149 | + expect(result[0].possibleDuplicate).toBe(b); |
| 150 | + }); |
| 151 | +}); |
0 commit comments