def test_to_carto_wrong_dataframe(mocker): # When with pytest.raises(ValueError) as e: to_carto(1234, '') # Then assert str( e.value ) == 'Wrong dataframe. You should provide a valid DataFrame instance.'
def test_to_carto_no_cartodbfy(mocker): # Given cm_mock = mocker.patch.object(ContextManager, 'copy_from') df = GeoDataFrame({'geometry': [Point([0, 0])]}) # When to_carto(df, '__table_name__', CREDENTIALS, cartodbfy=False) # Then assert cm_mock.call_args[0][3] is False
def test_to_carto_if_exists_replace(mocker): # Given cm_mock = mocker.patch.object(ContextManager, 'copy_from') df = GeoDataFrame({'geometry': [Point([0, 0])]}) # When to_carto(df, '__table_name__', CREDENTIALS, if_exists='replace') # Then assert cm_mock.call_args[0][2] == 'replace'
def test_to_carto_wrong_credentials(mocker): # Given df = GeoDataFrame({'geometry': [Point([0, 0])]}) # When with pytest.raises(ValueError) as e: to_carto(df, '__table_name__', 1234) # Then assert str(e.value) == 'Credentials attribute is required. Please pass a `Credentials` ' + \ 'instance or use the `set_default_credentials` function.'
def test_to_carto_wrong_table_name(mocker): # Given df = GeoDataFrame({'geometry': [Point([0, 0])]}) # When with pytest.raises(ValueError) as e: to_carto(df, 1234) # Then assert str( e.value) == 'Wrong table name. You should provide a valid table name.'
def test_transform_crs_to_carto(mocker): cm_mock = mocker.patch.object(ContextManager, 'copy_from') # Given gdf = GeoDataFrame({'geometry': [Point([0, 0])]}, crs='epsg:4326') # When to_carto(gdf, 'table_name', CREDENTIALS) # Then cm_mock.assert_called_once_with(mocker.ANY, 'table_name', 'fail', True)
def test_to_carto_wrong_if_exists(mocker): # Given df = GeoDataFrame({'geometry': [Point([0, 0])]}) # When with pytest.raises(ValueError) as e: to_carto(df, '__table_name__', if_exists='keep_calm') # Then assert str( e.value ) == 'Wrong option for the `if_exists` param. You should provide: fail, replace, append.'
def test_wrong_crs_to_carto(): # Given gdf = GeoDataFrame({'geometry': [Point([0, 0])]}, crs='epsg:2263') # When with pytest.raises(ValueError) as e: to_carto(gdf, 'table_name') # Then assert str( e.value ) == 'No valid geometry CRS "epsg:2263", it must be "epsg:4326".'
def test_to_carto_replace_geometry(mocker): # Given cm_mock = mocker.patch.object(ContextManager, 'copy_from') df = GeoDataFrame({'geom': 'POINT(1 1)', 'geometry': [Point([0, 0])]}) # When to_carto(df, '__table_name__', CREDENTIALS, geom_col='geom', cartodbfy=False) # Then assert str(cm_mock.call_args[0] [0]).strip() == 'the_geom\n0 POINT (1.00000 1.00000)'
def test_to_carto_chunks(mocker): # Given table_name = '__table_name__' cm_mock = mocker.patch.object(ContextManager, 'copy_from') cm_mock.return_value = table_name size = 4000 # About 1MB (1150000 bytes) gdf = GeoDataFrame([[ 'Calle Gran Vía 46', round(random.uniform(10, 100), 2), round(random.uniform(100, 1000), 2), round(random.uniform(1000, 10000), 2), 'POLYGON((-3.68831 40.42478, -3.68841 40.42478, -3.68841 40.42488, -3.68831 40.42478))' ] for _ in range(size)], columns=[ 'address', 'value1', 'value2', 'value3', 'polygon' ]) gdf.set_geometry(gdf['polygon'].apply(wkt.loads), inplace=True) # When norm_table_name = to_carto(gdf, table_name, CREDENTIALS, max_upload_size=100000, skip_quota_warning=True) # Then assert cm_mock.call_count == 12 # 12 chunks as max_upload_size is 100000 bytes and we are uploading 1150000 bytes assert cm_mock.call_args[0][1] == table_name assert cm_mock.call_args[0][2] in ['fail', 'append'] assert cm_mock.call_args[0][3] is True assert norm_table_name == table_name
def test_to_carto_quota_warning(mocker): class NoQuotaCredentials(Credentials): @property def me_data(self): return {'user_data': {'remaining_byte_quota': 0}} # Given table_name = '__table_name__' cm_mock = mocker.patch.object(ContextManager, 'copy_from') cm_mock.return_value = table_name df = GeoDataFrame({'geometry': [Point([0, 0])]}) # Then with pytest.raises(CartoException): to_carto(df, table_name, NoQuotaCredentials('fake_user', 'fake_api_key'), skip_quota_warning=False)
def test_to_carto(mocker): # Given table_name = '__table_name__' cm_mock = mocker.patch.object(ContextManager, 'copy_from') cm_mock.return_value = table_name df = GeoDataFrame({'geometry': [Point([0, 0])]}) # When norm_table_name = to_carto(df, table_name, CREDENTIALS) # Then assert cm_mock.call_args[0][1] == table_name assert cm_mock.call_args[0][2] == 'fail' assert cm_mock.call_args[0][3] is True assert norm_table_name == table_name
def test_to_carto_two_geom_columns(mocker): # Given table_name = '__table_name__' cm_mock = mocker.patch.object(ContextManager, 'copy_from') cm_mock.return_value = table_name df = GeoDataFrame({ 'geometry': [Point([0, 0])], 'the_geom': '010100000000000000000000000000000000000000' }) # When norm_table_name = to_carto(df, table_name, CREDENTIALS, skip_quota_warning=True) # Then assert cm_mock.call_args[0][1] == table_name assert cm_mock.call_args[0][2] == 'fail' assert cm_mock.call_args[0][3] is True assert norm_table_name == table_name
def test_to_carto_quota_warning_skip(mocker): class NoQuotaCredentials(Credentials): @property def me_data(self): return {'user_data': {'remaining_byte_quota': 0}} # Given table_name = '__table_name__' cm_mock = mocker.patch.object(ContextManager, 'copy_from') cm_mock.return_value = table_name df = GeoDataFrame({'geometry': [Point([0, 0])]}) # When norm_table_name = to_carto(df, table_name, NoQuotaCredentials('fake_user', 'fake_api_key'), skip_quota_warning=True) # Then assert cm_mock.call_args[0][1] == table_name assert cm_mock.call_args[0][2] == 'fail' assert cm_mock.call_args[0][3] is True assert norm_table_name == table_name