From 238f196166eecd42b563dbaf77b89217f4072abe Mon Sep 17 00:00:00 2001 From: Andy Grunwald Date: Sun, 1 Nov 2015 17:42:05 +0100 Subject: [PATCH] [api] Limit number of return rows by one during matching operation Closes #22 --- sortinghat/api.py | 5 +++-- tests/test_api.py | 12 ++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/sortinghat/api.py b/sortinghat/api.py index 2cd9ed914..fc62d6ef1 100644 --- a/sortinghat/api.py +++ b/sortinghat/api.py @@ -855,7 +855,7 @@ def match_identities(db, uuid, matcher): The function will search in the registry for similar identities to 'uuid'. The result will be a list matches containing unique identities objects. - This list will also include the given unique identity. + This list will not(!) include the given unique identity. The criteria used to check when an identity matches with another one is defined by 'matcher' parameter. This parameter is an instance @@ -879,7 +879,8 @@ def match_identities(db, uuid, matcher): if not uid: raise NotFoundError(entity=uuid) - candidates = session.query(UniqueIdentity).all() + # Get all identities expect of the one requested one query above (uid) + candidates = session.query(UniqueIdentity).filter(UniqueIdentity.uuid != uuid).all() for candidate in candidates: if not matcher.match(uid, candidate): diff --git a/tests/test_api.py b/tests/test_api.py index 45c786f90..b2d6395b1 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1922,37 +1922,37 @@ def test_default_matcher(self): m1 = api.match_identities(self.db, 'John Smith', matcher) uids = get_uuids(m1) - self.assertListEqual(uids, ['John Smith', 'Smith J.']) + self.assertListEqual(uids, ['Smith J.']) # Smith J. m1 = api.match_identities(self.db, 'Smith J.', matcher) uids = get_uuids(m1) - self.assertListEqual(uids, ['John Smith', 'Smith J.']) + self.assertListEqual(uids, ['John Smith']) # John Doe m2 = api.match_identities(self.db, 'John Doe', matcher) uids = get_uuids(m2) - self.assertListEqual(uids, ['John Doe']) + self.assertListEqual(uids, []) # Jane Rae m3 = api.match_identities(self.db, 'Jane Rae', matcher) uids = get_uuids(m3) - self.assertListEqual(uids, ['Jane', 'Jane Rae', 'JRae']) + self.assertListEqual(uids, ['Jane', 'JRae']) # JRae m3 = api.match_identities(self.db, 'JRae', matcher) uids = get_uuids(m3) - self.assertListEqual(uids, ['Jane', 'Jane Rae', 'JRae']) + self.assertListEqual(uids, ['Jane', 'Jane Rae']) # Jane m3 = api.match_identities(self.db, 'Jane', matcher) uids = get_uuids(m3) - self.assertListEqual(uids, ['Jane', 'Jane Rae', 'JRae']) + self.assertListEqual(uids, ['Jane Rae', 'JRae']) def test_empty_registry(self):