11"""Test Windows compatibility fixes for pytest-postgresql."""
22
3- import platform
4- import pytest
5- from unittest .mock import Mock , patch , MagicMock
63import subprocess
7- import signal
4+ from unittest . mock import MagicMock , patch
85
96from pytest_postgresql .executor import PostgreSQLExecutor
107
@@ -24,8 +21,8 @@ def test_get_base_command_windows(self):
2421 startparams = "-w" ,
2522 dbname = "test" ,
2623 )
27-
28- with patch (' platform.system' , return_value = ' Windows' ):
24+
25+ with patch (" platform.system" , return_value = " Windows" ):
2926 command = executor ._get_base_command ()
3027 # Windows command should not have single quotes around stderr
3128 assert "log_destination=stderr" in command
@@ -45,8 +42,8 @@ def test_get_base_command_unix(self):
4542 startparams = "-w" ,
4643 dbname = "test" ,
4744 )
48-
49- with patch (' platform.system' , return_value = ' Linux' ):
45+
46+ with patch (" platform.system" , return_value = " Linux" ):
5047 command = executor ._get_base_command ()
5148 # Unix command should have single quotes around stderr
5249 assert "log_destination='stderr'" in command
@@ -64,14 +61,14 @@ def test_windows_terminate_process(self):
6461 startparams = "-w" ,
6562 dbname = "test" ,
6663 )
67-
64+
6865 # Mock process
6966 mock_process = MagicMock ()
7067 executor .process = mock_process
71-
72- with patch (' platform.system' , return_value = ' Windows' ):
68+
69+ with patch (" platform.system" , return_value = " Windows" ):
7370 executor ._windows_terminate_process ()
74-
71+
7572 # Should call terminate first
7673 mock_process .terminate .assert_called_once ()
7774 mock_process .wait .assert_called ()
@@ -88,15 +85,15 @@ def test_windows_terminate_process_force_kill(self):
8885 startparams = "-w" ,
8986 dbname = "test" ,
9087 )
91-
88+
9289 # Mock process that times out
9390 mock_process = MagicMock ()
9491 mock_process .wait .side_effect = [subprocess .TimeoutExpired (cmd = "test" , timeout = 5 ), None ]
9592 executor .process = mock_process
96-
97- with patch (' platform.system' , return_value = ' Windows' ):
93+
94+ with patch (" platform.system" , return_value = " Windows" ):
9895 executor ._windows_terminate_process ()
99-
96+
10097 # Should call terminate, wait (timeout), then kill, then wait again
10198 mock_process .terminate .assert_called_once ()
10299 mock_process .kill .assert_called_once ()
@@ -114,14 +111,16 @@ def test_stop_method_windows(self):
114111 startparams = "-w" ,
115112 dbname = "test" ,
116113 )
117-
114+
118115 # Mock subprocess and process
119- with patch ('subprocess.check_output' ) as mock_subprocess , \
120- patch ('platform.system' , return_value = 'Windows' ), \
121- patch .object (executor , '_windows_terminate_process' ) as mock_terminate :
122-
116+ with (
117+ patch ("subprocess.check_output" ) as mock_subprocess ,
118+ patch ("platform.system" , return_value = "Windows" ),
119+ patch .object (executor , "_windows_terminate_process" ) as mock_terminate ,
120+ ):
121+
123122 result = executor .stop ()
124-
123+
125124 # Should call pg_ctl stop and Windows terminate
126125 mock_subprocess .assert_called_once ()
127126 mock_terminate .assert_called_once_with (None )
@@ -139,15 +138,17 @@ def test_stop_method_unix(self):
139138 startparams = "-w" ,
140139 dbname = "test" ,
141140 )
142-
141+
143142 # Mock subprocess and super().stop
144- with patch ('subprocess.check_output' ) as mock_subprocess , \
145- patch ('platform.system' , return_value = 'Linux' ), \
146- patch ('pytest_postgresql.executor.TCPExecutor.stop' ) as mock_super_stop :
147-
143+ with (
144+ patch ("subprocess.check_output" ) as mock_subprocess ,
145+ patch ("platform.system" , return_value = "Linux" ),
146+ patch ("pytest_postgresql.executor.TCPExecutor.stop" ) as mock_super_stop ,
147+ ):
148+
148149 mock_super_stop .return_value = executor
149150 result = executor .stop ()
150-
151+
151152 # Should call pg_ctl stop and parent class stop
152153 mock_subprocess .assert_called_once ()
153154 mock_super_stop .assert_called_once_with (None , None )
@@ -165,24 +166,28 @@ def test_stop_method_fallback_on_killpg_error(self):
165166 startparams = "-w" ,
166167 dbname = "test" ,
167168 )
168-
169+
169170 # Mock subprocess and super().stop to raise AttributeError
170- with patch ('subprocess.check_output' ) as mock_subprocess , \
171- patch ('platform.system' , return_value = 'Linux' ), \
172- patch ('pytest_postgresql.executor.TCPExecutor.stop' ,
173- side_effect = AttributeError ("module 'os' has no attribute 'killpg'" )), \
174- patch .object (executor , '_windows_terminate_process' ) as mock_terminate :
175-
171+ with (
172+ patch ("subprocess.check_output" ) as mock_subprocess ,
173+ patch ("platform.system" , return_value = "Linux" ),
174+ patch (
175+ "pytest_postgresql.executor.TCPExecutor.stop" ,
176+ side_effect = AttributeError ("module 'os' has no attribute 'killpg'" ),
177+ ),
178+ patch .object (executor , "_windows_terminate_process" ) as mock_terminate ,
179+ ):
180+
176181 result = executor .stop ()
177-
182+
178183 # Should call pg_ctl stop, fail on super().stop, then use Windows terminate
179184 mock_subprocess .assert_called_once ()
180185 mock_terminate .assert_called_once_with (None )
181186 assert result is executor
182187
183188 def test_command_formatting_windows (self ):
184189 """Test that command is properly formatted for Windows."""
185- with patch (' platform.system' , return_value = ' Windows' ):
190+ with patch (" platform.system" , return_value = " Windows" ):
186191 executor = PostgreSQLExecutor (
187192 executable = "C:/Program Files/PostgreSQL/bin/pg_ctl.exe" ,
188193 host = "localhost" ,
@@ -192,21 +197,21 @@ def test_command_formatting_windows(self):
192197 logfile = "C:/temp/log.txt" ,
193198 startparams = "-w -s" ,
194199 dbname = "testdb" ,
195- postgres_options = "-c shared_preload_libraries=test"
200+ postgres_options = "-c shared_preload_libraries=test" ,
196201 )
197-
202+
198203 # The command should be properly formatted without quotes around stderr
199204 expected_parts = [
200205 "C:/Program Files/PostgreSQL/bin/pg_ctl.exe start" ,
201206 '-D "C:/temp/data"' ,
202- " -o \ " -F -p 5555 -c log_destination=stderr" ,
207+ ' -o "-F -p 5555 -c log_destination=stderr' ,
203208 "-c logging_collector=off" ,
204209 "-c unix_socket_directories=C:/temp/socket" ,
205- " -c shared_preload_libraries=test\" " ,
210+ ' -c shared_preload_libraries=test"' ,
206211 '-l "C:/temp/log.txt"' ,
207- "-w -s"
212+ "-w -s" ,
208213 ]
209-
214+
210215 # Check if all expected parts are in the command
211216 command = executor .command
212217 for part in expected_parts :
0 commit comments