from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.test import TestCase from datetime import timedelta from django.utils import timezone from .models import Event, ActivityTemplate, Activity, Place, \ ActivityTag class ActivityInheritanceTest(TestCase): @classmethod def setUpTestData(cls): cls.erkan = User.objects.create( username='enarmanli', email='erkan.narmanli@ens.fr', first_name='Erkan', last_name='Narmanli', ) cls.event = Event.objects.create( title='La Nuit 2042', slug='nuit42', created_by=cls.erkan, creation_date=timezone.now(), description="La nuit c'est lol", beginning_date=timezone.now() + timedelta(days=30), ending_date=timezone.now() + timedelta(days=31), ) cls.loge = Place.objects.create(name="Loge 45") cls.aqua = Place.objects.create(name="Aquarium") cls.tag_foo = ActivityTag.objects.create( name="foo", is_public=True, color="#0F0F0F", ) cls.tag_bar = ActivityTag.objects.create( name="bar", is_public=True, color="#0F0F0F", ) def setUp(self): self.template_act = ActivityTemplate.objects.create() self.real_act = Activity.objects.create( parent=self.template_act, ) 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" ) class ActivityTagColorTest(TestCase): def test_positive_color_long(self): self.tag = ActivityTag.objects.create( name="bar", is_public=True, color="#0F0F0F", ) self.tag.full_clean() def test_positive_color_small(self): self.tag = ActivityTag.objects.create( name="bar", is_public=True, color="#0F0", ) self.tag.full_clean() def test_negative_color_1(self): self.tag = ActivityTag.objects.create( name="bar", is_public=True, color="#ABCDEG", ) with self.assertRaises(ValidationError): self.tag.full_clean() def test_negative_color_2(self): self.tag = ActivityTag.objects.create( name="bar", is_public=True, color="#ACDE-1", ) with self.assertRaises(ValidationError): self.tag.full_clean()