Skip to content

Commit 9437ca1

Browse files
fix: Ensure msg.send() returns boolean
Converted `msg.send()` result to a boolean in `._send_and_log_email()` for clarity and consistency. - Updated `msg.send()` handling to return `True` for success and `False` otherwise. - Adjusted related tests to reflect the updated behavior. - Added new test cases to verify boolean conversion of `msg.send()` results.
1 parent 1d5fd25 commit 9437ca1

File tree

3 files changed

+66
-41
lines changed

3 files changed

+66
-41
lines changed

CHANGES.md

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
**2.7.3** (2025-06-24)
4+
* Convert `msg.send()` result to boolean in `._send_and_log_email()`
5+
36
**2.7.2** (2025-05-30)
47
* Replaced freezegun with time_machine for tests
58
* Maintenance updates via ambient-package-update
@@ -19,131 +22,131 @@
1922
**2.6.2** (2025-04-03)
2023
* Maintenance updates via ambient-package-update
2124

22-
* *2.6.1* (2025-03-18)
25+
**2.6.1** (2025-03-18)
2326
* Fixed a bug where translations were not deactivated after sending an email
2427

25-
* *2.6.0* (2025-03-17)
28+
**2.6.0** (2025-03-17)
2629
* Added check for email structure validity
2730

28-
* *2.5.1* (2025-02-15)
31+
**2.5.1** (2025-02-15)
2932
* Maintenance updates via ambient-package-update
3033

31-
* *2.5.0* (2024-12-03)
34+
**2.5.0** (2024-12-03)
3235
* Added connection param to `BaseEmailService` (Thx to @sk-rama)
3336

34-
* *2.4.3* (2024-11-15)
37+
**2.4.3** (2024-11-15)
3538
* Move logic to render HTML and text content to dedicated methods
3639

37-
* *2.4.2* (2024-11-15)
40+
**2.4.2** (2024-11-15)
3841
* Internal updates via `ambient-package-update`
3942

40-
* *2.4.1* (2024-10-14)
43+
**2.4.1** (2024-10-14)
4144
* Added Python 3.13 support
4245
* Added Djade linter to pre-commit
4346
* Improved GitHub action triggers
4447
* Updated dev dependencies and linters
4548

46-
* *2.4.0* (2024-09-11)
49+
**2.4.0** (2024-09-11)
4750
* Allow custom subject delimiter
4851

49-
* *2.3.3* (2024-09-11)
52+
**2.3.3** (2024-09-11)
5053
* Fixed coverage setup due to GitHub changes
5154

52-
* *2.3.2* (2024-09-11)
55+
**2.3.2** (2024-09-11)
5356
* Fixed package name
5457

55-
* *2.3.1* (2024-08-12)
58+
**2.3.1** (2024-08-12)
5659
* Fixed test matrix
5760

58-
* *2.3.0* (2024-08-12)
61+
**2.3.0** (2024-08-12)
5962
* Added Django 5.1 support
6063

61-
* *2.2.2* (2024-07-18)
64+
**2.2.2** (2024-07-18)
6265
* Added SECURITY.md
6366
* Updated linters
6467
* Internal updates via `ambient-package-update`
6568

66-
* *2.2.1* (2024-07-16)
69+
**2.2.1** (2024-07-16)
6770
* Updated GitHub actions
6871

69-
* *2.2.0* (2024-07-15)
72+
**2.2.0** (2024-07-15)
7073
* Dropped Python 3.8 support
7174
* Added multiple ruff linters
7275

73-
* *2.1.3* (2024-06-21)
76+
**2.1.3** (2024-06-21)
7477
* Linted docs with `blacken-docs` via `ambient-package-update`
7578

76-
* *2.1.2* (2024-06-14)
79+
**2.1.2** (2024-06-14)
7780
* Internal updates via `ambient-package-update`
7881

79-
* *2.1.1* (2024-05-31)
82+
**2.1.1** (2024-05-31)
8083
* Changed log-level to "info" for successful dispatching
8184
* Improved configuration docs
8285

83-
* *2.1.0* (2024-05-27)
86+
**2.1.0** (2024-05-27)
8487
* Added `ThreadEmailService` for simple async sending of emails
8588
* Added basic logging with privacy configuration to mail class
8689
* Restructured documentation
8790
* Restructured unit-tests
8891
* Minor test improvements
8992

90-
* *2.0.0* (2024-04-11)
93+
**2.0.0** (2024-04-11)
9194
* Dropped Django 3.2 & 4.1 support (via `ambient-package-update`)
9295
* Internal updates via `ambient-package-update`
9396

94-
* *1.3.0** (2023-12-04)
97+
**1.3.0** (2023-12-04)
9598
* Added Django 5.0 support
9699

97-
* *1.2.5** (2023-11-13)
100+
**1.2.5** (2023-11-13)
98101
* Fixed wrong import path in docs
99102

100-
* *1.2.4** (2023-11-03)
103+
**1.2.4** (2023-11-03)
101104
* Switched formatter from `black` to `ruff`
102105

103-
* *1.2.3** (2023-10-20)
106+
**1.2.3** (2023-10-20)
104107
* Linter updaters including code adjustments
105108
* Updates from ambient updater
106109

107-
* *1.2.2** (2023-10-04)
110+
**1.2.2** (2023-10-04)
108111
* Dependency to ambient updater updated
109112

