Skip to content

Commit

Permalink
tests/contrib: adds tests for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
hklarner committed Oct 9, 2023
1 parent fb84534 commit ffea8ce
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions meinberlin/test/factories/extprojects.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ class Meta:

name = factory.Faker("sentence", nb_words=4)
organisation = factory.SubFactory(a4_factories.ORGANISATION_FACTORY)
is_draft = False
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")
Expand Down
Empty file added tests/contrib/__init__.py
Empty file.
50 changes: 50 additions & 0 deletions tests/contrib/test_caching.py
Original file line number Diff line number Diff line change
@@ -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_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()

0 comments on commit ffea8ce

Please sign in to comment.