From a5978b56e53d8c4a7ec93b0043bc12ea69bdba67 Mon Sep 17 00:00:00 2001 From: Irwan Fathurrahman Date: Thu, 24 Oct 2024 15:30:13 +0700 Subject: [PATCH] Add message functions and tests --- django_project/core/settings/project.py | 2 +- django_project/message/factories.py | 27 +++ .../message/fixtures/1.messages.json | 50 ++--- .../message/migrations/0001_initial.py | 4 +- django_project/message/models.py | 59 ++++- django_project/message/tests/__init__.py | 0 django_project/message/tests/test_message.py | 59 +++++ django_project/message/tests/test_models.py | 46 ++++ django_project/prise/factories.py | 20 ++ ...{1.messages.json => 1.prise_messages.json} | 0 .../prise/migrations/0001_initial.py | 8 +- django_project/prise/models.py | 41 +++- django_project/prise/tests/__init__.py | 0 django_project/prise/tests/test_message.py | 208 ++++++++++++++++++ django_project/prise/tests/test_models.py | 53 +++++ django_project/prise/variables.py | 2 +- 16 files changed, 542 insertions(+), 37 deletions(-) create mode 100644 django_project/message/factories.py create mode 100644 django_project/message/tests/__init__.py create mode 100644 django_project/message/tests/test_message.py create mode 100644 django_project/message/tests/test_models.py create mode 100644 django_project/prise/factories.py rename django_project/prise/fixtures/{1.messages.json => 1.prise_messages.json} (100%) create mode 100644 django_project/prise/tests/__init__.py create mode 100644 django_project/prise/tests/test_message.py create mode 100644 django_project/prise/tests/test_models.py diff --git a/django_project/core/settings/project.py b/django_project/core/settings/project.py index b50ff905..465d2934 100644 --- a/django_project/core/settings/project.py +++ b/django_project/core/settings/project.py @@ -35,8 +35,8 @@ 'gap', 'gap_api', 'spw', - 'prise', 'message', + 'prise', ) INSTALLED_APPS = INSTALLED_APPS + PROJECT_APPS diff --git a/django_project/message/factories.py b/django_project/message/factories.py new file mode 100644 index 00000000..21f54484 --- /dev/null +++ b/django_project/message/factories.py @@ -0,0 +1,27 @@ +# coding=utf-8 +""" +Tomorrow Now GAP. + +.. note:: Factory classes for Message +""" +import factory +from factory.django import DjangoModelFactory + +from message.models import MessageTemplate +from prise.variables import PriceMessageGroup + + +class MessageTemplateFactory(DjangoModelFactory): + """Factory class for MessageTemplate model.""" + + class Meta: # noqa + model = MessageTemplate + + code = factory.Sequence( + lambda n: f'code-{n}' + ) + name = factory.Sequence( + lambda n: f'name-{n}' + ) + template = factory.Faker('text') + group = PriceMessageGroup.START_SEASON diff --git a/django_project/message/fixtures/1.messages.json b/django_project/message/fixtures/1.messages.json index cbe457de..dfc429a9 100755 --- a/django_project/message/fixtures/1.messages.json +++ b/django_project/message/fixtures/1.messages.json @@ -3,7 +3,7 @@ "model": "message.messagetemplate", "pk": 1, "fields": { - "code": "beanfly_1", + "code": "prise_beanfly_1", "name": "feedback", "type": "Feedback", "application": "PRISE", @@ -18,7 +18,7 @@ "model": "message.messagetemplate", "pk": 2, "fields": { - "code": "beanfly_2", + "code": "prise_beanfly_2", "name": "identify", "type": "Introduction /Advice /Action", "application": "PRISE", @@ -33,7 +33,7 @@ "model": "message.messagetemplate", "pk": 3, "fields": { - "code": "beanfly_3", + "code": "prise_beanfly_3", "name": "intro2", "type": "Warning /Description", "application": "PRISE", @@ -48,7 +48,7 @@ "model": "message.messagetemplate", "pk": 4, "fields": { - "code": "beanfly_4", + "code": "prise_beanfly_4", "name": "intro3", "type": "PRISE overview", "application": "PRISE", @@ -63,7 +63,7 @@ "model": "message.messagetemplate", "pk": 5, "fields": { - "code": "beanfly_5", + "code": "prise_beanfly_5", "name": "feedback", "type": "Feedback", "application": "PRISE", @@ -78,7 +78,7 @@ "model": "message.messagetemplate", "pk": 6, "fields": { - "code": "beanfly_6", + "code": "prise_beanfly_6", "name": "monitor", "type": "Introduction", "application": "PRISE", @@ -93,7 +93,7 @@ "model": "message.messagetemplate", "pk": 7, "fields": { - "code": "beanfly_7", + "code": "prise_beanfly_7", "name": "control 1", "type": "Context", "application": "PRISE", @@ -108,7 +108,7 @@ "model": "message.messagetemplate", "pk": 8, "fields": { - "code": "beanfly_8", + "code": "prise_beanfly_8", "name": "bestdate", "type": "Warning /Description", "application": "PRISE", @@ -123,7 +123,7 @@ "model": "message.messagetemplate", "pk": 9, "fields": { - "code": "beanfly_9", + "code": "prise_beanfly_9", "name": "bestdate", "type": "Warning /Description", "application": "PRISE", @@ -138,7 +138,7 @@ "model": "message.messagetemplate", "pk": 10, "fields": { - "code": "beanfly_10", + "code": "prise_beanfly_10", "name": "feedback", "type": "Warning /Description /Feedback", "application": "PRISE", @@ -153,7 +153,7 @@ "model": "message.messagetemplate", "pk": 11, "fields": { - "code": "beanfly_11", + "code": "prise_beanfly_11", "name": "advice1", "type": "Warning /Description /Action /Advice1", "application": "PRISE", @@ -168,7 +168,7 @@ "model": "message.messagetemplate", "pk": 12, "fields": { - "code": "beanfly_12", + "code": "prise_beanfly_12", "name": "bestdate", "type": "Warning /Description", "application": "PRISE", @@ -183,7 +183,7 @@ "model": "message.messagetemplate", "pk": 13, "fields": { - "code": "fall_armyworm_1", + "code": "prise_fall_armyworm_1", "name": "feedback", "type": "Feedback", "application": "PRISE", @@ -198,7 +198,7 @@ "model": "message.messagetemplate", "pk": 14, "fields": { - "code": "fall_armyworm_2", + "code": "prise_fall_armyworm_2", "name": "intro1", "type": "Introduction /Advice /Action", "application": "PRISE", @@ -213,7 +213,7 @@ "model": "message.messagetemplate", "pk": 15, "fields": { - "code": "fall_armyworm_3", + "code": "prise_fall_armyworm_3", "name": "intro2", "type": "Warning /Description", "application": "PRISE", @@ -228,7 +228,7 @@ "model": "message.messagetemplate", "pk": 16, "fields": { - "code": "fall_armyworm_4", + "code": "prise_fall_armyworm_4", "name": "feedback", "type": "Feedback", "application": "PRISE", @@ -243,7 +243,7 @@ "model": "message.messagetemplate", "pk": 17, "fields": { - "code": "fall_armyworm_5", + "code": "prise_fall_armyworm_5", "name": "spray1", "type": "Introduction", "application": "PRISE", @@ -258,7 +258,7 @@ "model": "message.messagetemplate", "pk": 18, "fields": { - "code": "fall_armyworm_6", + "code": "prise_fall_armyworm_6", "name": "spray2", "type": "Context", "application": "PRISE", @@ -273,7 +273,7 @@ "model": "message.messagetemplate", "pk": 19, "fields": { - "code": "fall_armyworm_7", + "code": "prise_fall_armyworm_7", "name": "bestdate1", "type": "Warning /Description", "application": "PRISE", @@ -288,7 +288,7 @@ "model": "message.messagetemplate", "pk": 20, "fields": { - "code": "fall_armyworm_8", + "code": "prise_fall_armyworm_8", "name": "bestdate2", "type": "Warning /Description", "application": "PRISE", @@ -303,7 +303,7 @@ "model": "message.messagetemplate", "pk": 21, "fields": { - "code": "fall_armyworm_9", + "code": "prise_fall_armyworm_9", "name": "feedback", "type": "Feedback", "application": "PRISE", @@ -318,7 +318,7 @@ "model": "message.messagetemplate", "pk": 22, "fields": { - "code": "fall_armyworm_10", + "code": "prise_fall_armyworm_10", "name": "spray1", "type": "Introduction", "application": "PRISE", @@ -333,7 +333,7 @@ "model": "message.messagetemplate", "pk": 23, "fields": { - "code": "fall_armyworm_11", + "code": "prise_fall_armyworm_11", "name": "spray2", "type": "Context", "application": "PRISE", @@ -348,7 +348,7 @@ "model": "message.messagetemplate", "pk": 24, "fields": { - "code": "fall_armyworm_12", + "code": "prise_fall_armyworm_12", "name": "advice1", "type": "Warning /Description /Action /Advice1", "application": "PRISE", @@ -363,7 +363,7 @@ "model": "message.messagetemplate", "pk": 25, "fields": { - "code": "fall_armyworm_13", + "code": "prise_fall_armyworm_13", "name": "bestdate", "type": "Warning /Description", "application": "PRISE", diff --git a/django_project/message/migrations/0001_initial.py b/django_project/message/migrations/0001_initial.py index 1d835f94..067b8c36 100644 --- a/django_project/message/migrations/0001_initial.py +++ b/django_project/message/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.7 on 2024-10-24 06:39 +# Generated by Django 4.2.7 on 2024-10-24 08:20 from django.db import migrations, models @@ -26,7 +26,9 @@ class Migration(migrations.Migration): ('template_sw', models.TextField(help_text='Field for storing messages in translation. Include {{ context_key }} as a placeholder to be replaced with the appropriate context.', null=True)), ], options={ + 'db_table': 'message_template', 'ordering': ('code',), + 'indexes': [models.Index(fields=['group'], name='message_tem_group_2a17ef_idx'), models.Index(fields=['application'], name='message_tem_applica_7f4863_idx'), models.Index(fields=['name'], name='message_tem_name_311d8e_idx')], }, ), ] diff --git a/django_project/message/models.py b/django_project/message/models.py index 03e3f3ae..79d512ee 100644 --- a/django_project/message/models.py +++ b/django_project/message/models.py @@ -5,10 +5,23 @@ .. note:: Message models. """ +from django.conf import settings from django.db import models +from django.template import Template, Context from django.utils.translation import gettext_lazy as _ -from prise.variables import MessageType +from prise.variables import PriceMessageGroup + + +class MessageLanguageNotSupportedException(Exception): + """Message language not supported exception.""" + + def __init__(self): # noqa + self.message = ( + f'The language is not supported. ' + f'Choices: {[lang[0] for lang in settings.LANGUAGES]}'' ' + ) + super().__init__(self.message) class MessageApplication: @@ -37,12 +50,24 @@ class MessageTemplate(models.Model): max_length=512 ) group = models.CharField( - default=MessageType.START_SEASON, + default=PriceMessageGroup.START_SEASON, choices=( - (MessageType.START_SEASON, _(MessageType.START_SEASON)), - (MessageType.TIME_TO_ACTION_1, _(MessageType.TIME_TO_ACTION_1)), - (MessageType.TIME_TO_ACTION_2, _(MessageType.TIME_TO_ACTION_2)), - (MessageType.END_SEASON, _(MessageType.END_SEASON)), + ( + PriceMessageGroup.START_SEASON, + _(PriceMessageGroup.START_SEASON) + ), + ( + PriceMessageGroup.TIME_TO_ACTION_1, + _(PriceMessageGroup.TIME_TO_ACTION_1) + ), + ( + PriceMessageGroup.TIME_TO_ACTION_2, + _(PriceMessageGroup.TIME_TO_ACTION_2) + ), + ( + PriceMessageGroup.END_SEASON, + _(PriceMessageGroup.END_SEASON) + ), ), max_length=512 ) @@ -62,7 +87,29 @@ class MessageTemplate(models.Model): class Meta: # noqa ordering = ('code',) + db_table = 'message_template' + indexes = [ + models.Index(fields=['group']), + models.Index(fields=['application']), + models.Index(fields=['name']), + ] def __str__(self): """Return string representation of MessageTemplate.""" return self.code + + def get_message(self, context=dict, language_code: str = None): + """Return template by language code. + + Also auto assign the data from context to template. + """ + if not language_code: + language_code = settings.LANGUAGES[0][0] + try: + template = Template( + getattr(self, f'template_{language_code}') + ) + context_obj = Context(context) + return template.render(context_obj) + except AttributeError: + raise MessageLanguageNotSupportedException() diff --git a/django_project/message/tests/__init__.py b/django_project/message/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/django_project/message/tests/test_message.py b/django_project/message/tests/test_message.py new file mode 100644 index 00000000..aa64bdb8 --- /dev/null +++ b/django_project/message/tests/test_message.py @@ -0,0 +1,59 @@ +# coding=utf-8 +""" +Tomorrow Now GAP. + +.. note:: Unit tests for Message Models. +""" + +from django.test import TestCase + +from message.factories import MessageTemplateFactory +from message.models import MessageLanguageNotSupportedException + + +class MessageTemplateTest(TestCase): + """MessageTemplate test case.""" + + def test_get_template_language_not_found(self): + """Test create object.""" + message = MessageTemplateFactory() + + with self.assertRaises(MessageLanguageNotSupportedException): + message.get_message(language_code='af') + + def test_get_template_context_not_found(self): + """Test create object.""" + message = MessageTemplateFactory( + template='This text with {{ language_code }}', + ) + self.assertEqual(message.get_message(), 'This text with ') + + def test_get_template_context(self): + """Test create object.""" + message = MessageTemplateFactory( + template='This text with {{ language_code }}', + ) + self.assertEqual( + message.get_message( + language_code='en', context={ + 'language_code': 'en' + } + ), + 'This text with en' + ) + + def test_get_template_context_with_different_language(self): + """Test create object.""" + message = MessageTemplateFactory( + template='This text with {{ language_code }} in english', + template_sw='This text with {{ language_code }} in swahili', + ) + self.assertEqual( + message.get_message( + language_code='sw', + context={ + 'language_code': 'sw' + } + ), + 'This text with sw in swahili' + ) diff --git a/django_project/message/tests/test_models.py b/django_project/message/tests/test_models.py new file mode 100644 index 00000000..36cf2032 --- /dev/null +++ b/django_project/message/tests/test_models.py @@ -0,0 +1,46 @@ +# coding=utf-8 +""" +Tomorrow Now GAP. + +.. note:: Unit tests for Message Models. +""" + +from django.test import TestCase + +from message.factories import MessageTemplateFactory +from message.models import MessageTemplate + + +class MessageTemplateCRUDTest(TestCase): + """MessageTemplate test case.""" + + Factory = MessageTemplateFactory + Model = MessageTemplate + + def test_create_object(self): + """Test create object.""" + obj = self.Factory() + self.assertIsInstance(obj, self.Model) + self.assertTrue(self.Model.objects.filter(id=obj.id).exists()) + + def test_read_object(self): + """Test read object.""" + obj = self.Factory() + fetched_obj = self.Model.objects.get(id=obj.id) + self.assertEqual(obj, fetched_obj) + + def test_update_object(self): + """Test update object.""" + obj = self.Factory() + new_name = "Updated Name" + obj.name = new_name + obj.save() + updated_obj = self.Model.objects.get(id=obj.id) + self.assertEqual(updated_obj.name, new_name) + + def test_delete_object(self): + """Test delete object.""" + obj = self.Factory() + _id = obj.id + obj.delete() + self.assertFalse(self.Model.objects.filter(id=_id).exists()) diff --git a/django_project/prise/factories.py b/django_project/prise/factories.py new file mode 100644 index 00000000..dc7f647d --- /dev/null +++ b/django_project/prise/factories.py @@ -0,0 +1,20 @@ +# coding=utf-8 +""" +Tomorrow Now GAP. + +.. note:: Factory classes for Message +""" +import factory +from factory.django import DjangoModelFactory + +from gap.factories.crop_insight import PestFactory +from prise.models import PriseMessage + + +class PriseMessageFactory(DjangoModelFactory): + """Factory class for PriseMessage model.""" + + class Meta: # noqa + model = PriseMessage + + pest = factory.SubFactory(PestFactory) diff --git a/django_project/prise/fixtures/1.messages.json b/django_project/prise/fixtures/1.prise_messages.json similarity index 100% rename from django_project/prise/fixtures/1.messages.json rename to django_project/prise/fixtures/1.prise_messages.json diff --git a/django_project/prise/migrations/0001_initial.py b/django_project/prise/migrations/0001_initial.py index dffe1c9e..f3c77e7a 100644 --- a/django_project/prise/migrations/0001_initial.py +++ b/django_project/prise/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.7 on 2024-10-24 06:45 +# Generated by Django 4.2.7 on 2024-10-24 08:22 from django.db import migrations, models import django.db.models.deletion @@ -19,7 +19,11 @@ class Migration(migrations.Migration): fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('messages', models.ManyToManyField(blank=True, null=True, to='message.messagetemplate')), - ('pest', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='gap.pest')), + ('pest', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='gap.pest', unique=True)), ], + options={ + 'db_table': 'prise_message', + 'ordering': ('pest__name',), + }, ), ] diff --git a/django_project/prise/models.py b/django_project/prise/models.py index dca1eed7..ca64d288 100644 --- a/django_project/prise/models.py +++ b/django_project/prise/models.py @@ -11,11 +11,21 @@ from message.models import MessageTemplate +class PriseMessagePestDoesNotExist(Exception): + """Price message of pest does not exist exception.""" + + def __init__(self, pest: Pest): # noqa + self.message = ( + f'Prise message with pest {pest.name} does not exist.' + ) + super().__init__(self.message) + + class PriseMessage(models.Model): """Model that stores message template linked with pest.""" pest = models.ForeignKey( - Pest, on_delete=models.CASCADE + Pest, on_delete=models.CASCADE, unique=True ) messages = models.ManyToManyField( MessageTemplate, null=True, blank=True @@ -24,3 +34,32 @@ class PriseMessage(models.Model): def __str__(self): """Return string representation.""" return self.pest.name + + class Meta: # noqa + ordering = ('pest__name',) + db_table = 'prise_message' + + @staticmethod + def get_messages_objects(pest: Pest, message_group: str = None): + """Return message objects.""" + try: + message = PriseMessage.objects.get(pest=pest).messages.all() + if message_group: + message = message.filter(group=message_group) + return message + except PriseMessage.DoesNotExist: + raise PriseMessagePestDoesNotExist(pest) + + @staticmethod + def get_messages( + pest: Pest, message_group: str, context=dict, + language_code: str = None + + ): + """Return messages string.""" + return [ + message.get_message(context, language_code) + for message in PriseMessage.get_messages_objects( + pest, message_group + ) + ] diff --git a/django_project/prise/tests/__init__.py b/django_project/prise/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/django_project/prise/tests/test_message.py b/django_project/prise/tests/test_message.py new file mode 100644 index 00000000..9c910b0e --- /dev/null +++ b/django_project/prise/tests/test_message.py @@ -0,0 +1,208 @@ +# coding=utf-8 +""" +Tomorrow Now GAP. + +.. note:: Unit tests for Message Models. +""" + +from django.test import TestCase + +from gap.factories import PestFactory +from message.factories import MessageTemplateFactory +from prise.factories import PriseMessageFactory +from prise.models import PriseMessage, PriseMessagePestDoesNotExist +from prise.variables import PriceMessageGroup + + +class PriseMessageTest(TestCase): + """PriseMessage test case.""" + + def setUp(self): + """Set SalientIngestorBaseTest.""" + self.pest_1 = PestFactory(name='pest 1') + self.pest_2 = PestFactory(name='pest 2') + self.pest_3 = PestFactory(name='pest 3') + + message_1 = MessageTemplateFactory( + template='This is template 1 in english with {{ language_code }}', + template_sw=( + 'This is template 1 in swahili with {{ language_code }}' + ), + group=PriceMessageGroup.START_SEASON + ) + message_2 = MessageTemplateFactory( + template='This is template 2 in english with {{ language_code }}', + template_sw=( + 'This is template 2 in swahili with {{ language_code }}' + ), + group=PriceMessageGroup.START_SEASON + ) + message_3 = MessageTemplateFactory( + template='This is template 3 in english with {{ language_code }}', + template_sw=( + 'This is template 3 in swahili with {{ language_code }}' + ), + group=PriceMessageGroup.TIME_TO_ACTION_1 + ) + message_4 = MessageTemplateFactory( + template='This is template 4 in english with {{ language_code }}', + template_sw=( + 'This is template 4 in swahili with {{ language_code }}' + ), + group=PriceMessageGroup.START_SEASON + ) + prise_message_1 = PriseMessageFactory(pest=self.pest_1) + prise_message_1.messages.add(*[message_1, message_2, message_3]) + prise_message_2 = PriseMessageFactory(pest=self.pest_2) + prise_message_2.messages.add(*[message_4]) + + def test_pest_no_message_objects(self): + """Test return pest with no message.""" + with self.assertRaises(PriseMessagePestDoesNotExist): + PriseMessage.get_messages_objects(self.pest_3) + + def test_pest_1_message_objects(self): + """Test return pest 1.""" + self.assertEqual( + PriseMessage.get_messages_objects(self.pest_1).count(), 3 + ) + self.assertEqual( + PriseMessage.get_messages_objects( + self.pest_1, message_group=PriceMessageGroup.START_SEASON + ).count(), + 2 + ) + self.assertEqual( + PriseMessage.get_messages_objects( + self.pest_1, message_group=PriceMessageGroup.TIME_TO_ACTION_1 + ).count(), + 1 + ) + + def test_pest_2_message_objects(self): + """Test return pest 2.""" + self.assertEqual( + PriseMessage.get_messages_objects(self.pest_2).count(), 1 + ) + + def test_pest_1_messages_en_start_season(self): + """Test return pest 1.""" + self.assertEqual( + PriseMessage.get_messages( + self.pest_1, + language_code='en', + context={ + 'language_code': 'en' + }, + message_group=PriceMessageGroup.START_SEASON + ), + [ + 'This is template 1 in english with en', + 'This is template 2 in english with en', + ] + ) + + def test_pest_1_messages_sw_start_season(self): + """Test return pest 1.""" + self.assertEqual( + PriseMessage.get_messages( + self.pest_1, + language_code='sw', + context={ + 'language_code': 'sw' + }, + message_group=PriceMessageGroup.START_SEASON + ), + [ + 'This is template 1 in swahili with sw', + 'This is template 2 in swahili with sw', + ] + ) + + def test_pest_1_messages_en_time_to_action_1(self): + """Test return pest 1.""" + self.assertEqual( + PriseMessage.get_messages( + self.pest_1, + language_code='en', + context={ + 'language_code': 'en' + }, + message_group=PriceMessageGroup.TIME_TO_ACTION_1 + ), + [ + 'This is template 3 in english with en' + ] + ) + + def test_pest_1_messages_sw_time_to_action_1(self): + """Test return pest 1.""" + self.assertEqual( + PriseMessage.get_messages( + self.pest_1, + language_code='sw', + context={ + 'language_code': 'sw' + }, + message_group=PriceMessageGroup.TIME_TO_ACTION_1 + ), + [ + 'This is template 3 in swahili with sw', + ] + ) + + def test_pest_2_messages_en(self): + """Test return pest 1.""" + self.assertEqual( + PriseMessage.get_messages( + self.pest_2, + language_code='en', + context={ + 'language_code': 'en' + }, + message_group=PriceMessageGroup.START_SEASON + ), + [ + 'This is template 4 in english with en', + ] + ) + + def test_pest_2_messages_sw(self): + """Test return pest 1.""" + self.assertEqual( + PriseMessage.get_messages( + self.pest_2, + language_code='sw', + context={ + 'language_code': 'sw' + }, + message_group=PriceMessageGroup.START_SEASON + ), + [ + 'This is template 4 in swahili with sw', + ] + ) + + def test_pest_3_messages_en(self): + """Test return pest 1.""" + with self.assertRaises(PriseMessagePestDoesNotExist): + PriseMessage.get_messages( + self.pest_3, + language_code='en', + context={ + 'language_code': 'en' + }, + message_group=PriceMessageGroup.START_SEASON + ) + + def test_pest_3_messages_sw(self): + """Test return pest 1.""" + with self.assertRaises(PriseMessagePestDoesNotExist): + PriseMessage.get_messages( + self.pest_3, + language_code='sw', + context={ + 'language_code': 'sw' + }, + message_group=PriceMessageGroup.START_SEASON + ) diff --git a/django_project/prise/tests/test_models.py b/django_project/prise/tests/test_models.py new file mode 100644 index 00000000..61dab55d --- /dev/null +++ b/django_project/prise/tests/test_models.py @@ -0,0 +1,53 @@ +# coding=utf-8 +""" +Tomorrow Now GAP. + +.. note:: Unit tests for Prise Message Models. +""" + +from django.test import TestCase + +from message.factories import MessageTemplateFactory +from prise.factories import PriseMessageFactory, PestFactory +from prise.models import PriseMessage + + +class PriseMessageCRUDTest(TestCase): + """PriseMessage test case.""" + + Factory = PriseMessageFactory + Model = PriseMessage + + def test_create_object(self): + """Test create object.""" + obj = self.Factory() + obj.messages.add( + *[MessageTemplateFactory(), MessageTemplateFactory()] + ) + self.assertIsInstance(obj, self.Model) + self.assertTrue(self.Model.objects.filter(id=obj.id).exists()) + self.assertTrue( + self.Model.objects.filter(id=obj.id).first().messages.count(), 2 + ) + + def test_read_object(self): + """Test read object.""" + obj = self.Factory() + fetched_obj = self.Model.objects.get(id=obj.id) + self.assertEqual(obj, fetched_obj) + + def test_update_object(self): + """Test update object.""" + obj = self.Factory() + pest = PestFactory() + obj.pest = pest + obj.save() + updated_obj = self.Model.objects.get(id=obj.id) + self.assertEqual(updated_obj.pest, pest) + + def test_delete_object(self): + """Test delete object.""" + obj = self.Factory() + _id = obj.id + obj.delete() + self.assertFalse(self.Model.objects.filter(id=_id).exists()) diff --git a/django_project/prise/variables.py b/django_project/prise/variables.py index c0be4742..6a164c21 100644 --- a/django_project/prise/variables.py +++ b/django_project/prise/variables.py @@ -6,7 +6,7 @@ """ -class MessageType: +class PriceMessageGroup: """Type of message that being used by Price.""" START_SEASON = 'Start of Season'