2021-02-24 08:32:44 +01:00
|
|
|
import pytest
|
|
|
|
from infi.clickhouse_orm import Database
|
|
|
|
|
|
|
|
from ee.clickhouse.client import sync_execute
|
2021-10-07 10:30:13 +02:00
|
|
|
from ee.clickhouse.sql.dead_letter_queue import (
|
|
|
|
DEAD_LETTER_QUEUE_TABLE_MV_SQL,
|
|
|
|
KAFKA_DEAD_LETTER_QUEUE_TABLE_SQL,
|
2021-11-08 16:40:05 +01:00
|
|
|
TRUNCATE_DEAD_LETTER_QUEUE_TABLE_MV_SQL,
|
2021-10-07 10:30:13 +02:00
|
|
|
)
|
2022-02-02 13:47:49 +01:00
|
|
|
from ee.clickhouse.sql.person import COMMENT_DISTINCT_ID_COLUMN_SQL
|
2021-02-24 08:32:44 +01:00
|
|
|
from posthog.settings import (
|
|
|
|
CLICKHOUSE_DATABASE,
|
|
|
|
CLICKHOUSE_HTTP_URL,
|
|
|
|
CLICKHOUSE_PASSWORD,
|
|
|
|
CLICKHOUSE_USER,
|
|
|
|
CLICKHOUSE_VERIFY,
|
|
|
|
)
|
2021-02-26 14:31:22 +01:00
|
|
|
from posthog.test.base import TestMixin
|
2021-02-24 08:32:44 +01:00
|
|
|
|
|
|
|
|
2021-11-08 16:40:05 +01:00
|
|
|
def create_clickhouse_tables(num_tables: int):
|
2021-08-25 10:58:09 +02:00
|
|
|
# Reset clickhouse tables to default before running test
|
|
|
|
# Mostly so that test runs locally work correctly
|
2021-11-08 16:40:05 +01:00
|
|
|
from ee.clickhouse.sql.cohort import CREATE_COHORTPEOPLE_TABLE_SQL
|
|
|
|
from ee.clickhouse.sql.dead_letter_queue import DEAD_LETTER_QUEUE_TABLE_SQL
|
|
|
|
from ee.clickhouse.sql.events import EVENTS_TABLE_SQL
|
|
|
|
from ee.clickhouse.sql.groups import GROUPS_TABLE_SQL
|
2021-08-25 10:58:09 +02:00
|
|
|
from ee.clickhouse.sql.person import (
|
2021-12-08 15:47:57 +01:00
|
|
|
PERSON_DISTINCT_ID2_TABLE_SQL,
|
2021-08-25 10:58:09 +02:00
|
|
|
PERSON_STATIC_COHORT_TABLE_SQL,
|
|
|
|
PERSONS_DISTINCT_ID_TABLE_SQL,
|
|
|
|
PERSONS_TABLE_SQL,
|
|
|
|
)
|
2021-11-08 16:40:05 +01:00
|
|
|
from ee.clickhouse.sql.plugin_log_entries import PLUGIN_LOG_ENTRIES_TABLE_SQL
|
|
|
|
from ee.clickhouse.sql.session_recording_events import SESSION_RECORDING_EVENTS_TABLE_SQL
|
|
|
|
|
|
|
|
# REMEMBER TO ADD ANY NEW CLICKHOUSE TABLES TO THIS ARRAY!
|
|
|
|
TABLES_TO_CREATE_DROP = [
|
2021-12-08 15:07:34 +01:00
|
|
|
EVENTS_TABLE_SQL(),
|
|
|
|
PERSONS_TABLE_SQL(),
|
|
|
|
PERSONS_DISTINCT_ID_TABLE_SQL(),
|
2021-12-08 15:47:57 +01:00
|
|
|
PERSON_DISTINCT_ID2_TABLE_SQL(),
|
2021-12-08 15:07:34 +01:00
|
|
|
PERSON_STATIC_COHORT_TABLE_SQL(),
|
|
|
|
SESSION_RECORDING_EVENTS_TABLE_SQL(),
|
|
|
|
PLUGIN_LOG_ENTRIES_TABLE_SQL(),
|
|
|
|
CREATE_COHORTPEOPLE_TABLE_SQL(),
|
2022-01-31 09:21:08 +01:00
|
|
|
KAFKA_DEAD_LETTER_QUEUE_TABLE_SQL(),
|
2021-12-08 15:07:34 +01:00
|
|
|
DEAD_LETTER_QUEUE_TABLE_SQL(),
|
2021-11-08 16:40:05 +01:00
|
|
|
DEAD_LETTER_QUEUE_TABLE_MV_SQL,
|
2021-12-08 15:07:34 +01:00
|
|
|
GROUPS_TABLE_SQL(),
|
2021-11-08 16:40:05 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
if num_tables == len(TABLES_TO_CREATE_DROP):
|
|
|
|
return
|
|
|
|
|
|
|
|
for item in TABLES_TO_CREATE_DROP:
|
|
|
|
sync_execute(item)
|
|
|
|
|
|
|
|
|
|
|
|
def reset_clickhouse_tables():
|
|
|
|
# Reset clickhouse tables to default before running test
|
|
|
|
# Mostly so that test runs locally work correctly
|
|
|
|
from ee.clickhouse.sql.cohort import TRUNCATE_COHORTPEOPLE_TABLE_SQL
|
|
|
|
from ee.clickhouse.sql.dead_letter_queue import TRUNCATE_DEAD_LETTER_QUEUE_TABLE_SQL
|
|
|
|
from ee.clickhouse.sql.events import TRUNCATE_EVENTS_TABLE_SQL
|
|
|
|
from ee.clickhouse.sql.groups import TRUNCATE_GROUPS_TABLE_SQL
|
|
|
|
from ee.clickhouse.sql.person import (
|
2021-12-08 15:47:57 +01:00
|
|
|
TRUNCATE_PERSON_DISTINCT_ID2_TABLE_SQL,
|
2021-11-08 16:40:05 +01:00
|
|
|
TRUNCATE_PERSON_DISTINCT_ID_TABLE_SQL,
|
|
|
|
TRUNCATE_PERSON_STATIC_COHORT_TABLE_SQL,
|
|
|
|
TRUNCATE_PERSON_TABLE_SQL,
|
2021-08-25 10:58:09 +02:00
|
|
|
)
|
2021-11-08 16:40:05 +01:00
|
|
|
from ee.clickhouse.sql.plugin_log_entries import TRUNCATE_PLUGIN_LOG_ENTRIES_TABLE_SQL
|
|
|
|
from ee.clickhouse.sql.session_recording_events import TRUNCATE_SESSION_RECORDING_EVENTS_TABLE_SQL
|
2021-08-25 10:58:09 +02:00
|
|
|
|
|
|
|
# REMEMBER TO ADD ANY NEW CLICKHOUSE TABLES TO THIS ARRAY!
|
|
|
|
TABLES_TO_CREATE_DROP = [
|
2022-03-01 12:08:53 +01:00
|
|
|
TRUNCATE_EVENTS_TABLE_SQL(),
|
2021-11-08 16:40:05 +01:00
|
|
|
TRUNCATE_PERSON_TABLE_SQL,
|
|
|
|
TRUNCATE_PERSON_DISTINCT_ID_TABLE_SQL,
|
2021-12-08 15:47:57 +01:00
|
|
|
TRUNCATE_PERSON_DISTINCT_ID2_TABLE_SQL,
|
2021-11-08 16:40:05 +01:00
|
|
|
TRUNCATE_PERSON_STATIC_COHORT_TABLE_SQL,
|
2022-03-01 12:08:53 +01:00
|
|
|
TRUNCATE_SESSION_RECORDING_EVENTS_TABLE_SQL(),
|
2021-11-08 16:40:05 +01:00
|
|
|
TRUNCATE_PLUGIN_LOG_ENTRIES_TABLE_SQL,
|
|
|
|
TRUNCATE_COHORTPEOPLE_TABLE_SQL,
|
|
|
|
TRUNCATE_DEAD_LETTER_QUEUE_TABLE_SQL,
|
|
|
|
TRUNCATE_DEAD_LETTER_QUEUE_TABLE_MV_SQL,
|
|
|
|
TRUNCATE_GROUPS_TABLE_SQL,
|
2021-08-25 10:58:09 +02:00
|
|
|
]
|
2021-11-08 16:40:05 +01:00
|
|
|
|
2021-08-25 10:58:09 +02:00
|
|
|
for item in TABLES_TO_CREATE_DROP:
|
2021-11-08 16:40:05 +01:00
|
|
|
sync_execute(item)
|
2021-08-25 10:58:09 +02:00
|
|
|
|
|
|
|
|
2022-02-02 13:47:49 +01:00
|
|
|
def run_additional_setup_ops():
|
|
|
|
|
|
|
|
ADDITIONAL_OPS = [COMMENT_DISTINCT_ID_COLUMN_SQL()]
|
|
|
|
|
|
|
|
for item in ADDITIONAL_OPS:
|
|
|
|
sync_execute(item)
|
|
|
|
|
|
|
|
|
2022-01-18 20:32:28 +01:00
|
|
|
@pytest.fixture(scope="package")
|
|
|
|
def django_db_setup(django_db_setup, django_db_keepdb):
|
|
|
|
database = Database(
|
|
|
|
CLICKHOUSE_DATABASE,
|
|
|
|
db_url=CLICKHOUSE_HTTP_URL,
|
|
|
|
username=CLICKHOUSE_USER,
|
|
|
|
password=CLICKHOUSE_PASSWORD,
|
|
|
|
verify_ssl_cert=CLICKHOUSE_VERIFY,
|
|
|
|
)
|
|
|
|
|
|
|
|
if not django_db_keepdb:
|
|
|
|
try:
|
|
|
|
database.drop_database()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
database.create_database() # Create database if it doesn't exist
|
|
|
|
table_count = sync_execute(
|
|
|
|
"SELECT count() FROM system.tables WHERE database = %(database)s", {"database": CLICKHOUSE_DATABASE}
|
|
|
|
)[0][0]
|
|
|
|
create_clickhouse_tables(table_count)
|
2022-02-02 13:47:49 +01:00
|
|
|
run_additional_setup_ops()
|
2022-01-18 20:32:28 +01:00
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
if django_db_keepdb:
|
|
|
|
reset_clickhouse_tables()
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
database.drop_database()
|
|
|
|
except:
|
|
|
|
pass
|
2021-02-25 15:54:55 +01:00
|
|
|
|
|
|
|
|
2021-02-26 14:31:22 +01:00
|
|
|
@pytest.fixture
|
|
|
|
def base_test_mixin_fixture():
|
|
|
|
kls = TestMixin()
|
|
|
|
kls.setUp()
|
2021-04-01 04:00:58 +02:00
|
|
|
kls.setUpTestData()
|
2021-02-26 14:31:22 +01:00
|
|
|
|
|
|
|
return kls
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def team(base_test_mixin_fixture):
|
|
|
|
return base_test_mixin_fixture.team
|
2021-11-03 19:43:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
# :TRICKY: Integrate syrupy with unittest test cases
|
|
|
|
@pytest.fixture
|
|
|
|
def unittest_snapshot(request, snapshot):
|
|
|
|
request.cls.snapshot = snapshot
|