This allows to configure the test database by only using the PG* env
vars, while still leaving the option to wreck a database opt-in.
parser.addoption(
"--test-dsn",
metavar="DSN",
- default=os.environ.get("PSYCOPG_TEST_DSN") or None,
+ default=os.environ.get("PSYCOPG_TEST_DSN"),
help="Connection string to run database tests requiring a connection"
" [you can also use the PSYCOPG_TEST_DSN env var].",
)
def dsn(request):
"""Return the dsn used to connect to the `--test-dsn` database."""
dsn = request.config.getoption("--test-dsn")
- if not dsn:
+ if dsn is None:
pytest.skip("skipping test as no --test-dsn")
return dsn
assert dbname.dispsize == 20
parsed = pq.Conninfo.parse(dsn.encode())
- name = [o.val for o in parsed if o.keyword == b"dbname"][0]
- user = [o.val for o in parsed if o.keyword == b"user"][0]
+ # take the name and the user either from params or from env vars
+ name = [
+ o.val or os.environ.get(o.envvar.decode(), "").encode()
+ for o in parsed
+ if o.keyword == b"dbname"
+ ][0]
+ user = [
+ o.val or os.environ.get(o.envvar.decode(), "").encode()
+ for o in parsed
+ if o.keyword == b"user"
+ ][0]
assert dbname.val == (name or user)
pgconn.finish()