def test_where_scope_us_48_without_level(): name = 'oslo' oslo_in_norway = geodata.geocode(names=name).where( name, scope='norway').get_geocodes() oslo_in_usa = geodata.geocode(names=name).where( name, scope='us-48').get_geocodes() assert oslo_in_norway.id[0] != oslo_in_usa.id[0]
def test_error_when_names_and_parents_have_different_size(): check_validation_error( 'Invalid request: countries count(2) != names count(1)', lambda: geocode(names='foo').countries(['bar', 'baz'])) check_validation_error( 'Invalid request: states count(2) != names count(1)', lambda: geocode(names='foo').states(['bar', 'baz'])) check_validation_error( 'Invalid request: counties count(2) != names count(1)', lambda: geocode(names='foo').counties(['bar', 'baz']))
def test_regions_with_highlights(mock_geocoding): try: geocode(level=LEVEL, names=FILTER_LIST).scope(REGION_NAME).highlights(True).get_geocodes() except Exception: pass # response doesn't contain proper feature with ids - ignore mock_geocoding.assert_called_with( GeocodingRequest(requested_payload=[PayloadKind.highlights], resolution=None, region_queries=[REGION_QUERY_LA, REGION_QUERY_NY], scope=MapRegion.with_name(REGION_NAME), level=LEVEL_KIND, namesake_example_limit=NAMESAKES_EXAMPLE_LIMIT, allow_ambiguous=False ) )
def test_request_without_name(): assert_that(geocode(level='county').states('New York')) \ .has_level(eq(LevelKind.county)) \ .has_query(QueryMatcher() .with_name(None) .state(eq_map_region_with_name('New York')) )
def test_request_us_48_in_name(): assert_that(geocode(level='state', names='us-48'))\ .has_scope(ScopeMatcher().empty())\ .has_query(QueryMatcher() .with_name('us-48') .scope(empty()) )
def test_simple_scope(): florida_with_country = geodata.geocode( 'state', names=['florida', 'florida'], countries=['Uruguay', 'usa'] ).get_geocodes() assert florida_with_country[DF_COLUMN_ID][0] != florida_with_country[DF_COLUMN_ID][1] florida_with_scope = geodata.geocode( 'state', names=['florida'], scope='Uruguay' ).get_geocodes() assert florida_with_country[DF_COLUMN_ID][0] == florida_with_scope[DF_COLUMN_ID][0]
def test_no_parents_where_should_override_scope(): # without parents should update scope for matching name request = geocode(names='foo') \ .where('foo', scope='bar') \ ._build_request() assert_that(request) \ .has_query(no_parents(request=eq('foo'), scope=eq_map_region_with_name('bar')))
def test_when_regions_in_parent_should_take_region_id(): builder = geocode(names='foo') \ .states(make_simple_region('bar')) assert_that(builder) \ .has_query(QueryMatcher() .with_name('foo')\ .state(eq_map_region_with_id('bar_id')) )
def test_ambiguity_closest_to_boston_by_coord(): r = geodata.geocode( level='city', names='Warwick' ) \ .where('Warwick', closest_to=ShapelyPoint(BOSTON_LON, BOSTON_LAT)) assert_row(r.get_geocodes(), id=WARWICK_ID, found_name='Warwick') assert_row(r.get_centroids(), lon=WARWICK_LON, lat=WARWICK_LAT)
def test_ambiguity_closest_to_boston_by_name(): r = geodata.geocode( level='city', names='Warwick' ) \ .where('Warwick', closest_to=geodata.geocode_cities('boston')) assert_row(r.get_geocodes(), id=WARWICK_ID, found_name='Warwick') assert_row(r.get_centroids(), lon=WARWICK_LON, lat=WARWICK_LAT)
def test_allow_ambiguous(): request = geocode(names='foo')\ .allow_ambiguous()\ ._build_request() assert_that(request) \ .has_query(QueryMatcher() .with_name('foo') .ambiguity_resolver(eq(AmbiguityResolver(ignoring_strategy=IgnoringStrategyKind.take_namesakes))) )
def test_us48_with_extra_and_missing_names(): us48 = geodata.geocode(names=['texas', 'blahblahblah', 'us-48', 'nevada'])\ .ignore_not_found()\ .get_geocodes() # still 51 - drop missing completley assert 51 == len(us48) assert_request_and_found_name_are_equal(us48, range(1, 49)) assert_row(us48, index=0, names='texas', found_name='Texas') assert_row(us48, index=50, names='nevada', found_name='Nevada')
def test_resolution(): r = geodata.geocode(names=[ 'monaco', ], level='country') sizes = [] for res in range(1, 16): b = r.get_boundaries(res) sizes.append(len(b)) assert 15 == len(sizes)
def test_parents_in_regions_object_and_geo_data_frame(): boston = geodata.geocode_cities('boston').counties('suffolk').states('massachusetts').countries('usa') assert_row(boston.get_geocodes(), names='boston', county='suffolk', state='massachusetts', country='usa') assert_row(boston.get_limits(), names='boston', county='suffolk', state='massachusetts', country='usa') assert_row(boston.get_centroids(), names='boston', county='suffolk', state='massachusetts', country='usa') assert_row(boston.get_boundaries(), names='boston', county='suffolk', state='massachusetts', country='usa') # antimeridian ru = geodata.geocode(level='country', names='russia') assert_row(ru.get_geocodes(), country='russia', city=NO_COLUMN, county=NO_COLUMN, state=NO_COLUMN) assert_row(ru.get_limits(), country=['russia', 'russia'], city=NO_COLUMN, county=NO_COLUMN, state=NO_COLUMN)
def test_where_closets_to_point(): request = geocode(names=['foo']) \ .states(['bar']) \ .where(name='foo', state='bar', closest_to=shapely.geometry.Point(1, 2)) \ ._build_request() assert_that(request) \ .has_query(QueryMatcher() .with_name('foo') .state(eq_map_region_with_name('bar')) .ambiguity_resolver(eq(AmbiguityResolver(closest_coord=GeoPoint(1, 2)))) )
def test_where_scope_is_box(): request = geocode(names=['foo']) \ .states(['bar']) \ .where(name='foo', state='bar', scope=shapely.geometry.box(1, 2, 3, 4)) \ ._build_request() assert_that(request) \ .has_query(QueryMatcher() .with_name('foo') .state(eq_map_region_with_name('bar')) .ambiguity_resolver(eq(AmbiguityResolver(box=GeoRect(1, 2, 3, 4)))) )
def test_where_request_level_detection(): """ where('new york', region=geodata.geocode_states('new york')) gives county as first detected level where('boston', region=geodata.geocode_countries('usa')) gives city as first detected level But 'new york' also matches a city name so common level should be a city """ r = geodata.geocode(names=['new york', 'boston']) \ .where('new york', scope=geodata.geocode_states('new york')) \ .where('boston', scope=geodata.geocode_countries('usa')) \ .get_geocodes() assert [NYC_ID, BOSTON_ID] == r.id.tolist()
def test_parents_can_contain_nulls(): builder = geocode(names=['foo', 'bar'])\ .states([None, 'baz']) assert_that(builder) \ .has_query(QueryMatcher() .with_name('foo') \ .state(empty()) ) \ .has_query(QueryMatcher() .with_name('bar') \ .state(eq_map_region_with_name('baz')) )
def test_global_scope(): # scope should be applied to whole request, not to queries builder: NamesGeocoder = geocode(names='foo') # single str scope assert_that(builder.scope('bar')) \ .has_scope(ScopeMatcher().with_names(['bar'])) \ .has_query(QueryMatcher().with_name('foo').scope(empty())) # single regions scope assert_that(builder.scope(make_simple_region('bar', 'bar_id'))) \ .has_scope(ScopeMatcher().with_ids(['bar_id'])) \ .has_query(QueryMatcher().with_name('foo').scope(empty()))
def test_allow_ambiguous_and_closest_to(): request = geocode(names=['foo', 'bar'])\ .where('foo', closest_to=shapely.geometry.Point(1, 2))\ .allow_ambiguous()\ ._build_request() assert_that(request) \ .allows_ambiguous()\ .has_query(QueryMatcher() .with_name('foo') .ambiguity_resolver(eq(AmbiguityResolver(closest_coord=GeoPoint(1, 2)))) ) \ .has_query(QueryMatcher() .with_name('bar') .ambiguity_resolver(eq(AmbiguityResolver(ignoring_strategy=IgnoringStrategyKind.take_namesakes))) )
def test_where_with_given_country_should_be_used(): # should update scope only for matching name and parents - query with index 1 request = geocode(names=['foo', 'foo']) \ .countries(['bar', 'baz']) \ .where(name='foo', country='baz', scope='spam') \ ._build_request() assert_that(request) \ .has_query(QueryMatcher() .with_name('foo') .country(eq_map_region_with_name('bar')) .scope(empty()) ) \ .has_query(QueryMatcher() .with_name('foo') .country(eq_map_region_with_name('baz')) .scope(eq_map_region_with_name('spam')) )
def test_ambiguity_scope_boston_by_box(): boston = geodata.geocode_cities('boston').get_centroids().iloc[[0]] buffer = 0.6 boston_centroid = ShapelyPoint(boston.geometry.x, boston.geometry.y) r = geodata.geocode( level='city', names='Warwick' ) \ .where('Warwick', scope=shapely.geometry.box( boston_centroid.x - buffer, boston_centroid.y - buffer, boston_centroid.x + buffer, boston_centroid.y + buffer )) assert_row(r.get_geocodes(), id=WARWICK_ID, found_name='Warwick') assert_row(r.get_centroids(), lon=WARWICK_LON, lat=WARWICK_LAT)
def test_simple(): request = geocode(names='foo')\ ._build_request() assert_that(request) \ .has_query(no_parents(request=eq('foo')))
def test_parents_always_positional(): check_validation_error( "Invalid request: countries count(1) != names count(2)", lambda: geocode(names=['foo', 'bar']).countries('baz'))
def test_error_list_scopein_request(): check_validation_error( "Unsupported 'scope' type. Expected 'str' or 'Geocoder' but was 'list'", lambda: geocode(names='foo').scope(['bar', 'baz']))
def test_error_multi_entries_map_region_scope_in_request(): check_validation_error( "'scope' has 2 entries, but expected to have exactly 1", lambda: geocode(names='foo').scope( make_simple_region(['bar', 'baz'], ['bar_id', 'baz_id'])))
def test_error_for_where_with_unknown_name_and_parents(): check_validation_error( "bar(country=baz) is not found in names", lambda: geocode(names='foo').where('bar', country='baz', scope='spam'))
def test_error_for_where_with_unknown_name(): check_validation_error( "bar is not found in names", lambda: geocode(names='foo').where('bar', scope='baz'))
def test_error_where_scope_len_is_invalid(): check_validation_error( "Unsupported 'scope' type. Expected 'str' or 'Geocoder' but was 'list'", lambda: geocode(names='foo').where('foo', scope=['bar', 'baz']))
def test_error_when_country_and_scope_set_should_show_error(): # scope can't work with given country parent. check_validation_error( "Invalid request: countries and scope can't be used simultaneously", lambda: geocode(names='foo').countries('bar').scope('baz'))