From e1b0fd59efd622dbf3cf210c2432dcb34b3f3b9e Mon Sep 17 00:00:00 2001 From: Harald Nezbeda Date: Fri, 13 Oct 2023 17:22:05 +0200 Subject: [PATCH] Test caching for pandas dataframe --- .github/workflows/test.yml | 2 +- tests/functional/test_sync.py | 7 +++++++ tests/testapp/urls.py | 3 ++- tests/testapp/utils.py | 19 +++++++++++++++++++ tests/testapp/views/sync.py | 21 ++++++++++++++++++++- 5 files changed, 49 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b639813..b20f161 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 }}' diff --git a/tests/functional/test_sync.py b/tests/functional/test_sync.py index 22b4794..d96f69d 100644 --- a/tests/functional/test_sync.py +++ b/tests/functional/test_sync.py @@ -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) diff --git a/tests/testapp/urls.py b/tests/testapp/urls.py index ba88d50..fe2e038 100644 --- a/tests/testapp/urls.py +++ b/tests/testapp/urls.py @@ -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'), ] diff --git a/tests/testapp/utils.py b/tests/testapp/utils.py index 5652686..7aa32ef 100644 --- a/tests/testapp/utils.py +++ b/tests/testapp/utils.py @@ -1,3 +1,4 @@ +import pandas as pd from datetime import datetime from django_request_cache import cache_for_request @@ -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() + + diff --git a/tests/testapp/views/sync.py b/tests/testapp/views/sync.py index 81532e1..120c937 100644 --- a/tests/testapp/views/sync.py +++ b/tests/testapp/views/sync.py @@ -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()