Example #1
0
def test_postcodes_uk_to_dict_with_missing_space_letter():
    raw_postcode = 'AAAA'
    postcode = PostCodeUK(raw_postcode)
    assert postcode.is_valid is False
    assert postcode.to_dict() == {
        'attributes': {
            'area': 'AAAA',
            'district': None,
            'sector': None,
            'unit': None
        },
        'errors': {
            'area': 'Invalid area format.',
            'district': 'Invalid district format.',
            'missing_space': 'Missing space in the postcode',
            'sector': 'Invalid sector format.',
            'unit': 'Invalid unit format.'
        },
        'is_valid': False,
        'postcode': 'AAAA',
        'sides': {
            'inward': None,
            'outward': 'AAAA'
        }
    }
Example #2
0
def test_postcodes_uk_to_dict_with_valid_postcode():
    raw_postcode = 'AA9A 9AA'
    postcode = PostCodeUK(raw_postcode)
    assert postcode.is_valid is True
    assert postcode.to_dict() == {
        'postcode': 'AA9A 9AA',
        'is_valid': True,
        'attributes': {
            'area': 'AA',
            'district': '9A',
            'sector': '9',
            'unit': 'AA'
        },
        'sides': {
            'outward': 'AA9A',
            'inward': '9AA'
        },
        'errors': {}
    }
Example #3
0
def test_postcodes_uk_valid_formats(raw_postcode, area, district, sector,
                                    unit):
    postcode = PostCodeUK(raw_postcode)
    assert postcode.raw_postcode == raw_postcode
    assert postcode.full_postcode == raw_postcode
    assert postcode.area == area
    assert postcode.district == district
    assert postcode.sector == sector
    assert postcode.unit == unit
    assert postcode.is_valid is True
Example #4
0
def test_postcodes_uk_to_dict_with_invalid_unit():
    raw_postcode = 'AA9A 9'
    postcode = PostCodeUK(raw_postcode)
    assert postcode.is_valid is False
    assert postcode.to_dict() == {
        'attributes': {
            'area': 'AA',
            'district': '9A',
            'sector': '9',
            'unit': ''
        },
        'errors': {
            'unit': 'Invalid unit format.'
        },
        'is_valid': False,
        'postcode': 'AA9A 9',
        'sides': {
            'inward': '9',
            'outward': 'AA9A'
        }
    }
Example #5
0
def test_postcodes_uk_to_dict_with_invalid_combined_district_and_sector():
    raw_postcode = 'AA AA'
    postcode = PostCodeUK(raw_postcode)
    assert postcode.is_valid is False
    assert postcode.to_dict() == {
        'attributes': {
            'area': 'AA',
            'district': None,
            'sector': '',
            'unit': 'AA'
        },
        'errors': {
            'district': 'Invalid district format.',
            'sector': 'Invalid sector format.'
        },
        'is_valid': False,
        'postcode': 'AA AA',
        'sides': {
            'inward': 'AA',
            'outward': 'AA'
        }
    }
Example #6
0
 def get(self, request):
     raw_postcode = request.GET.get('postcode', '')
     postcode = PostCodeUK(raw_postcode)
     http_status = status.HTTP_200_OK if postcode.is_valid else status.HTTP_400_BAD_REQUEST
     http_payload = postcode.to_dict()
     return Response(http_payload, status=http_status)
Example #7
0
def test_postcodes_uk_invalid_unit(raw_postcode):
    postcode = PostCodeUK(raw_postcode)
    assert postcode.raw_postcode == raw_postcode
    assert postcode.full_postcode == raw_postcode
    assert postcode.is_valid is False
    assert postcode.errors == {'unit': 'Invalid unit format.'}
Example #8
0
def test_postcodes_uk_invalid_sector(raw_postcode):
    postcode = PostCodeUK(raw_postcode)
    assert postcode.raw_postcode == raw_postcode
    assert postcode.full_postcode == raw_postcode
    assert postcode.is_valid is False
    assert postcode.errors == {'sector': 'Invalid sector format.'}
Example #9
0
def test_postcodes_uk_invalid_area(raw_postcode):
    postcode = PostCodeUK(raw_postcode)
    assert postcode.raw_postcode == raw_postcode
    assert postcode.full_postcode == raw_postcode
    assert postcode.is_valid is False
    assert postcode.errors == {'area': 'Invalid area format.'}
Example #10
0
def test_postcodes_uk_uppercase_normalization():
    raw_postcode = 'aa9a 9aa'
    postcode = PostCodeUK(raw_postcode)
    assert postcode.raw_postcode == raw_postcode
    assert postcode.full_postcode == 'AA9A 9AA'
Example #11
0
def test_postcodes_uk_outward_and_inward_validations():
    raw_postcode = 'AA9A 9AA'
    outward, inward = raw_postcode.split(' ')
    postcode = PostCodeUK(raw_postcode)
    assert postcode.outward == outward
    assert postcode.inward == inward