Skip to content

Commit

Permalink
Test caching for pandas dataframe
Browse files Browse the repository at this point in the history
  • Loading branch information
nezhar committed Oct 13, 2023
1 parent f6b2acf commit e1b0fd5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
python-version: ${{ matrix.python-version }}

- name: Install CI dependencies
run: pip install flake8 codecov ${{ matrix.pytest-version }} ${{ matrix.pytest-django-version }}
run: pip install flake8 codecov pandas ${{ matrix.pytest-version }} ${{ matrix.pytest-django-version }}

- name: Install Django ${{ matrix.django-version }}
run: pip install 'Django==${{ matrix.django-version }}'
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ def test_sync_two_requests(self, mock_func):
self.client.get('/')
self.assertEqual(mock_func.call_count, 4)
self.assertEqual(cached_func.call_count, 2)

def test_sync_pandas_dataframe(self):
"""
Esure that the cache_for_request decorator works with pandas dataframes.
"""
response = self.client.get('/pandas_dataframe/')
self.assertEqual(response.status_code, 200)
3 changes: 2 additions & 1 deletion tests/testapp/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.urls import path

from testapp.views.sync import index_view
from testapp.views.sync import index_view, pandas_view

urlpatterns = [
path('', index_view, name='index'),
path('pandas_dataframe/', pandas_view, name='pandas_dataframe'),
]
19 changes: 19 additions & 0 deletions tests/testapp/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pandas as pd
from datetime import datetime

from django_request_cache import cache_for_request
Expand All @@ -6,3 +7,21 @@
@cache_for_request
def retrieve_time():
return datetime.now()


def retrieve_dataframe_uncached():
return pd.DataFrame({
'a': [1, 2, 3],
'b': [4.0, 5.0, 6.0],
'c': ['a', 'b', 'c'],
'd': [True, False, True],
'e': datetime.now(),
'f': pd.NA
})


@cache_for_request
def retrieve_dataframe_cached():
return retrieve_dataframe_uncached()


21 changes: 20 additions & 1 deletion tests/testapp/views/sync.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
from django.http import HttpResponse
from testapp.utils import retrieve_time
from testapp.utils import retrieve_time, retrieve_dataframe_cached, retrieve_dataframe_uncached


def index_view(request):
retrieve_time()
retrieve_time()
return HttpResponse()


def pandas_view(request):
"""
Esure that the cache_for_request decorator works with pandas dataframes.
"""
df1 = retrieve_dataframe_cached()
df2 = retrieve_dataframe_cached()

if not df1.equals(df2):
raise Exception("Dataframes are not equal")

df3 = retrieve_dataframe_uncached()
df4 = retrieve_dataframe_uncached()

if df3.equals(df4):
raise Exception("Dataframes are equal")

return HttpResponse()

0 comments on commit e1b0fd5

Please sign in to comment.