test(pytest): move pytest fixtures to conftest.py
There are a few benefits for this:
- All fixtures in one module, so single source of truth.
- The fixtures are automatically imported and injected by pytest
so no explicit imports needed for these.
- Linters won't complain about redefinition of outer scope objects.
Co-authored-by: Jens Troeger <jens.troeger@light-speed.de>
Signed-off-by: Taimoor Zaeem <taimoorzaeem@gmail.com>
This commit is contained in:
committed by
Steve Chavez
co-authored by
Jens Troeger
parent
322216c810
commit
c1d9728dc8
@@ -1,7 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
import shutil
|
import shutil
|
||||||
import pytest
|
|
||||||
import uuid
|
import uuid
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
@@ -13,71 +12,6 @@ POSTGREST_BIN = shutil.which("postgrest")
|
|||||||
SECRET = "reallyreallyreallyreallyverysafe"
|
SECRET = "reallyreallyreallyreallyverysafe"
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def dburi():
|
|
||||||
"Postgres database connection URI."
|
|
||||||
dbname = os.environ["PGDATABASE"]
|
|
||||||
host = os.environ["PGHOST"]
|
|
||||||
user = os.environ["PGUSER"]
|
|
||||||
return f"postgresql://?dbname={dbname}&host={host}&user={user}".encode()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def baseenv():
|
|
||||||
"Base environment to connect to PostgreSQL"
|
|
||||||
return {
|
|
||||||
"PGDATABASE": os.environ["PGDATABASE"],
|
|
||||||
"PGHOST": os.environ["PGHOST"],
|
|
||||||
"PGUSER": os.environ["PGUSER"],
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def defaultenv(baseenv):
|
|
||||||
"Default environment for PostgREST."
|
|
||||||
return {
|
|
||||||
**baseenv,
|
|
||||||
"PGRST_DB_CONFIG": "true",
|
|
||||||
"PGRST_LOG_LEVEL": "info",
|
|
||||||
"PGRST_DB_POOL": "1",
|
|
||||||
"PGRST_NOT_EXISTING": "should not break any tests",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def replicaenv(defaultenv):
|
|
||||||
"Default environment for a PostgREST replica."
|
|
||||||
conf = {
|
|
||||||
"PGRST_DB_ANON_ROLE": "postgrest_test_anonymous",
|
|
||||||
"PGRST_DB_SCHEMAS": "replica",
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
"primary": {
|
|
||||||
**defaultenv,
|
|
||||||
**conf,
|
|
||||||
},
|
|
||||||
"replica": {
|
|
||||||
**defaultenv,
|
|
||||||
**conf,
|
|
||||||
"PGHOST": os.environ["PGREPLICAHOST"] + "," + os.environ["PGHOST"],
|
|
||||||
"PGREPLICASLOT": os.environ["PGREPLICASLOT"],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def slow_schema_cache_env(defaultenv):
|
|
||||||
"Slow schema cache load environment PostgREST."
|
|
||||||
return {
|
|
||||||
**defaultenv,
|
|
||||||
"PGRST_INTERNAL_SCHEMA_CACHE_SLEEP": "1000", # this does a pg_sleep internally, it will cause the schema cache query to be slow
|
|
||||||
# the slow schema cache query will keep using one pool connection until it finishes
|
|
||||||
# to prevent requests waiting for PGRST_DB_POOL_ACQUISITION_TIMEOUT we'll increase the pool size (must be >= 2)
|
|
||||||
"PGRST_DB_POOL": "2",
|
|
||||||
"PGRST_DB_CHANNEL_ENABLED": "true",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def hpctixfile():
|
def hpctixfile():
|
||||||
"""
|
"""
|
||||||
Returns a unique filename for each postgrest process that is
|
Returns a unique filename for each postgrest process that is
|
||||||
|
|||||||
@@ -0,0 +1,95 @@
|
|||||||
|
import os
|
||||||
|
import pytest
|
||||||
|
from syrupy.extensions.json import SingleFileSnapshotExtension
|
||||||
|
from postgrest import run
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def dburi():
|
||||||
|
"Postgres database connection URI."
|
||||||
|
dbname = os.environ["PGDATABASE"]
|
||||||
|
host = os.environ["PGHOST"]
|
||||||
|
user = os.environ["PGUSER"]
|
||||||
|
return f"postgresql://?dbname={dbname}&host={host}&user={user}".encode()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def baseenv():
|
||||||
|
"Base environment to connect to PostgreSQL"
|
||||||
|
return {
|
||||||
|
"PGDATABASE": os.environ["PGDATABASE"],
|
||||||
|
"PGHOST": os.environ["PGHOST"],
|
||||||
|
"PGUSER": os.environ["PGUSER"],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def defaultenv(baseenv):
|
||||||
|
"Default environment for PostgREST."
|
||||||
|
return {
|
||||||
|
**baseenv,
|
||||||
|
"PGRST_DB_CONFIG": "true",
|
||||||
|
"PGRST_LOG_LEVEL": "info",
|
||||||
|
"PGRST_DB_POOL": "1",
|
||||||
|
"PGRST_NOT_EXISTING": "should not break any tests",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def replicaenv(defaultenv):
|
||||||
|
"Default environment for a PostgREST replica."
|
||||||
|
conf = {
|
||||||
|
"PGRST_DB_ANON_ROLE": "postgrest_test_anonymous",
|
||||||
|
"PGRST_DB_SCHEMAS": "replica",
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
"primary": {
|
||||||
|
**defaultenv,
|
||||||
|
**conf,
|
||||||
|
},
|
||||||
|
"replica": {
|
||||||
|
**defaultenv,
|
||||||
|
**conf,
|
||||||
|
"PGHOST": os.environ["PGREPLICAHOST"] + "," + os.environ["PGHOST"],
|
||||||
|
"PGREPLICASLOT": os.environ["PGREPLICASLOT"],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def slow_schema_cache_env(defaultenv):
|
||||||
|
"Slow schema cache load environment PostgREST."
|
||||||
|
return {
|
||||||
|
**defaultenv,
|
||||||
|
"PGRST_INTERNAL_SCHEMA_CACHE_SLEEP": "1000", # this does a pg_sleep internally, it will cause the schema cache query to be slow
|
||||||
|
# the slow schema cache query will keep using one pool connection until it finishes
|
||||||
|
# to prevent requests waiting for PGRST_DB_POOL_ACQUISITION_TIMEOUT we'll increase the pool size (must be >= 2)
|
||||||
|
"PGRST_DB_POOL": "2",
|
||||||
|
"PGRST_DB_CHANNEL_ENABLED": "true",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def metapostgrest():
|
||||||
|
"A shared postgrest instance to use for interacting with the database independently of the instance under test"
|
||||||
|
role = "meta_authenticator"
|
||||||
|
env = {
|
||||||
|
"PGDATABASE": os.environ["PGDATABASE"],
|
||||||
|
"PGHOST": os.environ["PGHOST"],
|
||||||
|
"PGUSER": role,
|
||||||
|
"PGRST_DB_ANON_ROLE": role,
|
||||||
|
"PGRST_DB_CONFIG": "true",
|
||||||
|
"PGRST_LOG_LEVEL": "info",
|
||||||
|
"PGRST_DB_POOL": "1",
|
||||||
|
}
|
||||||
|
with run(env=env) as postgrest:
|
||||||
|
yield postgrest
|
||||||
|
|
||||||
|
|
||||||
|
class YamlSnapshotExtension(SingleFileSnapshotExtension):
|
||||||
|
_file_extension = "yaml"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def snapshot_yaml(snapshot):
|
||||||
|
return snapshot.use_extension(YamlSnapshotExtension)
|
||||||
@@ -10,7 +10,6 @@ import tempfile
|
|||||||
import time
|
import time
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
|
||||||
import pytest
|
|
||||||
import requests
|
import requests
|
||||||
import requests_unixsocket
|
import requests_unixsocket
|
||||||
|
|
||||||
@@ -158,23 +157,6 @@ def run(
|
|||||||
process.wait()
|
process.wait()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
|
||||||
def metapostgrest():
|
|
||||||
"A shared postgrest instance to use for interacting with the database independently of the instance under test"
|
|
||||||
role = "meta_authenticator"
|
|
||||||
env = {
|
|
||||||
"PGDATABASE": os.environ["PGDATABASE"],
|
|
||||||
"PGHOST": os.environ["PGHOST"],
|
|
||||||
"PGUSER": role,
|
|
||||||
"PGRST_DB_ANON_ROLE": role,
|
|
||||||
"PGRST_DB_CONFIG": "true",
|
|
||||||
"PGRST_LOG_LEVEL": "info",
|
|
||||||
"PGRST_DB_POOL": "1",
|
|
||||||
}
|
|
||||||
with run(env=env) as postgrest:
|
|
||||||
yield postgrest
|
|
||||||
|
|
||||||
|
|
||||||
def freeport(used_port=None):
|
def freeport(used_port=None):
|
||||||
"Find a free port on localhost."
|
"Find a free port on localhost."
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from config import *
|
|
||||||
from util import *
|
from util import *
|
||||||
from postgrest import *
|
from postgrest import *
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ from operator import attrgetter
|
|||||||
import signal
|
import signal
|
||||||
import subprocess
|
import subprocess
|
||||||
import pytest
|
import pytest
|
||||||
from syrupy.extensions.json import SingleFileSnapshotExtension
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from config import *
|
from config import *
|
||||||
@@ -20,15 +19,6 @@ class ExtraNewLinesDumper(yaml.SafeDumper):
|
|||||||
super().write_line_break()
|
super().write_line_break()
|
||||||
|
|
||||||
|
|
||||||
class YamlSnapshotExtension(SingleFileSnapshotExtension):
|
|
||||||
_file_extension = "yaml"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def snapshot_yaml(snapshot):
|
|
||||||
return snapshot.use_extension(YamlSnapshotExtension)
|
|
||||||
|
|
||||||
|
|
||||||
def itemgetter(*items):
|
def itemgetter(*items):
|
||||||
"operator.itemgetter with None as fallback when key does not exist"
|
"operator.itemgetter with None as fallback when key does not exist"
|
||||||
if len(items) == 1:
|
if len(items) == 1:
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
"IO tests for PostgREST started on replicas"
|
"IO tests for PostgREST started on replicas"
|
||||||
|
|
||||||
from config import *
|
|
||||||
from util import *
|
from util import *
|
||||||
from postgrest import *
|
from postgrest import *
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from config import *
|
|
||||||
from util import *
|
from util import *
|
||||||
from postgrest import *
|
from postgrest import *
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user