Skip to content

Commit

Permalink
tests/projects: add test for topic_names
Browse files Browse the repository at this point in the history
  • Loading branch information
goapunk committed Dec 12, 2023
1 parent e26c42f commit 22b10df
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
31 changes: 31 additions & 0 deletions adhocracy4/test/factories/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from adhocracy4.phases.models import Phase
from adhocracy4.projects.enums import Access
from adhocracy4.projects.models import Project
from adhocracy4.projects.models import Topic
from tests.project.enums import TopicEnum


class GroupFactory(factory.django.DjangoModelFactory):
Expand Down Expand Up @@ -86,6 +88,35 @@ def moderators(self, create, extracted, **kwargs):
for user in extracted:
self.moderators.add(user)

@factory.post_generation
def topics(self, create, extracted, **kwargs):
if not create:
return
if extracted:
for topic in extracted:
self.topics.add(topic)


class TopicFactory(factory.django.DjangoModelFactory):
"""Create Topics from the TopicEnum class
Note: This factory can only create len(TopicEnum) topics because of the unique
constraint of the Topic model
"""

class Meta:
model = Topic

code = factory.Sequence(lambda n: TopicEnum.names[n])

@factory.post_generation
def projects(self, create, extracted, **kwargs):
if not create:
return
if extracted:
for project in extracted:
self.project_set.add(project)


class ModuleFactory(factory.django.DjangoModelFactory):
class Meta:
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def image_factory():
register(factories.UserFactory, "another_user")
register(factories.UserFactory, "staff_user", is_staff=True)
register(factories.ProjectFactory)
register(factories.TopicFactory)
register(factories.ModuleFactory)
register(factories.PhaseFactory)
register(q_factories.QuestionFactory)
Expand Down
16 changes: 16 additions & 0 deletions tests/projects/test_project_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,19 @@ def test_is_archivable(project_factory, phase_factory):
assert not project1.is_archivable
assert not project2.is_archivable
assert project3.is_archivable


@pytest.mark.django_db
def test_project_topics(project_factory, topic_factory):
project = project_factory()
assert project.topic_names == []
# delete to clear the cache of the cached_property
del project.topic_names
topic1 = topic_factory()
project.topics.add(topic1)
assert project.topic_names == [str(topic1)]
# delete to clear the cache of the cached_property
del project.topic_names
topic2 = topic_factory()
project.topics.add(topic2)
assert project.topic_names == [str(topic1), str(topic2)]

0 comments on commit 22b10df

Please sign in to comment.