1010import os
1111import os .path as osp
1212import shutil
13+ import tempfile
1314import unittest
1415from itertools import chain
1516
@@ -211,29 +212,30 @@ def test_save_and_load_01_simple(self):
211212 arr_names = sp .arr_names
212213 self .assertEqual (tp .results [arr_names [0 ] + ".fmt1" ], "test" )
213214 self .assertEqual (tp .results [arr_names [1 ] + ".fmt1" ], "test" )
214- fname = "test.pkl"
215- self ._created_files .add (fname )
216- sp .save_project (fname )
217- psy .close ()
218- tp .results .clear ()
219- sp = psy .Project .load_project (fname )
220- self .assertEqual (len (sp ), 2 )
221- self .assertEqual (sp [0 ].psy .ax .get_figure ().number , 1 )
222- self .assertEqual (get_row_num (sp [0 ].psy .ax ), 0 )
223- self .assertEqual (get_col_num (sp [0 ].psy .ax ), 0 )
224215
225- gs = sp [0 ].psy .ax .get_gridspec ()
216+ with tempfile .TemporaryDirectory () as tmpdir :
217+ fname = str (tmpdir + "test.pkl" )
218+ sp .save_project (fname )
219+ psy .close ()
220+ tp .results .clear ()
221+ sp = psy .Project .load_project (fname )
222+ self .assertEqual (len (sp ), 2 )
223+ self .assertEqual (sp [0 ].psy .ax .get_figure ().number , 1 )
224+ self .assertEqual (get_row_num (sp [0 ].psy .ax ), 0 )
225+ self .assertEqual (get_col_num (sp [0 ].psy .ax ), 0 )
226226
227- self .assertEqual (gs .ncols , 2 )
228- self .assertEqual (gs .nrows , 2 )
229- self .assertEqual (sp [1 ].psy .ax .get_figure ().number , 2 )
230- self .assertEqual (get_row_num (sp [1 ].psy .ax ), 0 )
231- self .assertEqual (get_col_num (sp [1 ].psy .ax ), 0 )
227+ gs = sp [0 ].psy .ax .get_gridspec ()
232228
233- gs = sp [1 ].psy .ax .get_gridspec ()
229+ self .assertEqual (gs .ncols , 2 )
230+ self .assertEqual (gs .nrows , 2 )
231+ self .assertEqual (sp [1 ].psy .ax .get_figure ().number , 2 )
232+ self .assertEqual (get_row_num (sp [1 ].psy .ax ), 0 )
233+ self .assertEqual (get_col_num (sp [1 ].psy .ax ), 0 )
234234
235- self .assertEqual (gs .ncols , 2 )
236- self .assertEqual (gs .nrows , 2 )
235+ gs = sp [1 ].psy .ax .get_gridspec ()
236+
237+ self .assertEqual (gs .ncols , 2 )
238+ self .assertEqual (gs .nrows , 2 )
237239
238240 def test_save_and_load_02_alternative_axes (self ):
239241 """Test the saving and loading of a Project providing alternative axes"""
@@ -268,30 +270,31 @@ def test_save_and_load_02_alternative_axes(self):
268270 arr_names = sp .arr_names
269271 self .assertEqual (tp .results [arr_names [0 ] + ".fmt1" ], "test" )
270272 self .assertEqual (tp .results [arr_names [1 ] + ".fmt1" ], "test" )
271- fname = "test.pkl"
272- self ._created_files .add (fname )
273- sp .save_project (fname )
274- psy .close ()
275- tp .results .clear ()
276- fig , axes = plt .subplots (1 , 2 )
277- sp = psy .Project .load_project (fname , alternative_axes = axes .ravel ())
278- self .assertEqual (len (sp ), 2 )
279- self .assertEqual (sp [0 ].psy .ax .get_figure ().number , 1 )
280- self .assertEqual (get_row_num (sp [0 ].psy .ax ), 0 )
281- self .assertEqual (get_col_num (sp [0 ].psy .ax ), 0 )
282273
283- gs = sp [0 ].psy .ax .get_gridspec ()
274+ with tempfile .TemporaryDirectory () as tmpdir :
275+ fname = str (tmpdir + "test.pkl" )
276+ sp .save_project (fname )
277+ psy .close ()
278+ tp .results .clear ()
279+ fig , axes = plt .subplots (1 , 2 )
280+ sp = psy .Project .load_project (fname , alternative_axes = axes .ravel ())
281+ self .assertEqual (len (sp ), 2 )
282+ self .assertEqual (sp [0 ].psy .ax .get_figure ().number , 1 )
283+ self .assertEqual (get_row_num (sp [0 ].psy .ax ), 0 )
284+ self .assertEqual (get_col_num (sp [0 ].psy .ax ), 0 )
284285
285- self .assertEqual (gs .ncols , 2 )
286- self .assertEqual (gs .nrows , 1 )
287- self .assertEqual (sp [1 ].psy .ax .get_figure ().number , 1 )
288- self .assertEqual (get_row_num (sp [1 ].psy .ax ), 0 )
289- self .assertEqual (get_col_num (sp [1 ].psy .ax ), 1 )
286+ gs = sp [0 ].psy .ax .get_gridspec ()
290287
291- gs = sp [1 ].psy .ax .get_gridspec ()
288+ self .assertEqual (gs .ncols , 2 )
289+ self .assertEqual (gs .nrows , 1 )
290+ self .assertEqual (sp [1 ].psy .ax .get_figure ().number , 1 )
291+ self .assertEqual (get_row_num (sp [1 ].psy .ax ), 0 )
292+ self .assertEqual (get_col_num (sp [1 ].psy .ax ), 1 )
292293
293- self .assertEqual (gs .ncols , 2 )
294- self .assertEqual (gs .nrows , 1 )
294+ gs = sp [1 ].psy .ax .get_gridspec ()
295+
296+ self .assertEqual (gs .ncols , 2 )
297+ self .assertEqual (gs .nrows , 1 )
295298
296299 def test_save_and_load_03_alternative_ds (self ):
297300 """Test the saving and loading of a Project providing alternative axes"""
@@ -326,33 +329,34 @@ def test_save_and_load_03_alternative_ds(self):
326329 arr_names = sp .arr_names
327330 self .assertEqual (tp .results [arr_names [0 ] + ".fmt1" ], "test" )
328331 self .assertEqual (tp .results [arr_names [1 ] + ".fmt1" ], "test" )
329- fname = "test.pkl"
330- self ._created_files .add (fname )
331- sp .save_project (fname )
332- psy .close ()
333- tp .results .clear ()
334- fig , axes = plt .subplots (1 , 2 )
335- ds = psy .open_dataset (bt .get_file ("circumpolar_test.nc" ))
336- sp = psy .Project .load_project (fname , datasets = [ds ], new_fig = False )
337- self .assertEqual (len (sp ), 2 )
338- self .assertEqual (sp [0 ].psy .ax .get_figure ().number , 1 )
339- self .assertEqual (get_row_num (sp [0 ].psy .ax ), 0 )
340- self .assertEqual (get_col_num (sp [0 ].psy .ax ), 0 )
341-
342- gs = sp [0 ].psy .ax .get_gridspec ()
343-
344- self .assertEqual (gs .ncols , 2 )
345- self .assertEqual (gs .nrows , 2 )
346- self .assertEqual (sp [1 ].psy .ax .get_figure ().number , 2 )
347- self .assertEqual (get_row_num (sp [1 ].psy .ax ), 0 )
348- self .assertEqual (get_col_num (sp [1 ].psy .ax ), 0 )
349332
350- gs = sp [1 ].psy .ax .get_gridspec ()
351-
352- self .assertEqual (gs .ncols , 2 )
353- self .assertEqual (gs .nrows , 2 )
354- self .assertIs (sp [0 ].psy .base , ds )
355- self .assertIs (sp [1 ].psy .base , ds )
333+ with tempfile .TemporaryDirectory () as tmpdir :
334+ fname = str (tmpdir + "test.pkl" )
335+ sp .save_project (fname )
336+ psy .close ()
337+ tp .results .clear ()
338+ fig , axes = plt .subplots (1 , 2 )
339+ ds = psy .open_dataset (bt .get_file ("circumpolar_test.nc" ))
340+ sp = psy .Project .load_project (fname , datasets = [ds ], new_fig = False )
341+ self .assertEqual (len (sp ), 2 )
342+ self .assertEqual (sp [0 ].psy .ax .get_figure ().number , 1 )
343+ self .assertEqual (get_row_num (sp [0 ].psy .ax ), 0 )
344+ self .assertEqual (get_col_num (sp [0 ].psy .ax ), 0 )
345+
346+ gs = sp [0 ].psy .ax .get_gridspec ()
347+
348+ self .assertEqual (gs .ncols , 2 )
349+ self .assertEqual (gs .nrows , 2 )
350+ self .assertEqual (sp [1 ].psy .ax .get_figure ().number , 2 )
351+ self .assertEqual (get_row_num (sp [1 ].psy .ax ), 0 )
352+ self .assertEqual (get_col_num (sp [1 ].psy .ax ), 0 )
353+
354+ gs = sp [1 ].psy .ax .get_gridspec ()
355+
356+ self .assertEqual (gs .ncols , 2 )
357+ self .assertEqual (gs .nrows , 2 )
358+ self .assertIs (sp [0 ].psy .base , ds )
359+ self .assertIs (sp [1 ].psy .base , ds )
356360
357361 def test_save_and_load_04_alternative_fname (self ):
358362 """Test the saving and loading of a Project providing alternative axes"""
@@ -387,42 +391,43 @@ def test_save_and_load_04_alternative_fname(self):
387391 arr_names = sp .arr_names
388392 self .assertEqual (tp .results [arr_names [0 ] + ".fmt1" ], "test" )
389393 self .assertEqual (tp .results [arr_names [1 ] + ".fmt1" ], "test" )
390- fname = "test.pkl"
391- self ._created_files .add (fname )
392- sp .save_project (fname )
393- psy .close ()
394- tp .results .clear ()
395- fig , axes = plt .subplots (1 , 2 )
396- sp = psy .Project .load_project (
397- fname ,
398- alternative_paths = [bt .get_file ("circumpolar_test.nc" )],
399- new_fig = False ,
400- )
401- self .assertEqual (len (sp ), 2 )
402- self .assertEqual (sp [0 ].psy .ax .get_figure ().number , 1 )
403- self .assertEqual (get_row_num (sp [0 ].psy .ax ), 0 )
404- self .assertEqual (get_col_num (sp [0 ].psy .ax ), 0 )
405394
406- gs = sp [0 ].psy .ax .get_gridspec ()
395+ with tempfile .TemporaryDirectory () as tmpdir :
396+ fname = str (tmpdir + "test.pkl" )
397+ sp .save_project (fname )
398+ psy .close ()
399+ tp .results .clear ()
400+ fig , axes = plt .subplots (1 , 2 )
401+ sp = psy .Project .load_project (
402+ fname ,
403+ alternative_paths = [bt .get_file ("circumpolar_test.nc" )],
404+ new_fig = False ,
405+ )
406+ self .assertEqual (len (sp ), 2 )
407+ self .assertEqual (sp [0 ].psy .ax .get_figure ().number , 1 )
408+ self .assertEqual (get_row_num (sp [0 ].psy .ax ), 0 )
409+ self .assertEqual (get_col_num (sp [0 ].psy .ax ), 0 )
407410
408- self .assertEqual (gs .ncols , 2 )
409- self .assertEqual (gs .nrows , 2 )
410- self .assertEqual (sp [1 ].psy .ax .get_figure ().number , 2 )
411- self .assertEqual (get_row_num (sp [1 ].psy .ax ), 0 )
412- self .assertEqual (get_col_num (sp [1 ].psy .ax ), 0 )
411+ gs = sp [0 ].psy .ax .get_gridspec ()
413412
414- gs = sp [1 ].psy .ax .get_gridspec ()
413+ self .assertEqual (gs .ncols , 2 )
414+ self .assertEqual (gs .nrows , 2 )
415+ self .assertEqual (sp [1 ].psy .ax .get_figure ().number , 2 )
416+ self .assertEqual (get_row_num (sp [1 ].psy .ax ), 0 )
417+ self .assertEqual (get_col_num (sp [1 ].psy .ax ), 0 )
415418
416- self .assertEqual (gs .ncols , 2 )
417- self .assertEqual (gs .nrows , 2 )
418- self .assertEqual (
419- psyd .get_filename_ds (sp [0 ].psy .base )[0 ],
420- bt .get_file ("circumpolar_test.nc" ),
421- )
422- self .assertEqual (
423- psyd .get_filename_ds (sp [1 ].psy .base )[0 ],
424- bt .get_file ("circumpolar_test.nc" ),
425- )
419+ gs = sp [1 ].psy .ax .get_gridspec ()
420+
421+ self .assertEqual (gs .ncols , 2 )
422+ self .assertEqual (gs .nrows , 2 )
423+ self .assertEqual (
424+ psyd .get_filename_ds (sp [0 ].psy .base )[0 ],
425+ bt .get_file ("circumpolar_test.nc" ),
426+ )
427+ self .assertEqual (
428+ psyd .get_filename_ds (sp [1 ].psy .base )[0 ],
429+ bt .get_file ("circumpolar_test.nc" ),
430+ )
426431
427432 def test_save_and_load_05_pack (self ):
428433 import tempfile
@@ -534,17 +539,18 @@ def test_save_and_load_06_post_fmt(self):
534539 post = 'self.ax.set_title("test")' ,
535540 )
536541 self .assertEqual (sp .plotters [0 ].ax .get_title (), "test" )
537- fname = "test.pkl"
538- self ._created_files .add (fname )
539- sp .save_project (fname )
540- psy .close ("all" )
541- # test without enabled post
542- sp = psy .Project .load_project (fname )
543- self .assertEqual (sp .plotters [0 ].ax .get_title (), "" )
544- psy .close ("all" )
545- # test with enabled post
546- sp = psy .Project .load_project (fname , enable_post = True )
547- self .assertEqual (sp .plotters [0 ].ax .get_title (), "test" )
542+
543+ with tempfile .TemporaryDirectory () as tmpdir :
544+ fname = str (tmpdir + "test.pkl" )
545+ sp .save_project (fname )
546+ psy .close ("all" )
547+ # test without enabled post
548+ sp = psy .Project .load_project (fname )
549+ self .assertEqual (sp .plotters [0 ].ax .get_title (), "" )
550+ psy .close ("all" )
551+ # test with enabled post
552+ sp = psy .Project .load_project (fname , enable_post = True )
553+ self .assertEqual (sp .plotters [0 ].ax .get_title (), "test" )
548554
549555 def test_save_and_load_07_sharedx (self ):
550556 """Test whether shared x- and y-axis are restored correctly"""
@@ -562,25 +568,26 @@ def test_save_and_load_07_sharedx(self):
562568 )
563569 axes [0 ].set_xlim (5 , 10 )
564570 self .assertEqual (list (axes [1 ].get_xlim ()), [5 , 10 ])
565- # save the project
566- fname = "test.pkl"
567- self ._created_files .add (fname )
568- sp .save_project (fname )
569- psy .close ("all" )
570571
571- # load the project
572- sp = psy .Project .load_project (fname )
573- self .assertEqual (len (sp .axes ), 3 , msg = sp .axes )
574- sp [0 ].psy .ax .set_xlim (10 , 15 )
575- self .assertEqual (list (sp [1 ].psy .ax .get_xlim ()), [10 , 15 ])
576-
577- # now we test, if it still works, if we remove the source axes
578- names2use = sp .arr_names [1 :]
579- psy .close ("all" )
580- sp = psy .Project .load_project (fname , only = names2use )
581- self .assertEqual (len (sp .axes ), 2 , msg = sp .axes )
582- sp [0 ].psy .ax .set_xlim (10 , 15 )
583- self .assertEqual (list (sp [1 ].psy .ax .get_xlim ()), [10 , 15 ])
572+ # save the project
573+ with tempfile .TemporaryDirectory () as tmpdir :
574+ fname = str (tmpdir + "test.pkl" )
575+ sp .save_project (fname )
576+ psy .close ("all" )
577+
578+ # load the project
579+ sp = psy .Project .load_project (fname )
580+ self .assertEqual (len (sp .axes ), 3 , msg = sp .axes )
581+ sp [0 ].psy .ax .set_xlim (10 , 15 )
582+ self .assertEqual (list (sp [1 ].psy .ax .get_xlim ()), [10 , 15 ])
583+
584+ # now we test, if it still works, if we remove the source axes
585+ names2use = sp .arr_names [1 :]
586+ psy .close ("all" )
587+ sp = psy .Project .load_project (fname , only = names2use )
588+ self .assertEqual (len (sp .axes ), 2 , msg = sp .axes )
589+ sp [0 ].psy .ax .set_xlim (10 , 15 )
590+ self .assertEqual (list (sp [1 ].psy .ax .get_xlim ()), [10 , 15 ])
584591
585592 def test_save_and_load_08_sharedy (self ):
586593 """Test whether shared x- and y-axis are restored correctly"""
@@ -598,25 +605,26 @@ def test_save_and_load_08_sharedy(self):
598605 )
599606 axes [0 ].set_ylim (5 , 10 )
600607 self .assertEqual (list (axes [1 ].get_ylim ()), [5 , 10 ])
601- # save the project
602- fname = "test.pkl"
603- self ._created_files .add (fname )
604- sp .save_project (fname )
605- psy .close ("all" )
606608
607- # load the project
608- sp = psy .Project .load_project (fname )
609- self .assertEqual (len (sp .axes ), 3 , msg = sp .axes )
610- sp [0 ].psy .ax .set_ylim (10 , 15 )
611- self .assertEqual (list (sp [1 ].psy .ax .get_ylim ()), [10 , 15 ])
612-
613- # now we test, if it still works, if we remove the source axes
614- names2use = sp .arr_names [1 :]
615- psy .close ("all" )
616- sp = psy .Project .load_project (fname , only = names2use )
617- self .assertEqual (len (sp .axes ), 2 , msg = sp .axes )
618- sp [0 ].psy .ax .set_ylim (10 , 15 )
619- self .assertEqual (list (sp [1 ].psy .ax .get_ylim ()), [10 , 15 ])
609+ # save the project
610+ with tempfile .TemporaryDirectory () as tmpdir :
611+ fname = str (tmpdir + "test.pkl" )
612+ sp .save_project (fname )
613+ psy .close ("all" )
614+
615+ # load the project
616+ sp = psy .Project .load_project (fname )
617+ self .assertEqual (len (sp .axes ), 3 , msg = sp .axes )
618+ sp [0 ].psy .ax .set_ylim (10 , 15 )
619+ self .assertEqual (list (sp [1 ].psy .ax .get_ylim ()), [10 , 15 ])
620+
621+ # now we test, if it still works, if we remove the source axes
622+ names2use = sp .arr_names [1 :]
623+ psy .close ("all" )
624+ sp = psy .Project .load_project (fname , only = names2use )
625+ self .assertEqual (len (sp .axes ), 2 , msg = sp .axes )
626+ sp [0 ].psy .ax .set_ylim (10 , 15 )
627+ self .assertEqual (list (sp [1 ].psy .ax .get_ylim ()), [10 , 15 ])
620628
621629 def test_versions_and_patch (self ):
622630 import warnings
0 commit comments