Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert test suite from Nose to Pytest (required for testing on Python ≥ 3.10) #240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
set -ex
REPORT_PATH="${REPORT_PATH:-./}"
nosetests --with-xunit --with-coverage --cover-xml --cover-xml-file $REPORT_PATH/coverage.xml --xunit-file=$REPORT_PATH/nosetests.xml --cover-package=talon .
REPORT_PATH="${REPORT_PATH:-.}"
pytest --cov=talon --cov-report=term --cov-report="xml:$REPORT_PATH/coverage.xml" --junitxml="$REPORT_PATH/nosetests.xml" .
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ def finalize_options(self):
"joblib",
],
tests_require=[
"mock",
"nose",
"coverage"
"pytest",
"pytest-cov"
]
)
5 changes: 2 additions & 3 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
coverage
mock
nose>=1.2.1
pytest
pytest-cov
3 changes: 1 addition & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import absolute_import
from nose.tools import *
from mock import *
from unittest.mock import *

import talon

Expand Down
106 changes: 52 additions & 54 deletions tests/html_quotations_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import re
from unittest.mock import Mock, patch

from nose.tools import assert_false, assert_true, eq_, ok_

from tests.fixtures import (OLK_SRC_BODY_SECTION,
REPLY_QUOTATIONS_SHARE_BLOCK,
REPLY_SEPARATED_BY_HR)
Expand All @@ -31,8 +29,8 @@ def test_quotation_splitter_inside_blockquote():

</blockquote>"""

eq_("<html><head></head><body>Reply</body></html>",
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body)))
assert "<html><head></head><body>Reply</body></html>" == \
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body))


def test_quotation_splitter_outside_blockquote():
Expand All @@ -48,8 +46,8 @@ def test_quotation_splitter_outside_blockquote():
</div>
</blockquote>
"""
eq_("<html><head></head><body>Reply</body></html>",
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body)))
assert "<html><head></head><body>Reply</body></html>" == \
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body))


def test_regular_blockquote():
Expand All @@ -66,8 +64,8 @@ def test_regular_blockquote():
</div>
</blockquote>
"""
eq_("<html><head></head><body>Reply<blockquote>Regular</blockquote></body></html>",
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body)))
assert "<html><head></head><body>Reply<blockquote>Regular</blockquote></body></html>" == \
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body))


def test_no_blockquote():
Expand All @@ -94,12 +92,12 @@ def test_no_blockquote():
Reply

</body></html>"""
eq_(RE_WHITESPACE.sub('', reply),
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body)))
assert RE_WHITESPACE.sub('', reply) == \
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body))


def test_empty_body():
eq_('', quotations.extract_from_html(''))
assert '' == quotations.extract_from_html('')


def test_validate_output_html():
Expand All @@ -117,10 +115,10 @@ def test_validate_output_html():
<div/>
"""
out = quotations.extract_from_html(msg_body)
ok_('<html>' in out and '</html>' in out,
'Invalid HTML - <html>/</html> tag not present')
ok_('<div/>' not in out,
'Invalid HTML output - <div/> element is not valid')
assert '<html>' in out and '</html>' in out, \
'Invalid HTML - <html>/</html> tag not present'
assert '<div/>' not in out, \
'Invalid HTML output - <div/> element is not valid'


def test_gmail_quote():
Expand All @@ -133,8 +131,8 @@ def test_gmail_quote():
</div>
</div>
</div>"""
eq_("<html><head></head><body>Reply</body></html>",
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body)))
assert "<html><head></head><body>Reply</body></html>" == \
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body))


def test_gmail_quote_compact():
Expand All @@ -144,8 +142,8 @@ def test_gmail_quote_compact():
'<div>Test</div>' \
'</div>' \
'</div>'
eq_("<html><head></head><body>Reply</body></html>",
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body)))
assert "<html><head></head><body>Reply</body></html>" == \
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body))


def test_gmail_quote_blockquote():
Expand All @@ -156,8 +154,8 @@ def test_gmail_quote_blockquote():
<br/>
</div>
</blockquote>"""
eq_(RE_WHITESPACE.sub('', msg_body),
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body)))
assert RE_WHITESPACE.sub('', msg_body) == \
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body))


def test_unicode_in_reply():
Expand All @@ -171,9 +169,9 @@ def test_unicode_in_reply():
Quote
</blockquote>"""

eq_("<html><head></head><body>Reply&#160;&#160;Text<br><div><br></div>"
"</body></html>",
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body)))
assert "<html><head></head><body>Reply&#160;&#160;Text<br><div><br></div>" \
"</body></html>" == \
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body))


def test_blockquote_disclaimer():
Expand Down Expand Up @@ -210,8 +208,8 @@ def test_blockquote_disclaimer():
</body>
</html>
"""
eq_(RE_WHITESPACE.sub('', stripped_html),
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body)))
assert RE_WHITESPACE.sub('', stripped_html) == \
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body))


def test_date_block():
Expand All @@ -229,8 +227,8 @@ def test_date_block():
</div>
</div>
"""
eq_('<html><head></head><body><div>message<br></div></body></html>',
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body)))
assert '<html><head></head><body><div>message<br></div></body></html>' == \
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body))


