Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

python - Mocking datetime yields bug in pyarrow

For testing, I want to mock datetime.datetime.now() like here.

The function I am testing is reading a table via pyarrow:

import pytest
import pyarrow.parquet as pq
import datetime

mockdate = datetime.datetime(2000, 1, 1, 0, 0, 0)

@pytest.fixture(autouse=True)
def patch_datetime_now(monkeypatch):
    class mydatetime:
        @classmethod
        def now(cls):
            return mockdate

    monkeypatch.setattr(datetime, 'datetime', mydatetime)
    
def function_to_test():
    date = datetime.datetime.now()
    some_table = pq.read_table("abc/def")
    return some_table, date
    
def test_function_to_test():
    
    table, date = function_to_test()
    
    assert date == mockdate

For some reason, mocking the now() function creates this very weird bug in pyarrow:

ValueError: datetime.datetime size changed, may indicate binary incompatibility. Expected 48 from C header, got 32 from PyObject

Any idea how to fix this? Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Thanks chepner for the hint. For future reference, here is a working solution:

import pytest
import pyarrow.parquet as pq
import datetime

mockdate = datetime.datetime(2000, 1, 1, 0, 0, 0)
    
def function_to_test():
    date = datetime.datetime.now()
    some_table = pq.read_table("abc/def")
    return some_table, date
    
def test_function_to_test(mocker):

    mocker.patch(__name__  + '.datetime')
    datetime.datetime.now.return_value = mockdate
    
    table, date = function_to_test()
    
    assert date == mockdate

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...