LP1840050 IDL pkeyMatches function/tests
authorBill Erickson <berickxx@gmail.com>
Fri, 16 Aug 2019 20:48:51 +0000 (16:48 -0400)
committerGalen Charlton <gmc@equinoxinitiative.org>
Fri, 6 Sep 2019 16:58:18 +0000 (12:58 -0400)
Function to compare whether two IdlObjects are of the same class and
have matching pkey values.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/src/eg2/src/app/core/idl.service.ts
Open-ILS/src/eg2/src/app/core/idl.spec.ts

index 21ec24a..b5abf61 100644 (file)
@@ -196,5 +196,14 @@ export class IdlService {
 
         return hash;
     }
+
+    // Returns true if both objects have the same IDL class and pkey value.
+    pkeyMatches(obj1: IdlObject, obj2: IdlObject) {
+        if (!obj1 || !obj2) { return false; }
+        const idlClass = obj1.classname;
+        if (idlClass !== obj2.classname) { return false; }
+        const pkeyField = this.classes[idlClass].pkey || 'id';
+        return obj1[pkeyField]() === obj2[pkeyField]();
+    }
 }
 
index 8138bf4..49439fd 100644 (file)
@@ -24,5 +24,33 @@ describe('IdlService', () => {
         expect(org.name()).toBe('AN ORG');
     });
 
+    it('should correctly compare IDL pkey values', () => {
+        service.parseIdl();
+        const org1 = service.create('aou');
+        const org2 = service.create('aou');
+        org1.id(123);
+        org2.id(123);
+        expect(service.pkeyMatches(org1, org2)).toBe(true);
+    });
+
+    it('should correctly compare IDL pkey values', () => {
+        service.parseIdl();
+        const org1 = service.create('aou');
+        const org2 = service.create('aou');
+        org1.id(123);
+        org2.id(456);
+        expect(service.pkeyMatches(org1, org2)).toBe(false);
+    });
+
+    it('should correctly compare IDL classes in pkey match', () => {
+        service.parseIdl();
+        const org = service.create('aou');
+        const user = service.create('au');
+        org.id(123);
+        user.id(123);
+        expect(service.pkeyMatches(org, user)).toBe(false);
+    });
+
+
 });