1
- import redis
1
+ import pandas as pd
2
2
import pytest
3
+ import redis
3
4
4
5
from pems_data .cache import Cache , redis_connection
5
6
@@ -107,7 +108,7 @@ def test_get(self, cache: Cache, mock_redis_connection, spy_connect):
107
108
spy_connect .assert_called_once ()
108
109
mock_redis_connection .get .assert_called_once_with ("test-key" )
109
110
110
- def test_get_mutate (self , cache : Cache , mock_redis_connection , spy_connect ):
111
+ def test_get__mutate (self , cache : Cache , mock_redis_connection , spy_connect ):
111
112
expected = 2
112
113
mock_redis_connection .get .return_value = 1
113
114
@@ -123,10 +124,60 @@ def test_set(self, cache: Cache, mock_redis_connection, spy_connect):
123
124
spy_connect .assert_called_once ()
124
125
mock_redis_connection .set .assert_called_once_with ("test-key" , "test-value" )
125
126
126
- def test_set_mutate (self , cache : Cache , mock_redis_connection , spy_connect ):
127
+ def test_set__mutate (self , cache : Cache , mock_redis_connection , spy_connect ):
127
128
expected = 2
128
129
129
130
cache .set ("test-key" , 1 , lambda v : v + 1 )
130
131
131
132
spy_connect .assert_called_once ()
132
133
mock_redis_connection .set .assert_called_once_with ("test-key" , expected )
134
+
135
+ def test_get_df (self , cache : Cache , mock_redis_connection , mocker , spy_connect ):
136
+ # Mock arrow_bytes_to_df to return a DataFrame
137
+ df = mocker .Mock (spec = pd .DataFrame )
138
+ arrow_bytes = b"arrow-bytes"
139
+ mock_redis_connection .get .return_value = arrow_bytes
140
+ mock_arrow_bytes_to_df = mocker .patch ("pems_data.cache.arrow_bytes_to_df" , return_value = df )
141
+
142
+ result = cache .get_df ("df-key" )
143
+
144
+ spy_connect .assert_called_once ()
145
+ mock_redis_connection .get .assert_called_once_with ("df-key" )
146
+ mock_arrow_bytes_to_df .assert_called_once_with (arrow_bytes )
147
+ assert result == df
148
+
149
+ def test_set_df (self , cache : Cache , mock_redis_connection , spy_connect , df ):
150
+ """Test setting a DataFrame in the cache"""
151
+ cache .set_df ("test-key" , df )
152
+
153
+ spy_connect .assert_called_once ()
154
+ mock_redis_connection .set .assert_called_once ()
155
+ # Verify first arg is the key
156
+ assert mock_redis_connection .set .call_args [0 ][0 ] == "test-key"
157
+ # Verify second arg is bytes (arrow serialized)
158
+ assert isinstance (mock_redis_connection .set .call_args [0 ][1 ], bytes )
159
+
160
+ def test_set_df__empty_df (self , cache : Cache , mock_redis_connection , spy_connect ):
161
+ empty_df = pd .DataFrame ()
162
+ cache .set_df ("test-key" , empty_df )
163
+
164
+ spy_connect .assert_called_once ()
165
+ mock_redis_connection .set .assert_called_once ()
166
+ # Verify empty DataFrame is handled
167
+ assert isinstance (mock_redis_connection .set .call_args [0 ][1 ], bytes )
168
+
169
+ def test_set_df__roundtrip (self , cache : Cache , mock_redis_connection , spy_connect , df , mocker ):
170
+ # Setup mock to return the serialized value on get
171
+ def mock_set (key , value ):
172
+ mock_redis_connection .get .return_value = value
173
+
174
+ mock_redis_connection .set .side_effect = mock_set
175
+
176
+ # Set the DataFrame
177
+ cache .set_df ("test-key" , df )
178
+
179
+ # Get it back
180
+ result = cache .get_df ("test-key" )
181
+
182
+ pd .testing .assert_frame_equal (result , df )
183
+ spy_connect .assert_has_calls ([mocker .call (), mocker .call ()])
0 commit comments