|
import pytest |
|
from phonepad import ( |
|
normalise_phone_number, |
|
letters_to_digits, |
|
multimatch, |
|
phone_pad_match |
|
) |
|
|
|
test_phone_numbers = [ |
|
('3662277', '3662277'), |
|
(' 253 6368 ', '2536368'), |
|
('1-(800)-356-9377', '18003569377') |
|
] |
|
@pytest.mark.parametrize('raw_phone_number,expected', test_phone_numbers) |
|
def test_normalise_phone_number(raw_phone_number, expected): |
|
assert normalise_phone_number(raw_phone_number) == expected |
|
|
|
test_words = [ |
|
('flowers', '3569377'), |
|
('clement', '2536368'), |
|
('clemdot', '2536368'), |
|
('CLEM DOT!', '2536368'), |
|
] |
|
@pytest.mark.parametrize('word,expected', test_words) |
|
def test_letters_to_digits(word, expected): |
|
assert letters_to_digits(word) == expected |
|
|
|
test_matches = [ |
|
('clement', [ 'a', 'b', 't', 'm'], ['t', 'm']), |
|
('dog', ['d', 'do', 'dog', 'dot'], ['d', 'do', 'dog']), |
|
('flowers', ['flower', 'lower', 'flour'], ['flower', 'lower']), |
|
('flower', ['lowers', 'flower', 'lower', 'flour'], ['flower', 'lower']), |
|
('flower', ['flowers', 'low', 'flow', 'flo'], ['low', 'flow', 'flo']), |
|
('foobar', ['foo', 'bar'], ['foo', 'bar']), |
|
('baz', ['bar'], []), |
|
('otheater', ['theater', 'other'], ['theater']), |
|
( |
|
'abccab', |
|
['a', 'ab', 'bab', 'bc', 'bca', 'c', 'caa'], |
|
['a', 'ab', 'bc', 'c'] |
|
) |
|
] |
|
@pytest.mark.parametrize('haystack,needles,expected', test_matches) |
|
def test_multimatch(haystack, needles, expected): |
|
matched = multimatch(haystack, needles) |
|
assert matched == set(expected) |
|
|
|
test_needles = ( |
|
'flowers', 'clement', 'clemdot', |
|
'foo', 'bar', 'baz', 'foobar', 'emo', 'cap', 'car', 'cat' |
|
) |
|
test_phone_pad_matches = [ |
|
('1-(800)-356-9377', ['flowers']), |
|
('253 6368', ['clement', 'clemdot']), |
|
('3662277', ['foo', 'bar', 'foobar', 'emo', 'cap', 'car']) |
|
] |
|
class TestPhonePadMatch: |
|
@pytest.mark.parametrize('raw_phone_number,expected', test_phone_pad_matches) |
|
def test_phone_pad_match(self, raw_phone_number, expected): |
|
matched = phone_pad_match(raw_phone_number, test_needles) |
|
assert matched == set(expected) |
|
|
|
def test_empty_phone_number(self): |
|
with pytest.raises(ValueError): |
|
phone_pad_match('', ['foo']) |
|
|
|
def test_no_needles(self): |
|
with pytest.raises(ValueError): |
|
phone_pad_match('1-(800)-356-9377', []) |
|
|
|
def test_empty_needle(self): |
|
with pytest.raises(ValueError): |
|
phone_pad_match('1-(800)-356-9377', ['foo', '', 'bar']) |