From 1b7bf1d9072242cde4c019ab327f4cfdeb49b60e Mon Sep 17 00:00:00 2001 From: hklarner Date: Mon, 9 Oct 2023 12:12:09 +0200 Subject: [PATCH] tests/contrib: adds tests for caching --- meinberlin/test/factories/extprojects.py | 1 + tests/conftest.py | 5 +++ tests/contrib/__init__.py | 0 tests/contrib/test_caching.py | 50 ++++++++++++++++++++++++ 4 files changed, 56 insertions(+) create mode 100644 tests/contrib/__init__.py create mode 100644 tests/contrib/test_caching.py diff --git a/meinberlin/test/factories/extprojects.py b/meinberlin/test/factories/extprojects.py index bc602bd063..c2956ecf21 100644 --- a/meinberlin/test/factories/extprojects.py +++ b/meinberlin/test/factories/extprojects.py @@ -10,3 +10,4 @@ class Meta: name = factory.Faker("sentence", nb_words=4) organisation = factory.SubFactory(a4_factories.ORGANISATION_FACTORY) + is_draft = False diff --git a/tests/conftest.py b/tests/conftest.py index b58ca94200..89950aa15a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,7 @@ import factory import pytest from celery import Celery +from django.core.cache import cache from django.urls import reverse from pytest_factoryboy import register from rest_framework.test import APIClient @@ -23,6 +24,10 @@ def pytest_configure(config): Celery(task_always_eager=True) +def pytest_sessionfinish(session, exitstatus): + cache.clear() + + register(factories.UserFactory) register(factories.UserFactory, "user2") register(factories.AdminFactory, "admin") diff --git a/tests/contrib/__init__.py b/tests/contrib/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/contrib/test_caching.py b/tests/contrib/test_caching.py new file mode 100644 index 0000000000..5ee8bcbca6 --- /dev/null +++ b/tests/contrib/test_caching.py @@ -0,0 +1,50 @@ +import pytest +from django.core.cache import cache +from django.urls import reverse + +from adhocracy4.projects.enums import Access +from adhocracy4.test.factories import ProjectFactory +from meinberlin.apps.contrib import caching +from meinberlin.test.factories.extprojects import ExternalProjectFactory +from meinberlin.test.factories.plans import PlanFactory +from meinberlin.test.factories.projectcontainers import ProjectContainerFactory + + +def test_cache_delete(): + ... + + +def test_cache_add_or_query(): + ... + + +def test_cache_add_or_serialize(): + ... + + +@pytest.mark.django_db +@pytest.mark.parametrize( + "url_name,factory,factory_kwargs", + [ + ("projects-list", ProjectFactory, {}), + ("plans-list", PlanFactory, {}), + ("extprojects-list", ExternalProjectFactory, {"access": Access.PUBLIC}), + ("containers-list", ProjectContainerFactory, {}), + # ("projects-list", ProjectFactory, {"access": Access.PRIVATE}), + ], +) +def test_project_list_url(client, url_name, factory, factory_kwargs): + n_objects = 3 + url = reverse(url_name) + cache_key = caching.get_key(namespace=url) + cache_value_before = cache.get(cache_key) + + objects = factory.create_batch(size=n_objects, **factory_kwargs) + response = client.get(url) + cache_value_after = cache.get(cache_key) + + assert response.status_code == 200 + assert cache_value_before is None + assert len(cache_value_after) == len(objects) == n_objects + + cache.clear()