From 6365704d2dbad51131d7af6d2e72bd3f8c1f24e3 Mon Sep 17 00:00:00 2001 From: Qwann Date: Thu, 13 Oct 2016 11:51:41 +0200 Subject: [PATCH] models & tests de event normalement c'est fini --- event/models.py | 63 +++++++++++++++++--------- event/tests.py | 115 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 138 insertions(+), 40 deletions(-) diff --git a/event/models.py b/event/models.py index ec68e13..a688096 100644 --- a/event/models.py +++ b/event/models.py @@ -42,8 +42,8 @@ class Event(models.Model): ending_date = models.DateTimeField(_('Date de fin')) class Meta: - verbose_name = _("Évènement") - verbose_name_plural = _("Évènements") + verbose_name = _("évènement") + verbose_name_plural = _("évènements") def __str__(self): return self.title @@ -57,8 +57,8 @@ class Place(models.Model): description = models.TextField(blank=True) class Meta: - verbose_name = _("Lieu") - verbose_name_plural = _("Lieux") + verbose_name = _("lieu") + verbose_name_plural = _("lieux") def __str__(self): return self.name @@ -79,8 +79,8 @@ class ActivityTag(models.Model): ) class Meta: - verbose_name = _("Tag") - verbose_name_plural = _("Tags") + verbose_name = _("tag") + verbose_name_plural = _("tags") def __str__(self): return self.name @@ -93,6 +93,13 @@ class ActivityTemplate(models.Model): blank=True, null=True, ) + # FIXME: voir comment on traite l'héritage de `event` + event = models.ForeignKey( + Event, + related_name="activities", + blank=True, + null=True, + ) is_public = models.NullBooleanField( blank=True, ) @@ -125,18 +132,16 @@ class ActivityTemplate(models.Model): ActivityTag, related_name="activities", blank=True, - null=True, ) place = models.ManyToManyField( Place, related_name="activities", blank=True, - null=True, ) class Meta: - verbose_name = _("Template activité") - verbose_name_plural = _("Templates activité") + verbose_name = _("template activité") + verbose_name_plural = _("templates activité") def __str__(self): return self.title @@ -152,22 +157,40 @@ class Activity(ActivityTemplate): related_name="in_perm_activities", blank=True, ) - # TODO : Utiliser db_constraint ou through # equipement = models.ManyToManyField(Equipement) - class Meta: - verbose_name = _("Activité") - verbose_name_plural = _("Activités") - - # Si le champ de l'activité n'est pas spécifié - # alors on va chercher celui de self.parent - def __getattr__(self, attrname): - attr = super(Activity, self).__getattr__(attrname) + def get_herited(self, attrname): + attr = super(Activity, self).__getattribute__(attrname) if (attrname == 'parent' or attrname == 'staff' or attrname == 'equipement'): return attr + elif (attrname == 'place' or attrname == 'tags'): + if attr.exists(): + return attr + else: + return self.parent.__getattribute__(attrname) elif attr is None: - return self.parent.__getattr__(attrname) + return self.parent.__getattribute__(attrname) else: return attr + + class Meta: + verbose_name = _("activité") + verbose_name_plural = _("activités") + + # Si le champ de l'activité n'est pas spécifié + # alors on va chercher celui de self.parent + # def __getattribute__(self, attrname): + # attr = super(Activity, self).__getattribute__(attrname) + # if (attrname == 'parent' + # or attrname == 'staff' + # or attrname == 'equipement'): + # return attr + # elif attr is None: + # return self.parent.__getattribute__(attrname) + # else: + # return attr + + # def __setattr__(self, name, value): + # self.__dict__[name] = value diff --git a/event/tests.py b/event/tests.py index 4aebeaf..4db30c8 100644 --- a/event/tests.py +++ b/event/tests.py @@ -2,9 +2,11 @@ from django.contrib.auth.models import User from django.test import TestCase from datetime import timedelta from django.utils import timezone -from .models import Event, ActivityTemplate, Activity, Place +from .models import Event, ActivityTemplate, Activity, Place, \ + ActivityTag +# TODO: héritage de `event` ? class ActivityInheritanceTest(TestCase): @classmethod def setUpTestData(cls): @@ -26,25 +28,98 @@ class ActivityInheritanceTest(TestCase): + timedelta(days=31), ) cls.loge = Place.objects.create(name="Loge 45") - cls.permtemp = ActivityTemplate.objects.create( - title="Permanence loge", - # is_public non spécifié - has_perm=False, - description="Les perms c'est lol", - # remark non spécifié - ) - cls.permtemp.place.add(cls.loge) - cls.perm = Activity.objects.create( - parent=cls.permtemp, - # title non spécifié + cls.aqua = Place.objects.create(name="Aquarium") + cls.tag_foo = ActivityTag.objects.create( + name="foo", is_public=True, - # has_perm non spécifié - # description non spécifiée - remarks="lol", + color="#0F0F0F", + ) + cls.tag_bar = ActivityTag.objects.create( + name="bar", + is_public=True, + color="#0F0F0F", ) - def test_event_name(self): - self.assertEqual(self.event.title, 'La Nuit 2042') - self.assertEqual(self.loge.name, 'Loge 45') - self.assertEqual(self.permtemp.title, 'Permanence loge') - self.assertEqual(self.permtemp.place.name, 'Loge 45') + def setUp(self): + self.template_act = ActivityTemplate.objects.create( + # title + # is_public + # has_perm + # min_perm + # max_perm + # description + # remarks + # tags + # place + ) + self.real_act = Activity.objects.create( + # les mêmes plus : + parent=self.template_act, + # staff + ) + + def test_inherites_title(self): + self.template_act.title = "parent" + self.assertEqual(self.real_act.get_herited('title'), "parent") + self.real_act.title = "enfant" + self.assertEqual(self.real_act.get_herited('title'), "enfant") + + def test_inherites_description(self): + self.template_act.description = "parent" + self.assertEqual(self.real_act.get_herited('description'), "parent") + self.real_act.description = "enfant" + self.assertEqual(self.real_act.get_herited('description'), "enfant") + + def test_inherites_remarks(self): + self.template_act.remarks = "parent" + self.assertEqual(self.real_act.get_herited('remarks'), "parent") + self.real_act.remarks = "enfant" + self.assertEqual(self.real_act.get_herited('remarks'), "enfant") + + def test_inherites_is_public(self): + self.template_act.is_public = True + self.assertEqual(self.real_act.get_herited('is_public'), True) + self.real_act.is_public = False + self.assertEqual(self.real_act.get_herited('is_public'), False) + + def test_inherites_has_perm(self): + self.template_act.has_perm = True + self.assertEqual(self.real_act.get_herited('has_perm'), True) + self.real_act.has_perm = False + self.assertEqual(self.real_act.get_herited('has_perm'), False) + + def test_inherites_min_perm(self): + self.template_act.min_perm = 42 + self.assertEqual(self.real_act.get_herited('min_perm'), 42) + self.real_act.min_perm = 1 + self.assertEqual(self.real_act.get_herited('min_perm'), 1) + + def test_inherites_max_perm(self): + self.template_act.max_perm = 42 + self.assertEqual(self.real_act.get_herited('max_perm'), 42) + self.real_act.max_perm = 1 + self.assertEqual(self.real_act.get_herited('max_perm'), 1) + + def test_inherites_place(self): + self.template_act.place.add(self.loge) + self.assertEqual( + self.real_act.get_herited('place').get().name, + "Loge 45" + ) + self.real_act.place.add(self.aqua) + self.assertEqual( + self.real_act.get_herited('place').get().name, + "Aquarium" + ) + + def test_inherites_tags(self): + self.template_act.tags.add(self.tag_foo) + self.assertEqual( + self.real_act.get_herited('tags').get().name, + "foo" + ) + self.real_act.tags.add(self.tag_bar) + self.assertEqual( + self.real_act.get_herited('tags').get().name, + "bar" + )