@@ -238,6 +238,27 @@ def test_cli_shell_postgres(self, mock_execute_interactive):
238238 "PGUSER" : "postgres" ,
239239 }
240240
241+ @mock .patch ("airflow.cli.commands.db_command.execute_interactive" )
242+ @mock .patch (
243+ "airflow.cli.commands.db_command.settings.engine.url" ,
244+ make_url ("postgresql+psycopg://postgres:airflow@postgres:5432/airflow" ),
245+ )
246+ def test_cli_shell_postgres_ppg3 (self , mock_execute_interactive ):
247+ pytest .importorskip ("psycopg" , reason = "Test only runs when psycopg v3 is installed." )
248+
249+ db_command .shell (self .parser .parse_args (["db" , "shell" ]))
250+ mock_execute_interactive .assert_called_once_with (["psql" ], env = mock .ANY )
251+ _ , kwargs = mock_execute_interactive .call_args
252+ env = kwargs ["env" ]
253+ postgres_env = {k : v for k , v in env .items () if k .startswith ("PG" )}
254+ assert postgres_env == {
255+ "PGDATABASE" : "airflow" ,
256+ "PGHOST" : "postgres" ,
257+ "PGPASSWORD" : "airflow" ,
258+ "PGPORT" : "5432" ,
259+ "PGUSER" : "postgres" ,
260+ }
261+
241262 @mock .patch ("airflow.cli.commands.db_command.execute_interactive" )
242263 @mock .patch (
243264 "airflow.cli.commands.db_command.settings.engine.url" ,
@@ -257,6 +278,27 @@ def test_cli_shell_postgres_without_port(self, mock_execute_interactive):
257278 "PGUSER" : "postgres" ,
258279 }
259280
281+ @mock .patch ("airflow.cli.commands.db_command.execute_interactive" )
282+ @mock .patch (
283+ "airflow.cli.commands.db_command.settings.engine.url" ,
284+ make_url ("postgresql+psycopg://postgres:airflow@postgres/airflow" ),
285+ )
286+ def test_cli_shell_postgres_without_port_ppg3 (self , mock_execute_interactive ):
287+ pytest .importorskip ("psycopg" , reason = "Test only runs when psycopg v3 is installed." )
288+
289+ db_command .shell (self .parser .parse_args (["db" , "shell" ]))
290+ mock_execute_interactive .assert_called_once_with (["psql" ], env = mock .ANY )
291+ _ , kwargs = mock_execute_interactive .call_args
292+ env = kwargs ["env" ]
293+ postgres_env = {k : v for k , v in env .items () if k .startswith ("PG" )}
294+ assert postgres_env == {
295+ "PGDATABASE" : "airflow" ,
296+ "PGHOST" : "postgres" ,
297+ "PGPASSWORD" : "airflow" ,
298+ "PGPORT" : "5432" ,
299+ "PGUSER" : "postgres" ,
300+ }
301+
260302 @mock .patch (
261303 "airflow.cli.commands.db_command.settings.engine.url" ,
262304 make_url ("invalid+psycopg2://postgres:airflow@postgres/airflow" ),
@@ -265,6 +307,16 @@ def test_cli_shell_invalid(self):
265307 with pytest .raises (AirflowException , match = r"Unknown driver: invalid\+psycopg2" ):
266308 db_command .shell (self .parser .parse_args (["db" , "shell" ]))
267309
310+ @mock .patch (
311+ "airflow.cli.commands.db_command.settings.engine.url" ,
312+ make_url ("invalid+psycopg://postgres:airflow@postgres/airflow" ),
313+ )
314+ def test_cli_shell_invalid_ppg3 (self ):
315+ pytest .importorskip ("psycopg" , reason = "Test only runs when psycopg v3 is installed." )
316+
317+ with pytest .raises (AirflowException , match = r"Unknown driver: invalid\+psycopg" ):
318+ db_command .shell (self .parser .parse_args (["db" , "shell" ]))
319+
268320 @pytest .mark .parametrize (
269321 "args, match" ,
270322 [
0 commit comments