<commit_before>from django.core.exceptions import ObjectDoesNotExist
from django.test import TestCase
from data_finder.helpers import (
    AddressBaseGeocoderAdapter, CodesNotFoundException, MultipleCouncilsException
)


class AddressBaseGeocoderAdapterTest(TestCase):

    fixtures = ['test_addressbase.json']

    def test_no_records(self):
        """
        We can't find any records for the given postcode in the AddressBase table

        Exception of class ObjectDoesNotExist should be thrown
        """
        addressbase = AddressBaseGeocoderAdapter('DD1 1DD')
        exception_thrown = False
        try:
            result = addressbase.geocode()
        except ObjectDoesNotExist:
            exception_thrown = True
        self.assertTrue(exception_thrown)

        # point only geocode should also fail
        try:
            result = addressbase.geocode_point_only()
        except ObjectDoesNotExist:
            exception_thrown = True
        self.assertTrue(exception_thrown)

    def test_no_codes(self):
        """
        We find records for the given postcode in the AddressBase table
        but there are no corresponding records in the ONSUD for the UPRNs we found

        Exception of class CodesNotFoundException should be thrown
        """
        addressbase = AddressBaseGeocoderAdapter('AA11AA')
        exception_thrown = False
        try:
            result = addressbase.geocode()
        except CodesNotFoundException:
            exception_thrown = True
        self.assertTrue(exception_thrown)

        # point only geocode should return a result anyway
        result = addressbase.geocode_point_only()
        self.assertEqual('addressbase', result['source'])

    def test_multiple_councils(self):
        """
        We find records for the given postcode in the AddressBase table
        There are corresponding records in the ONSUD for the UPRNs we found
        The UPRNs described by this postcode map to more than one local authority

        Exception of class MultipleCouncilsException should be thrown
        """
        addressbase = AddressBaseGeocoderAdapter('CC11CC')
        exception_thrown = False
        try:
            result = addressbase.geocode()
        except MultipleCouncilsException:
            exception_thrown = True
        self.assertTrue(exception_thrown)

        # point only geocode should return a result anyway
        result = addressbase.geocode_point_only()
        self.assertEqual('addressbase', result['source'])

    def test_valid(self):
        """
        We find records for the given postcode in the AddressBase table
        There are some corresponding records in the ONSUD for the UPRNs we found

        Valid result should be returned

        Note that in this case, the ONSUD table does not contain corresponding
        records for *all* of the UPRNs we found, but we accept the result anyway
        """
        addressbase = AddressBaseGeocoderAdapter('bb 1   1B B')  # intentionally spurious whitespace and case
        result = addressbase.geocode()
        self.assertEqual('addressbase', result['source'])
        self.assertEqual('B01000001', result['council_gss'])<commit_msg>rewrite tests in a more succinct way<commit_after>from django.core.exceptions import ObjectDoesNotExist
from django.test import TestCase
from data_finder.helpers import (
    AddressBaseGeocoderAdapter, CodesNotFoundException, MultipleCouncilsException
)


class AddressBaseGeocoderAdapterTest(TestCase):

    fixtures = ['test_addressbase.json']

    def test_no_records(self):
        """
        We can't find any records for the given postcode in the AddressBase table

        Exception of class ObjectDoesNotExist should be thrown
        """
        addressbase = AddressBaseGeocoderAdapter('DD1 1DD')
        with self.assertRaises(ObjectDoesNotExist):
            result = addressbase.geocode()

        # point only geocode should also fail
        with self.assertRaises(ObjectDoesNotExist):
            result = addressbase.geocode_point_only()

    def test_no_codes(self):
        """
        We find records for the given postcode in the AddressBase table
        but there are no corresponding records in the ONSUD for the UPRNs we found

        Exception of class CodesNotFoundException should be thrown
        """
        addressbase = AddressBaseGeocoderAdapter('AA11AA')
        with self.assertRaises(CodesNotFoundException):
            result = addressbase.geocode()

        # point only geocode should return a result anyway
        result = addressbase.geocode_point_only()
        self.assertEqual('addressbase', result['source'])

    def test_multiple_councils(self):
        """
        We find records for the given postcode in the AddressBase table
        There are corresponding records in the ONSUD for the UPRNs we found
        The UPRNs described by this postcode map to more than one local authority

        Exception of class MultipleCouncilsException should be thrown
        """
        addressbase = AddressBaseGeocoderAdapter('CC11CC')
        with self.assertRaises(MultipleCouncilsException):
            result = addressbase.geocode()

        # point only geocode should return a result anyway
        result = addressbase.geocode_point_only()
        self.assertEqual('addressbase', result['source'])

    def test_valid(self):
        """
        We find records for the given postcode in the AddressBase table
        There are some corresponding records in the ONSUD for the UPRNs we found

        Valid result should be returned

        Note that in this case, the ONSUD table does not contain corresponding
        records for *all* of the UPRNs we found, but we accept the result anyway
        """
        addressbase = AddressBaseGeocoderAdapter('bb 1   1B B')  # intentionally spurious whitespace and case
        result = addressbase.geocode()
        self.assertEqual('addressbase', result['source'])
        self.assertEqual('B01000001', result['council_gss'])
{'fraction_non_alphanumeric': 0.05793492695883134, 'fraction_numerical': 0.005312084993359893, 'mean_word_length': 2.8326972010178118, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5713703', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1490, 'n_words': 517}