diff options
author | Matthew Lemon <lemon@matthewlemon.com> | 2020-10-10 16:28:23 +0100 |
---|---|---|
committer | Matthew Lemon <lemon@matthewlemon.com> | 2020-10-10 16:28:23 +0100 |
commit | 41f7fa0164523b574da0ee2c0dc2dc0e6f1a614e (patch) | |
tree | 143c6b9142b272a71259d850b6e8ee3576120ae7 /ctrack/register | |
parent | b77af5bb3ecd3aef29223d5c3645295144e790fd (diff) |
slogging through testing these event models
Diffstat (limited to 'ctrack/register')
-rw-r--r-- | ctrack/register/models.py | 78 | ||||
-rw-r--r-- | ctrack/register/tests/test_engagement_events.py | 88 |
2 files changed, 85 insertions, 81 deletions
diff --git a/ctrack/register/models.py b/ctrack/register/models.py index 6cfe8d8..c94e440 100644 --- a/ctrack/register/models.py +++ b/ctrack/register/models.py @@ -1,3 +1,4 @@ +import datetime from datetime import date as std_date from typing import Optional, Dict @@ -5,6 +6,7 @@ from django.contrib.auth import get_user_model from django.db import models from ctrack.organisations.models import Person +from ctrack.users.models import User def _style_descriptor(days: int) -> str: @@ -23,6 +25,73 @@ def _day_string(days: int) -> str: return "day" +class AuditableEventBase(models.Model): + user = models.OneToOneField(User, on_delete=models.CASCADE) + created_date = models.DateTimeField() + modified_date = models.DateTimeField() + + class Meta: + abstract = True + + def save(self, *args, **kwargs): + if not self.pk: + self.created_date = datetime.datetime.now() + self.modified_date = datetime.datetime.now() + + return super().save(*args, **kwargs) + + +class EngagementEventBase(AuditableEventBase): + type_descriptor = "Base Type" + short_description = models.CharField( + max_length=50, + help_text="Short description of the event. Use Comments field for full detail.", + ) + participants = models.ManyToManyField(Person, null=True, blank=True) + document_link = models.URLField( + max_length=1000, + blank=True, + null=True, + help_text="URL only - do not try to drag a file here.", + ) + response_date_requested = models.DateField(blank=True, null=True) + comments = models.TextField(max_length=1000, blank=True, null=True, + help_text="Use this to provide further detail about the event.") + + class Meta: + abstract = True + + +class MeetingEventMixin(models.Model): + participants = models.ManyToManyField(Person, blank=False, null=False) + location = models.CharField(max_length=100, blank=True, null=True) + + class Meta: + abstract = True + + +class SingleDateTimeEventMixin(models.Model): + datetime = models.DateTimeField() + + class Meta: + abstract = True + + +# class SingleDateCAFEvent(EngagementEventBase): +# type = models.ForeignKey( +# EngagementType, default=event_type, on_delete=models.CASCADE +# ) +# caf_related = models.BooleanField(default=True) +# date = models.DateField(blank=False, null=False) + + +class MeetingEvent(EngagementEventBase, MeetingEventMixin, SingleDateTimeEventMixin): + MEETING_TYPES = [ + ("Meeting", "Meeting") + ] + type_descriptor = models.CharField(max_length=50, choices=MEETING_TYPES) + + class EngagementType(models.Model): """ Examples here are Phone, Email, Letter, Site visit, Meeting, Audit, Inspection, etc. @@ -60,13 +129,16 @@ class EngagementEvent(models.Model): user = models.ForeignKey(get_user_model(), on_delete=models.SET(get_sentinel_user)) date = models.DateTimeField() end_date = models.DateTimeField(blank=True, null=True, help_text="Should be used for periodic events.") - document_link = models.URLField(max_length=1000, blank=True, null=True, help_text="URL only - do not try to drag a file here.") + document_link = models.URLField(max_length=1000, blank=True, null=True, + help_text="URL only - do not try to drag a file here.") response_date_requested = models.DateField(blank=True, null=True) response_received = models.DateField(blank=True, null=True) related_caf = models.ForeignKey( - "caf.CAF", blank=True, on_delete=models.CASCADE, null=True, help_text="If the event relates to a CAF, refer to it here." + "caf.CAF", blank=True, on_delete=models.CASCADE, null=True, + help_text="If the event relates to a CAF, refer to it here." ) - comments = models.TextField(max_length=1000, blank=True, null=True, help_text="Use this to provide further detail about the event.") + comments = models.TextField(max_length=1000, blank=True, null=True, + help_text="Use this to provide further detail about the event.") def days_to_response_due(self) -> Optional[Dict[int, str]]: if self.response_date_requested: diff --git a/ctrack/register/tests/test_engagement_events.py b/ctrack/register/tests/test_engagement_events.py index 5e9db31..3cbeafe 100644 --- a/ctrack/register/tests/test_engagement_events.py +++ b/ctrack/register/tests/test_engagement_events.py @@ -1,91 +1,23 @@ import pytest -from django.db import models +from django.contrib.auth import get_user_model -from ctrack.organisations.models import Person -from ctrack.register.models import EngagementType -from ctrack.register.tests.factories import EngagementTypeFactory +from ctrack.register.models import MeetingEvent pytestmark = pytest.mark.django_db -def event_type(): - return EngagementTypeFactory.create(descriptor="CAF type") - - -type = models.ForeignKey(EngagementType, on_delete=models.CASCADE) - - -class EngagementEventBase(models.Model): - type_descriptor = "Base Type" - short_description = models.CharField( - max_length=50, - help_text="Short description of the event. Use Comments field for full detail.", - ) - participants = models.ManyToManyField(Person, null=True, blank=True) - document_link = models.URLField( - max_length=1000, - blank=True, - null=True, - help_text="URL only - do not try to drag a file here.", - ) - response_date_requested = models.DateField(blank=True, null=True) - comments = models.TextField(max_length=1000, blank=True, null=True, - help_text="Use this to provide further detail about the event.") - - class Meta: - abstract = True - - -class SingleDateCAFEvent(EngagementEventBase): - type = models.ForeignKey( - EngagementType, default=event_type, on_delete=models.CASCADE - ) - caf_related = models.BooleanField(default=True) - date = models.DateField(blank=False, null=False) - - -def test_event_inheritance(): - caf_single_date_event = SingleDateCAFEvent.objects.create( - short_description="Test Short Description", date="2010-10-10" - ) - assert isinstance(caf_single_date_event.type, EngagementType) - assert caf_single_date_event.short_description == "Test Short Description" - assert caf_single_date_event.caf_related is True - assert len(caf_single_date_event.participants.all()) == 0 - assert caf_single_date_event.date == "2010-10-10" - assert caf_single_date_event.document_link is None - assert caf_single_date_event.response_date_requested is None - - -class MeetingEventMixin(models.Model): - participants = models.ManyToManyField(Person, blank=True) - location = models.CharField(max_length=100, blank=True, null=True) - - class Meta: - abstract = True - - -class SingleDateTimeEventMixin(models.Model): - datetime = models.DateTimeField() - - class Meta: - abstract = True - - -class MeetingEvent(EngagementEventBase, MeetingEventMixin, SingleDateTimeEventMixin): - MEETING_CHOICES = [ - ("Meeting", "Meeting") - ] - type_descriptor = models.CharField(max_length=50, choices=MEETING_CHOICES) - - -def test_meeting_event(org_with_people, person): +def test_meeting_event(person, user): + uname = user.name e = MeetingEvent.objects.create( type_descriptor="Meeting", short_description="Big Important Meeting", datetime="2020-10-10T15:00", comments="Nice comments", - location="Harvey's House" + location="Harvey's House", + user = user ) - assert len(e.participants.all()) == 0 + e.participants.add(person) + assert len(e.participants.all()) == 1 assert e.type_descriptor == "Meeting" + assert person in e.participants.all() + assert e.user.name == uname |