From faaea75a993cf9e049fee1c94fa0ea2ed44b30a4 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 6 Mar 2018 12:39:58 +0000 Subject: [PATCH] Do more normalisation for better partial matching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Phone numbers sometimes contain stuff we normalise out. This matches perfectly if we have a full phone number, because we can normalise the thing we’re searching for in the same way as the search term. With partial search terms we can’t do this completely, because we can’t work out if ‘123’ is part of a UK number, an international number, the start of the phone number, the last 3 digits, etc. What we can do is remove some stuff that we can know will cause partial search terms to not match: - leading pluses - leading `0`s - any brackets - any spaces --- app/dao/notifications_dao.py | 5 +++ .../notification_dao/test_notification_dao.py | 35 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 321a0f7d3..d095b176b 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -444,6 +444,11 @@ def dao_get_notifications_by_to_field(service_id, search_term, statuses=None): except InvalidEmailError: normalised = search_term + for character in ['(', ')', ' ']: + normalised = normalised.replace(character, '') + + normalised = normalised.lstrip('+0') + filters = [ Notification.service_id == service_id, Notification.normalised_to.like("%{}%".format(normalised)), diff --git a/tests/app/dao/notification_dao/test_notification_dao.py b/tests/app/dao/notification_dao/test_notification_dao.py index 41ea1ac8f..7c98035ea 100644 --- a/tests/app/dao/notification_dao/test_notification_dao.py +++ b/tests/app/dao/notification_dao/test_notification_dao.py @@ -1766,6 +1766,41 @@ def test_dao_get_notifications_by_to_field_matches_partial_emails(sample_templat assert notification_2.id not in notification_ids +@pytest.mark.parametrize('search_term', [ + '001', + '100', + '09001', + '077009001', + '07700 9001', + '(0)7700 9001', + '4477009001', + '+4477009001', + pytest.mark.skip('+44077009001', reason='No easy way to normalise this'), + pytest.mark.skip('+44(0)77009001', reason='No easy way to normalise this'), +]) +def test_dao_get_notifications_by_to_field_matches_partial_phone_numbers( + sample_template, + search_term, +): + + notification_1 = create_notification( + template=sample_template, + to_field='+447700900100', + normalised_to='447700900100', + ) + notification_2 = create_notification( + template=sample_template, + to_field='+447700900200', + normalised_to='447700900200', + ) + results = dao_get_notifications_by_to_field(notification_1.service_id, search_term) + notification_ids = [notification.id for notification in results] + + assert len(results) == 1 + assert notification_1.id in notification_ids + assert notification_2.id not in notification_ids + + @pytest.mark.parametrize('to', [ 'not@email', '123' ])