def database(connection): create = 'CREATE DATABASE IF NOT EXISTS {db}' drop = 'DROP DATABASE IF EXISTS {db}' try: yield execute(create, connection=connection) finally: execute(drop, connection=connection)
def database(connection): # drop database key, because it's not existing yet connection = dict(host=connection['host']) create = 'CREATE DATABASE IF NOT EXISTS test' drop = 'DROP DATABASE IF EXISTS test' try: yield execute(create, connection=connection) finally: execute(drop, connection=connection)
def decimals(connection, database): create = ''' CREATE TABLE IF NOT EXISTS {db}.decimals ( A UInt64, B Int32, C UInt16, D Date ) ENGINE = MergeTree(D, (A), 8192) ''' drop = 'DROP TABLE IF EXISTS {db}.decimals' try: yield execute(create, connection=connection) finally: execute(drop, connection=connection)
def xyz(connection, database): create = ''' CREATE TABLE IF NOT EXISTS {db}.xyz ( id Int64, sss String, date Date ) ENGINE = MergeTree(date, (id), 8192); ''' drop = 'DROP TABLE IF EXISTS {db}.xyz' try: yield execute(create, connection=connection) finally: execute(drop, connection=connection)
def nullable(connection, database): create = """ CREATE TABLE IF NOT EXISTS {db}.nullable ( A Int64, B Nullable(Float32), C Nullable(Int32), D Date ) ENGINE = MergeTree(D, (A), 8192) """ drop = "DROP TABLE IF EXISTS {db}.nullable" try: yield execute(create, connection=connection) finally: execute(drop, connection=connection)
def xyz2(connection, database): create = """ CREATE TABLE IF NOT EXISTS {db}.xyz2 ( id Int64, joe UInt64, sss String, date Date, jessy Int32 ) ENGINE = MergeTree(date, (id), 8192); """ drop = "DROP TABLE IF EXISTS {db}.xyz2" try: yield execute(create, connection=connection) finally: execute(drop, connection=connection)
def test_execute_long_query(decimals, connection): where_clause = " WHERE A IN {0}".format(tuple(range(1, 4000))) query = "SELECT count(*) FROM {db}.decimals " + where_clause execute(query=query, connection=connection, stream=True)
def test_execute(connection): query = "DESC system.parts FORMAT CSV;" response = execute(query, connection=connection) assert isinstance(response, bytes)
def test_wrong_query(connection): query = "SELECT * FROM default.nonexisting" with pytest.raises((ClickhouseException, RequestException)): execute(query, connection=connection)
def test_wrong_host(): query = "DESC system.parts FORMAT CSV;" with pytest.raises(ConnectionError): execute(query, connection={"host": "http://local"})
def test_execute_stream(connection): query = "DESC system.parts FORMAT CSV;" response = execute(query, stream=True, connection=connection) result = response.read() assert result
def test_wrong_host(): query = 'DESC system.parts FORMAT CSV;' with pytest.raises(ConnectionError): execute(query, connection={'host': 'http://local'})