11from __future__ import annotations
22
3+ import importlib
34import tempfile
45
56import pytest
67from luigi import LocalTarget
78
9+ from gokart import file_processor
810from gokart .file_processor import PolarsCsvFileProcessor , PolarsFeatherFileProcessor , PolarsJsonFileProcessor
911
12+
1013pl = pytest .importorskip ('polars' , reason = 'polars required' )
1114pl_testing = pytest .importorskip ('polars.testing' , reason = 'polars required' )
1215
1316
14- def test_dump_csv_with ():
17+ @pytest .fixture
18+ def reload_processor (monkeypatch ):
19+ """
20+ A pytest fixture that reloads the `gokart.file_processor` module after modifying
21+ the environment variable `GOKART_DATAFRAME_FRAMEWORK`. This ensures that polars
22+ is used when reloading the module.
23+
24+ Returns:
25+ Tuple[Type[PolarsCsvFileProcessor], Type[PolarsFeatherFileProcessor], Type[PolarsJsonFileProcessor]]:
26+ The reloaded classes from the `gokart.file_processor` module.
27+ """
28+ monkeypatch .setenv ('GOKART_DATAFRAME_FRAMEWORK' , 'polars' )
29+ importlib .reload (file_processor )
30+ from gokart .file_processor import PolarsCsvFileProcessor , PolarsFeatherFileProcessor , PolarsJsonFileProcessor
31+
32+ yield PolarsCsvFileProcessor , PolarsFeatherFileProcessor , PolarsJsonFileProcessor
33+
34+
35+ def test_dump_csv (reload_processor ):
36+ PolarsCsvFileProcessor , _ , _ = reload_processor
1537 df = pl .DataFrame ({'あ' : [1 , 2 , 3 ], 'い' : [4 , 5 , 6 ]})
1638 processor = PolarsCsvFileProcessor ()
1739
@@ -27,7 +49,8 @@ def test_dump_csv_with():
2749 pl_testing .assert_frame_equal (df , loaded_df )
2850
2951
30- def test_load_csv ():
52+ def test_load_csv (reload_processor ):
53+ PolarsCsvFileProcessor , _ , _ = reload_processor
3154 df = pl .DataFrame ({'あ' : [1 , 2 , 3 ], 'い' : [4 , 5 , 6 ]})
3255 processor = PolarsCsvFileProcessor ()
3356
@@ -63,7 +86,8 @@ def test_load_csv():
6386 pytest .param ('records' , {}, '' , id = 'With Records Orient for Empty Dict' ),
6487 ],
6588)
66- def test_dump_and_load_json (orient , input_data , expected_json ):
89+ def test_dump_and_load_json (reload_processor , orient , input_data , expected_json ):
90+ _ , _ , PolarsJsonFileProcessor = reload_processor
6791 processor = PolarsJsonFileProcessor (orient = orient )
6892
6993 with tempfile .TemporaryDirectory () as temp_dir :
@@ -82,7 +106,8 @@ def test_dump_and_load_json(orient, input_data, expected_json):
82106 pl_testing .assert_frame_equal (df_input , loaded_df )
83107
84108
85- def test_feather_should_return_same_dataframe ():
109+ def test_feather_should_return_same_dataframe (reload_processor ):
110+ _ , PolarsFeatherFileProcessor , _ = reload_processor
86111 df = pl .DataFrame ({'a' : [1 ]})
87112 # TODO: currently we set store_index_in_feather True but it is ignored
88113 processor = PolarsFeatherFileProcessor (store_index_in_feather = True )
0 commit comments