2424meta = meta
2525
2626
27- class TestCheckedSession (CheckedSession ):
27+ class CheckedSessionExample (CheckedSession ):
2828 b = b
2929 b024 = b024
3030 a : Axis
@@ -42,7 +42,7 @@ class TestCheckedSession(CheckedSession):
4242
4343@pytest .fixture ()
4444def checkedsession ():
45- return TestCheckedSession (a = a , a2 = a2 , a01 = a01 , e = e , g = g , f = f , h = h )
45+ return CheckedSessionExample (a = a , a2 = a2 , a01 = a01 , e = e , g = g , f = f , h = h )
4646
4747
4848def test_create_checkedsession_instance (meta ):
@@ -53,7 +53,7 @@ def test_create_checkedsession_instance(meta):
5353 declared_variable_keys = ['a' , 'a2' , 'a01' , 'c' , 'e' , 'g' , 'f' , 'h' , 'b' , 'b024' , 'anonymous' , 'ano01' , 'd' ]
5454
5555 # setting variables without default values
56- cs = TestCheckedSession (a , a01 , a2 = a2 , e = e , f = f , g = g , h = h )
56+ cs = CheckedSessionExample (a , a01 , a2 = a2 , e = e , f = f , g = g , h = h )
5757 assert list (cs .keys ()) == declared_variable_keys
5858 assert cs .b .equals (b )
5959 assert cs .b024 .equals (b024 )
@@ -70,18 +70,19 @@ def test_create_checkedsession_instance(meta):
7070 assert cs .h .equals (h )
7171
7272 # metadata
73- cs = TestCheckedSession (a , a01 , a2 = a2 , e = e , f = f , g = g , h = h , meta = meta )
73+ cs = CheckedSessionExample (a , a01 , a2 = a2 , e = e , f = f , g = g , h = h , meta = meta )
7474 assert cs .meta == meta
7575
7676 # override default value
7777 b_alt = Axis ('b=b0..b4' )
78- cs = TestCheckedSession (a , a01 , b = b_alt , a2 = a2 , e = e , f = f , g = g , h = h )
78+ cs = CheckedSessionExample (a , a01 , b = b_alt , a2 = a2 , e = e , f = f , g = g , h = h )
7979 assert cs .b is b_alt
8080
8181 # test for "NOT_LOADED" variables
82- with must_warn (UserWarning , msg = "No value passed for the declared variable 'a'" , check_file = False ):
83- TestCheckedSession (a01 = a01 , a2 = a2 , e = e , f = f , g = g , h = h )
84- cs = TestCheckedSession ()
82+ with must_warn (UserWarning , msg = "No value passed for the declared variable 'a'" ):
83+ CheckedSessionExample (a01 = a01 , a2 = a2 , e = e , f = f , g = g , h = h )
84+ with must_warn (UserWarning , match = r"No value passed for the declared variable '\w+'" , num_expected = 7 ):
85+ cs = CheckedSessionExample ()
8586 assert list (cs .keys ()) == declared_variable_keys
8687 # --- variables with default values ---
8788 assert cs .b .equals (b )
@@ -100,17 +101,17 @@ def test_create_checkedsession_instance(meta):
100101 assert isinstance (cs .h , NotLoaded )
101102
102103 # passing a scalar to set all elements a CheckedArray
103- cs = TestCheckedSession (a , a01 , a2 = a2 , e = e , f = f , g = g , h = 5 )
104+ cs = CheckedSessionExample (a , a01 , a2 = a2 , e = e , f = f , g = g , h = 5 )
104105 assert cs .h .axes == AxisCollection ((a3 , b2 ))
105106 assert cs .h .equals (full (axes = (a3 , b2 ), fill_value = 5 ))
106107
107108 # add the undeclared variable 'i'
108- with must_warn (UserWarning , f"'i' is not declared in '{ cs .__class__ .__name__ } '" , check_file = False ):
109- cs = TestCheckedSession (a , a01 , a2 = a2 , i = 5 , e = e , f = f , g = g , h = h )
109+ with must_warn (UserWarning , f"'i' is not declared in '{ cs .__class__ .__name__ } '" ):
110+ cs = CheckedSessionExample (a , a01 , a2 = a2 , i = 5 , e = e , f = f , g = g , h = h )
110111 assert list (cs .keys ()) == declared_variable_keys + ['i' ]
111112
112113 # test inheritance between checked sessions
113- class TestInheritance (TestCheckedSession ):
114+ class TestInheritance (CheckedSessionExample ):
114115 # override variables
115116 b = b2
116117 c : int = 5
@@ -145,7 +146,7 @@ class TestInheritance(TestCheckedSession):
145146
146147@needs_pytables
147148def test_init_checkedsession_hdf ():
148- cs = TestCheckedSession (inputpath ('test_session.h5' ))
149+ cs = CheckedSessionExample (inputpath ('test_session.h5' ))
149150 assert set (cs .keys ()) == {'b' , 'b024' , 'a' , 'a2' , 'anonymous' , 'a01' , 'ano01' , 'c' , 'd' , 'e' , 'g' , 'f' , 'h' }
150151
151152
@@ -260,7 +261,7 @@ def test_add_cs(checkedsession):
260261 test_add (cs )
261262
262263 u = Axis ('u=u0..u2' )
263- with must_warn (UserWarning , msg = f"'u' is not declared in '{ cs .__class__ .__name__ } '" , check_file = False ):
264+ with must_warn (UserWarning , msg = f"'u' is not declared in '{ cs .__class__ .__name__ } '" ):
264265 cs .add (u )
265266
266267
@@ -276,7 +277,8 @@ def test_iter_cs(checkedsession):
276277def test_filter_cs (checkedsession ):
277278 # see comment in test_iter_cs() about fields ordering
278279 cs = checkedsession
279- cs .ax = 'ax'
280+ with must_warn (UserWarning , msg = "'ax' is not declared in 'CheckedSessionExample'" ):
281+ cs .ax = 'ax'
280282 assert_seq_equal (cs .filter (), [a , a2 , a01 , c , e , g , f , h , b , b024 , anonymous , ano01 , d , 'ax' ])
281283 assert_seq_equal (cs .filter ('a*' ), [a , a2 , a01 , anonymous , ano01 , 'ax' ])
282284 assert list (cs .filter ('a*' , dict )) == []
@@ -306,9 +308,10 @@ def _test_io_cs(tmpdir, meta, engine, ext):
306308
307309 # a) - all typed variables have a defined value
308310 # - no extra variables are added
309- csession = TestCheckedSession (a = a , a2 = a2 , a01 = a01 , d = d , e = e , g = g , f = f , h = h , meta = meta )
311+ csession = CheckedSessionExample (a = a , a2 = a2 , a01 = a01 , d = d , e = e , g = g , f = f , h = h , meta = meta )
310312 csession .save (fpath , engine = engine )
311- cs = TestCheckedSession ()
313+ with must_warn (UserWarning , match = r"No value passed for the declared variable '\w+'" , num_expected = 7 ):
314+ cs = CheckedSessionExample ()
312315 cs .load (fpath , engine = engine )
313316 # --- keys ---
314317 assert list (cs .keys ()) == list (csession .keys ())
@@ -343,12 +346,14 @@ def _test_io_cs(tmpdir, meta, engine, ext):
343346
344347 # b) - not all typed variables have a defined value
345348 # - no extra variables are added
346- csession = TestCheckedSession (a = a , d = d , e = e , h = h , meta = meta )
349+ with must_warn (UserWarning , match = r"No value passed for the declared variable '\w+'" , num_expected = 4 ):
350+ csession = CheckedSessionExample (a = a , d = d , e = e , h = h , meta = meta )
347351 if 'csv' in engine :
348352 import shutil
349353 shutil .rmtree (fpath )
350354 csession .save (fpath , engine = engine )
351- cs = TestCheckedSession ()
355+ with must_warn (UserWarning , match = r"No value passed for the declared variable '\w+'" , num_expected = 7 ):
356+ cs = CheckedSessionExample ()
352357 cs .load (fpath , engine = engine )
353358 # --- keys ---
354359 assert list (cs .keys ()) == list (csession .keys ())
@@ -379,10 +384,27 @@ def _test_io_cs(tmpdir, meta, engine, ext):
379384 i = ndtest (6 )
380385 j = ndtest ((3 , 3 ))
381386 k = ndtest ((2 , 2 ))
382- csession = TestCheckedSession (a = a , a2 = a2 , a01 = a01 , d = d , e = e , g = g , f = f , h = h , k = k , j = j , i = i , meta = meta )
387+ with must_warn (UserWarning , match = r"'\w' is not declared in 'CheckedSessionExample'" , num_expected = 3 ):
388+ csession = CheckedSessionExample (a = a , a2 = a2 , a01 = a01 , d = d , e = e , g = g , f = f , h = h , k = k , j = j , i = i , meta = meta )
383389 csession .save (fpath , engine = engine )
384- cs = TestCheckedSession ()
385- cs .load (fpath , engine = engine )
390+ with must_warn (UserWarning , match = r"No value passed for the declared variable '\w+'" , num_expected = 7 ):
391+ cs = CheckedSessionExample ()
392+
393+ # number of expected warnings is different depending on engine
394+ expected_warnings = {
395+ 'pandas_excel' : 3 ,
396+ 'xlwings_excel' : 3 ,
397+ 'pandas_csv' : 3 ,
398+ 'pandas_hdf' : 47 , # FIXME: there is something fishy here
399+ 'pickle' : 3 ,
400+ }
401+ num_expected = expected_warnings [engine ]
402+ # FIXME: we should try to fix the bad warning line instead of ignoring it
403+ check_file = engine != 'pandas_hdf'
404+ with must_warn (UserWarning , match = r"'\w' is not declared in 'CheckedSessionExample'" ,
405+ check_file = check_file , num_expected = num_expected ):
406+ cs .load (fpath , engine = engine )
407+
386408 # --- names ---
387409 # we do not use keys() since order of undeclared variables
388410 # may not be preserved (at least for the HDF format)
@@ -394,15 +416,29 @@ def _test_io_cs(tmpdir, meta, engine, ext):
394416
395417 # Update a Group + an Axis + an array (overwrite=False)
396418 # -----------------------------------------------------
397- csession = TestCheckedSession (a = a , a2 = a2 , a01 = a01 , d = d , e = e , g = g , f = f , h = h , meta = meta )
419+ csession = CheckedSessionExample (a = a , a2 = a2 , a01 = a01 , d = d , e = e , g = g , f = f , h = h , meta = meta )
398420 csession .save (fpath , engine = engine )
399421 a4 = Axis ('a=0..3' )
400422 a4_01 = a3 ['0,1' ] >> 'a01'
401423 e2 = ndtest ((a4 , 'b=b0..b2' ))
402424 h2 = full_like (h , fill_value = 10 )
403- TestCheckedSession (a = a4 , a01 = a4_01 , e = e2 , h = h2 ).save (fpath , overwrite = False , engine = engine )
404- cs = TestCheckedSession ()
405- cs .load (fpath , engine = engine )
425+ with must_warn (UserWarning , match = r"No value passed for the declared variable '\w+'" , num_expected = 3 ):
426+ CheckedSessionExample (a = a4 , a01 = a4_01 , e = e2 , h = h2 ).save (fpath , overwrite = False , engine = engine )
427+ with must_warn (UserWarning , match = r"No value passed for the declared variable '\w+'" , num_expected = 7 ):
428+ cs = CheckedSessionExample ()
429+
430+ # number of expected warnings is different depending on engine
431+ expected_warnings = {
432+ 'pandas_excel' : 0 ,
433+ 'xlwings_excel' : 0 ,
434+ 'pandas_hdf' : 0 ,
435+ 'pandas_csv' : 3 ,
436+ 'pickle' : 0 ,
437+ }
438+ num_expected = expected_warnings [engine ]
439+ with must_warn (UserWarning , match = r"'\w' is not declared in 'CheckedSessionExample'" ,
440+ num_expected = num_expected ):
441+ cs .load (fpath , engine = engine )
406442 # --- variables with default values ---
407443 assert cs .b .equals (b )
408444 assert cs .b024 .equals (b024 )
@@ -440,9 +476,10 @@ def _test_io_cs(tmpdir, meta, engine, ext):
440476
441477 # Load only some objects
442478 # ----------------------
443- csession = TestCheckedSession (a = a , a2 = a2 , a01 = a01 , d = d , e = e , g = g , f = f , h = h , meta = meta )
479+ csession = CheckedSessionExample (a = a , a2 = a2 , a01 = a01 , d = d , e = e , g = g , f = f , h = h , meta = meta )
444480 csession .save (fpath , engine = engine )
445- cs = TestCheckedSession ()
481+ with must_warn (UserWarning , match = r"No value passed for the declared variable '\w+'" , num_expected = 7 ):
482+ cs = CheckedSessionExample ()
446483 names_to_load = ['e' , 'h' ] if is_excel_or_csv else ['a' , 'a01' , 'a2' , 'e' , 'h' ]
447484 cs .load (fpath , names = names_to_load , engine = engine )
448485 # --- keys ---
0 commit comments