2021-02-24 08:32:44 +01:00
|
|
|
import pytest
|
|
|
|
from infi.clickhouse_orm import Database
|
|
|
|
|
|
|
|
from ee.clickhouse.client import sync_execute
|
|
|
|
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-09-16 15:50:47 +02:00
|
|
|
from posthog.utils import is_clickhouse_enabled
|
2021-02-24 08:32:44 +01:00
|
|
|
|
|
|
|
|
2021-08-25 10:58:09 +02:00
|
|
|
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 CREATE_COHORTPEOPLE_TABLE_SQL, DROP_COHORTPEOPLE_TABLE_SQL
|
|
|
|
from ee.clickhouse.sql.events import DROP_EVENTS_TABLE_SQL, EVENTS_TABLE_SQL
|
|
|
|
from ee.clickhouse.sql.person import (
|
|
|
|
DROP_PERSON_DISTINCT_ID_TABLE_SQL,
|
|
|
|
DROP_PERSON_STATIC_COHORT_TABLE_SQL,
|
|
|
|
DROP_PERSON_TABLE_SQL,
|
|
|
|
PERSON_STATIC_COHORT_TABLE_SQL,
|
|
|
|
PERSONS_DISTINCT_ID_TABLE_SQL,
|
|
|
|
PERSONS_TABLE_SQL,
|
|
|
|
)
|
|
|
|
from ee.clickhouse.sql.plugin_log_entries import DROP_PLUGIN_LOG_ENTRIES_TABLE_SQL, PLUGIN_LOG_ENTRIES_TABLE_SQL
|
|
|
|
from ee.clickhouse.sql.session_recording_events import (
|
|
|
|
DROP_SESSION_RECORDING_EVENTS_TABLE_SQL,
|
|
|
|
SESSION_RECORDING_EVENTS_TABLE_SQL,
|
|
|
|
)
|
|
|
|
|
|
|
|
# REMEMBER TO ADD ANY NEW CLICKHOUSE TABLES TO THIS ARRAY!
|
|
|
|
TABLES_TO_CREATE_DROP = [
|
|
|
|
(DROP_EVENTS_TABLE_SQL, EVENTS_TABLE_SQL),
|
|
|
|
(DROP_PERSON_TABLE_SQL, PERSONS_TABLE_SQL),
|
|
|
|
(DROP_PERSON_DISTINCT_ID_TABLE_SQL, PERSONS_DISTINCT_ID_TABLE_SQL),
|
|
|
|
(DROP_PERSON_STATIC_COHORT_TABLE_SQL, PERSON_STATIC_COHORT_TABLE_SQL),
|
|
|
|
(DROP_SESSION_RECORDING_EVENTS_TABLE_SQL, SESSION_RECORDING_EVENTS_TABLE_SQL),
|
|
|
|
(DROP_PLUGIN_LOG_ENTRIES_TABLE_SQL, PLUGIN_LOG_ENTRIES_TABLE_SQL),
|
|
|
|
(DROP_COHORTPEOPLE_TABLE_SQL, CREATE_COHORTPEOPLE_TABLE_SQL),
|
|
|
|
]
|
|
|
|
for item in TABLES_TO_CREATE_DROP:
|
2021-09-22 12:22:52 +02:00
|
|
|
sync_execute(item[0])
|
|
|
|
sync_execute(item[1])
|
2021-08-25 10:58:09 +02:00
|
|
|
|
|
|
|
|
2021-09-16 15:50:47 +02:00
|
|
|
if is_clickhouse_enabled():
|
2021-02-24 08:32:44 +01:00
|
|
|
|
2021-09-16 15:50:47 +02: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,
|
|
|
|
)
|
2021-02-24 08:32:44 +01:00
|
|
|
|
2021-09-16 15:50:47 +02:00
|
|
|
if not django_db_keepdb:
|
|
|
|
try:
|
|
|
|
database.drop_database()
|
|
|
|
except:
|
|
|
|
pass
|
2021-02-24 08:32:44 +01:00
|
|
|
|
2021-09-16 15:50:47 +02:00
|
|
|
if not django_db_keepdb or not database.db_exists:
|
|
|
|
database.create_database()
|
2021-02-24 08:32:44 +01:00
|
|
|
|
2021-09-16 15:50:47 +02:00
|
|
|
reset_clickhouse_tables()
|
2021-02-24 08:32:44 +01:00
|
|
|
|
2021-09-16 15:50:47 +02:00
|
|
|
yield
|
|
|
|
|
|
|
|
if not django_db_keepdb:
|
|
|
|
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
|