Skip to content

Commit

Permalink
Adding in test file
Browse files Browse the repository at this point in the history
  • Loading branch information
leohentschker committed Dec 16, 2024
1 parent 46ddddc commit 397fa3f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cl/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,9 @@ def invert_user_logs(
dates = make_date_str_list(start, end)
for d in dates:
for version in ["v3", "v4"]:
pipe.zrange(f"api:{version}.user.d:{d}.counts", 0, -1, withscores=True)
pipe.zrange(
f"api:{version}.user.d:{d}.counts", 0, -1, withscores=True
)
results = pipe.execute()

# results is a list of results for each of the zrange queries above. Zip
Expand Down
30 changes: 30 additions & 0 deletions cl/tests/api/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import unittest
from unittest.mock import MagicMock, patch

class TestApiUsage(unittest.TestCase):
def setUp(self):
self.start_date = '2023-01-01'
self.end_date = '2023-01-07'
self.expected_dates = ['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05', '2023-01-06', '2023-01-07']
self.v3_results = [('user1', 10), ('user2', 20)]
self.v4_results = [('user3', 15), ('user4', 25)]

@patch('cl.api.utils.get_redis_interface')
def test_recent_api_usage(self, mock_get_redis_interface):
mock_redis = MagicMock()
mock_pipeline = MagicMock()
mock_get_redis_interface.return_value = mock_redis
mock_redis.pipeline.return_value = mock_pipeline

mock_pipeline.execute.return_value = [
self.v3_results] * len(self.expected_dates) +
[self.v4_results] * len(self.expected_dates)

results = get_recent_api_usage(self.start_date, self.end_date)

for date in self.expected_dates:
mock_pipeline.zrange.assert_any_call(f"api:v3.user.d:{date}.counts", 0, -1, withscores=True)
mock_pipeline.zrange.assert_any_call(f"api:v4.user.d:{date}.counts", 0, -1, withscores=True)

self.assertIn(('user1', 10), results)
self.assertIn(('user3', 15), results)

0 comments on commit 397fa3f

Please sign in to comment.