This commit is contained in:
Yatsishin Ilya 2022-01-21 17:03:48 +00:00
parent 25547dee91
commit 23cb226904

View File

@ -1,5 +1,4 @@
import pytest
import time
import psycopg2
from helpers.cluster import ClickHouseCluster
@ -10,10 +9,14 @@ cluster = ClickHouseCluster(__file__)
node1 = cluster.add_instance('node1', main_configs=["configs/named_collections.xml"], with_postgres=True)
postgres_table_template = """
CREATE TABLE IF NOT EXISTS {} (
CREATE TABLE {} (
id Integer NOT NULL, value Integer, PRIMARY KEY (id))
"""
postgres_drop_table_template = """
DROP TABLE {}
"""
def get_postgres_conn(cluster, database=False):
if database == True:
conn_string = f"host={cluster.postgres_ip} port={cluster.postgres_port} dbname='test_database' user='postgres' password='mysecretpassword'"
@ -31,6 +34,9 @@ def create_postgres_table(cursor, table_name):
# database was specified in connection string
cursor.execute(postgres_table_template.format(table_name))
def drop_postgres_table(cursor, table_name):
# database was specified in connection string
cursor.execute(postgres_drop_table_template.format(table_name))
@pytest.fixture(scope="module")
def started_cluster():
@ -66,6 +72,8 @@ def test_postgres_database_engine_with_postgres_ddl(started_cluster):
node1.query("DROP DATABASE test_database")
assert 'test_database' not in node1.query('SHOW DATABASES')
drop_postgres_table(cursor, 'test_table')
def test_postgresql_database_engine_with_clickhouse_ddl(started_cluster):
conn = get_postgres_conn(started_cluster, True)
@ -92,6 +100,8 @@ def test_postgresql_database_engine_with_clickhouse_ddl(started_cluster):
node1.query("DROP DATABASE test_database")
assert 'test_database' not in node1.query('SHOW DATABASES')
drop_postgres_table(cursor, 'test_table')
def test_postgresql_database_engine_queries(started_cluster):
conn = get_postgres_conn(started_cluster, True)
@ -106,7 +116,7 @@ def test_postgresql_database_engine_queries(started_cluster):
node1.query("INSERT INTO test_database.test_table SELECT number, number from numbers(10000)")
assert node1.query("SELECT count() FROM test_database.test_table").rstrip() == '10000'
cursor.execute('DROP TABLE test_table;')
drop_postgres_table(cursor, 'test_table')
assert 'test_table' not in node1.query('SHOW TABLES FROM test_database')
node1.query("DROP DATABASE test_database")
@ -121,7 +131,7 @@ def test_get_create_table_query_with_multidim_arrays(started_cluster):
"CREATE DATABASE test_database ENGINE = PostgreSQL('postgres1:5432', 'test_database', 'postgres', 'mysecretpassword')")
cursor.execute("""
CREATE TABLE IF NOT EXISTS array_columns (
CREATE TABLE array_columns (
b Integer[][][] NOT NULL,
c Integer[][][]
)""")
@ -144,6 +154,7 @@ def test_get_create_table_query_with_multidim_arrays(started_cluster):
node1.query("DROP DATABASE test_database")
assert 'test_database' not in node1.query('SHOW DATABASES')
drop_postgres_table(cursor, 'array_columns')
def test_postgresql_database_engine_table_cache(started_cluster):
@ -187,9 +198,6 @@ def test_postgresql_database_with_schema(started_cluster):
conn = get_postgres_conn(started_cluster, True)
cursor = conn.cursor()
cursor.execute('DROP SCHEMA IF EXISTS test_schema CASCADE')
cursor.execute('DROP SCHEMA IF EXISTS "test.nice.schema" CASCADE')
cursor.execute('CREATE SCHEMA test_schema')
cursor.execute('CREATE TABLE test_schema.table1 (a integer)')
cursor.execute('CREATE TABLE test_schema.table2 (a integer)')
@ -207,6 +215,9 @@ def test_postgresql_database_with_schema(started_cluster):
assert node1.query("SELECT count() FROM test_database.table1").rstrip() == '10000'
node1.query("DROP DATABASE test_database")
cursor.execute('DROP SCHEMA test_schema CASCADE')
cursor.execute('DROP TABLE table3')
def test_predefined_connection_configuration(started_cluster):
cursor = started_cluster.postgres_conn.cursor()
@ -218,7 +229,6 @@ def test_predefined_connection_configuration(started_cluster):
node1.query("INSERT INTO postgres_database.test_table SELECT number, number from numbers(100)")
assert (node1.query(f"SELECT count() FROM postgres_database.test_table").rstrip() == '100')
cursor.execute('DROP SCHEMA IF EXISTS test_schema')
cursor.execute('CREATE SCHEMA test_schema')
cursor.execute('CREATE TABLE test_schema.test_table (a integer)')
@ -236,6 +246,8 @@ def test_predefined_connection_configuration(started_cluster):
node1.query("DROP DATABASE postgres_database")
cursor.execute(f'DROP TABLE test_table ')
cursor.execute('DROP SCHEMA IF EXISTS test_schema CASCADE')
if __name__ == '__main__':