77import subprocess
88import pytest
99import os
10+ import psycopg
1011
1112
1213@pytest .mark .pgtap
13- def test_pgtap_setup_sql ():
14+ def test_pgtap_setup_sql (pg_cluster ):
1415 """Run pgTAP setup.sql tests."""
1516 # Check if pgTAP is available by testing if we can create the extension and run a simple test
16- check_result = subprocess . run (
17- [ "psql" , "-c" , "CREATE EXTENSION IF NOT EXISTS pgtap; SELECT plan(1); SELECT pass('pgTAP is working'); SELECT * FROM finish();" ],
18- capture_output = True ,
19- text = True ,
20- timeout = 10
21- )
22-
23- assert check_result . returncode == 0 , f"pgTAP not available - pgTAP tests require pgTAP to be installed. Error: { check_result . stderr } "
17+ with psycopg . connect ( pg_cluster . dsn ()) as conn :
18+ try :
19+ conn . execute ( "CREATE EXTENSION IF NOT EXISTS pgtap" )
20+ result = conn . execute ( "SELECT plan(1)" )
21+ conn . execute ( "SELECT pass('pgTAP is working')" )
22+ conn . execute ( "SELECT * FROM finish()" )
23+ except psycopg . Error as e :
24+ pytest . fail ( f"pgTAP not available - pgTAP tests require pgTAP to be installed. Error: { e } " )
2425
2526 # Change to pgtap directory
2627 old_cwd = os .getcwd ()
@@ -29,12 +30,17 @@ def test_pgtap_setup_sql():
2930 try :
3031 os .chdir (pgtap_dir )
3132
33+ # Set up environment for run_tests.sh to use the test cluster
34+ env = pg_cluster .client_env ()
35+ env ["PGDATABASE" ] = pg_cluster .database
36+
3237 # Run the existing run_tests.sh script
3338 result = subprocess .run (
3439 ["./run_tests.sh" ],
3540 capture_output = True ,
3641 text = True ,
37- timeout = 60 # 60 second timeout
42+ timeout = 60 , # 60 second timeout
43+ env = env
3844 )
3945
4046 # Check that pgTAP ran successfully
@@ -52,15 +58,9 @@ def test_pgtap_setup_sql():
5258
5359
5460@pytest .mark .pgtap
55- def test_pgtap_basic_functionality ():
61+ def test_pgtap_basic_functionality (pg_cluster ):
5662 """Test that pgTAP can run basic tests."""
5763 # Simple test to verify pgTAP is working
58- result = subprocess .run (
59- ["psql" , "-c" , "SELECT 1 as test;" ],
60- capture_output = True ,
61- text = True ,
62- timeout = 10
63- )
64-
65- assert result .returncode == 0 , f"Basic psql test failed: { result .stderr } "
66- assert "1" in result .stdout , f"Unexpected psql output: { result .stdout } "
64+ with psycopg .connect (pg_cluster .dsn ()) as conn :
65+ result = conn .execute ("SELECT 1 as test" ).fetchone ()
66+ assert result [0 ] == 1 , f"Unexpected query result: { result } "
0 commit comments