Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix tests
  • Loading branch information
Jesse De Loore committed Jan 29, 2023
commit 08ad1f84e5c5deaf6a6a5a2274fd4dd1b13eafc8
8 changes: 6 additions & 2 deletions asyncpg/connect_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,15 +597,19 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user,

if target_session_attrs is None:

target_session_attrs = os.getenv("PGTARGETSESSIONATTRS", SessionAttribute.any)
target_session_attrs = os.getenv(
"PGTARGETSESSIONATTRS", SessionAttribute.any
)
try:

target_session_attrs = SessionAttribute(target_session_attrs)
except ValueError as exc:
raise exceptions.InterfaceError(
"target_session_attrs is expected to be one of "
"{!r}"
", got {!r}".format(SessionAttribute.__members__.values, target_session_attrs)
", got {!r}".format(
SessionAttribute.__members__.values, target_session_attrs
)
) from exc

params = _ConnectionParameters(
Expand Down
4 changes: 2 additions & 2 deletions asyncpg/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from . import serverversion
from . import transaction
from . import utils
from .connect_utils import SessionAttribute


class ConnectionMeta(type):
Expand Down Expand Up @@ -2017,7 +2016,8 @@ async def connect(dsn=None, *,
none of the listed hosts is a standby server,
return any of them.

If not specified will try to use PGTARGETSESSIONATTRS from the environment.
If not specified will try to use PGTARGETSESSIONATTRS
from the environment.
Defaults to "any" if no value is set.

:return: A :class:`~asyncpg.connection.Connection` instance.
Expand Down
66 changes: 53 additions & 13 deletions tests/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ class TestConnectParams(tb.TestCase):
'password': 'passw',
'database': 'testdb',
'ssl': True,
'sslmode': SSLMode.prefer})
'sslmode': SSLMode.prefer,
'target_session_attrs': 'any'})
},

{
Expand All @@ -406,7 +407,8 @@ class TestConnectParams(tb.TestCase):
'result': ([('host2', 456)], {
'user': 'user2',
'password': 'passw2',
'database': 'db2'})
'database': 'db2',
'target_session_attrs': 'any'})
},

{
Expand Down Expand Up @@ -434,7 +436,8 @@ class TestConnectParams(tb.TestCase):
'password': 'passw2',
'database': 'db2',
'sslmode': SSLMode.disable,
'ssl': False})
'ssl': False,
'target_session_attrs': 'any'})
},

{
Expand All @@ -455,7 +458,8 @@ class TestConnectParams(tb.TestCase):
'password': '123123',
'database': 'abcdef',
'ssl': True,
'sslmode': SSLMode.allow})
'sslmode': SSLMode.allow,
'target_session_attrs': 'any'})
},

{
Expand Down Expand Up @@ -483,7 +487,8 @@ class TestConnectParams(tb.TestCase):
'password': 'passw2',
'database': 'db2',
'sslmode': SSLMode.disable,
'ssl': False})
'ssl': False,
'target_session_attrs': 'any'})
},

{
Expand All @@ -504,7 +509,8 @@ class TestConnectParams(tb.TestCase):
'password': '123123',
'database': 'abcdef',
'ssl': True,
'sslmode': SSLMode.prefer})
'sslmode': SSLMode.prefer,
'target_session_attrs': 'any'})
},

{
Expand All @@ -513,7 +519,8 @@ class TestConnectParams(tb.TestCase):
'result': ([('localhost', 5555)], {
'user': 'user3',
'password': '123123',
'database': 'abcdef'})
'database': 'abcdef',
'target_session_attrs': 'any'})
},

{
Expand All @@ -522,6 +529,7 @@ class TestConnectParams(tb.TestCase):
'result': ([('host1', 5432), ('host2', 5432)], {
'database': 'db',
'user': 'user',
'target_session_attrs': 'any',
})
},

Expand All @@ -531,6 +539,7 @@ class TestConnectParams(tb.TestCase):
'result': ([('host1', 1111), ('host2', 2222)], {
'database': 'db',
'user': 'user',
'target_session_attrs': 'any',
})
},

Expand All @@ -540,6 +549,7 @@ class TestConnectParams(tb.TestCase):
'result': ([('2001:db8::1234%eth0', 5432), ('::1', 5432)], {
'database': 'db',
'user': 'user',
'target_session_attrs': 'any',
})
},

Expand All @@ -549,6 +559,7 @@ class TestConnectParams(tb.TestCase):
'result': ([('2001:db8::1234', 1111), ('::1', 2222)], {
'database': 'db',
'user': 'user',
'target_session_attrs': 'any',
})
},

Expand All @@ -558,6 +569,7 @@ class TestConnectParams(tb.TestCase):
'result': ([('2001:db8::1234', 5432), ('::1', 5432)], {
'database': 'db',
'user': 'user',
'target_session_attrs': 'any',
})
},

Expand All @@ -572,6 +584,7 @@ class TestConnectParams(tb.TestCase):
'result': ([('host1', 1111), ('host2', 2222)], {
'database': 'db',
'user': 'foo',
'target_session_attrs': 'any',
})
},

Expand All @@ -584,6 +597,7 @@ class TestConnectParams(tb.TestCase):
'result': ([('host1', 1111), ('host2', 2222)], {
'database': 'db',
'user': 'foo',
'target_session_attrs': 'any',
})
},

Expand All @@ -597,6 +611,7 @@ class TestConnectParams(tb.TestCase):
'result': ([('host1', 5432), ('host2', 5432)], {
'database': 'db',
'user': 'foo',
'target_session_attrs': 'any',
})
},

