0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-21 13:39:22 +01:00

Set debug, test, and primary_db env variables based on context (#7250)

* set debug, test, and primary_db env variables based on context

* remove unnecessary print

* also use pytest.ini for some tests run straight from pytest at the CLI
This commit is contained in:
Paul D'Ambra 2021-11-20 18:18:39 +01:00 committed by GitHub
parent 9a3c807c07
commit 50b12d9bb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 62 additions and 2 deletions

View File

@ -23,7 +23,5 @@ if [ $# -eq 0 ]; then
fi
export REDIS_URL='redis:///'
export TEST=1
export DEBUG=1
psql posthog -c "drop database if exists test_posthog"
nodemon -w ./posthog -w ./ee --ext py --exec "OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES pytest --reuse-db -s $* --snapshot-update; mypy posthog ee"

12
ee/pytest.ini Normal file
View File

@ -0,0 +1,12 @@
[pytest]
env =
PRIMARY_DB=clickhouse
DEBUG=1
TEST=1
DJANGO_SETTINGS_MODULE = posthog.settings
addopts = -p no:warnings --reuse-db
markers =
ee
clickhouse_only
skip_on_multitenancy

View File

@ -51,6 +51,49 @@ def get_list(text: str) -> List[str]:
return [item.strip() for item in text.split(",")]
"""
There are several options:
1) running in pycharm
second argument is "test"
2) running pytest at the CLI
first argument is the path to pytest and ends pytest
3) running pytest using the script at /bin/tests
first argument is the path to pytest and ends pytest
4) running in some other context (e.g. in prod)
first argument does not end pytest
second argument is not test
Arguments to the application will be slightly different in each case
So, in order to set test variables we need to look in slightly different places
The /bin/tests file also runs mypy to do type checking. This needs DEBUG=1 set too
Running pytest directly does not always load django settings but sometimes needs these environment variables.
We use pytest-env to let us set environment variables from the closest pytest.ini
We can't rely only on pytest.ini as some tests evaluate this file before its environment variables have been read
"""
runner = sys.argv[0] if len(sys.argv) >= 1 else None
if runner:
cmd = sys.argv[1] if len(sys.argv) >= 2 else None
if cmd == "test" or runner.endswith("pytest") or runner.endswith("mypy"):
print("Running in test mode. Setting DEBUG and TEST environment variables.")
os.environ["DEBUG"] = "1"
os.environ["TEST"] = "1"
try:
path = sys.argv[2] if cmd == "test" else sys.argv[3]
if path.startswith("ee"):
print("Running EE tests. Setting clickhouse as primary database.")
os.environ["PRIMARY_DB"] = "clickhouse"
except IndexError:
# there was no path, we don't want to set PRIMARY_DB
pass
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

View File

@ -1,4 +1,7 @@
[pytest]
env =
DEBUG=1
TEST=1
DJANGO_SETTINGS_MODULE = posthog.settings
addopts = -p no:warnings --reuse-db

View File

@ -26,6 +26,7 @@ black
isort
pytest
pytest-django
pytest-env
pytest-mock
pytest-cov
pytest-split

View File

@ -127,6 +127,8 @@ pytest-cov==2.12.1
# via -r requirements-dev.in
pytest-django==4.1.0
# via -r requirements-dev.in
pytest-env==0.6.2
# via -r requirements-dev.in
pytest-mock==3.5.1
# via -r requirements-dev.in
pytest-split==0.3.3
@ -136,6 +138,7 @@ pytest==6.2.2
# -r requirements-dev.in
# pytest-cov
# pytest-django
# pytest-env
# pytest-mock
# pytest-split
# syrupy