Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test caching for pandas dataframe #18

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading