|
| 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) |
0 commit comments