Skip to content

Instantly share code, notes, and snippets.

@tgalv
Created May 23, 2016 11:00
Show Gist options
  • Save tgalv/31f1b3158feb16e193ffa53b403a73f9 to your computer and use it in GitHub Desktop.
Save tgalv/31f1b3158feb16e193ffa53b403a73f9 to your computer and use it in GitHub Desktop.
US42
"""
1. Conveyancer must be alerted if more names are on the register than on the mortgage deed
2. All names on the register must be on the mortgage deed
3. There can be more names on the mortgage deed than on the register
4. Conveyancer does not need to be alerted that there are more names on mortgage deed than register
5. Names do not need to be in the same order that they are on the register
6. Names must not be case sensitive
7. Deed must be rejected if the names do not match
"""
import unittest
from validate_names import validate_names, _set_no_duplicates
class TestValidateNames(unittest.TestCase):
def test_names_register_names_proprietor_names(self):
"""
Scenarios 2, 5, 6.
"""
ret_val = validate_names(['PeTeR', 'PaUl', 'MaRy'],
['pAuL', 'mArY', 'pEtEr'])
self.assertTrue(ret_val)
def test_more_names_on_register(self):
"""
Scenario 1.
"""
ret_val = validate_names(['Peter', 'Paul', 'Mary'],
['Peter'])
self.assertFalse(ret_val)
def test_register_subset_of_deed(self):
"""
Scenarios 2, 3, 4.
"""
ret_val = validate_names(['Peter'],
['Peter', 'Paul', 'Mary'])
self.assertTrue(ret_val)
def test_dupes_on_register_subset_of_deed(self):
ret_val = validate_names(['Peter', 'Peter'],
['Peter', 'Paul', 'Mary'])
self.assertFalse(ret_val)
def test_uniquify(self):
names = _set_no_duplicates(['foo', 'foo', 'bar', 'foo'])
self.assertEqual(set(['foo.0', 'foo.1', 'bar.0', 'foo.2']), names)
if __name__ == "__main__":
unittest.main()
"""
Spike to work through the scenarios in US42
"""
import collections
import itertools
def _set_no_duplicates(names):
"""
Cast to a `set` but first making the elements unique
by adding a count suffix.
:type names: list
:rtype: set
"""
return set(itertools.chain(*[["{0}.{1}".format(j, i)
for i, j in enumerate(y*[x.lower()])]
for x, y in
collections.Counter(names).items()]))
def validate_names(proprietor_names, deed_names):
"""
Checks that the names on the deed are valid.
i.e. That the names on the Register (proprietor_names)
are a subset of the names on the Deed.
:param proprietor_names: The names on the Register.
:type proprietor_names: list
:param deed_names: The names on the Deed.
:type deed_names: list
:rtype: bool
"""
register_set = _set_no_duplicates(proprietor_names)
deed_set = _set_no_duplicates(deed_names)
register_is_subset = register_set.issubset(deed_set)
if not register_is_subset:
print([name for name in register_set if name not in deed_set])
return register_is_subset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment