def add_record(): """Add a new person to the set of records This method expects json in the following format { 'separator': ',', 'record': 'LastName, FirstName, Gender, FavoriteColor, DateOfBirth' } """ content = request.json if content is None: raise InvalidUsage("Invalid json palyload", status_code=400) separator = content.get("separator") record = content.get("record") if separator is None or separator not in ",| ": raise InvalidUsage( "Separator is missing or not one of comma, pipe, or space", status_code=400) if record is None: raise InvalidUsage("Record is missing", status_code=400) try: global current_records current_records = update_records(current_records, [record], separator) except ValueError as e: raise InvalidUsage(str(e), status_code=400) return jsonify([record.to_dict() for record in current_records]), 201
def test_update_records__space_separated(self): new_records = ["Trate Josh Male green 08/14/1995"] self.assertListEqual( [ Record( last_name="Trate", first_name="Josh", gender=Gender.MALE, favorite_color="green", date_of_birth=datetime.date(1995, 8, 14), ) ], update_records([], new_records, " "), )
def test_update_records__no_existing_records(self): new_records = ["Trate, Josh, Male, green, 08/14/1995"] self.assertListEqual( [ Record( last_name="Trate", first_name="Josh", gender=Gender.MALE, favorite_color="green", date_of_birth=datetime.date(1995, 8, 14), ) ], update_records([], new_records, ","), )
def test_update_records__existing_records(self): existing_records = [ Record( last_name="Trate", first_name="Josh", gender=Gender.MALE, favorite_color="green", date_of_birth=datetime.date(1995, 8, 14), ) ] new_records = ["Smith, Josh, Male, green, 09/14/1997"] self.assertIn( Record( last_name="Smith", first_name="Josh", gender=Gender.MALE, favorite_color="green", date_of_birth=datetime.date(1997, 9, 14), ), update_records(existing_records, new_records, ","), )
def main( comma_separated_file_name: str, pipe_separated_file_name: str, space_separated_file_name: str, sort_order: RecordSortOrder, ) -> None: """Read records from provided files and print them in the desired order Args: comma_separated_file_name: name of file with comma separated records pipe_separated_file_name: name of the file with pipe separated records space_separated_file_name: name of the file with space separated records sort_order: order to print the parsed records in """ records = [] record_files = zip( ( comma_separated_file_name, pipe_separated_file_name, space_separated_file_name, ), (",", "|", " "), ) for file_name, delimiter in record_files: with open(file_name) as file: records = update_records(records, file.readlines(), delimiter) sorted_records = records_sorted_by_order(records, sort_order) print( tabulate.tabulate( sorted_records, headers=( "Last Name", "First Name", "Gender", "Favorite Color", "Date of Birth", ), ))
def test_update_records__missing_fields(self): new_records = ["Trate Josh Male green"] with self.assertRaises(ValueError): update_records([], new_records, " ")