简体   繁体   English

pyfakefs fixture 导致 pandas.read_csv() 在 pytest 中失败

[英]pyfakefs fixture causes pandas.read_csv() to fail in pytest

I am using pandas read_csv() function to read some CSV content and want to use "high" or "round_trip" floating-point precision.我正在使用pandas read_csv() function 读取一些 CSV 内容,并希望使用“高”或“round_trip”浮点精度。

The following works in the Python REPL or running a program with the python interpreter dircectly.以下工作在 Python REPL 中或直接使用 python 解释器运行程序。

import pandas as pd
from io import StringIO

df = pd.read_csv( StringIO('0.00042119645,3.4,8.8244e-5\r\n'), float_precision='round_trip' )

If I put in a pytest test it also works:)如果我输入pytest测试它也有效:)

However, if I use the pyfakes fixture then it fails !!但是,如果我使用pyfakes fixture 那么它就会失败! For some reason the pd.read_csv() function uses the python engine instead of the default C engine.出于某种原因, pd.read_csv() function 使用python引擎而不是默认的C引擎。 This is true even if I explicitly set engine='c' .即使我明确设置engine='c'也是如此。

The error reported is:报错是:

ValueError("The 'float_precision' option is not supported with the 'python' engine")
import pandas as pd
from io import StringIO

def test_pandas_read_csv(
        #mocker,     #! pytest-mock test fixture
        fs,         #! pyfakefs test fixture
        ):
    try :
        df = pd.read_csv( StringIO('0.00042119645,3.4,8.8244e-5\r\n'), float_precision='round_trip' )
        assert True
    except Exception as exc:
        assert False

How do I use the pandas read_csv() function with the default c engine in my pytest tests that also require the pyfakes fixture?如何在我的pytest测试中使用默认c引擎的 pandas read_csv() function 也需要pyfakes夹具?

Here is my pytest code.这是我的 pytest 代码。 The tests fail - comment out the fs, line to get it to pass all tests.测试失败 - 注释掉fs,行以使其通过所有测试。

import pytest

import pandas as pd
from io import StringIO

class Test_Pandas_Read_CSV :

    @pytest.mark.parametrize(
            'csv_str,kwargs,exp_status',
            [
                (
                    ('0.00042119645,3.4,8.8244e-5\r\n'),    #! csv_str
                    dict(                                   #! kwargs
                        float_precision='round_trip',
                        # engine='c',
                    ),
                    True,                                   #! exp_status
                ),
                (
                    ('0.00042119645,3.4,8.8244e-5\r\n'),    #! csv_str
                    dict(                                   #! kwargs
                        float_precision='round_trip',
                        engine='c',
                    ),
                    True,                                   #! exp_status
                ),
                (
                    ('0.00042119645,3.4,8.8244e-5\r\n'),    #! csv_str
                    dict(                                   #! kwargs
                        float_precision='round_trip',
                        engine='python',
                    ),
                    False,                                  #! exp_status
                ),
            ]
        )
    def test_pandas_read_csv(
            self,
            # mocker,                                         #! pytest-mock test fixture
            fs,                                             #! pyfakefs test fixture
            csv_str     : str,
            kwargs,
            exp_status  : bool,
        ) :

        try :
            df = pd.read_csv( StringIO(csv_str), **kwargs )
            status = True
        except Exception as exc:
            status = False

        assert status == exp_status

My work-around involves mocking the pandas.read_csv() call with a function that first pops the float_precision key from the keyword arguments, and then calls the proper/original pandas.read_csv() function. I wrapped that in a customized pyfakes fixture to do the mocking My work-around involves mocking the pandas.read_csv() call with a function that first pops the float_precision key from the keyword arguments, and then calls the proper/original pandas.read_csv() function. I wrapped that in a customized pyfakes fixture to做 mocking

Though this makes the tests pass, I am concerned that the real C based functions aren't being called and thus the results may vary from what would run on the real target.虽然这使测试通过,但我担心没有调用真正的基于 C 的函数,因此结果可能与在真实目标上运行的结果不同。

orig_pandas_read_csv = pd.read_csv

def mock_pandas_read_csv( *args, **kwargs ):
    kwargs.pop('float_precision', None)
    return orig_pandas_read_csv( *args, **kwargs )


@pytest.fixture
def my_fs(mocker, fs):
    mocker.patch.object( pd, 'read_csv', new=mock_pandas_read_csv )
    yield fs

...

    def test_pandas_read_csv(
            self,
            # mocker,                                         #! pytest-mock test fixture
            my_fs,                                          #! pyfakefs test fixture (with mocked pandas functions)
            csv_str     : str,
            kwargs,
            exp_status  : bool,
            ) :
        try :
            df = pd.read_csv( StringIO(csv_str), **kwargs )
            status = True
        except Exception as exc:
            status = False

        assert status == exp_status

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM