@@ -32,3 +32,45 @@ def test_postgresql_peer_auth_omits_empty_tcp_args() -> None:
3232 assert "port" not in kwargs
3333 assert "user" not in kwargs
3434 assert "password" not in kwargs
35+
36+
37+ def test_postgresql_uses_custom_port_when_server_left_blank () -> None :
38+ """Regression test for issue #205.
39+
40+ A user pointed sqlit at a docker Postgres on 127.0.0.1:5433. They left
41+ the Server field blank (its placeholder says 'localhost') and set the
42+ Port field to 5433. libpq then reported it couldn't reach
43+ /run/postgresql/.s.PGSQL.5432 — i.e. neither host nor port made it
44+ through.
45+
46+ Expected: with a port set, the adapter must pass both port=5433 and
47+ a sensible host (localhost, matching the form placeholder) to
48+ psycopg2.connect so the connection actually reaches the docker
49+ container.
50+ """
51+ mock_psycopg2 = MagicMock ()
52+ with patch .dict ("sys.modules" , {"psycopg2" : mock_psycopg2 }):
53+ from sqlit .domains .connections .providers .postgresql .adapter import PostgreSQLAdapter
54+
55+ adapter = PostgreSQLAdapter ()
56+ config = ConnectionConfig (
57+ name = "timescale" ,
58+ db_type = "postgresql" ,
59+ server = "" , # blank — UI placeholder says "localhost"
60+ port = "5433" , # user explicitly chose 5433
61+ database = "postgres" ,
62+ username = "postgres" ,
63+ password = "password" ,
64+ )
65+
66+ adapter .connect (config )
67+
68+ kwargs = mock_psycopg2 .connect .call_args .kwargs
69+ assert kwargs .get ("port" ) == 5433 , (
70+ "port=5433 must reach psycopg2.connect, but the adapter "
71+ f"silently drops it when server is blank. kwargs={ kwargs !r} "
72+ )
73+ assert kwargs .get ("host" ) == "localhost" , (
74+ "blank server must default to 'localhost' (matching the "
75+ f"connection form placeholder). kwargs={ kwargs !r} "
76+ )
0 commit comments