def compare(email, match): if 'to' in match and email.to != match['to']: return False if 'subject' in match and email.subject != match['subject']: return False if 'subject_re' in match and not re.search(match['subject_re'], email.subject): return False if 'body' in match and email.text_body != match['body']: return False if 'body_re' in match and not re.search(match['body_re'], email.text_body, re.S): return False if 'reply_to' in match and not email.reply_to == match['reply_to']: return False valid_keys = set(['to', 'subject', 'subject_re', 'body', 'body_re', 'reply_to']) found_keys = set(match.keys()) if found_keys - valid_keys != set(): harness().handle_error(TestFailureException("Developer mistake: trying to use a key to check emails with that doesn't exist: %s" % (found_keys - valid_keys), observed=found_keys, expected=valid_keys)) return True
def is_equal(observed, expected, message=None): if observed == expected: return if message is None: message = "is_equal check" harness().handle_error(TestFailureException(message, observed=observed, expected=expected))
def isnt_like(observed, regexp, message=None): if not re.search(regexp, observed): return if message is None: message = "isnt_like check" harness().handle_error(TestFailureException(message, observed=observed, expected=regexp))
def check(*matches, **kwargs): """Checks what emails the system has sent. Pass a series of dicts, one for each email you wish to check. For details on what keys the dict can have for matching checks, see compare(). Checking has two modes: "absolute" (the default), and "incremental". In absolute mode, you must pass a dict describing every single email the system has sent since begin_capturing() was called. In incremental mode, you can pass dicts describing any of the emails that have been sent. You don't have to describe them all. Emails that are matched will be taken off the list to be checked, allowing you to do this: mail.begin_capturing() operation_that_sends_four_emails() mail.check(incremental=True, dict(... one of the emails...)) mail.check(incremental=True, dict(... one of the other emails...)) mail.check(dict(... one of the last two ...), dict(... the last one left ...)) """ incremental = kwargs.get('incremental', False) message("Checking emails (incremental=%s)" % incremental) emails = set(CapturedEmail.objects) if not incremental: is_equal(len(emails), len(matches), "Correct number of emails") emails_to_delete = set() for match in matches: found_match = False for email in emails: if compare(email, match): emails.remove(email) emails_to_delete.add(email) found_match = True break if not found_match: harness().handle_error(TestFailureException("Failed to find match for specified email", observed=[x.as_string() for x in emails], expected=match)) for email in emails_to_delete: CapturedEmail.objects.get(id=email.id).delete() return emails_to_delete
def message(message): frame = sys._getframe() depth = 0 test_code = harness().current_test_object.__class__.run.__func__.__code__ frame_code = frame.f_code while frame and (test_code.co_filename != frame_code.co_filename or test_code.co_firstlineno != frame_code.co_firstlineno): frame = frame.f_back if frame: frame_code = frame.f_code depth += 1 for i in range(depth-1): print " ", print message
def failure(message): harness().handle_error(TestFailureException(message))
def get(path, browser=None): browser = browser_for(browser) uri = harness().base_uri.copy().join(str(path)) browser.get(str(uri))
def browser_for(browser): if isinstance(browser, basestring): return harness().browser_for_key(browser) return harness().current_browser()