Skip to content

Commit

Permalink
Merge 0533cd2 into af4b6a2
Browse files Browse the repository at this point in the history
  • Loading branch information
michellemho committed Sep 8, 2017
2 parents af4b6a2 + 0533cd2 commit c728584
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
5 changes: 3 additions & 2 deletions cartoframes/context.py
Expand Up @@ -205,11 +205,12 @@ def delete(self, table_name):
None
"""
try:
self.auth_client.send(
resp = self.auth_client.send(
'api/v1/viz/{table_name}'.format(table_name=table_name),
http_method='DELETE'
)
except CartoException as err:
resp.raise_for_status()
except requests.exceptions.HTTPError as err:
warn('Failed to delete the following table from CARTO '
'account: `{table_name}`. ({err})'.format(
table_name=table_name,
Expand Down
32 changes: 32 additions & 0 deletions test/test_context.py
Expand Up @@ -9,6 +9,7 @@
from carto.auth import APIKeyAuthClient
from carto.sql import SQLClient
import pandas as pd
import warnings


class TestCartoContext(unittest.TestCase):
Expand Down Expand Up @@ -192,6 +193,37 @@ def test_cartocontext_table_exists(self):
with self.assertRaises(NameError):
cc._table_exists(self.test_read_table)

def test_cartocontext_delete(self):
"""CartoContext.delete"""
cc = cartoframes.CartoContext(base_url=self.baseurl,
api_key=self.apikey)
data = {'col1': [1,2,3],
'col2': ['a','b','c']}
schema = {'col1': int,
'col2': 'object'}
df = pd.DataFrame(data).astype(schema)

cc.write(df, self.test_delete_table)
cc.delete(self.test_delete_table)

# check that querying recently deleted table raises an exception
with self.assertRaises(CartoException):
cc.sql_client.send('select * from {}'.format(self.test_delete_table))

# try to delete a table that does not exists
def fxn():
cc.delete('non_existent_table')

with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
# Trigger a warning.
fxn()
# Verify one warning, subclass is UserWarning, and expected message
# is in warning
assert len(w) == 1
assert issubclass(w[-1].category, UserWarning)
assert "Failed to delete" in str(w[-1].message)

def test_cartocontext_send_dataframe(self):
"""CartoContext._send_dataframe"""
Expand Down

0 comments on commit c728584

Please sign in to comment.