def test_positive_format_postcode2(): """Function to test format_postcode method for positive cases arguments: formatting_positive_value -- list of valid uk post codes """ postcode = Postcode() print(postcode.format_postcode("w1a0ax")) assert postcode.message == "Formatted" and postcode.formatted_postcode == "W1A 0AX"
def test_negative_format_postcode1(formatting_negative_value_length): """Function to test format_postcode method for negative cases(invalid length) arguments: formatting_positive_value -- list of post codes with invalid length """ postcode = Postcode() print(postcode.format_postcode(formatting_negative_value_length)) assert postcode.valid == False and postcode.message == "ERROR: 5 to 8 characters only"
def test_positive_split_postcode2(): """Function to test split_validate_postcode method for positive cases; negative cases are handled by the methods this function call """ postcode = Postcode() postcode.split_validate_postcode("M1 1AE") # Valid is equal to true for valid uk post codes and they splitted into respective components assert postcode.valid == True and postcode.postcode_area == "M" and postcode.postcode_district == "1" and\ postcode.postcode_sector == "1" and postcode.postcode_unit == "AE"
def test_negative_validate_postcode1(validate_negative_values): """Function to test validate_postcode method for negative cases arguments: validate_negative_values -- invalid uk post codes """ postcode = Postcode() postcode.split_validate_postcode(validate_negative_values) assert postcode.valid == False and postcode.message == "INVALID: the post code is invalid"
def test_positive_validate_postcode1(formatting_positive_value): """Function to test validate_postcode method for positive cases arguments: formatting_positive_value -- list of valid uk post codes """ postcode = Postcode() postcode.validate_postcode(formatting_positive_value) assert postcode.valid == True and postcode.message == "VALID: the post code is valid"
def test_negative_format_postcode2(formatting_negative_value_special_char): """Function to test format_postcode method for negative cases(Special chars) arguments: formatting_positive_value -- list of post codes with special char """ postcode = Postcode() print(postcode.format_postcode(formatting_negative_value_special_char)) assert postcode.valid == False and postcode.message == "ERROR: No special Characters allowed"
def test_positive_format_postcode1(formatting_positive_value): """Function to test format_postcode method for positive cases arguments: formatting_positive_value -- list of valid uk post codes """ postcode = Postcode() print(postcode.format_postcode(formatting_positive_value)) # Message is "Formatted" if formatting of post code is successful assert postcode.message == "Formatted"
def check(request): """function to perform formatting, validation and splitting of UK postcodes""" if request.method == 'POST': # Get the list of postcodes postcodes = request.POST.get('postcodes', '') # Split the postcpdes by semicolon postcode_list = postcodes.split(";") postcode_checked_list = [] # iterate through all the input postcodes for postcode in postcode_list: postcode_obj = Postcode() # Check for validity and split postcode_obj.split_validate_postcode(postcode) # Add post code object list postcode_checked_list.append(postcode_obj) return render(request, 'validate_postcode.html', locals()) return render(request, 'validate_postcode.html')
#! /usr/bin/env python3.6 import sys from ukpostcode.postcode import Postcode if __name__ == '__main__': # Get the input postcode string as the last parameter from the commandline postcode_str = sys.argv[-1] print("\nInput Post Code String: " + postcode_str) # Formatting the post code print("\n\n######### Formatting Postcode #########") postcode = Postcode() postcode.format_postcode(postcode_str) print("Message:", postcode.message) print("Formatted post code:", postcode.formatted_postcode) # Splitting the post code print("\n\n######### Formatting, Validating and Splitting Postcode #########") postcode = Postcode() postcode.split_validate_postcode(postcode_str) print("Status:", postcode.valid) print("Message:", postcode.message) print("Formatted post code:", postcode.formatted_postcode) print("Outward code:", postcode.outward_code) print("Inward code:", postcode.inward_code) print("postcode area:", postcode.postcode_area) print("postcode district:", postcode.postcode_district)