Skip to content

Commit 0ce20db

Browse files
committed
Fixes test_to_dataframe by mocking pandas when it is not installed
1 parent d4fd6a3 commit 0ce20db

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

tests/test_api_entitylist.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,19 @@ def test_to_dataframe(self):
7676
mock_dataframe.__len__ = MagicMock(return_value=3)
7777
mock_dataframe.__getitem__ = MagicMock(return_value=MagicMock(__getitem__=lambda self, key: 123))
7878

79-
with patch('pandas.DataFrame') as mock_df_class, \
80-
patch('pandas.json_normalize') as mock_json_normalize:
81-
mock_df_class.from_dict.return_value = mock_dataframe
82-
mock_json_normalize.return_value = mock_dataframe
83-
79+
# Create mock pandas module
80+
mock_pandas = MagicMock()
81+
mock_pandas.DataFrame.from_dict.return_value = mock_dataframe
82+
mock_pandas.json_normalize.return_value = mock_dataframe
83+
84+
with patch.dict('sys.modules', {'pandas': mock_pandas}):
8485
dataframe = entitylist.to_dataframe()
8586
assert len(dataframe) == 3
86-
mock_df_class.from_dict.assert_called_once()
87+
mock_pandas.DataFrame.from_dict.assert_called_once()
8788

8889
dataframe = entitylist.to_dataframe(True)
8990
assert len(dataframe) == 3
90-
mock_json_normalize.assert_called_once()
91+
mock_pandas.json_normalize.assert_called_once()
9192
assert int(dataframe["location.x"][-1:]) == 123
9293

9394

0 commit comments

Comments
 (0)