]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Allow PSYCOPG_TEST_DSN to be an empty string
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 29 Sep 2021 19:20:31 +0000 (19:20 +0000)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 30 Sep 2021 00:57:57 +0000 (00:57 +0000)
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.

tests/fix_db.py
tests/pq/test_pgconn.py

index c63e283dd3c3732843f3774b6c56f6928a499edf..738d06a6ef0ed77c54786790ea31133bbec4b5ce 100644 (file)
@@ -8,7 +8,7 @@ def pytest_addoption(parser):
     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].",
     )
@@ -27,7 +27,7 @@ def pytest_configure(config):
 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
 
index 89df2953e80b45a2d3481c28c3c5dd3b1aa0c268..e846adf2edfcaaf75fb9105bcc1c3d57d2e6432a 100644 (file)
@@ -113,8 +113,17 @@ def test_info(dsn, pgconn):
     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()