Expand All @@ -616,7 +631,8 @@ class TestConnectParams(tb.TestCase):
'password': 'ask',
'database': 'db',
'ssl': True,
'sslmode': SSLMode.require})
'sslmode': SSLMode.require,
'target_session_attrs': 'any'})
},

{
Expand All @@ -637,15 +653,17 @@ class TestConnectParams(tb.TestCase):
'password': 'ask',
'database': 'db',
'sslmode': SSLMode.verify_full,
'ssl': True})
'ssl': True,
'target_session_attrs': 'any'})
},

{
'name': 'dsn_only_unix',
'dsn': 'postgresql:///dbname?host=/unix_sock/test&user=spam',
'result': ([os.path.join('/unix_sock/test', '.s.PGSQL.5432')], {
'user': 'spam',
'database': 'dbname'})
'database': 'dbname',
'target_session_attrs': 'any'})
},

{
Expand All @@ -657,6 +675,7 @@ class TestConnectParams(tb.TestCase):
'user': 'us@r',
'password': 'p@ss',
'database': 'db',
'target_session_attrs': 'any',
}
)
},
Expand All @@ -670,6 +689,7 @@ class TestConnectParams(tb.TestCase):
'user': 'user',
'password': 'p',
'database': 'db',
'target_session_attrs': 'any',
}
)
},
Expand All @@ -682,6 +702,7 @@ class TestConnectParams(tb.TestCase):
{
'user': 'us@r',
'database': 'db',
'target_session_attrs': 'any',
}
)
},
Expand Down Expand Up @@ -709,7 +730,8 @@ class TestConnectParams(tb.TestCase):
'user': 'user',
'database': 'user',
'sslmode': SSLMode.disable,
'ssl': None
'ssl': None,
'target_session_attrs': 'any',
}
)
},
Expand All @@ -723,7 +745,8 @@ class TestConnectParams(tb.TestCase):
'.s.PGSQL.5432'
)], {
'user': 'spam',
'database': 'db'
'database': 'db',
'target_session_attrs': 'any',
}
)
},
Expand All @@ -744,6 +767,7 @@ class TestConnectParams(tb.TestCase):
'database': 'db',
'ssl': True,
'sslmode': SSLMode.prefer,
'target_session_attrs': 'any',
}
)
},
Expand Down Expand Up @@ -874,7 +898,9 @@ def test_test_connect_params_run_testcase(self):
'host': 'abc',
'result': (
[('abc', 5432)],
{'user': '__test__', 'database': '__test__'}
{'user': '__test__',
'database': '__test__',
'target_session_attrs': 'any'}
)
})

Expand Down Expand Up @@ -912,6 +938,7 @@ def test_connect_pgpass_regular(self):
'password': 'password from pgpass for user@abc',
'user': 'user',
'database': 'db',
'target_session_attrs': 'any',
}
)
})
Expand All @@ -928,6 +955,7 @@ def test_connect_pgpass_regular(self):
'password': 'password from pgpass for user@abc',
'user': 'user',
'database': 'db',
'target_session_attrs': 'any',
}
)
})
Expand All @@ -942,6 +970,7 @@ def test_connect_pgpass_regular(self):
'password': 'password from pgpass for user@abc',
'user': 'user',
'database': 'db',
'target_session_attrs': 'any',
}
)
})
Expand All @@ -957,6 +986,7 @@ def test_connect_pgpass_regular(self):
'password': 'password from pgpass for localhost',
'user': 'user',
'database': 'db',
'target_session_attrs': 'any',
}
)
})
Expand All @@ -974,6 +1004,7 @@ def test_connect_pgpass_regular(self):
'password': 'password from pgpass for localhost',
'user': 'user',
'database': 'db',
'target_session_attrs': 'any',
}
)
})
Expand All @@ -991,6 +1022,7 @@ def test_connect_pgpass_regular(self):
'password': 'password from pgpass for cde:5433',
'user': 'user',
'database': 'db',
'target_session_attrs': 'any',
}
)
})
Expand All @@ -1007,6 +1039,7 @@ def test_connect_pgpass_regular(self):
'password': 'password from pgpass for testuser',
'user': 'testuser',
'database': 'db',
'target_session_attrs': 'any',
}
)
})
Expand All @@ -1023,6 +1056,7 @@ def test_connect_pgpass_regular(self):
'password': 'password from pgpass for testdb',
'user': 'user',
'database': 'testdb',
'target_session_attrs': 'any',
}
)
})
Expand All @@ -1039,6 +1073,7 @@ def test_connect_pgpass_regular(self):
'password': 'password from pgpass with escapes',
'user': R'test\\',
'database': R'test\:db',
'target_session_attrs': 'any',
}
)
})
Expand Down Expand Up @@ -1066,6 +1101,7 @@ def test_connect_pgpass_badness_mode(self):
{
'user': 'user',
'database': 'db',
'target_session_attrs': 'any',
}
)
})
Expand All @@ -1086,6 +1122,7 @@ def test_connect_pgpass_badness_non_file(self):
{
'user': 'user',
'database': 'db',
'target_session_attrs': 'any',
}
)
})
Expand All @@ -1102,6 +1139,7 @@ def test_connect_pgpass_nonexistent(self):
{
'user': 'user',
'database': 'db',
'target_session_attrs': 'any',
}
)
})
Expand All @@ -1122,6 +1160,7 @@ def test_connect_pgpass_inaccessible_file(self):
{
'user': 'user',
'database': 'db',
'target_session_attrs': 'any',
}
)
})
Expand All @@ -1144,6 +1183,7 @@ def test_connect_pgpass_inaccessible_directory(self):
{
'user': 'user',
'database': 'db',
'target_session_attrs': 'any',
}
)
})
Expand Down