Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_batchjobstatus_methods(self):
"""context.BatchJobStatus methods"""
from cartoframes.batch import BatchJobStatus
from carto.sql import BatchSQLClient
cc = cartoframes.CartoContext(base_url=self.baseurl,
api_key=self.apikey)
batch_client = BatchSQLClient(cc.auth_client)
job_response = batch_client.create(['select 1', ])
job_status = BatchJobStatus(cc, job_response)
possible_status = ('pending', 'running', 'done',
'canceled', 'unknown', )
self.assertTrue(job_status.get_status() in possible_status)
job_status._set_status('foo')
self.assertEqual(job_status.get_status(), 'foo')
new_status = job_status.status()
self.assertSetEqual(set(new_status.keys()),
{'status', 'updated_at', 'created_at'})
# job_id as str
str_bjs = BatchJobStatus(cc, 'foo')
def test_batch_create(api_key_auth_client_usr):
sql = BatchSQLClient(api_key_auth_client_usr)
# Create query
data = sql.create(BATCH_SQL_SINGLE_QUERY)
# Get job ID
job_id = data['job_id']
# Cancel if not finished
cancel_job_if_not_finished(sql, job_id)
def test_execute_long_running_query(self, mocker):
# Given
mocker.patch('cartoframes.core.managers.context_manager._create_auth_client')
mock = mocker.patch.object(BatchSQLClient, 'create_and_wait_for_completion')
# When
cm = ContextManager(self.credentials)
cm.execute_long_running_query('query')
# Then
mock.assert_called_once_with('query')
def test_batch_multi_sql(api_key_auth_client_usr):
sql = BatchSQLClient(api_key_auth_client_usr)
# Create query
data = sql.create(BATCH_SQL_MULTI_QUERY)
# Get job ID
job_id = data['job_id']
# Cancel if not finished
cancel_job_if_not_finished(sql, job_id)
help='Set the base URL. For example:' +
' https://username.carto.com/ ' +
'(defaults to env variable CARTO_API_URL)')
parser.add_argument('--api_key', dest='CARTO_API_KEY',
default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '',
help='Api key of the account' +
' (defaults to env variable CARTO_API_KEY)')
args = parser.parse_args()
# Set authentification to CARTO
if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization:
auth_client = APIKeyAuthClient(
args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization)
batchSQLClient = BatchSQLClient(auth_client)
else:
logger.error('You need to provide valid credentials, run with -h parameter for details')
import sys
sys.exit(1)
job = batchSQLClient.create_and_wait_for_completion(args.query)
logger.info('Job finished with status {status}'.format(status=job['status']))
help='Set the base URL. For example:' +
' https://username.carto.com/ ' +
'(defaults to env variable CARTO_API_URL)')
parser.add_argument('--api_key', dest='CARTO_API_KEY',
default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '',
help='Api key of the account' +
' (defaults to env variable CARTO_API_KEY)')
args = parser.parse_args()
# Set authentification to CARTO
if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization:
auth_client = APIKeyAuthClient(
args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization)
batchSQLClient = BatchSQLClient(auth_client)
else:
logger.error('You need to provide valid credentials, run with -h parameter for details')
import sys
sys.exit(1)
# Batch SQL API operations
if args.operation == 'create':
# create a batch api job
createJob = batchSQLClient.create(args.query)
for a, b in createJob.items():
logger.info('{key}: {value}'.format(key=a, value=b))
elif args.operation == 'read':
readJob = batchSQLClient.read(args.job_id)
for a, b in readJob.items():
logger.info('{key}: {value}'.format(key=a, value=b))
elif args.operation == 'update':
def __init__(self, carto_context, job):
if isinstance(job, dict):
self.job_id = job.get('job_id')
self.last_status = job.get('status')
self.created_at = job.get('created_at')
elif isinstance(job, str):
self.job_id = job
self.last_status = None
self.created_at = None
self._batch_client = BatchSQLClient(carto_context.auth_client)
def __init__(self, carto_context, job):
if isinstance(job, dict):
self.job_id = job.get('job_id')
self.last_status = job.get('status')
self.created_at = job.get('created_at')
elif isinstance(job, str):
self.job_id = job
self.last_status = None
self.created_at = None
self._batch_client = BatchSQLClient(carto_context.auth_client)
geom_col, pgcolnames,
kwargs)
self._set_schema(_df, final_table_name, pgcolnames)
# create geometry column from long/lats if requested
if lnglat:
query = '''
UPDATE "{table_name}"
SET the_geom = CDB_LatLng("{lat}"::numeric,
"{lng}"::numeric);
SELECT CDB_TableMetadataTouch('{table_name}'::regclass);
'''.format(table_name=final_table_name,
lng=norm_colname(lnglat[0]),
lat=norm_colname(lnglat[1]))
if _df.shape[0] > MAX_ROWS_LNGLAT:
batch_client = BatchSQLClient(self.auth_client)
status = batch_client.create([query, ])
tqdm.write(
'Table successfully written to CARTO: {table_url}\n'
'`the_geom` column is being populated from `{lnglat}`. '
'Check the status of the operation with:\n'
' \033[1mBatchJobStatus(CartoContext(), \'{job_id}\''
').status()\033[0m\n'
'or try reading the table from CARTO in a couple of '
'minutes.\n'
'\033[1mNote:\033[0m `CartoContext.map` will not work on '
'this table until its geometries are created.'.format(
table_url=join_url((self.creds.base_url(),
'dataset',
final_table_name, )),
job_id=status.get('job_id'),
lnglat=str(lnglat)))
def __init__(self, credentials):
self.auth_client = APIKeyAuthClient(
base_url=credentials.base_url,
api_key=credentials.api_key,
session=credentials.session,
client_id='cartoframes_{}'.format(__version__),
user_agent='cartoframes_{}'.format(__version__)
)
self.sql_client = SQLClient(self.auth_client)
self.copy_client = CopySQLClient(self.auth_client)
self.batch_sql_client = BatchSQLClient(self.auth_client)