def test_location_map(self): loc_map = CachedValueMap(lambda loc: loc.full_location) cleaned_location = Location('New York City', 'New York', 'United States') loc_map.add_mapping('New York, New York', cleaned_location) unclean_location = Location('New York', 'New York') self.assertEqual(cleaned_location, loc_map.get_mapping(unclean_location))
def test_full_location_is_set_given_two_location_fields(self): self.assert_full_loc('Baltimore, Maryland', Location(city='Baltimore', state='Maryland')) self.assert_full_loc( 'Baltimore, United States', Location(city='Baltimore', country='United States')) self.assert_full_loc( 'Maryland, United States', Location(state='Maryland', country='United States'))
def test_full_location_responds_to_field_updates(self): location = Location(city='Baltimore', state='Maryland', country='United States') self.assert_full_loc('Baltimore, Maryland, United States', location) location.city = 'London' self.assert_full_loc('London, Maryland, United States', location) location.state = 'England' location.country = 'United Kingdom' self.assert_full_loc('London, England, United Kingdom', location)
def test_build_mapping_for_empty_location(self): test_input = [{ 'raw_location': '$8jg$gjfi', 'city': '', 'state': '', 'country': '' }] location_map = ValueMapBuilder.build_cached_location_map( test_input, 'raw_location') self.assert_mapping_is(Location(), Location('$8jg$gjfi'), location_map)
def parser_func(self): values = [value.strip() for value in self._value.split(',')] if len(values) == 1: return Location(city=values[0]) elif len(values) == 2: return Location(city=values[0], state=values[1]) elif len(values) == 3: return Location(city=values[0], state=values[1], country=values[2]) else: country = ', '.join(values[2:]) return Location(city=values[0], state=values[1], country=country)
def test_location_map_returns_a_value_if_value_is_already_clean(self): loc_map = CachedValueMap(lambda loc: loc.full_location) cleaned_location = Location('New York City', 'New York', 'United States') loc_map.add_mapping('New York, New York', cleaned_location) self.assertEqual(cleaned_location, loc_map.get_mapping(cleaned_location))
def _populate_location_map(location_map, raw_mapping_data: List[dict], from_field: str): for row in raw_mapping_data: clean_location = Location(city=row['city'], state=row['state'], country=row['country']) raw_location = row[from_field] location_map.add_mapping(raw_location, clean_location) return location_map
def test_to_dict(self): self.assertEqual( { 'city': 'Baltimore', 'state': 'Maryland', 'country': 'United States' }, Location(city='Baltimore', state='Maryland', country='United States').to_dict())
def test_build_mapping_for_multiple_locations(self): test_input = [{ 'raw_location': 'Folsom', 'city': 'Folsom', 'state': 'California', 'country': 'United States' }, { 'raw_location': 'New York, New York', 'city': 'New York City', 'state': 'New York', 'country': 'United States' }, { 'raw_location': 'Beijing, China', 'city': 'Beijing', 'state': '', 'country': 'China' }] location_map = ValueMapBuilder.build_cached_location_map( test_input, 'raw_location') self.assert_mapping_is( Location('Folsom', 'California', 'United States'), Location('Folsom'), location_map) self.assert_mapping_is( Location('New York City', 'New York', 'United States'), Location('New York, New York'), location_map) self.assert_mapping_is(Location('Beijing', None, 'China'), Location('Beijing', 'China'), location_map)
def parse(self): if self._value == '': return Location() else: return self.parser_func()
def test_parses_everything_after_the_second_comma_into_country(self): self.assert_location_parsed_into( Location(city='Baltimore', state='Maryland', country='United States, Earth, The Universe'), ' Baltimore, Maryland,United States , Earth,The Universe ')
def test_parses_two_comma_string_into_full_location(self): self.assert_location_parsed_into( Location(city='Baltimore', state='Maryland', country='United States'), ' Baltimore, Maryland,United States ')
def test_parses_single_comma_string_into_city_and_state(self): self.assert_location_parsed_into( Location(city='Baltimore', state='United States'), ' Baltimore, United States ')
def test_trims_input(self): self.assert_location_parsed_into(Location(city='Baltimore'), ' Baltimore ')
def test_parses_commaless_string_into_city_field(self): self.assert_location_parsed_into(Location(city='Baltimore'), 'Baltimore') self.assert_location_parsed_into( Location(city='Some - complex ##58$ str'), 'Some - complex ##58$ str')
def test_parses_empty_str_into_empty_location(self): self.assert_location_parsed_into(Location(), '')
def assert_location_parsed_into(self, expected: Location, input_str: str): self.assertEqual(expected.to_dict(), LocationParser(input_str).parse().to_dict())
def test_null_location(self): self.assert_full_loc('', Location())
def assert_mapping_is(self, expected: Location, unclean_location: Location, loc_map): self.assertEqual(expected.to_dict(), loc_map.get_mapping(unclean_location).to_dict())
def test_passing_empty_string_counts_as_null(self): self.assertEqual(Location(), Location(city='', state='', country='')) self.assertEqual(Location(city='Hong Kong'), Location(city='Hong Kong', state='', country=''))
def test_full_location_is_set_given_only_one_location_field(self): self.assert_full_loc('Baltimore', Location(city='Baltimore')) self.assert_full_loc('Maryland', Location(state='Maryland')) self.assert_full_loc('United States', Location(country='United States'))