def test_from_block():
Expand All @@ -246,8 +244,8 @@ def test_from_block():
text
</div></div>
"""
eq_('<html><head></head><body><div>message<br></div></body></html>',
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body)))
assert '<html><head></head><body><div>message<br></div></body></html>' == \
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body))


def test_reply_shares_div_with_from_block():
Expand All @@ -264,26 +262,26 @@ def test_reply_shares_div_with_from_block():

</div>
</body>'''
eq_('<html><head></head><body><div>Blah<br><br></div></body></html>',
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body)))
assert '<html><head></head><body><div>Blah<br><br></div></body></html>' == \
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body))


def test_reply_quotations_share_block():
stripped_html = quotations.extract_from_plain(REPLY_QUOTATIONS_SHARE_BLOCK)
ok_(stripped_html)
ok_('From' not in stripped_html)
assert stripped_html
assert 'From' not in stripped_html


def test_OLK_SRC_BODY_SECTION_stripped():
eq_('<html><head></head><body><div>Reply</div></body></html>',
assert '<html><head></head><body><div>Reply</div></body></html>' == \
RE_WHITESPACE.sub(
'', quotations.extract_from_html(OLK_SRC_BODY_SECTION)))
'', quotations.extract_from_html(OLK_SRC_BODY_SECTION))


def test_reply_separated_by_hr():
eq_('<html><head></head><body><div>Hi<div>there</div></div></body></html>',
assert '<html><head></head><body><div>Hi<div>there</div></div></body></html>' == \
RE_WHITESPACE.sub(
'', quotations.extract_from_html(REPLY_SEPARATED_BY_HR)))
'', quotations.extract_from_html(REPLY_SEPARATED_BY_HR))


def test_from_block_and_quotations_in_separate_divs():
Expand All @@ -302,8 +300,8 @@ def test_from_block_and_quotations_in_separate_divs():
</div>
</div>
'''
eq_('<html><head></head><body>Reply<div><hr></div></body></html>',
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body)))
assert '<html><head></head><body>Reply<div><hr></div></body></html>' == \
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body))


def extract_reply_and_check(filename):
Expand All @@ -318,8 +316,8 @@ def extract_reply_and_check(filename):
reply = quotations.extract_from_html(msg_body)
plain_reply = u.html_to_text(reply)

eq_(RE_WHITESPACE.sub('', "Hi. I am fine.\n\nThanks,\nAlex"),
RE_WHITESPACE.sub('', plain_reply))
assert RE_WHITESPACE.sub('', "Hi. I am fine.\n\nThanks,\nAlex") == \
RE_WHITESPACE.sub('', plain_reply)


def test_gmail_reply():
Expand Down Expand Up @@ -363,8 +361,8 @@ def test_CRLF():
"""
symbol = '&#13;'
extracted = quotations.extract_from_html('<html>\r\n</html>')
assert_false(symbol in extracted)
eq_('<html></html>', RE_WHITESPACE.sub('', extracted))
assert symbol not in extracted
assert '<html></html>' == RE_WHITESPACE.sub('', extracted)

msg_body = """My
reply
Expand All @@ -381,16 +379,16 @@ def test_CRLF():
</blockquote>"""
msg_body = msg_body.replace('\n', '\r\n')
extracted = quotations.extract_from_html(msg_body)
assert_false(symbol in extracted)
assert symbol not in extracted
# Keep new lines otherwise "My reply" becomes one word - "Myreply"
eq_("<html><head></head><body>My\nreply\n</body></html>", extracted)
assert "<html><head></head><body>My\nreply\n</body></html>" == extracted


def test_gmail_forwarded_msg():
msg_body = """<div dir="ltr"><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Bob</b> <span dir="ltr">&lt;<a href="mailto:[email protected]">[email protected]</a>&gt;</span><br>Date: Fri, Feb 11, 2010 at 5:59 PM<br>Subject: Bob WFH today<br>To: Mary &lt;<a href="mailto:[email protected]">[email protected]</a>&gt;<br><br><br><div dir="ltr">eom</div>
</div><br></div>"""
extracted = quotations.extract_from_html(msg_body)
eq_(RE_WHITESPACE.sub('', msg_body), RE_WHITESPACE.sub('', extracted))
assert RE_WHITESPACE.sub('', msg_body) == RE_WHITESPACE.sub('', extracted)


def test_readable_html_empty():
Expand All @@ -407,14 +405,14 @@ def test_readable_html_empty():

</blockquote>"""

eq_(RE_WHITESPACE.sub('', msg_body),
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body)))
assert RE_WHITESPACE.sub('', msg_body) == \
RE_WHITESPACE.sub('', quotations.extract_from_html(msg_body))


@patch.object(quotations, 'html_document_fromstring', Mock(return_value=None))
def test_bad_html():
bad_html = "<html></html>"
eq_(bad_html, quotations.extract_from_html(bad_html))
assert bad_html == quotations.extract_from_html(bad_html)


def test_remove_namespaces():
Expand All @@ -430,8 +428,8 @@ def test_remove_namespaces():

rendered = quotations.extract_from_html(msg_body)

assert_true("<p>" in rendered)
assert_true("xmlns" in rendered)
assert "<p>" in rendered
assert "xmlns" in rendered

assert_true("<o:p>" not in rendered)
assert_true("<xmlns:o>" not in rendered)
assert "<o:p>" not in rendered
assert "<xmlns:o>" not in rendered
6 changes: 3 additions & 3 deletions tests/quotations_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ def test_extract_from_respects_content_type(extract_from_plain,
quotations.extract_from(msg_body, 'text/html')
extract_from_html.assert_called_with(msg_body)

eq_(msg_body, quotations.extract_from(msg_body, 'text/blah'))
assert msg_body == quotations.extract_from(msg_body, 'text/blah')


@patch.object(quotations, 'extract_from_plain', Mock(side_effect=Exception()))
def test_crash_inside_extract_from():
msg_body = "Hi there"
eq_(msg_body, quotations.extract_from(msg_body, 'text/plain'))
assert msg_body == quotations.extract_from(msg_body, 'text/plain')


def test_empty_body():
eq_('', quotations.extract_from_plain(''))
assert '' == quotations.extract_from_plain('')
Loading