22import pathlib
33import random
44import string
5+ import sys
56import tempfile
67
78import numpy
@@ -143,27 +144,20 @@ def test_tfs_write_empty_index_dataframe(self, _test_file: str):
143144 assert_dict_equal (df .headers , new .headers , compare_keys = True )
144145
145146 def test_write_int_float_str_columns (self , _test_file : str ):
146- """ This test is more of an extension of the test below
147- (this dataframe was not affected by the bug) """
147+ """This test is more of an extension of the test below
148+ (this dataframe was not affected by the bug)"""
148149 df = TfsDataFrame (
149- data = [[1 , 1. , "one" ],
150- [2 , 2. , "two" ],
151- [3 , 3. , "three" ]],
152- columns = ["Int" , "Float" , "String" ]
150+ data = [[1 , 1.0 , "one" ], [2 , 2.0 , "two" ], [3 , 3.0 , "three" ]],
151+ columns = ["Int" , "Float" , "String" ],
153152 )
154153 write_tfs (_test_file , df )
155154 new = read_tfs (_test_file )
156155 assert_frame_equal (df , new )
157156
158157 def test_write_int_float_columns (self , _test_file : str ):
159- """ This test is here because of numeric conversion bug
160- upon writing back in v2.0.1 """
161- df = TfsDataFrame (
162- data = [[1 , 1. ],
163- [2 , 2. ],
164- [3 , 3. ]],
165- columns = ["Int" , "Float" ]
166- )
158+ """This test is here because of numeric conversion bug
159+ upon writing back in v2.0.1"""
160+ df = TfsDataFrame (data = [[1 , 1.0 ], [2 , 2.0 ], [3 , 3.0 ]], columns = ["Int" , "Float" ])
167161 write_tfs (_test_file , df )
168162 new = read_tfs (_test_file )
169163 assert_frame_equal (df , new )
@@ -178,24 +172,33 @@ def test_absent_attributes_and_keys(self, _tfs_file_str: str):
178172 with pytest .raises (KeyError ):
179173 _ = test_file ["Not_HERE" ]
180174
181- def test_fail_on_non_unique_columns (self , caplog ):
175+ def test_raising_on_non_unique_columns (self , caplog ):
182176 df = TfsDataFrame (columns = ["A" , "B" , "A" ])
183177 with pytest .raises (TfsFormatError ):
184- write_tfs ("" , df )
178+ write_tfs ("" , df , non_unique_behavior = "raise" )
185179
186180 for record in caplog .records :
187- assert record .levelname == "ERROR "
181+ assert record .levelname == "WARNING "
188182 assert "Non-unique column names found" in caplog .text
189183
190- def test_fail_on_non_unique_index (self , caplog ):
184+ def test_raising_on_non_unique_index (self , caplog ):
191185 df = TfsDataFrame (index = ["A" , "B" , "A" ])
192186 with pytest .raises (TfsFormatError ):
193- write_tfs ("" , df )
187+ write_tfs ("" , df , non_unique_behavior = "raise" )
194188
195189 for record in caplog .records :
196- assert record .levelname == "ERROR "
190+ assert record .levelname == "WARNING "
197191 assert "Non-unique indices found" in caplog .text
198192
193+ def test_raising_on_non_unique_both (self , caplog ):
194+ df = TfsDataFrame (index = ["A" , "B" , "A" ], columns = ["A" , "B" , "A" ])
195+ with pytest .raises (TfsFormatError ):
196+ write_tfs ("" , df , non_unique_behavior = "raise" )
197+
198+ for record in caplog .records :
199+ assert record .levelname == "WARNING"
200+ assert "Non-unique indices found" in caplog .text # first checked and raised
201+
199202 def test_fail_on_wrong_column_type (self , caplog ):
200203 df = TfsDataFrame (columns = range (5 ))
201204 with pytest .raises (TfsFormatError ):
@@ -291,6 +294,11 @@ def test_id_to_type_handles_typo_str_id(self):
291294 with pytest .raises (TfsFormatError ):
292295 _ = tfs .handler ._id_to_type (typoed_str_id )
293296
297+ def test_validate_raises_on_wrong_unique_behavior (self , caplog ):
298+ df = TfsDataFrame (index = ["A" , "B" , "A" ], columns = ["A" , "B" , "A" ])
299+ with pytest .raises (KeyError ):
300+ tfs .handler ._validate (df , "" , non_unique_behavior = "invalid" )
301+
294302
295303def test_id_to_type_handles_madx_string_identifier ():
296304 madx_str_id = "%20s"
@@ -305,14 +313,44 @@ def test_warn_unphysical_values(self, caplog):
305313 assert record .levelname == "WARNING"
306314 assert "contains non-physical values at Index:" in caplog .text
307315
316+ @pytest .mark .skipif (
317+ sys .version_info >= (3 , 7 ),
318+ reason = "Our workers on 3.7+ install pandas >= 1.3.0 which has fixed the .convert_dtypes() bug "
319+ "we try...except in _autoset_pandas_types and test here" ,
320+ )
308321 def test_empty_df_warns_on_types_inference (self , caplog ):
309322 empty_df = pandas .DataFrame ()
310323 converted_df = tfs .handler ._autoset_pandas_types (empty_df )
311324 assert_frame_equal (converted_df , empty_df )
312325
313326 for record in caplog .records :
314327 assert record .levelname == "WARNING"
315- assert "An empty dataframe was provided, no types were infered" in caplog .text
328+ assert "An empty dataframe was provided, no types were inferred" in caplog .text
329+
330+ def test_warning_on_non_unique_columns (self , tmp_path , caplog ):
331+ df = TfsDataFrame (columns = ["A" , "B" , "A" ])
332+ write_tfs (tmp_path / "temporary.tfs" , df )
333+
334+ for record in caplog .records :
335+ assert record .levelname == "WARNING"
336+ assert "Non-unique column names found" in caplog .text
337+
338+ def test_warning_on_non_unique_index (self , tmp_path , caplog ):
339+ df = TfsDataFrame (index = ["A" , "B" , "A" ])
340+ write_tfs (tmp_path / "temporary.tfs" , df )
341+
342+ for record in caplog .records :
343+ assert record .levelname == "WARNING"
344+ assert "Non-unique indices found" in caplog .text
345+
346+ def test_warning_on_non_unique_both (self , tmp_path , caplog ):
347+ df = TfsDataFrame (index = ["A" , "B" , "A" ], columns = ["A" , "B" , "A" ])
348+ write_tfs (tmp_path / "temporary.tfs" , df )
349+
350+ for record in caplog .records :
351+ assert record .levelname == "WARNING"
352+ assert "Non-unique column names found" in caplog .text
353+ assert "Non-unique indices found" in caplog .text
316354
317355
318356class TestPrinting :
@@ -359,7 +397,10 @@ def _tfs_dataframe() -> TfsDataFrame:
359397@pytest .fixture ()
360398def _dataframe_empty_headers () -> TfsDataFrame :
361399 return TfsDataFrame (
362- index = range (3 ), columns = "a b c d e" .split (), data = numpy .random .rand (3 , 5 ), headers = {},
400+ index = range (3 ),
401+ columns = "a b c d e" .split (),
402+ data = numpy .random .rand (3 , 5 ),
403+ headers = {},
363404 )
364405
365406
@@ -431,7 +472,9 @@ def _no_colnames_tfs_path() -> pathlib.Path:
431472@pytest .fixture ()
432473def _pd_dataframe () -> pandas .DataFrame :
433474 return pandas .DataFrame (
434- index = range (3 ), columns = "a b c d e" .split (), data = numpy .random .rand (3 , 5 ),
475+ index = range (3 ),
476+ columns = "a b c d e" .split (),
477+ data = numpy .random .rand (3 , 5 ),
435478 )
436479
437480
0 commit comments