def __init__(self, mock_mf): """Create an instance of TransactionMailCampaign for testing. Also test if it is initialized correctly.""" self.tmc = TransactionMailCampaign(**self.args) assert len(self.tmc._mails) == 2 # MailFactory calls it `variables` and not `global_vars` # Rename it. Laziness global_vars = self.args["global_vars"] mailing_list = self.args["mailing_list"] self.args.pop("global_vars", None) self.args["variables"] = self.variables[0] self.args["mailing_list"] = [mailing_list[0]["email"]] # acquire the actual Envelope object self.tmc._mails[0] = MailFactory(**self.args) mock_mf.assert_any_call(**self.args) self.args["variables"] = self.variables[1] self.args["mailing_list"] = [mailing_list[1]["email"]] # acquire the actual Envelope object self.tmc._mails[1] = MailFactory(**self.args) mock_mf.assert_any_call(**self.args) # Undo the changes self.args["mailing_list"] = mailing_list self.args["global_vars"] = global_vars self.args.pop("variables", None)
def __init__(self, mock_mf): """Create an instance of TransactionMailCampaign for testing. Also test if it is initialized correctly.""" self.tmc = TransactionMailCampaign(**self.args) assert len(self.tmc._mails) == 2 # MailFactory calls it `variables` and not `global_vars` # Rename it. Laziness global_vars = self.args["global_vars"] mailing_list = self.args["mailing_list"] self.args.pop("global_vars", None) self.args["variables"] = self.variables[0] self.args["mailing_list"] = [mailing_list[0]["email"]] # acquire the actual Envelope object self.tmc._mails[0] = MailFactory(**self.args) mock_mf.assert_any_call(**self.args) self.args["variables"] = self.variables[1] self.args["mailing_list"] = [mailing_list[1]["email"]] # acquire the actual Envelope object self.tmc._mails[1] = MailFactory(**self.args) mock_mf.assert_any_call(**self.args) # Undo the changes self.args["mailing_list"] = mailing_list self.args["global_vars"] = global_vars self.args.pop("variables", None)
class TestTransactionMailCampaign(object): """Class to test deltamail.campaign.TransactionMailCampaign class""" args = { "from_addr": "*****@*****.**", "subject": "TransactionMail : Hey {{name}}, Greetings from {{company}}", "mailing_list": [{ "email": '*****@*****.**', "variables": { "name": "Job", "msg": "You got a job!" } }, { "email": '*****@*****.**', "variables": { "name": "Pop", "msg": "You got popped!" } }], "template_str": "Hello {{name}}, greetings from {{company}}.\n{{msg}}\nCopyright @ {{year}}", "global_vars": { "company": "Festember", "year": 2015 } } evaled = [{ "subject": "TransactionMail : Hey Job, Greetings from Festember", "body": "Hello Job, greetings from Festember.\nYou got a job!\nCopyright @ 2015", }, { "subject": "TransactionMail : Hey Pop, Greetings from Festember", "body": "Hello Pop, greetings from Festember.\nYou got popped!\nCopyright @ 2015", }] variables = [{ "name": "Job", "msg": "You got a job!", "company": "Festember", "year": 2015 }, { "name": "Pop", "msg": "You got popped!", "company": "Festember", "year": 2015 }] @patch('deltamail.campaign.MailFactory', autospec=True) def __init__(self, mock_mf): """Create an instance of TransactionMailCampaign for testing. Also test if it is initialized correctly.""" self.tmc = TransactionMailCampaign(**self.args) assert len(self.tmc._mails) == 2 # MailFactory calls it `variables` and not `global_vars` # Rename it. Laziness global_vars = self.args["global_vars"] mailing_list = self.args["mailing_list"] self.args.pop("global_vars", None) self.args["variables"] = self.variables[0] self.args["mailing_list"] = [mailing_list[0]["email"]] # acquire the actual Envelope object self.tmc._mails[0] = MailFactory(**self.args) mock_mf.assert_any_call(**self.args) self.args["variables"] = self.variables[1] self.args["mailing_list"] = [mailing_list[1]["email"]] # acquire the actual Envelope object self.tmc._mails[1] = MailFactory(**self.args) mock_mf.assert_any_call(**self.args) # Undo the changes self.args["mailing_list"] = mailing_list self.args["global_vars"] = global_vars self.args.pop("variables", None) @patch('deltamail.campaign.os.path.isdir', autospec=True) @patch('deltamail.campaign.os.mkdir', autospec=True) @patch('deltamail.campaign.open') def test_preview_default(self, mk_open, mock_mkdir, mock_isdir): """Test TransactionMailCampaign.preview() with no args""" newdirpath = path.join(os.getcwd(), "email-preview", "") open_return_mock = Mock() mk_open.return_value = open_return_mock mock_isdir.return_value = True self.tmc.preview() mock_mkdir.assert_called_once_with(newdirpath) for i in range(len(self.evaled)): fname = self.evaled[i]["subject"] + "-" + self.args[ "mailing_list"][i]["email"] fname = fname[:MAX_PREVIEW_FILE_LEN] + ".html" fname = fname.replace(r"/", "-") fname = fname.replace("\\", "-") fname = fname.replace(r":", "-") fpath = path.join(newdirpath, fname) mk_open.assert_any_call(fpath, "w") open_return_mock.write.assert_any_call(self.evaled[i]["body"]) assert mk_open.call_count == 2 assert open_return_mock.write.call_count == 2 assert open_return_mock.close.call_count == 2 @patch('deltamail.campaign.os.path.isdir', autospec=True) @patch('deltamail.campaign.os.mkdir', autospec=True) @patch('deltamail.campaign.open') def test_preview_with_location(self, mk_open, mock_mkdir, mock_isdir): """Test TransactionMailCampaign.preview() with location argument""" newdirpath = "./tests/preview-mails/TestBulkMailCampaign/" open_return_mock = Mock() mk_open.return_value = open_return_mock self.tmc.preview(newdirpath) mock_mkdir.assert_not_called() for i in range(len(self.evaled)): fname = self.evaled[i]["subject"] + "-" + self.args[ "mailing_list"][i]["email"] fname = fname[:MAX_PREVIEW_FILE_LEN] + ".html" fname = fname.replace(r"/", "-") fname = fname.replace("\\", "-") fname = fname.replace(r":", "-") fpath = path.join(newdirpath, fname) mk_open.assert_any_call(fpath, "w") open_return_mock.write.assert_any_call(self.evaled[i]["body"]) assert mk_open.call_count == 2 assert open_return_mock.write.call_count == 2 assert open_return_mock.close.call_count == 2 def test_send(self): """Test the TransactionMailCampaign.send() method""" mock_mailer = Mock(spec=['send']) self.tmc.send(mock_mailer) mock_mailer.send.assert_any_call(self.tmc._mails[0]) mock_mailer.send.assert_any_call(self.tmc._mails[0]) assert mock_mailer.send.call_count == 2 def test_preview_in_browser(self): """Test the preview_in_browser() method""" # UNTESTED! pass
class TestTransactionMailCampaign(object): """Class to test deltamail.campaign.TransactionMailCampaign class""" args = { "from_addr": "*****@*****.**", "subject": "TransactionMail : Hey {{name}}, Greetings from {{company}}", "mailing_list": [ {"email": "*****@*****.**", "variables": {"name": "Job", "msg": "You got a job!"}}, {"email": "*****@*****.**", "variables": {"name": "Pop", "msg": "You got popped!"}}, ], "template_str": "Hello {{name}}, greetings from {{company}}.\n{{msg}}\nCopyright @ {{year}}", "global_vars": {"company": "Festember", "year": 2015}, } evaled = [ { "subject": "TransactionMail : Hey Job, Greetings from Festember", "body": "Hello Job, greetings from Festember.\nYou got a job!\nCopyright @ 2015", }, { "subject": "TransactionMail : Hey Pop, Greetings from Festember", "body": "Hello Pop, greetings from Festember.\nYou got popped!\nCopyright @ 2015", }, ] variables = [ {"name": "Job", "msg": "You got a job!", "company": "Festember", "year": 2015}, {"name": "Pop", "msg": "You got popped!", "company": "Festember", "year": 2015}, ] @patch("deltamail.campaign.MailFactory", autospec=True) def __init__(self, mock_mf): """Create an instance of TransactionMailCampaign for testing. Also test if it is initialized correctly.""" self.tmc = TransactionMailCampaign(**self.args) assert len(self.tmc._mails) == 2 # MailFactory calls it `variables` and not `global_vars` # Rename it. Laziness global_vars = self.args["global_vars"] mailing_list = self.args["mailing_list"] self.args.pop("global_vars", None) self.args["variables"] = self.variables[0] self.args["mailing_list"] = [mailing_list[0]["email"]] # acquire the actual Envelope object self.tmc._mails[0] = MailFactory(**self.args) mock_mf.assert_any_call(**self.args) self.args["variables"] = self.variables[1] self.args["mailing_list"] = [mailing_list[1]["email"]] # acquire the actual Envelope object self.tmc._mails[1] = MailFactory(**self.args) mock_mf.assert_any_call(**self.args) # Undo the changes self.args["mailing_list"] = mailing_list self.args["global_vars"] = global_vars self.args.pop("variables", None) @patch("deltamail.campaign.os.path.isdir", autospec=True) @patch("deltamail.campaign.os.mkdir", autospec=True) @patch("deltamail.campaign.open") def test_preview_default(self, mk_open, mock_mkdir, mock_isdir): """Test TransactionMailCampaign.preview() with no args""" newdirpath = path.join(os.getcwd(), "email-preview", "") open_return_mock = Mock() mk_open.return_value = open_return_mock mock_isdir.return_value = True self.tmc.preview() mock_mkdir.assert_called_once_with(newdirpath) for i in range(len(self.evaled)): fname = self.evaled[i]["subject"] + "-" + self.args["mailing_list"][i]["email"] fname = fname[:MAX_PREVIEW_FILE_LEN] + ".html" fname = fname.replace(r"/", "-") fname = fname.replace("\\", "-") fname = fname.replace(r":", "-") fpath = path.join(newdirpath, fname) mk_open.assert_any_call(fpath, "w") open_return_mock.write.assert_any_call(self.evaled[i]["body"]) assert mk_open.call_count == 2 assert open_return_mock.write.call_count == 2 assert open_return_mock.close.call_count == 2 @patch("deltamail.campaign.os.path.isdir", autospec=True) @patch("deltamail.campaign.os.mkdir", autospec=True) @patch("deltamail.campaign.open") def test_preview_with_location(self, mk_open, mock_mkdir, mock_isdir): """Test TransactionMailCampaign.preview() with location argument""" newdirpath = "./tests/preview-mails/TestBulkMailCampaign/" open_return_mock = Mock() mk_open.return_value = open_return_mock self.tmc.preview(newdirpath) mock_mkdir.assert_not_called() for i in range(len(self.evaled)): fname = self.evaled[i]["subject"] + "-" + self.args["mailing_list"][i]["email"] fname = fname[:MAX_PREVIEW_FILE_LEN] + ".html" fname = fname.replace(r"/", "-") fname = fname.replace("\\", "-") fname = fname.replace(r":", "-") fpath = path.join(newdirpath, fname) mk_open.assert_any_call(fpath, "w") open_return_mock.write.assert_any_call(self.evaled[i]["body"]) assert mk_open.call_count == 2 assert open_return_mock.write.call_count == 2 assert open_return_mock.close.call_count == 2 def test_send(self): """Test the TransactionMailCampaign.send() method""" mock_mailer = Mock(spec=["send"]) self.tmc.send(mock_mailer) mock_mailer.send.assert_any_call(self.tmc._mails[0]) mock_mailer.send.assert_any_call(self.tmc._mails[0]) assert mock_mailer.send.call_count == 2 def test_preview_in_browser(self): """Test the preview_in_browser() method""" # UNTESTED! pass