Skip to content

Commit

Permalink
TemplateTransactional: add validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Aug 20, 2024
1 parent df8e4cc commit 0f02815
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
31 changes: 31 additions & 0 deletions lemarche/conversations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.validators import ValidationError
from django.db import IntegrityError, models
from django.db.models import Count, Func, IntegerField, Q
from django.utils import timezone
Expand All @@ -13,6 +14,7 @@

from lemarche.conversations import constants as conversation_constants
from lemarche.utils.apis import api_brevo, api_mailjet
from lemarche.utils.data import add_validation_error


class ConversationQuerySet(models.QuerySet):
Expand Down Expand Up @@ -256,6 +258,35 @@ def __str__(self):
return f"{self.name} ({self.code})"
return self.name

def clean(self, *args, **kwargs):
# dict to store all ValidationErrors
validation_errors = dict()
# validation rules
if self.is_active:
if self.source == conversation_constants.SOURCE_MAILJET and not self.mailjet_id:
validation_errors = add_validation_error(
validation_errors,
"mailjet_id",
"Le mailjet_id est requis pour un template actif",
)
elif self.source == conversation_constants.SOURCE_BREVO and not self.brevo_id:
validation_errors = add_validation_error(
validation_errors,
"brevo_id",
"Le brevo_id est requis pour un template actif",
)
# return
if bool(validation_errors):
raise ValidationError(validation_errors)
super().clean(*args, **kwargs)

def save(self, *args, **kwargs):
"""
- run validations
"""
self.full_clean()
super().save(*args, **kwargs)

@property
def get_template_id(self):
if self.source and self.code:
Expand Down
34 changes: 32 additions & 2 deletions lemarche/conversations/tests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from datetime import timedelta

from django.test import TestCase
from django.core.exceptions import ValidationError
from django.db import IntegrityError
from django.test import TestCase, TransactionTestCase
from django.utils import timezone

from lemarche.conversations import constants as conversation_constants
from lemarche.conversations.factories import ConversationFactory
from lemarche.conversations.factories import ConversationFactory, TemplateTransactionalFactory
from lemarche.conversations.models import Conversation, TemplateTransactional
from lemarche.siaes.factories import SiaeFactory

Expand Down Expand Up @@ -99,3 +101,31 @@ def test_get_template_id(self):
self.assertEqual(self.tt_inactive.get_template_id, self.tt_inactive.mailjet_id)
self.assertEqual(self.tt_active_mailjet.get_template_id, self.tt_active_mailjet.mailjet_id)
self.assertEqual(self.tt_active_brevo.get_template_id, self.tt_active_brevo.brevo_id)


class TemplateTransactionalModelSaveTest(TransactionTestCase):
@classmethod
def setUpTestData(cls):
pass

def test_template_transactional_validation_on_save(self):
TemplateTransactionalFactory(
mailjet_id=None, brevo_id=None, source=conversation_constants.SOURCE_BREVO, is_active=False
)
self.assertRaises(IntegrityError, TemplateTransactionalFactory, source=None)
self.assertRaises(
ValidationError,
TemplateTransactionalFactory,
mailjet_id=123,
brevo_id=None,
source=conversation_constants.SOURCE_BREVO,
is_active=True,
)
self.assertRaises(
ValidationError,
TemplateTransactionalFactory,
mailjet_id=None,
brevo_id=123,
source=conversation_constants.SOURCE_MAILJET,
is_active=True,
)
15 changes: 15 additions & 0 deletions lemarche/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,18 @@ def phone_number_display(phone_number_model_field):
return phone_number_model_field.as_e164
except AttributeError:
return phone_number_model_field


def add_validation_error(dict, key, value):
"""
Build a dictionary of validation errors
{"field1": ["error1", "error2"], "field2": ["error1"]}
"""
if key not in dict:
dict[key] = value
else:
if type(dict[key]) is list:
dict[key] += [value]
if type(dict[key]) is str:
dict[key] = [dict[key], value]
return dict

0 comments on commit 0f02815

Please sign in to comment.