-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apps/cms: add tests for storefront models and management command - fixes
- Loading branch information
1 parent
2151bd6
commit 08fe0a0
Showing
5 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import factory | ||
|
||
from adhocracy4.test.factories import AdministrativeDistrictFactory | ||
from adhocracy4.test.factories import ProjectFactory | ||
from meinberlin.apps.cms.models import storefronts | ||
|
||
|
||
class StorefrontFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = storefronts.Storefront | ||
|
||
|
||
class StorefrontItemFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = storefronts.StorefrontItem | ||
|
||
district = factory.SubFactory(AdministrativeDistrictFactory) | ||
project = factory.SubFactory(ProjectFactory) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from pytest_factoryboy import register | ||
|
||
from adhocracy4.test.factories import ProjectFactory | ||
from meinberlin.test.factories import cms as cms_factories | ||
from meinberlin.test.factories import extprojects as extproject_factories | ||
from meinberlin.test.factories import ideas as ideas_factories | ||
from meinberlin.test.factories import plans as plan_factories | ||
|
||
register(ProjectFactory) | ||
register(cms_factories.StorefrontFactory) | ||
register(cms_factories.StorefrontItemFactory) | ||
register(extproject_factories.ExternalProjectFactory) | ||
register(ideas_factories.IdeaFactory) | ||
register(plan_factories.PlanFactory) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import pytest | ||
from dateutil.parser import parse | ||
from django.core.management import call_command | ||
|
||
from adhocracy4.projects.enums import Access | ||
from adhocracy4.test.helpers import freeze_phase | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_update_num_entries(storefront_factory, idea_factory, comment_factory): | ||
storefront = storefront_factory(num_entries=2) | ||
assert storefront.num_entries == 2 | ||
|
||
idea = idea_factory() | ||
idea_factory() | ||
idea_factory() | ||
comment_factory(content_object=idea) | ||
comment_factory(content_object=idea) | ||
comment_factory(content_object=idea) | ||
comment_factory(content_object=idea) | ||
|
||
call_command("update_storefront_counts") | ||
storefront.refresh_from_db() | ||
assert storefront.num_entries == 7 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_update_num_projects(storefront_factory, phase_factory, plan_factory): | ||
storefront = storefront_factory(num_projects=1) | ||
assert storefront.num_projects == 1 | ||
|
||
phase_factory( | ||
start_date=parse("2022-01-01 17:00:00 UTC"), | ||
end_date=parse("2022-01-31 18:00:00 UTC"), | ||
) | ||
phase_23 = phase_factory( | ||
start_date=parse("2023-01-01 17:00:00 UTC"), | ||
end_date=parse("2023-01-31 18:00:00 UTC"), | ||
) | ||
phase_24 = phase_factory( | ||
start_date=parse("2024-01-01 17:00:00 UTC"), | ||
end_date=parse("2024-01-31 18:00:00 UTC"), | ||
) | ||
# draft project (not counted) | ||
phase_24_draft = phase_factory( | ||
start_date=parse("2024-01-01 17:00:00 UTC"), | ||
end_date=parse("2024-01-31 18:00:00 UTC"), | ||
) | ||
project_draft = phase_24_draft.module.project | ||
project_draft.is_draft = True | ||
project_draft.save() | ||
# semipublic project (counted) | ||
phase_24_semi = phase_factory( | ||
start_date=parse("2024-01-01 17:00:00 UTC"), | ||
end_date=parse("2024-01-31 18:00:00 UTC"), | ||
) | ||
project_semi = phase_24_semi.module.project | ||
project_semi.access = Access.SEMIPUBLIC | ||
project_semi.save() | ||
phase_24_private = phase_factory( | ||
start_date=parse("2024-01-01 17:00:00 UTC"), | ||
end_date=parse("2024-01-31 18:00:00 UTC"), | ||
) | ||
# private project (not counted) | ||
project_private = phase_24_private.module.project | ||
project_private.access = Access.PRIVATE | ||
project_private.save() | ||
# plans not counted | ||
plan_factory(status=0) | ||
|
||
with freeze_phase(phase_23): | ||
call_command("update_storefront_counts") | ||
storefront.refresh_from_db() | ||
assert storefront.num_projects == 3 | ||
|
||
with freeze_phase(phase_24): | ||
call_command("update_storefront_counts") | ||
storefront.refresh_from_db() | ||
assert storefront.num_projects == 2 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_update_district_project_count( | ||
storefront_item_factory, | ||
phase_factory, | ||
plan_factory, | ||
administrative_district_factory, | ||
): | ||
administrative_district = administrative_district_factory() | ||
storefront_item = storefront_item_factory( | ||
district=administrative_district, district_project_count=1 | ||
) | ||
assert storefront_item.district_project_count == 1 | ||
|
||
# project from 22 (not counted as past) | ||
phase_22 = phase_factory( | ||
start_date=parse("2022-01-01 17:00:00 UTC"), | ||
end_date=parse("2022-01-31 18:00:00 UTC"), | ||
) | ||
project_22 = phase_22.module.project | ||
project_22.administrative_district = administrative_district | ||
project_22.save() | ||
# project from 23 (only counted when active) | ||
phase_23 = phase_factory( | ||
start_date=parse("2023-01-01 17:00:00 UTC"), | ||
end_date=parse("2023-01-31 18:00:00 UTC"), | ||
) | ||
project_23 = phase_23.module.project | ||
project_23.administrative_district = administrative_district | ||
project_23.save() | ||
# project from 24 (counted as future or active) | ||
phase_24 = phase_factory( | ||
start_date=parse("2024-01-01 17:00:00 UTC"), | ||
end_date=parse("2024-01-31 18:00:00 UTC"), | ||
) | ||
project_24 = phase_24.module.project | ||
project_24.administrative_district = administrative_district | ||
project_24.save() | ||
# project in future from different district (not counted) | ||
phase_24_diff_district = phase_factory( | ||
start_date=parse("2024-01-01 17:00:00 UTC"), | ||
end_date=parse("2024-01-31 18:00:00 UTC"), | ||
) | ||
project_diff_district = phase_24_diff_district.module.project | ||
different_administrative_district = administrative_district_factory() | ||
project_diff_district.administrative_district = different_administrative_district | ||
project_diff_district.save() | ||
# draft project (not counted) | ||
phase_24_draft = phase_factory( | ||
start_date=parse("2024-01-01 17:00:00 UTC"), | ||
end_date=parse("2024-01-31 18:00:00 UTC"), | ||
) | ||
project_draft = phase_24_draft.module.project | ||
project_draft.is_draft = True | ||
project_draft.administrative_district = administrative_district | ||
project_draft.save() | ||
# semipublic project (counted) | ||
phase_24_semi = phase_factory( | ||
start_date=parse("2024-01-01 17:00:00 UTC"), | ||
end_date=parse("2024-01-31 18:00:00 UTC"), | ||
) | ||
project_semi = phase_24_semi.module.project | ||
project_semi.access = Access.SEMIPUBLIC | ||
project_semi.administrative_district = administrative_district | ||
project_semi.save() | ||
# private project (not counted) | ||
phase_24_private = phase_factory( | ||
start_date=parse("2024-01-01 17:00:00 UTC"), | ||
end_date=parse("2024-01-31 18:00:00 UTC"), | ||
) | ||
project_private = phase_24_private.module.project | ||
project_private.access = Access.PRIVATE | ||
project_private.administrative_district = administrative_district | ||
project_private.save() | ||
|
||
# only running plans from same district counted | ||
plan_factory(status=0, district=administrative_district) | ||
plan_factory(status=1, district=administrative_district) | ||
plan_factory(status=0) | ||
|
||
with freeze_phase(phase_23): | ||
call_command("update_storefront_counts") | ||
storefront_item.refresh_from_db() | ||
assert storefront_item.district_project_count == 4 | ||
|
||
with freeze_phase(phase_24): | ||
call_command("update_storefront_counts") | ||
storefront_item.refresh_from_db() | ||
assert storefront_item.district_project_count == 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import pytest | ||
|
||
from adhocracy4.test.helpers import freeze_phase | ||
from adhocracy4.test.helpers import setup_phase | ||
from meinberlin.apps.ideas import phases | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_storefront_str(storefront): | ||
assert str(storefront) == storefront.title | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_storefront_num_entries(storefront_factory, idea, comment_factory): | ||
comment_factory(content_object=idea) | ||
comment_factory(content_object=idea) | ||
storefront = storefront_factory() | ||
assert storefront.num_entries == 3 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_storefront_num_projects( | ||
storefront_factory, project_factory, plan_factory, phase_factory | ||
): | ||
phase, _, _, _ = setup_phase(phase_factory, None, phases.CollectPhase) | ||
project_factory() | ||
plan_factory(status=0) | ||
with freeze_phase(phase): | ||
storefront = storefront_factory() | ||
assert storefront.num_projects == 1 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_storefront_item_str(storefront_item): | ||
assert str(storefront_item) == str(storefront_item.pk) | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_storefront_item_district_project_count( | ||
storefront_item_factory, | ||
project_factory, | ||
plan_factory, | ||
phase_factory, | ||
administrative_district, | ||
): | ||
phase, _, project, _ = setup_phase(phase_factory, None, phases.CollectPhase) | ||
project.administrative_district = administrative_district | ||
project.save() | ||
project_factory(administrative_district=administrative_district) | ||
plan_factory(status=0, district=administrative_district) | ||
with freeze_phase(phase): | ||
storefront_item = storefront_item_factory(district=administrative_district) | ||
assert storefront_item.district_project_count == 2 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_storefront_item_item_type(storefront_item_factory, project, external_project): | ||
project_storefront_item = storefront_item_factory(project=project) | ||
external_project_storefront_item = storefront_item_factory(project=external_project) | ||
assert project_storefront_item.item_type == "project" | ||
assert external_project_storefront_item.item_type == "external" | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_storefront_item_project_url( | ||
storefront_item_factory, project, external_project | ||
): | ||
project_storefront_item = storefront_item_factory(project=project) | ||
external_project_storefront_item = storefront_item_factory(project=external_project) | ||
assert project_storefront_item.project_url == project.get_absolute_url() | ||
assert ( | ||
external_project_storefront_item.project_url | ||
== external_project.externalproject.url | ||
) |