def valid_file_contents(file_contents): """ This function checks to see that the file contents are valid: no required fields are missing, location information is complete, birth_date and passport/visa number is valid. :param file_contents: a list of dictionaries denoting returning citizens :return: True if the file contents are valid, False otherwise """ valid_file = True for person in file_contents: for item in REQUIRED_FIELDS: if item not in person: valid_file = False for item in person: if item in LOCATION_FIELDS: if not valid_location_field(person[item]): valid_file = False elif item is "passport": if not valid_passport_format(person[item]): valid_file = False elif item is "visa": if not valid_date_format(person["visa"]["date"]): valid_file = False if not valid_visa_format(person["visa"]["code"]): valid_file = False elif item is "entry_reason": if person[item] not in REASON_FOR_ENTRY: valid_file = False elif item is "birth_date": if not valid_date_format(person[item]): valid_file = False if unknown_location_exists(person, COUNTRIES): valid_file = False return valid_file
def test_valid_location_field(): d1 = {'city': 'city_name', 'region': 'region_name', 'country': 'LUG'} assert valid_location_field(d1) d2 = {'city': 'city_name', 'country': 'KRA', 'region': 'region_name'} assert valid_location_field(d2) d3 = {'city': 'city_name', 'province': 'province_name', 'country': 'JIK'} assert valid_location_field(d3) is False d4 = {'municipality': 'municipality_name', 'state': 'state_name', 'country': 'KRA'} assert valid_location_field(d4) is False d5 = {'city': 'city_name', 'country': 'KRA'} assert valid_location_field(d5) is False d6 = {'city': 'city_name', 'region': 'region_name', 'province': 'province_name', 'country': 'LUG'} assert valid_location_field(d6) is False
def test_decide_missing_required_information_file(): """ Ensure that the file contains citizens with missing information fields. """ with open("test_decide_missing_required_information.json", "r") as citizen_file: citizen_content = citizen_file.read() citizen_json = json.loads(citizen_content) assert valid_file_contents(citizen_json) is False for person in citizen_json: required_fields_included = True for item in REQUIRED_FIELDS: if item not in person: required_fields_included = False if item is "passport" and "passport" in person: assert valid_passport_format(person[item]) is True for item in LOCATION_FIELDS: if item in person: if not valid_location_field(person[item]): required_fields_included = False assert required_fields_included is False
def test_valid_location_field(): d1 = {'city': 'city_name', 'region': 'region_name', 'country': 'LUG'} assert valid_location_field(d1) d2 = {'city': 'city_name', 'country': 'KRA', 'region': 'region_name'} assert valid_location_field(d2) d3 = {'city': 'city_name', 'province': 'province_name', 'country': 'JIK'} assert valid_location_field(d3) is False d4 = { 'municipality': 'municipality_name', 'state': 'state_name', 'country': 'KRA' } assert valid_location_field(d4) is False d5 = {'city': 'city_name', 'country': 'KRA'} assert valid_location_field(d5) is False d6 = { 'city': 'city_name', 'region': 'region_name', 'province': 'province_name', 'country': 'LUG' } assert valid_location_field(d6) is False