110-
* *1.2.1** (2023-10-04)
113+
**1.2.1** (2023-10-04)
111114
* Downgraded Python to 3.11 for readthedocs.org
112115

113-
* *1.2.0** (2023-10-04)
116+
**1.2.0** (2023-10-04)
114117
* Added Python 3.12 support
115118
* Updated internal linter packages
116119

117-
* *1.1.6** (2023-09-08)
120+
**1.1.6** (2023-09-08)
118121
* Metadata update via ambient package updater
119122

120-
* *1.1.5** (2023-09-08)
123+
**1.1.5** (2023-09-08)
121124
* Metadata update via ambient package updater
122125
* Cleaned up test matrix
123126

124-
* **1.1.4** (2023-08-31)
127+
**1.1.4** (2023-08-31)
125128
* Fixed downloads badge
126129

127-
* **1.1.3** (2023-08-31)
130+
**1.1.3** (2023-08-31)
128131
* Improved type-hinting for constructor of `BaseEmailServiceFactory` and `BaseEmailService`
129132

130-
* **1.1.2** (2023-08-14)
133+
**1.1.2** (2023-08-14)
131134
* Updated linters and add more strict linting rules
132135

133-
* **1.1.1** (2023-05-10)
136+
**1.1.1** (2023-05-10)
134137
* Updated readme "maintenance" section
135138
* Updated package linters
136139
* Added release scripts for windows and UNIX
137140

138-
* **1.1.0** (2023-05-04)
141+
**1.1.0** (2023-05-04)
139142
* Support for Django 4.2 added
140143

141-
* **1.0.2** (2023-05-04)
144+
**1.0.2** (2023-05-04)
142145
* Updated documentation
143146

144-
* **1.0.1** (2023-05-03)
147+
**1.0.1** (2023-05-03)
145148
* Added missing translations
146149
* Updated documentation
147150

148-
* **1.0.0** (2023-05-01)
151+
**1.0.0** (2023-05-01)
149152
* Release as a separate package at PyPI (was before a part of [ai-django-core](https://pypi.org/project/ai-django-core/))

django_pony_express/services/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,9 @@ def _send_and_log_email(self, msg: EmailMultiAlternatives) -> bool:
343343
result = False
344344
recipients_as_string = " ".join(self.recipient_email_list)
345345
try:
346-
result = msg.send()
346+
# msg.send() returns an int: 0 if no recipients exist, 1 if the message sending was successful
347+
# Since we want to return a boolean, we check for "== 1" here
348+
result = msg.send() == 1
347349
if PONY_LOG_RECIPIENTS:
348350
self._logger.info(_('Email "%s" successfully sent to %s.') % (msg.subject, recipients_as_string))
349351
else:

tests/services/base/test_base_mail_service.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_init_connection_regular(self):
3838
service.template_name = "testapp/test_email.html"
3939

4040
result = service.process()
41-
self.assertIs(result, 1)
41+
self.assertIs(result, True)
4242

4343
# Assert email was "sent"
4444
self.assertGreater(len(mail.outbox), 0)
@@ -398,7 +398,7 @@ def test_send_and_log_email_success_privacy_active(self, mock_logger):
398398
)
399399

400400
mock_logger.info.assert_called_with('Email "The Pony Express" successfully sent.')
401-
self.assertEqual(result, 1)
401+
self.assertEqual(result, True)
402402

403403
@mock.patch("django_pony_express.services.base.BaseEmailService._logger")
404404
@mock.patch("django_pony_express.services.base.PONY_LOG_RECIPIENTS", True)
@@ -409,7 +409,7 @@ def test_send_and_log_success_privacy_inactive(self, mock_logger):
409409
)
410410

411411
mock_logger.info.assert_called_with('Email "The Pony Express" successfully sent to [email protected].')
412-
self.assertEqual(result, 1)
412+
self.assertEqual(result, True)
413413

414414
@mock.patch.object(EmailMultiAlternatives, "send", side_effect=Exception("Broken pony"))
415415
@mock.patch("django_pony_express.services.base.BaseEmailService._logger")
@@ -439,6 +439,26 @@ def test_send_and_log_failure_privacy_inactive(self, mock_logger, *args):
439439
)
440440
self.assertFalse(result)
441441

442+
@mock.patch.object(EmailMultiAlternatives, "send", return_value=1)
443+
def test_send_and_log_email_returns_true_when_msg_send_returns_one(self, mock_send):
444+
service = BaseEmailService(recipient_email_list=["[email protected]"])
445+
result = service._send_and_log_email(
446+
msg=EmailMultiAlternatives(subject="The Pony Express", to=["[email protected]"])
447+
)
448+
449+
mock_send.assert_called_once()
450+
self.assertEqual(result, True)
451+
452+
@mock.patch.object(EmailMultiAlternatives, "send", return_value=99)
453+
def test_send_and_log_email_returns_false_when_msg_send_returns_any_other_number_than_one(self, mock_send):
454+
service = BaseEmailService(recipient_email_list=["[email protected]"])
455+
result = service._send_and_log_email(
456+
msg=EmailMultiAlternatives(subject="The Pony Express", to=["[email protected]"])
457+
)
458+
459+
mock_send.assert_called_once()
460+
self.assertEqual(result, False)
461+
442462
def test_process_regular(self):
443463
444464
subject = "Test email"

0 commit comments

Comments
 (0)