-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…tests Extended unit tests (#10)
- Loading branch information
Showing
5 changed files
with
131 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
-r ./requirements-package.in | ||
|
||
flake8 | ||
hypothesis | ||
jupyter | ||
pip-tools | ||
pytest | ||
pytest-cov | ||
Sphinx | ||
tox | ||
twine | ||
twine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,49 @@ | ||
from unittest import TestCase | ||
from unittest.mock import patch | ||
|
||
from secretsanta.main.core import SecretSanta | ||
|
||
|
||
class InitializeSecretSanta(TestCase): | ||
class TestSecretSanta(TestCase): | ||
def test_secret_santa_init___result_has_helper_rudolph(self): | ||
res = SecretSanta('[email protected]', 'you') | ||
self.assertEqual('rudolph', res.helper) | ||
|
||
def test_secret_santa_init___email_attribute_is_string(self): | ||
res = SecretSanta('[email protected]', 'you') | ||
self.assertTrue(isinstance(res.email, str)) | ||
|
||
@patch('smtplib.SMTP') | ||
def test_secret_santa_send(self, mock_smtp): | ||
""" | ||
Test that the send implementation sends a message containing the specified variable content and metadata | ||
""" | ||
santa_email = '[email protected]' | ||
res = SecretSanta(santa_email, 'you') | ||
from_address = "[email protected]" | ||
subject = "Santa Unit Test" | ||
message = "It's a unit test" | ||
res.send(subject, from_address, message, mock_smtp, test=True) | ||
# use a custom validator (we only care that the message sent to SMTP contains our parameters) | ||
mock_smtp.sendmail.assert_called_with(from_address, santa_email, | ||
SantaMockMessageValidator("Santa", subject, | ||
santa_email, message)) | ||
|
||
|
||
class SantaMockMessageValidator(object): | ||
""" | ||
Mock object validator: overrides equality operator to control match conditions inside `assert_called_with`, | ||
using the provided parameters to check that the produced string (`other`) contains the expected values. | ||
""" | ||
def __init__(self, sender: str, subject: str, santa_email: str, message: str): | ||
self.sender = sender | ||
self.subject = subject | ||
self.santa_email = santa_email | ||
self.message = message | ||
|
||
def __eq__(self, other: str): | ||
needed_elements = [self.sender, self.subject, self.santa_email, self.message] | ||
test = all(fragment in other for fragment in needed_elements) | ||
if not test: | ||
print("one of %s is missing in the produced SMTP message!" % needed_elements) | ||
return test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import unittest | ||
from typing import Union, List | ||
from unittest.mock import patch | ||
from hypothesis import given | ||
from secretsanta.main import funs | ||
from hypothesis.strategies import text, lists, integers, characters | ||
|
||
|
||
class MakeSantaDict(unittest.TestCase): | ||
# we specify some character classes to avoid unprintable characters that cause issues when used as dictionary keys. | ||
# the min / max parameters passed to integers match the accepted range for seeds. | ||
@given(lists(text(alphabet=characters(whitelist_categories=["Lu", "Ll", "Nd", "Pc", "Pd"], | ||
whitelist_characters=["@"]), min_size=2, max_size=20), 2, 10, unique=True), | ||
integers(min_value=0, max_value=2**32 - 1)) | ||
def test_all_different_assign(self, test_list, seed): | ||
""" | ||
Test that a generated list of unique names, turned into a dictionary, are all assigned to one another, without | ||
self-assignment. | ||
:param test_list: list of names | ||
:param seed: seed for random choice picking | ||
""" | ||
test_dict = dict(zip(test_list, test_list)) | ||
assignment = funs.make_santa_dict(test_dict, seed) | ||
assert len(assignment) == len(test_list) | ||
for left, right in assignment.items(): | ||
assert (left != right) | ||
|
||
# We don't want to actually send e-mail, and we are testing the send_santa_dict functionality (not the SecretSanta | ||
# class internals) | ||
@patch('secretsanta.main.core.SecretSanta') | ||
@patch('smtplib.SMTP') | ||
def test_send_santa_dict(self, mock_smtp, mock_santa): | ||
""" | ||
Test that the function calls our email sending logic with the expected parameters, the expected number of times | ||
""" | ||
test_dict = dict(zip(["a", "b", "c"], ["[email protected]", "[email protected]", "[email protected]"])) | ||
|
||
smtpserverwithport = "lalaland:1337" | ||
|
||
def mocksantabuilder(email: Union[str, List[str]], person: str): | ||
return mock_santa(email, person) | ||
|
||
funs.internal_send_santa_dict(smtpserverwithport, "mr.jack", "NoOneCanGuess1234", test_dict, mocksantabuilder) | ||
mock_smtp.assert_called_with(smtpserverwithport) | ||
mock_santa.assert_called_with("[email protected]", "c") | ||
assert mock_santa.call_count == 3 |