def test_df_rows_duplication_should_be_processed_correctly( self, mock_request): foo_id = '123' foo_name = 'foo' bar_id = '234' bar_name = 'bar' geocoding_result = Regions([ self.foo.set_query('').build_geocoded(), self.bar.set_query('').build_geocoded(), self.foo.set_query('').build_geocoded() ]) mock_request.return_value = make_success_response() \ .set_geocoded_features( [ self.foo.set_query(foo_id).set_centroid(GeoPoint(0, 1)).build_geocoded(), self.bar.set_query(bar_id).set_centroid(GeoPoint(0, 1)).build_geocoded() ] ).build() df = geocoding_result.centroids() mock_request.assert_called_with( RequestBuilder() \ .set_request_kind(RequestKind.explicit) \ .set_ids([self.foo.id, self.bar.id]) \ .set_requested_payload([PayloadKind.centroids]) \ .build() ) assert [self.foo.name, self.bar.name, self.foo.name] == df[DF_REQUEST].tolist()
def test_df_request_when_query_is_empty_should_be_taken_from_found_name_column( self, mock_request): foo_id = '123' foo_name = 'foo' geocoding_result = Regions([ FeatureBuilder().set_id(foo_id).set_query('').set_name( foo_name).build_geocoded() ]) mock_request.return_value = make_success_response() \ .set_geocoded_features( [ FeatureBuilder().set_id(foo_id).set_query(foo_id).set_name(foo_name).set_centroid(GeoPoint(0, 1)).build_geocoded() ] ).build() df = geocoding_result.centroids() mock_request.assert_called_with( RequestBuilder() \ .set_request_kind(RequestKind.explicit) .set_ids([foo_id]) \ .set_requested_payload([PayloadKind.centroids]) \ .build() ) assert foo_name == df[DF_REQUEST][0]
def test_df_rows_order(self, mock_request): geocoding_result = Regions([ self.foo.set_query('').build_geocoded(), self.bar.set_query('').build_geocoded(), self.baz.set_query('').build_geocoded(), ]) mock_request.return_value = make_success_response() \ .set_geocoded_features( [ self.foo.set_query(self.bar_id).set_centroid(GeoPoint(0, 1)).build_geocoded(), self.bar.set_query(self.foo_id).set_centroid(GeoPoint(0, 1)).build_geocoded(), self.baz.set_query(self.baz_id).set_centroid(GeoPoint(0, 1)).build_geocoded(), ] ).build() df = geocoding_result.centroids() mock_request.assert_called_with( RequestBuilder() \ .set_request_kind(RequestKind.explicit) \ .set_ids([self.foo.id, self.bar.id, self.baz.id]) \ .set_requested_payload([PayloadKind.centroids]) \ .build() ) assert [self.foo.name, self.bar.name, self.baz.name] == df[DF_REQUEST].tolist()
def test_to_dataframe(self): df = Regions([ self.foo.set_query('').set_id('123').build_geocoded(), self.bar.set_query('').set_id('456').build_geocoded(), ]).to_data_frame() assert [self.foo.name, self.bar.name] == df[DF_REQUEST].tolist()
def make_regions(self) -> Regions: regions = Regions([ make_region(USA_REQUEST, USA_NAME, USA_ID, USA_HIGHLIGHTS), make_region(RUSSIA_REQUEST, RUSSIA_NAME, RUSSIA_ID, RUSSIA_HIGHLIGHTS) ]) return regions
def make_geocode_region(request: str, name: str, geo_object_id: str, highlights: List[str], level_kind: LevelKind = LevelKind.city) -> Regions: return Regions(level_kind, [make_region(request, name, geo_object_id, highlights)])
def test_as_list(self): regions = Regions( [self.foo.build_geocoded(), self.bar.build_geocoded()]).as_list() assert 2 == len(regions) assert_region_df(self.foo, regions[0].to_data_frame()) assert_region_df(self.bar, regions[1].to_data_frame())
def test_with_regions(): actual = \ regions_builder( request=names(FOO), within=Regions(LevelKind.city, [FOO.feature])) \ ._get_queries() expected = [query(FOO.name, FOO.region)] assert expected == actual
def test_request_remove_duplicated_ids(mock_request): try: Regions([ make_region(REQUEST, REGION_NAME, REGION_ID, REGION_HIGHLIGHTS), make_region(REQUEST, REGION_NAME, REGION_ID, REGION_HIGHLIGHTS) ]).centroids() except ValueError: pass # response doesn't contain proper feature with ids - ignore mock_request.assert_called_with( ExplicitRequest(requested_payload=[PayloadKind.centroids], ids=[REGION_ID]))
def test_reorder_for_centroids_should_happen(mock_request): mock_request.return_value = make_success_response() \ .set_geocoded_features( [ FeatureBuilder().set_id('2').set_query('New York').set_name('New York').set_centroid(GeoPoint(0, 0)).build_geocoded(), FeatureBuilder().set_id('3').set_query('Las Vegas').set_name('Las Vegas').set_centroid(GeoPoint(0, 0)).build_geocoded(), FeatureBuilder().set_id('1').set_query('Los Angeles').set_name('Los Angeles').set_centroid(GeoPoint(0, 0)).build_geocoded() ] ).build() df = Regions([ make_region('Los Angeles', 'Los Angeles', '1', []), make_region('New York', 'New York', '2', []), make_region('Las Vegas', 'Las Vegas', '3', []), make_region('Los Angeles', 'Los Angeles', '1', []), ]).centroids() assert ['Los Angeles', 'New York', 'Las Vegas', 'Los Angeles'] == df[DF_FOUND_NAME].tolist()
def regions_list(*queries: Query) -> List[Regions]: return [Regions(LevelKind.city, [query.feature]) for query in queries]
def single_region(*queries: Query) -> Regions: return Regions(LevelKind.city, [query.feature for query in queries])
def regions_list(*queries: Query) -> List[Regions]: return [Regions([query.feature]) for query in queries]
def single_region(*queries: Query) -> Regions: return Regions([query.feature for query in queries])
def make_geocode_region(request: str, name: str, geo_object_id: str, highlights: List[str]) -> Regions: return Regions([make_region(request, name, geo_object_id, highlights)])
'fooid').build_geocoded() BAR = FeatureBuilder().set_query('foo').set_name('barname').set_id( 'barid').build_geocoded() @pytest.mark.parametrize( 'location,expected', [ # none (None, None), # single string ('name', MapRegion.with_name('name')), # single region (Regions([FOO, BAR]), MapRegion.with_ids([FOO.id, BAR.id])), # list of strings (['foo', 'bar' ], [MapRegion.with_name('foo'), MapRegion.with_name('bar')]), # list of regions ([Regions([FOO]), Regions([BAR]) ], [MapRegion.with_ids([FOO.id]), MapRegion.with_ids([BAR.id])]), # mix of strings and regions ([ 'foo', Regions([BAR]),