Skip to content

Commit

Permalink
tests: add test for module publish signal to reset cache
Browse files Browse the repository at this point in the history
  • Loading branch information
goapunk authored and m4ra committed Aug 20, 2024
1 parent f0c2136 commit 96245e7
Showing 1 changed file with 124 additions and 0 deletions.
124 changes: 124 additions & 0 deletions tests/projects/test_tasks_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import pytest
from django.core.cache import cache
from django.db.models import signals
from django.urls import reverse
from factory.django import mute_signals
from freezegun import freeze_time

from adhocracy4.projects.enums import Access
from adhocracy4.test.helpers import redirect_target
from meinberlin.apps.projects.tasks import get_next_projects_end
from meinberlin.apps.projects.tasks import get_next_projects_start
from meinberlin.apps.projects.tasks import schedule_reset_cache_for_projects
Expand Down Expand Up @@ -412,3 +414,125 @@ def test_new_phase_resets_cache(
assert len(active_projects) == 3
assert len(past_projects) == 0
assert len(future_projects) == 0


@pytest.mark.django_db
def test_module_publish_resets_cache(
client,
user,
phase_factory,
project_factory,
django_assert_num_queries,
django_capture_on_commit_callbacks,
):
n_objects = 3
objects = project_factory.create_batch(size=n_objects)

# make dates to work with phase_factory
now = datetime.now(tz=timezone.utc)
last_week = now - timedelta(days=7)
next_week = now + timedelta(days=7)

# make active projects
active_project = objects[0]
phase_factory(
start_date=last_week,
end_date=next_week,
module__project=active_project,
)

past_project = objects[1]
phase_factory(
start_date=last_week,
end_date=last_week + timedelta(days=6),
module__project=past_project,
)
# make future project
future_project = objects[2]
phase_factory(
start_date=next_week,
end_date=next_week + timedelta(days=7),
module__project=future_project,
)

# check function set_cache_for_projects
set_cache_for_projects()

# check projects got cached correctly
with freeze_time(now):
with django_assert_num_queries(0):
active_projects = cache.get("projects_activeParticipation")
past_projects = cache.get("projects_pastParticipation")
future_projects = cache.get("projects_futureParticipation")
assert len(active_projects) == 1
assert len(past_projects) == 1
assert len(future_projects) == 1

# turn future project into active
phase_active = phase_factory(
start_date=last_week,
end_date=next_week,
module__project=future_project,
)
# turn past project into future project
phase_future = phase_factory(
start_date=next_week,
end_date=next_week + timedelta(days=7),
module__project=past_project,
)

module1 = phase_active.module
module2 = phase_future.module
module1.is_draft = True
module2.is_draft = True

with django_capture_on_commit_callbacks(execute=True):
module1.save()
module2.save()

# check cache didn't change (as new modules are not published)
with freeze_time(now):
with django_assert_num_queries(0):
active_projects = cache.get("projects_activeParticipation")
past_projects = cache.get("projects_pastParticipation")
future_projects = cache.get("projects_futureParticipation")
assert len(active_projects) == 1
assert len(past_projects) == 1
assert len(future_projects) == 1

module1_publish_url = reverse(
"a4dashboard:module-publish", kwargs={"module_slug": module1.slug}
)
module2_publish_url = reverse(
"a4dashboard:module-publish", kwargs={"module_slug": module2.slug}
)

data = {"action": "publish"}
organisation = module1.project.organisation
organisation.initiators.add(user)
organisation = module2.project.organisation
organisation.initiators.add(user)

# publish new modules
with django_capture_on_commit_callbacks(execute=True):
client.login(username=user, password="password")
response = client.post(module1_publish_url, data)
assert redirect_target(response) == "project-edit"
client.login(username=user, password="password")
response = client.post(module2_publish_url, data)
assert redirect_target(response) == "project-edit"

module1.refresh_from_db()
assert module1.is_draft is False
module2.refresh_from_db()
assert module2.is_draft is False

# check cache was updated correctly
with freeze_time(now):
with django_assert_num_queries(0):
active_projects = cache.get("projects_activeParticipation")
past_projects = cache.get("projects_pastParticipation")
future_projects = cache.get("projects_futureParticipation")
assert len(active_projects) == 2
assert len(past_projects) == 0
assert len(future_projects) == 1

0 comments on commit 96245e7

Please sign in to comment.