Skip to content

Commit c1d9728

Browse files
taimoorzaeemjenstroeger
authored andcommitted
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 <[email protected]> Signed-off-by: Taimoor Zaeem <[email protected]>
1 parent 322216c commit c1d9728

File tree

7 files changed

+95
-97
lines changed

7 files changed

+95
-97
lines changed

test/io/config.py

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import pathlib
33
import shutil
4-
import pytest
54
import uuid
65
import yaml
76

@@ -13,71 +12,6 @@
1312
SECRET = "reallyreallyreallyreallyverysafe"
1413

1514

16-
@pytest.fixture
17-
def dburi():
18-
"Postgres database connection URI."
19-
dbname = os.environ["PGDATABASE"]
20-
host = os.environ["PGHOST"]
21-
user = os.environ["PGUSER"]
22-
return f"postgresql://?dbname={dbname}&host={host}&user={user}".encode()
23-
24-
25-
@pytest.fixture
26-
def baseenv():
27-
"Base environment to connect to PostgreSQL"
28-
return {
29-
"PGDATABASE": os.environ["PGDATABASE"],
30-
"PGHOST": os.environ["PGHOST"],
31-
"PGUSER": os.environ["PGUSER"],
32-
}
33-
34-
35-
@pytest.fixture
36-
def defaultenv(baseenv):
37-
"Default environment for PostgREST."
38-
return {
39-
**baseenv,
40-
"PGRST_DB_CONFIG": "true",
41-
"PGRST_LOG_LEVEL": "info",
42-
"PGRST_DB_POOL": "1",
43-
"PGRST_NOT_EXISTING": "should not break any tests",
44-
}
45-
46-
47-
@pytest.fixture
48-
def replicaenv(defaultenv):
49-
"Default environment for a PostgREST replica."
50-
conf = {
51-
"PGRST_DB_ANON_ROLE": "postgrest_test_anonymous",
52-
"PGRST_DB_SCHEMAS": "replica",
53-
}
54-
return {
55-
"primary": {
56-
**defaultenv,
57-
**conf,
58-
},
59-
"replica": {
60-
**defaultenv,
61-
**conf,
62-
"PGHOST": os.environ["PGREPLICAHOST"] + "," + os.environ["PGHOST"],
63-
"PGREPLICASLOT": os.environ["PGREPLICASLOT"],
64-
},
65-
}
66-
67-
68-
@pytest.fixture
69-
def slow_schema_cache_env(defaultenv):
70-
"Slow schema cache load environment PostgREST."
71-
return {
72-
**defaultenv,
73-
"PGRST_INTERNAL_SCHEMA_CACHE_SLEEP": "1000", # this does a pg_sleep internally, it will cause the schema cache query to be slow
74-
# the slow schema cache query will keep using one pool connection until it finishes
75-
# to prevent requests waiting for PGRST_DB_POOL_ACQUISITION_TIMEOUT we'll increase the pool size (must be >= 2)
76-
"PGRST_DB_POOL": "2",
77-
"PGRST_DB_CHANNEL_ENABLED": "true",
78-
}
79-
80-
8115
def hpctixfile():
8216
"""
8317
Returns a unique filename for each postgrest process that is

test/io/conftest.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import os
2+
import pytest
3+
from syrupy.extensions.json import SingleFileSnapshotExtension
4+
from postgrest import run
5+
6+
7+
@pytest.fixture
8+
def dburi():
9+
"Postgres database connection URI."
10+
dbname = os.environ["PGDATABASE"]
11+
host = os.environ["PGHOST"]
12+
user = os.environ["PGUSER"]
13+
return f"postgresql://?dbname={dbname}&host={host}&user={user}".encode()
14+
15+
16+
@pytest.fixture
17+
def baseenv():
18+
"Base environment to connect to PostgreSQL"
19+
return {
20+
"PGDATABASE": os.environ["PGDATABASE"],
21+
"PGHOST": os.environ["PGHOST"],
22+
"PGUSER": os.environ["PGUSER"],
23+
}
24+
25+
26+
@pytest.fixture
27+
def defaultenv(baseenv):
28+
"Default environment for PostgREST."
29+
return {
30+
**baseenv,
31+
"PGRST_DB_CONFIG": "true",
32+
"PGRST_LOG_LEVEL": "info",
33+
"PGRST_DB_POOL": "1",
34+
"PGRST_NOT_EXISTING": "should not break any tests",
35+
}
36+
37+
38+
@pytest.fixture
39+
def replicaenv(defaultenv):
40+
"Default environment for a PostgREST replica."
41+
conf = {
42+
"PGRST_DB_ANON_ROLE": "postgrest_test_anonymous",
43+
"PGRST_DB_SCHEMAS": "replica",
44+
}
45+
return {
46+
"primary": {
47+
**defaultenv,
48+
**conf,
49+
},
50+
"replica": {
51+
**defaultenv,
52+
**conf,
53+
"PGHOST": os.environ["PGREPLICAHOST"] + "," + os.environ["PGHOST"],
54+
"PGREPLICASLOT": os.environ["PGREPLICASLOT"],
55+
},
56+
}
57+
58+
59+
@pytest.fixture
60+
def slow_schema_cache_env(defaultenv):
61+
"Slow schema cache load environment PostgREST."
62+
return {
63+
**defaultenv,
64+
"PGRST_INTERNAL_SCHEMA_CACHE_SLEEP": "1000", # this does a pg_sleep internally, it will cause the schema cache query to be slow
65+
# the slow schema cache query will keep using one pool connection until it finishes
66+
# to prevent requests waiting for PGRST_DB_POOL_ACQUISITION_TIMEOUT we'll increase the pool size (must be >= 2)
67+
"PGRST_DB_POOL": "2",
68+
"PGRST_DB_CHANNEL_ENABLED": "true",
69+
}
70+
71+
72+
@pytest.fixture
73+
def metapostgrest():
74+
"A shared postgrest instance to use for interacting with the database independently of the instance under test"
75+
role = "meta_authenticator"
76+
env = {
77+
"PGDATABASE": os.environ["PGDATABASE"],
78+
"PGHOST": os.environ["PGHOST"],
79+
"PGUSER": role,
80+
"PGRST_DB_ANON_ROLE": role,
81+
"PGRST_DB_CONFIG": "true",
82+
"PGRST_LOG_LEVEL": "info",
83+
"PGRST_DB_POOL": "1",
84+
}
85+
with run(env=env) as postgrest:
86+
yield postgrest
87+
88+
89+
class YamlSnapshotExtension(SingleFileSnapshotExtension):
90+
_file_extension = "yaml"
91+
92+
93+
@pytest.fixture
94+
def snapshot_yaml(snapshot):
95+
return snapshot.use_extension(YamlSnapshotExtension)

test/io/postgrest.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import time
1111
import urllib.parse
1212

13-
import pytest
1413
import requests
1514
import requests_unixsocket
1615

@@ -158,23 +157,6 @@ def run(
158157
process.wait()
159158

160159

161-
@pytest.fixture(scope="module")
162-
def metapostgrest():
163-
"A shared postgrest instance to use for interacting with the database independently of the instance under test"
164-
role = "meta_authenticator"
165-
env = {
166-
"PGDATABASE": os.environ["PGDATABASE"],
167-
"PGHOST": os.environ["PGHOST"],
168-
"PGUSER": role,
169-
"PGRST_DB_ANON_ROLE": role,
170-
"PGRST_DB_CONFIG": "true",
171-
"PGRST_LOG_LEVEL": "info",
172-
"PGRST_DB_POOL": "1",
173-
}
174-
with run(env=env) as postgrest:
175-
yield postgrest
176-
177-
178160
def freeport(used_port=None):
179161
"Find a free port on localhost."
180162
while True:

test/io/test_big_schema.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import pytest
44

5-
from config import *
65
from util import *
76
from postgrest import *
87

test/io/test_cli.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import signal
55
import subprocess
66
import pytest
7-
from syrupy.extensions.json import SingleFileSnapshotExtension
87
import yaml
98

109
from config import *
@@ -20,15 +19,6 @@ def write_line_break(self, data=None):
2019
super().write_line_break()
2120

2221

23-
class YamlSnapshotExtension(SingleFileSnapshotExtension):
24-
_file_extension = "yaml"
25-
26-
27-
@pytest.fixture
28-
def snapshot_yaml(snapshot):
29-
return snapshot.use_extension(YamlSnapshotExtension)
30-
31-
3222
def itemgetter(*items):
3323
"operator.itemgetter with None as fallback when key does not exist"
3424
if len(items) == 1:

test/io/test_replica.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"IO tests for PostgREST started on replicas"
22

3-
from config import *
43
from util import *
54
from postgrest import *
65

test/io/test_sanity.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import pytest
44

5-
from config import *
65
from util import *
76
from postgrest import *
87

0 commit comments

Comments
 (0)