def test_cartocontext_execute(self): """context.CartoContext.execute""" cc = cartoframes.CartoContext(base_url=self.baseurl, api_key=self.apikey) df = pd.DataFrame({'vals': list('abcd'), 'ids': list('wxyz')}) df = df.astype({'vals': str, 'ids': str}) cc.write(df, self.test_write_table, overwrite=True) self.assertEquals(Dataset.from_table(context=cc, table_name=self.test_write_table).exists(), True) cc.execute(''' DROP TABLE {table_name} '''.format(table_name=self.test_write_table)) self.assertEquals(Dataset.from_table(context=cc, table_name=self.test_write_table).exists(), False)
def test_dataset_download_and_upload(self): self.assertNotExistsTable(self.test_write_table) query = 'SELECT 1 as fakec' dataset = Dataset.from_query(query=query, context=self.cc) dataset.upload(table_name=self.test_write_table) dataset = Dataset.from_table(table_name=self.test_write_table, context=self.cc) dataset.download() dataset.upload(table_name=self.test_write_table, if_exists=Dataset.REPLACE)
def test_dataset_from_table(self): table_name = 'fake_table' dataset = Dataset.from_table(table_name=table_name, context=self.cc) self.assertIsInstance(dataset, Dataset) self.assertEqual(dataset.table_name, table_name) self.assertEqual(dataset.schema, 'public') self.assertIsNone(dataset.query) self.assertIsNone(dataset.df) self.assertIsNone(dataset.gdf) self.assertEqual(dataset.cc, self.cc) self.assertEqual(dataset.state, Dataset.STATE_REMOTE)
def test_dataset_download_bool_null(self): self.assertNotExistsTable(self.test_write_table) query = 'SELECT * FROM (values (true, true), (false, false), (false, null)) as x(fakec_bool, fakec_bool_null)' dataset = Dataset.from_query(query=query, context=self.cc) dataset.upload(table_name=self.test_write_table) dataset = Dataset.from_table(table_name=self.test_write_table, context=self.cc) df = dataset.download() self.assertEqual(df['fakec_bool'].dtype, 'bool') self.assertEqual(df['fakec_bool_null'].dtype, 'object') self.assertEqual(list(df['fakec_bool']), [True, False, False]) self.assertEqual(list(df['fakec_bool_null']), [True, False, None])
def test_dataset_schema_from_org_context(self): username = '******' class FakeCreds(): def username(self): return username class FakeContext(): def __init__(self): self.is_org = True self.creds = FakeCreds() def get_default_schema(self): return username dataset = Dataset.from_table(table_name='fake_table', context=FakeContext()) self.assertEqual(dataset.schema, username)
def test_dataset_download_validations(self): self.assertNotExistsTable(self.test_write_table) df = load_geojson(self.test_geojson) dataset = Dataset.from_dataframe(df=df) error_msg = 'You should provide a context and a table_name or query to download data.' with self.assertRaises(ValueError, msg=error_msg): dataset.download() query = 'SELECT 1 as fakec' dataset = Dataset.from_query(query=query, context=self.cc) dataset.upload(table_name=self.test_write_table) dataset.table_name = 'non_used_table' df = dataset.download() self.assertEqual('fakec' in df.columns, True) dataset = Dataset.from_table(table_name=self.test_write_table, context=self.cc) df = dataset.download() self.assertEqual('fakec' in df.columns, True)
def test_dataset_schema_from_non_org_context(self): dataset = Dataset.from_table(table_name='fake_table', context=self.cc) self.assertEqual(dataset.schema, 'public')
def test_dataset_schema_from_parameter(self): schema = 'fake_schema' dataset = Dataset.from_table(table_name='fake_table', schema=schema, context=self.cc) self.assertEqual(dataset.schema, schema)
def test_dataset_upload_validation_fails_only_with_table_name(self): table_name = 'fake_table' dataset = Dataset.from_table(table_name=table_name, context=self.cc) err_msg = 'Nothing to upload. We need data in a DataFrame or GeoDataFrame or a query to upload data to CARTO.' with self.assertRaises(ValueError, msg=err_msg): dataset.upload()