Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
XanderVertegaal committed Mar 27, 2024
1 parent 37cf8ed commit 4267d53
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 20 deletions.
59 changes: 53 additions & 6 deletions backend/conftest.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
import pytest
from source.models import Source
from case_study.models import CaseStudy
from letter.models import LetterBase
from letter.models import Letter, LetterDescription
from event.models import (
EpistolaryEvent,
LetterActionBase,
LetterAction,
LetterActionCategory,
LetterActionDescription,
WorldEvent,
LetterEventDate,
)
from person.models import Agent
from person.models import Agent, AgentDescription


@pytest.fixture()
def letter(db):
letter = LetterBase.objects.create()
letter = Letter.objects.create()
letter.name = "letter for testing"
letter.save()
return letter


@pytest.fixture()
def letter_description(db, source):
letter_description = LetterDescription.objects.create(
source=source, location="Hoofdstuk 3"
)
letter_description.name = "description of letter for testing"
letter_description.save()
return letter_description


@pytest.fixture()
def agent(db):
agent = Agent.objects.create()
Expand All @@ -27,6 +39,14 @@ def agent(db):
return agent


@pytest.fixture()
def agent_description(db, source):
agent_description = AgentDescription.objects.create(source=source)
agent_description.name = "Bertus"
agent_description.save()
return agent_description


@pytest.fixture()
def agent_2(db):
agent = Agent.objects.create()
Expand All @@ -44,9 +64,28 @@ def agent_group(db):
return agent_group


@pytest.fixture()
def letter_action(db, letter, agent):
letter_action = LetterAction.objects.create()
letter_action.letters.add(letter)
letter_action.actors.add(agent)
return letter_action


@pytest.fixture()
def letter_action_description(db, letter_description, agent_description, source):
letter_action_description = LetterActionDescription.objects.create(
source=source, location="Hoofdstuk 2"
)
letter_action_description.letters.add(letter_description)
letter_action_description.actors.add(agent_description)

return letter_action_description


@pytest.fixture()
def letter_action_writing(db, letter, agent):
letter_action = LetterActionBase.objects.create()
letter_action = LetterAction.objects.create()
letter_action.letters.add(letter)
letter_action.actors.add(agent)

Expand All @@ -64,7 +103,7 @@ def letter_action_writing(db, letter, agent):

@pytest.fixture()
def letter_action_reading(db, letter, agent_2):
letter_action = LetterActionBase.objects.create()
letter_action = LetterAction.objects.create()
letter_action.letters.add(letter)
letter_action.actors.add(agent_2)

Expand Down Expand Up @@ -102,3 +141,11 @@ def world_event(db):
name="Test World Event", note="Test World Event note", year_exact=612
)
return world_event


@pytest.fixture()
def source(db):
source = Source.objects.create(
name="De Fabeltjeskrant", bibliographical_info="first edition"
)
return source
14 changes: 8 additions & 6 deletions backend/event/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from core.models import Field, Historical, LettercraftDate, SourceDescription
from case_study.models import CaseStudy
from person.models import Agent
from person.models import AgentBase
from letter.models import GiftBase, LetterBase
from space.models import SpaceDescriptionBase

Expand Down Expand Up @@ -67,7 +67,7 @@ class LetterActionBase(models.Model):
)

actors = models.ManyToManyField(
to=Agent,
to=AgentBase,
through="Role",
related_name="events",
)
Expand Down Expand Up @@ -102,13 +102,12 @@ def description(self):
categories = self.categories.all()
category_names = [category.get_value_display() for category in categories]
letters = ", ".join(letter.__str__() for letter in self.letters.all())
if len(category_names) > 1:
if len(category_names) > 0:
category_desc = ", ".join(category_names)
return f"{category_desc} of {letters}"
else:
return f"unknown action involving {letters}"


def __str__(self):
return f"{self.description} ({self.display_date})"

Expand Down Expand Up @@ -159,10 +158,12 @@ class LetterEventDate(Field, LettercraftDate, models.Model):
def __str__(self):
return f"{self.letter_action} ({self.display_date})"


class LetterAction(Historical, LetterActionBase, models.Model):
"""
An aggregate model that represents a letter action in history. This model is based on one or multiple SourceDescriptions and other sources that are not part of this database.
"""

pass


Expand All @@ -180,7 +181,8 @@ class LetterActionDescription(SourceDescription, LetterActionBase, models.Model)
)

def __str__(self):
return f"Description of {super().__str__()} in {self.source}"
return f"{super().__str__()} (described in {self.source})"


class Role(Field, models.Model):
"""
Expand All @@ -201,7 +203,7 @@ class RoleOptions(models.TextChoices):
OTHER = "other", "Other"

agent = models.ForeignKey(
to=Agent,
to=AgentBase,
on_delete=models.CASCADE,
null=False,
)
Expand Down
19 changes: 16 additions & 3 deletions backend/event/tests/test_event_models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
from event.models import (
EpistolaryEvent,
WorldEvent,
WorldEventSelfTrigger,
WorldEventTrigger,
)


def test_letter_action_name(letter, letter_action_writing):
def test_letter_action_name(letter, letter_action):
action_str = str(letter_action)
assert (
str(action_str) == f"unknown action involving letter for testing (unknown date)"
)


def test_letter_action_description_name(letter_description, letter_action_description):
action_str = str(letter_action_description)
assert (
str(action_str)
== f"unknown action involving description of letter for testing (unknown date) (described in De Fabeltjeskrant)"
)


def test_letter_action_name_with_action(letter, letter_action_writing):
letter_action_writing.date.year_exact = 500
letter_action_writing.save()
action_str = str(letter_action_writing)
Expand Down
8 changes: 5 additions & 3 deletions backend/letter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ def __str__(self):
"""
Inherits the __str__ method from the base model and adds the source to it.
"""
return f"{super().__str__()} (description in {self.source})"
return f"{super().__str__()} (described in {self.source})"


class Gift(Historical, GiftBase, models.Model):
"""
An aggregate model that represents a gift in history. This model is based on one or multiple SourceDescriptions and other sources that are not part of this database.
"""

pass


Expand Down Expand Up @@ -200,10 +201,11 @@ class Letter(Historical, LetterBase, models.Model):
"""
An aggregate model that represents a letter in history. This model is based on one or multiple SourceDescriptions and other sources that are not part of this database.
"""

pass

def __str__(self) -> str:
return f"{super().__str__()} (historical)"
return f"{super().__str__()}"


class LetterDescription(SourceDescription, LetterBase, models.Model):
Expand All @@ -223,4 +225,4 @@ def __str__(self):
"""
Inherits the __str__ method from the base model and adds the source to it.
"""
return f"{super().__str__()} (description in {self.source})"
return f"{super().__str__()} (described in {self.source})"
3 changes: 2 additions & 1 deletion backend/person/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class Agent(Historical, AgentBase):
"""
An aggregate model that represents an agent (person or group) in history. This model is based on one or multiple SourceDescriptions and other sources that are not part of this database.
"""

pass


Expand All @@ -178,4 +179,4 @@ def __str__(self):
"""
Inherits the __str__ method from the base model and adds the source to it.
"""
return f"{super().__str__()} (description in {self.source})"
return f"{super().__str__()} (described in {self.source})"
2 changes: 1 addition & 1 deletion backend/space/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,4 @@ def __str__(self):
"""
Inherits the __str__ method from the base model and adds the source to it.
"""
return f"{super().__str__()} (description in {self.source})"
return f"{super().__str__()} (described in {self.source})"

0 comments on commit 4267d53

Please sign in to comment.