Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add equality to Email #739

Merged
merged 3 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions sendgrid/helpers/mail/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ def __init__(self,
if p is not None:
self.personalization = p

def __eq__(self, other):
"""Email objects are equal if they have the same email and the same name

:param other: object to compare with
:type other: any
"""
if isinstance(other, Email):
return self.email == other.email and self.name == other.name
return NotImplemented

def __ne__(self, other):
"""Inverse of __eq__ (required for Python 2)

:param other: object to compare with
:type other: any
"""
x = self.__eq__(other)
if x is not NotImplemented:
return not x
return NotImplemented

@property
def name(self):
"""Name associated with this email.
Expand Down
48 changes: 48 additions & 0 deletions test/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,51 @@ def test_add_unicode_name_with_comma(self):
email.name = name

self.assertEqual(email.name, u'"' + name + u'"')

def test_equality_email_name(self):
address = "test@example.com"
name = "SomeName"
email1 = Email(address, name)
email2 = Email(address, name)

self.assertEqual(email1, email2)

def test_equality_email(self):
address = "test@example.com"
email1 = Email(address)
email2 = Email(address)

self.assertEqual(email1, email2)

def test_equality_name(self):
name = "SomeName"
email1 = Email()
email1.name = name
email2 = Email()
email2.name = name

self.assertEqual(email1, email2)

def test_equality_different_emails(self):
address1 = "test1@example.com"
email1 = Email(address1)
address2 = "test2@example.com"
email2 = Email(address2)

self.assertNotEqual(email1, email2)

def test_equality_different_name(self):
name1 = "SomeName1"
email1 = Email()
email1.name = name1
name2 = "SomeName2"
email2 = Email()
email2.name = name2

self.assertNotEqual(email1, email2)

def test_equality_non_email(self):
address = "test@example.com"
email = Email(address)

self.assertNotEqual(email, address)