@@ -235,31 +235,47 @@ def test_save_csv(self):
235235 executor , handle = _make_executor_with_data ()
236236 self ._patch (executor )
237237 try :
238- with tempfile .NamedTemporaryFile (suffix = ".csv" , delete = False ) as f :
239- path = f .name
240- result = save_data_tool (handle , path = path , format = "csv" )
238+ with tempfile .TemporaryDirectory () as d :
239+ path = str (Path (d ) / "out.csv" )
240+ result = save_data_tool (handle , path = path , format = "csv" )
241+ assert result ["success" ]
242+ assert result ["format" ] == "csv"
243+ assert result ["rows" ] == 60
244+ assert result ["overwritten" ] is False
245+ assert Path (result ["saved_path" ]).exists ()
241246 finally :
242247 self ._unpatch ()
243248
244- assert result ["success" ]
245- assert result ["format" ] == "csv"
246- assert result ["rows" ] == 60
247- assert Path (result ["saved_path" ]).exists ()
248- Path (path ).unlink ()
249-
250249 def test_save_json (self ):
251250 executor , handle = _make_executor_with_data ()
252251 self ._patch (executor )
253252 try :
254- with tempfile .NamedTemporaryFile (suffix = ".json" , delete = False ) as f :
255- path = f .name
256- result = save_data_tool (handle , path = path , format = "json" )
253+ with tempfile .TemporaryDirectory () as d :
254+ path = str (Path (d ) / "out.json" )
255+ result = save_data_tool (handle , path = path , format = "json" )
256+ assert result ["success" ]
257+ assert result ["format" ] == "json"
257258 finally :
258259 self ._unpatch ()
259260
260- assert result ["success" ]
261- assert result ["format" ] == "json"
262- Path (path ).unlink ()
261+ def test_save_refuses_existing_file (self ):
262+ executor , handle = _make_executor_with_data ()
263+ self ._patch (executor )
264+ try :
265+ with tempfile .TemporaryDirectory () as d :
266+ path = str (Path (d ) / "out.csv" )
267+ first = save_data_tool (handle , path = path , format = "csv" )
268+ assert first ["success" ]
269+ # second write to the same path is refused without overwrite
270+ second = save_data_tool (handle , path = path , format = "csv" )
271+ assert not second ["success" ]
272+ assert "already exists" in second ["error" ]
273+ # ...and allowed with overwrite=True
274+ third = save_data_tool (handle , path = path , format = "csv" , overwrite = True )
275+ assert third ["success" ]
276+ assert third ["overwritten" ] is True
277+ finally :
278+ self ._unpatch ()
263279
264280 def test_save_unsupported_format (self ):
265281 executor , handle = _make_executor_with_data ()
0 commit comments