Example #1
0
def small_csv():
    """
    Tests that we can select some data with the client
    """
    cleanup()
    logger.debug('starting setup')

    # put the file onto s3
    datafile = os.path.join(cur_dir, 'data/small_unquoted.csv')
    s3.Bucket(bucket).upload_file(datafile, key)
    logger.debug('uploaded file')

    # create the table in Athena
    query = '''CREATE EXTERNAL TABLE `{db}`.`{table}`
        (`id` int, `date` date, `float` float, `message` string)
        ROW FORMAT DELIMITED
            FIELDS TERMINATED BY ','
            LINES TERMINATED BY '\\n'
        LOCATION 's3://{bucket}/{path}/'
        tblproperties ("skip.header.line.count"="1")'''.format(db=database,
                                                               table=table,
                                                               bucket=bucket,
                                                               path=path)

    Client(results=bucket).athena_query(
        'create database if not exists {}'.format(database))
    logger.debug('created database')

    Client(results=bucket).athena_query(query)
    logger.debug('created table')

    yield
    cleanup()
Example #2
0
def test_wait(mocker):
    """ 
    Assert that for some good result, the wait function succeeds
    """
    client = Client()
    mocker.patch.object(client.client, 'get_query_execution')

    response = {'QueryExecution': {'Status': {'State': 'SUCCEEDED'}}}

    client.client.get_query_execution.return_value = response

    result = client.wait_for_results('abc123')
    assert result
Example #3
0
def cleanup():
    Client(results=bucket).athena_query(
        'drop database if exists {} cascade'.format(database))
    logger.debug('dropped database')

    boto3.client('s3').delete_object(Bucket=bucket, Key=key)
    logger.debug('deleted object')
Example #4
0
def test_show_databases(small_csv):
    """
    Test that we can inspect databases
    """
    query = 'show databases'
    result = Client(results='pythena-int').execute(query)
    print(type(result))
Example #5
0
def test_select_data(small_csv):
    """
    Tests that we can select some data with the client
    """
    query = 'select * from {}.{}'.format(database, table)
    result = Client(results='pythena-int').execute(query)
    print(result)
    print(type(result))
Example #6
0
def test_select_constant(small_csv):
    """
    Tests that we can select a constant value (i.e. 1) - no data dependency
    """
    query = 'select 1'
    result = Client(results='pythena-int').execute(query)
    print(result)
    print(type(result))
Example #7
0
def test_show_tables(small_csv):
    """
    Test that we can inspect list of tables in our test database
    """
    query = 'show tables in {}'.format(database)
    result = Client(results='pythena-int').execute(query)
    print(result)
    print(type(result))
Example #8
0
def test_select_single_row(small_csv):
    """
    Tests that a where clause helps and works
    """
    query = 'select * from {}.{} where id = 1'.format(database, table)
    result = Client(results='pythena-int').athena_query(query)
    assert len(result) == 1
    assert result['message'][0] == 'something great!'
Example #9
0
def test_show_databases(small_csv):
    """
    Test that we can inspect databases
    """
    query = 'show databases'
    result = Client(results='pythena-int').athena_query(query)
    print(type(result))
    assert sum(result[0] == database) == 1
Example #10
0
def test_select_constant(small_csv):
    """
    Tests that we can select a constant value (i.e. 1) - no data dependency
    """
    query = 'select 1'
    result = Client(results='pythena-int').athena_query(query)
    print(result)
    print(type(result))
    assert result['_col0'][0] == 1
Example #11
0
def test_is_select_query():
    """ Tests for select queries """
    client = Client()
    assert client._is_select_query('select 1')
    assert client._is_select_query('  SELECT 1')
    assert client._is_select_query('SELECT `insert`, `update`, from nothing')
Example #12
0
def test_parse_create():
    """ Test that a create query doesn't return columns """
    client = Client()
    columns = client._get_column_names('create table abc (id int) ')
    assert columns is None
Example #13
0
def test_parse_simple_select():
    """ Test that a very simple select statement can be parsed """
    client = Client()
    columns = client._get_column_names('select a, b, c from table')
    assert columns == ['a', 'b', 'c']
Example #14
0
def test_is_not_select_query():
    """ Tests for not select queries """
    client = Client()
    assert not client._is_select_query('update something set a = b')
    assert not client._is_select_query('create table something awful')
    assert not client._is_select_query('show databases')