Skip to content

Commit

Permalink
Added tests for branch coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
GitRon committed Sep 11, 2024
1 parent f9afc28 commit ff60109
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tests/services/asynchronous/test_thread_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@ def test_process_regular(self, mocked_start):

self.assertIsNone(service.process())
mocked_start.assert_called_once()

@mock.patch.object(Thread, "start")
@mock.patch.object(ThreadEmailService, "is_valid", return_value=False)
def test_process_invalid(self, mocked_service, mocked_start):
email = "[email protected]"
subject = "Test email"
service = ThreadEmailService(recipient_email_list=[email])
service.subject = subject
service.template_name = "testapp/test_email.html"

self.assertIsNone(service.process())
mocked_start.assert_not_called()
8 changes: 8 additions & 0 deletions tests/services/base/test_base_mail_factory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest import mock

from django.test import TestCase

from django_pony_express.errors import EmailServiceConfigError
Expand Down Expand Up @@ -80,3 +82,9 @@ def test_process_with_exception(self):
factory.service_class = self.TestMailService
with self.assertRaises(EmailServiceConfigError):
factory.process()

@mock.patch.object(BaseEmailServiceFactory, "is_valid", return_value=False)
def test_process_is_valid_invalid(self, *args):
factory = BaseEmailServiceFactory()
factory.service_class = self.TestMailService
self.assertEqual(factory.process(), 0)
21 changes: 21 additions & 0 deletions tests/services/base/test_base_mail_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.test import TestCase, override_settings
from django.utils import translation
from freezegun import freeze_time

from django_pony_express.errors import EmailServiceAttachmentError, EmailServiceConfigError
Expand Down Expand Up @@ -228,6 +229,21 @@ def test_build_mail_object_with_attachments(self):
self.assertEqual(len(msg_obj.attachments), 1)
self.assertEqual(msg_obj.attachments[0][0], basename(file_path))

@mock.patch.object(BaseEmailService, "get_translation", return_value=None)
def test_build_mail_object_missing_language(self, *args):
email = "[email protected]"
my_var = "Lorem ipsum dolor!"
file_path = settings.BASE_PATH / "tests/files/testfile.txt"
service = BaseEmailService(
recipient_email_list=[email], context_data={"my_var": my_var}, attachment_list=[file_path]
)

service.template_name = "testapp/test_email.html"

with mock.patch.object(translation, "activate") as mocked_activate:
service._build_mail_object()
mocked_activate.assert_not_called()

def test_setting_txt_templates_works(self):
my_var = "Lorem ipsum dolor!"
service = BaseEmailService(
Expand Down Expand Up @@ -372,3 +388,8 @@ def test_process_with_error(self):
service.template_name = "testapp/test_email.html"
with self.assertRaises(EmailServiceConfigError):
service.process()

@mock.patch.object(BaseEmailService, "is_valid", return_value=False)
def test_process_is_valid_invalid(self, *args):
factory = BaseEmailService()
self.assertEqual(factory.process(), 0)

0 comments on commit ff60109

Please sign in to comment.