Skip to content

Commit

Permalink
feat: Add equality to Email (#739)
Browse files Browse the repository at this point in the history
Email objects are defined as equal iff they have the same email and the same name.
This is useful for unit tests where we want to verify that the Email objects generated are as expected.
  • Loading branch information
mcintyre94 authored Mar 6, 2020
1 parent ea90f7c commit b2ca1f4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
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 = "[email protected]"
name = "SomeName"
email1 = Email(address, name)
email2 = Email(address, name)

self.assertEqual(email1, email2)

def test_equality_email(self):
address = "[email protected]"
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 = "[email protected]"
email1 = Email(address1)
address2 = "[email protected]"
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 = "[email protected]"
email = Email(address)

self.assertNotEqual(email, address)

0 comments on commit b2ca1f4

Please sign in to comment.