浏览代码

fix: add back `in_test` override for `imap_folders` test

Since a lot of tests still uses the old way i.e, use `in_test` and execute custome (non-production) code to get results. Kept the same code for now else as a major test refactor needs to be done.
version-14
kamaljohnson 3 年前
父节点
当前提交
caab903edb
共有 2 个文件被更改,包括 21 次插入20 次删除
  1. +3
    -9
      frappe/email/doctype/email_account/email_account.py
  2. +18
    -11
      frappe/email/doctype/email_account/test_email_account.py

+ 3
- 9
frappe/email/doctype/email_account/email_account.py 查看文件

@@ -457,7 +457,7 @@ class EmailAccount(Document):
if exceptions: if exceptions:
raise Exception(frappe.as_json(exceptions)) raise Exception(frappe.as_json(exceptions))


def get_inbound_mails(self, test_mails=None, messages=None) -> List[InboundMail]:
def get_inbound_mails(self, test_mails=None) -> List[InboundMail]:
"""retrive and return inbound mails. """retrive and return inbound mails.


""" """
@@ -471,14 +471,8 @@ class EmailAccount(Document):
# only append the emails with status != 'SEEN' if sync option is set to 'UNSEEN' # only append the emails with status != 'SEEN' if sync option is set to 'UNSEEN'
mails.append(InboundMail(message, self, uid, seen_status, append_to)) mails.append(InboundMail(message, self, uid, seen_status, append_to))


# if frappe.local.flags.in_test:
# if self.enable_incoming and not test_mails and messages:
# for folder in self.imap_folder:
# _messages = messages[folder.folder_name] if folder.folder_name in messages and (messages[folder.folder_name] is not None) else {}
# process_mail(_messages, folder.append_to)
# return mails
# else:
# return [InboundMail(msg, self) for msg in test_mails or []]
if frappe.local.flags.in_test:
return [InboundMail(msg, self) for msg in test_mails or []]


if not self.enable_incoming: if not self.enable_incoming:
return [] return []


+ 18
- 11
frappe/email/doctype/email_account/test_email_account.py 查看文件

@@ -257,10 +257,7 @@ class TestEmailAccount(unittest.TestCase):
self.assertTrue(communication.reference_name) self.assertTrue(communication.reference_name)
self.assertTrue(frappe.db.exists(communication.reference_doctype, communication.reference_name)) self.assertTrue(frappe.db.exists(communication.reference_doctype, communication.reference_name))


@patch("frappe.email.receive.EmailServer.select_imap_folder", return_value=True)
@patch("frappe.email.receive.EmailServer.logout", side_effect=lambda: None)
# @patch("frappe.email.receive.EmailServer.get_messages", side_effect=get_mocked_messages)
def test_append_to_with_imap_folders(self, mocked_logout, mocked_select_imap_folder):
def test_append_to_with_imap_folders(self):
mail_content_1 = self.get_test_mail(fname="incoming-1.raw") mail_content_1 = self.get_test_mail(fname="incoming-1.raw")
mail_content_2 = self.get_test_mail(fname="incoming-2.raw") mail_content_2 = self.get_test_mail(fname="incoming-2.raw")
mail_content_3 = self.get_test_mail(fname="incoming-3.raw") mail_content_3 = self.get_test_mail(fname="incoming-3.raw")
@@ -287,14 +284,10 @@ class TestEmailAccount(unittest.TestCase):
'uid_list': [2] 'uid_list': [2]
} }
} }
from frappe.email.receive import EmailServer
def get_mocked_messages(**args):
return messages[args["folder"]]


with patch.object(EmailServer, "get_messages", side_effect=get_mocked_messages):
email_account = frappe.get_doc("Email Account", "_Test Email Account 1")
mails = email_account.get_inbound_mails(messages=messages)
self.assertEqual(len(mails), 3)
email_account = frappe.get_doc("Email Account", "_Test Email Account 1")
mails = TestEmailAccount.mocked_get_inbound_mails(email_account, messages)
self.assertEqual(len(mails), 3)


inbox_mails = 0 inbox_mails = 0
test_folder_mails = 0 test_folder_mails = 0
@@ -313,6 +306,20 @@ class TestEmailAccount(unittest.TestCase):
self.assertEqual(inbox_mails, 2) self.assertEqual(inbox_mails, 2)
self.assertEqual(test_folder_mails, 1) self.assertEqual(test_folder_mails, 1)


@patch("frappe.local.flags.in_test", False)
@patch("frappe.email.receive.EmailServer.select_imap_folder", return_value=True)
@patch("frappe.email.receive.EmailServer.logout", side_effect=lambda: None)
def mocked_get_inbound_mails(email_account, messages={}, mocked_logout=None, mocked_select_imap_folder=None):
from frappe.email.receive import EmailServer

def get_mocked_messages(**kwargs):
return messages[kwargs["folder"]]

with patch.object(EmailServer, "get_messages", side_effect=get_mocked_messages):
mails = email_account.get_inbound_mails()

return mails

class TestInboundMail(unittest.TestCase): class TestInboundMail(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):


正在加载...
取消
保存