Skip to content

Commit

Permalink
Merge pull request #1426 from CartoDB/1416-cartodbfy-default-true
Browse files Browse the repository at this point in the history
Cartodbfy by default in to_carto
  • Loading branch information
Jesus89 committed Dec 31, 2019
2 parents 895e205 + 4751118 commit b6fd722
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cartoframes/data/services/geocoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def _cached_geocode(self, source, table_name, street, city, state, country, dry_
if self._source_manager.is_table():
raise ValueError('cached geocoding cannot be used with tables')

to_carto(source, tmp_table_name, self._credentials, force_cartodbfy=True, log_enabled=False)
to_carto(source, tmp_table_name, self._credentials, log_enabled=False)

self._execute_query(
"""
Expand Down Expand Up @@ -334,7 +334,7 @@ def _table_for_geocoding(self, source, table_name, if_exists, dry_run):
if not input_table_name:
input_table_name = self._new_temporary_table_name()
is_temporary = True
to_carto(source, input_table_name, self._credentials, if_exists, force_cartodbfy=True, log_enabled=False)
to_carto(source, input_table_name, self._credentials, if_exists, log_enabled=False)
return (input_table_name, is_temporary)

# Note that this can be optimized for non in-place cases (table_name is not None), e.g.
Expand Down
9 changes: 4 additions & 5 deletions cartoframes/io/carto.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def read_carto(source, credentials=None, limit=None, retry_times=3, schema=None,


def to_carto(dataframe, table_name, credentials=None, if_exists='fail', geom_col=None, index=False, index_label=None,
force_cartodbfy=False, log_enabled=True):
cartodbfy=True, log_enabled=True):
"""
Upload a Dataframe to CARTO.
Expand All @@ -74,6 +74,8 @@ def to_carto(dataframe, table_name, credentials=None, if_exists='fail', geom_col
index (bool, optional): write the index in the table. Default is False.
index_label (str, optional): name of the index column in the table. By default it
uses the name of the index from the dataframe.
cartodbfy (bool, optional): convert the table to CARTO format. Default True. More info
`here <https://carto.com/developers/sql-api/guides/creating-tables/#create-tables>`.
Raises:
ValueError:
Expand Down Expand Up @@ -106,13 +108,10 @@ def to_carto(dataframe, table_name, credentials=None, if_exists='fail', geom_col
# Decode geometry column
cdf.set_geometry(geom_col, inplace=True)

has_geometry = cdf.has_geometry()
if has_geometry:
if cdf.has_geometry():
# Prepare geometry column for the upload
cdf.rename_geometry(GEOM_COLUMN_NAME, inplace=True)

cartodbfy = force_cartodbfy or has_geometry

table_name = context_manager.copy_from(cdf, table_name, if_exists, cartodbfy)

if log_enabled:
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/io/test_carto.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,20 @@ def test_read_carto_decode_geom_false(mocker):
assert expected.equals(cdf)


def test_to_carto(mocker):
# Given
cm_mock = mocker.patch.object(ContextManager, 'copy_from')
df = GeoDataFrame({'geometry': [Point([0, 0])]})

# When
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


def test_to_carto_wrong_dataframe(mocker):
# When
with pytest.raises(ValueError) as e:
Expand Down Expand Up @@ -223,6 +237,30 @@ def test_to_carto_wrong_if_exists(mocker):
assert str(e.value) == 'Wrong option for the `if_exists` param. You should provide: fail, replace, append.'


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_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_copy_table_wrong_table_name(mocker):
# When
with pytest.raises(ValueError) as e:
Expand Down

0 comments on commit b6fd722

Please sign in to comment.