def test__init_maps_size(self): """Layout should init properly""" layout = Layout( [Map(Layer(Source(SOURCE))), Map(Layer(Source(SOURCE)))], 1, 2) assert layout._n_size == 1 assert layout._m_size == 2
def test_is_static(self): """Layout should set correctly is_static property for each map""" layout = Layout([ Map(Layer(Source(SOURCE))), Map(Layer(Source(SOURCE))) ], viewport={'zoom': 5}) assert layout._layout[0].get('is_static') is True assert layout._layout[1].get('is_static') is True
def test_custom_viewport(self): """Layout should return a different viewport for every map""" layout = Layout([ Map(Layer(Source(SOURCE)), viewport={'zoom': 2}), Map(Layer(Source(SOURCE))) ], viewport={'zoom': 5}) assert layout._layout[0].get('viewport') == {'zoom': 2} assert layout._layout[1].get('viewport') == {'zoom': 5}
def test_global_viewport(self): """Layout should return the same viewport for every map""" layout = Layout([ Map(Layer(Source(SOURCE))), Map(Layer(Source(SOURCE))) ], viewport={'zoom': 5}) assert layout._layout[0].get('viewport') == {'zoom': 5} assert layout._layout[1].get('viewport') == {'zoom': 5}
def test_two_layers(self): """Map layer should be able to initialize two layers in the correct order""" source_1 = Source(build_geodataframe([-10, 0], [-10, 0])) source_2 = Source(build_geodataframe([0, 10], [10, 0])) layer_1 = Layer(source_1) layer_2 = Layer(source_2) map = Map([layer_1, layer_2]) assert map.layers == [layer_1, layer_2]
def test_custom_camera(self): """Layout should return the correct camera for each map""" layout = Layout([ Map(Layer(Source(SOURCE)), viewport={'zoom': 2}), Map(Layer(Source(SOURCE))) ], viewport={'zoom': 5}) assert layout._layout[0].get('camera') == { 'bearing': None, 'center': None, 'pitch': None, 'zoom': 2} assert layout._layout[1].get('camera') == { 'bearing': None, 'center': None, 'pitch': None, 'zoom': 5}
def test_source_get_credentials_username(self, mocker): """Source should return the correct credentials when username is provided""" setup_mocks(mocker) source = Source('faketable', credentials=Credentials(username='******', api_key='1234')) credentials = source.get_credentials() assert credentials['username'] == 'fakeuser' assert credentials['api_key'] == '1234' assert credentials['base_url'] == 'https://fakeuser.carto.com'
def test_source_get_credentials_base_url(self, mocker): """Source should return the correct credentials when base_url is provided""" setup_mocks(mocker) source = Source( 'faketable', credentials=Credentials(base_url='https://fakeuser.carto.com')) credentials = source.get_credentials() assert credentials['username'] == 'user' assert credentials['api_key'] == 'default_public' assert credentials['base_url'] == 'https://fakeuser.carto.com'
def test_kuviz_publisher_set_layers(self, mocker): setup_mocks(mocker, self.credentials) source_1 = Source(build_geodataframe([-10, 0], [-10, 0])) source_2 = Source(build_geodataframe([0, 10], [10, 0])) layer_1 = Layer(source_1) layer_2 = Layer(source_2) vmap = Map([layer_1, layer_2]) kuviz_publisher = KuvizPublisher(None) kuviz_publisher.set_layers(vmap.layers) assert kuviz_publisher._layers != vmap.layers assert len(kuviz_publisher._layers) == len(vmap.layers)
def test__init_maps_valid(self): """Layout should raise an error if any element in the map list is not a Map""" msg = 'All the elements in the Layout should be an instance of Map.' with pytest.raises(Exception) as e: Layout([Layer(Source(SOURCE))]) assert str(e.value) == msg
def test_nan_geometries(self): df = pd.DataFrame( {'geom': ['POINT(0 0)', np.nan, np.nan, 'POINT(0 0)', None]}) source = Source(df, geom_col='geom') assert len(source.gdf) == 2
def test_different_geometry_types_source(self, features): geojson = {"type": "FeatureCollection", "features": features} gdf = gpd.GeoDataFrame.from_features(geojson) source = Source(gdf) assert len(source.gdf) == len(features) assert source.gdf.equals(gdf)
def test_interactive_layer(self): """Map layer should indicate if the layer has interactivity configured""" source_1 = Source( build_geodataframe([-10, 0], [-10, 0], ['pop', 'name'])) layer = Layer( source_1, popup_click=[popup_element('pop'), popup_element('name')], popup_hover=[popup_element('pop', 'Pop')]) map = Map(layer) assert map.layer_defs[0].get('interactivity') == [{ 'event': 'click', 'attrs': { 'name': 'v8e0f74', 'title': 'name' } }, { 'event': 'click', 'attrs': { 'name': 'v559339', 'title': 'pop' } }, { 'event': 'hover', 'attrs': { 'name': 'v559339', 'title': 'Pop' } }]
def test_interactive_layer(self): """Map layer should indicate if the layer has interactivity configured""" source_1 = Source( build_geodataframe([-10, 0], [-10, 0], ['pop', 'name'])) layer = Layer( source_1, popup_click=[popup_element('pop'), popup_element('name')], popup_hover=[popup_element('pop', 'Pop')]) map = Map(layer) layer_def = map.layers[0].get_layer_def() assert layer_def.get('interactivity') == [{ 'event': 'click', 'attrs': { 'name': 'v6ae999', 'title': 'name', 'format': None } }, { 'event': 'click', 'attrs': { 'name': 'v4f197c', 'title': 'pop', 'format': None } }, { 'event': 'hover', 'attrs': { 'name': 'v4f197c', 'title': 'Pop', 'format': None } }]
def test_default_interactive_layer(self): """Map layer should get the default event if the interactivity is set to []""" source_1 = Source(build_geodataframe([-10, 0], [-10, 0])) layer = Layer(source_1) map = Map(layer) assert map.layer_defs[0].get('interactivity') == []
def test_map_publish_with_password(self, mocker): setup_mocks(mocker) map = Map(Layer(Source('fake_table', credentials=self.credentials))) name = 'cf_publish' kuviz_dict = map.publish(name, '1234', credentials=self.credentials) self.assert_kuviz_dict(kuviz_dict, name, 'password')
def test_source_no_credentials(self): """Source should raise an exception if there are no credentials""" with pytest.raises(ValueError) as e: Source('faketable') assert str(e.value) == ('Credentials attribute is required. ' 'Please pass a `Credentials` instance or use ' 'the `set_default_credentials` function.')
def test_different_geometry_types_source_fail(self, features): geojson = {"type": "FeatureCollection", "features": features} gdf = gpd.GeoDataFrame.from_features(geojson) with pytest.raises(ValueError) as e: Source(gdf) assert str(e.value).startswith('No valid geometry column types')
def test_empty_geometries(self): geojson = { "type": "FeatureCollection", "features": [EMPTY, POINT, EMPTY, EMPTY, POINT, EMPTY] } gdf = gpd.GeoDataFrame.from_features(geojson) source = Source(gdf) assert len(source.gdf) == 2
def test_map_publish_update_password(self, mocker): setup_mocks(mocker) map = Map(Layer(Source('fake_table', credentials=self.credentials))) name = 'cf_publish' map.publish(name, None, credentials=self.credentials) kuviz_dict = map.update_publication(name, '1234"') self.assert_kuviz_dict(kuviz_dict, name, 'password')
def test_map_publish_deletion(self, mocker): setup_mocks(mocker) map = Map(Layer(Source('fake_table', credentials=self.credentials))) name = 'cf_publish' map.publish(name, None, credentials=self.credentials) response = map.delete_publication() assert response is True
def test_create_api_key_several_sources(self, mocker): setup_mocks(mocker) source = Source('fake_table', credentials=Credentials('fakeuser')) api_key_name = 'fake_name' auth_api_client = AuthAPIClient() token, tables = auth_api_client.create_api_key( [source, source, source], name=api_key_name) assert token == TOKEN_MOCK
def test_dates_in_source(self): df = pd.DataFrame({ 'date_column': ['2019-11-10', '2019-11-11'], 'lat': [1, 2], 'lon': [1, 2] }) df['date_column'] = pd.to_datetime(df['date_column']) gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.lon, df.lat)) assert df.dtypes['date_column'] == np.dtype('datetime64[ns]') source = Source(gdf) assert source.datetime_column_names == ['date_column'] assert source.gdf.dtypes['date_column'] == np.object
def test_initialization_objects(self, mocker): """Layer should initialize layer attributes""" setup_mocks(mocker, 'layer_source') layer = Layer( Source('layer_source', credentials=Credentials('fakeuser'))) assert layer.is_basemap is False assert layer.source_data == 'SELECT * FROM "public"."layer_source"' assert isinstance(layer.source, Source) assert isinstance(layer.style, Style) assert isinstance(layer.popups, PopupList) assert isinstance(layer.legends, LegendList) assert isinstance(layer.widgets, WidgetList) assert layer.interactivity == []
def test_one_layer(self): """Map layer should be able to initialize one layer""" source = Source(build_geodataframe([-10, 0], [-10, 0])) layer = Layer(source) map = Map(layer) assert map.layers == [layer] assert len(map.layer_defs) == 1 assert map.layer_defs[0].get('interactivity') == [] assert map.layer_defs[0].get('credentials') is None assert map.layer_defs[0].get('legends') is not None assert map.layer_defs[0].get('widgets') is not None assert map.layer_defs[0].get('data') is not None assert map.layer_defs[0].get('type') == 'GeoJSON' assert map.layer_defs[0].get('viz') is not None