Mercurial > kallithea
changeset 5911:fed636881606
pytest migration: other: switch to standard assert statements
Use unittest2pytest to replace unittest-style assert statements (e.g.
assertEqual) with standard Python assert statements to benefit from pytest's
improved reporting on assert failures.
The conversion by unittest2pytest was correct except for:
- 'import pytest' is not automatically added when needed
- line wrapping in string formatting caused a syntax error in the
transformed code. Reported upstream at
https://github.com/pytest-dev/unittest2pytest/issues/3 .
author | Thomas De Schampheleire <thomas.de.schampheleire@gmail.com> |
---|---|
date | Sun, 08 May 2016 21:17:09 +0200 |
parents | 313930ae35ad |
children | 7483b3f3bea5 |
files | kallithea/tests/other/manual_test_vcs_operations.py kallithea/tests/other/test_libs.py kallithea/tests/other/test_mail.py kallithea/tests/other/test_validators.py |
diffstat | 4 files changed, 136 insertions(+), 127 deletions(-) [+] |
line wrap: on
line diff
--- a/kallithea/tests/other/manual_test_vcs_operations.py Fri May 06 21:21:50 2016 +0200 +++ b/kallithea/tests/other/manual_test_vcs_operations.py Sun May 08 21:17:09 2016 +0200 @@ -274,7 +274,7 @@ key = CacheInvalidation.query().filter(CacheInvalidation.cache_key ==HG_REPO).all() - self.assertEqual(key, []) + assert key == [] def test_push_invalidates_cache_git(self): key = CacheInvalidation.query().filter(CacheInvalidation.cache_key @@ -296,7 +296,7 @@ key = CacheInvalidation.query().filter(CacheInvalidation.cache_key ==GIT_REPO).all() - self.assertEqual(key, []) + assert key == [] def test_push_wrong_credentials_hg(self): DEST = _get_tmp_dir()
--- a/kallithea/tests/other/test_libs.py Fri May 06 21:21:50 2016 +0200 +++ b/kallithea/tests/other/test_libs.py Sun May 08 21:17:09 2016 +0200 @@ -72,12 +72,12 @@ @parametrize('test_url,expected,expected_creds', TEST_URLS) def test_uri_filter(self, test_url, expected, expected_creds): from kallithea.lib.utils2 import uri_filter - self.assertEqual(uri_filter(test_url), expected) + assert uri_filter(test_url) == expected @parametrize('test_url,expected,expected_creds', TEST_URLS) def test_credentials_filter(self, test_url, expected, expected_creds): from kallithea.lib.utils2 import credentials_filter - self.assertEqual(credentials_filter(test_url), expected_creds) + assert credentials_filter(test_url) == expected_creds @parametrize('str_bool,expected', [ ('t', True), @@ -100,7 +100,7 @@ ]) def test_str2bool(self, str_bool, expected): from kallithea.lib.utils2 import str2bool - self.assertEqual(str2bool(str_bool), expected) + assert str2bool(str_bool) == expected def test_mention_extractor(self): from kallithea.lib.utils2 import extract_mentioned_usernames @@ -115,7 +115,7 @@ expected = set([ '2one_more22', 'first', 'lukaszb', 'one', 'one_more22', 'UPPER', 'cAmEL', 'john', 'marian.user', 'marco-polo', 'marco_polo', 'world']) - self.assertEqual(expected, set(extract_mentioned_usernames(sample))) + assert expected == set(extract_mentioned_usernames(sample)) @parametrize('age_args,expected', [ (dict(), u'just now'), @@ -138,7 +138,7 @@ from dateutil import relativedelta n = datetime.datetime(year=2012, month=5, day=17) delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs) - self.assertEqual(age(n + delt(**age_args), now=n), expected) + assert age(n + delt(**age_args), now=n) == expected @parametrize('age_args,expected', [ (dict(), u'just now'), @@ -162,7 +162,7 @@ from dateutil import relativedelta n = datetime.datetime(year=2012, month=5, day=17) delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs) - self.assertEqual(age(n + delt(**age_args), show_short_version=True, now=n), expected) + assert age(n + delt(**age_args), show_short_version=True, now=n) == expected @parametrize('age_args,expected', [ (dict(), u'just now'), @@ -180,7 +180,7 @@ from dateutil import relativedelta n = datetime.datetime(year=2012, month=5, day=17) delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs) - self.assertEqual(age(n + delt(**age_args), now=n), expected) + assert age(n + delt(**age_args), now=n) == expected def test_tag_extractor(self): sample = ( @@ -191,12 +191,12 @@ ) from kallithea.lib.helpers import urlify_text res = urlify_text(sample, stylize=True) - self.assertIn('<div class="metatag" tag="tag">tag</div>', res) - self.assertIn('<div class="metatag" tag="obsolete">obsolete</div>', res) - self.assertIn('<div class="metatag" tag="stale">stale</div>', res) - self.assertIn('<div class="metatag" tag="lang">python</div>', res) - self.assertIn('<div class="metatag" tag="requires">requires => <a href="/url">url</a></div>', res) - self.assertIn('<div class="metatag" tag="tag">tag</div>', res) + assert '<div class="metatag" tag="tag">tag</div>' in res + assert '<div class="metatag" tag="obsolete">obsolete</div>' in res + assert '<div class="metatag" tag="stale">stale</div>' in res + assert '<div class="metatag" tag="lang">python</div>' in res + assert '<div class="metatag" tag="requires">requires => <a href="/url">url</a></div>' in res + assert '<div class="metatag" tag="tag">tag</div>' in res def test_alternative_gravatar(self): from kallithea.lib.helpers import gravatar_url @@ -269,7 +269,7 @@ from kallithea.lib.utils2 import get_clone_url clone_url = get_clone_url(uri_tmpl=tmpl, qualified_home_url='http://vps1:8000'+prefix, repo_name=repo_name, repo_id=23, **overrides) - self.assertEqual(clone_url, expected) + assert clone_url == expected def _quick_url(self, text, tmpl="""<a class="revision-link" href="%s">%s</a>""", url_=None): """ @@ -322,7 +322,7 @@ with mock.patch('pylons.url', fake_url): from kallithea.lib.helpers import urlify_changesets - self.assertEqual(urlify_changesets(sample, 'repo_name'), expected) + assert urlify_changesets(sample, 'repo_name') == expected @parametrize('sample,expected,url_', [ ("", @@ -349,7 +349,7 @@ from kallithea.lib.helpers import urlify_text expected = self._quick_url(expected, tmpl="""<a href="%s">%s</a>""", url_=url_) - self.assertEqual(urlify_text(sample), expected) + assert urlify_text(sample) == expected @parametrize('test,expected', [ ("", None), @@ -371,5 +371,4 @@ def test_get_repo_by_id(self, test, expected): from kallithea.lib.utils import _extract_id_from_repo_name _test = _extract_id_from_repo_name(test) - self.assertEqual(_test, expected, msg='url:%s, got:`%s` expected: `%s`' - % (test, _test, expected)) + assert _test == expected, 'url:%s, got:`%s` expected: `%s`' % (test, _test, expected)
--- a/kallithea/tests/other/test_mail.py Fri May 06 21:21:50 2016 +0200 +++ b/kallithea/tests/other/test_mail.py Sun May 08 21:17:09 2016 +0200 @@ -38,12 +38,12 @@ with mock.patch('kallithea.lib.celerylib.tasks.config', config_mock): kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body) - self.assertSetEqual(smtplib_mock.lastdest, set(recipients)) - self.assertEqual(smtplib_mock.lastsender, envelope_from) - self.assertIn('From: %s' % envelope_from, smtplib_mock.lastmsg) - self.assertIn('Subject: %s' % subject, smtplib_mock.lastmsg) - self.assertIn(body, smtplib_mock.lastmsg) - self.assertIn(html_body, smtplib_mock.lastmsg) + assert smtplib_mock.lastdest == set(recipients) + assert smtplib_mock.lastsender == envelope_from + assert 'From: %s' % envelope_from in smtplib_mock.lastmsg + assert 'Subject: %s' % subject in smtplib_mock.lastmsg + assert body in smtplib_mock.lastmsg + assert html_body in smtplib_mock.lastmsg def test_send_mail_no_recipients(self): mailserver = 'smtp.mailserver.org' @@ -62,12 +62,12 @@ with mock.patch('kallithea.lib.celerylib.tasks.config', config_mock): kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body) - self.assertSetEqual(smtplib_mock.lastdest, set([TEST_USER_ADMIN_EMAIL, email_to])) - self.assertEqual(smtplib_mock.lastsender, envelope_from) - self.assertIn('From: %s' % envelope_from, smtplib_mock.lastmsg) - self.assertIn('Subject: %s' % subject, smtplib_mock.lastmsg) - self.assertIn(body, smtplib_mock.lastmsg) - self.assertIn(html_body, smtplib_mock.lastmsg) + assert smtplib_mock.lastdest == set([TEST_USER_ADMIN_EMAIL, email_to]) + assert smtplib_mock.lastsender == envelope_from + assert 'From: %s' % envelope_from in smtplib_mock.lastmsg + assert 'Subject: %s' % subject in smtplib_mock.lastmsg + assert body in smtplib_mock.lastmsg + assert html_body in smtplib_mock.lastmsg def test_send_mail_no_recipients_no_email_to(self): mailserver = 'smtp.mailserver.org' @@ -84,12 +84,12 @@ with mock.patch('kallithea.lib.celerylib.tasks.config', config_mock): kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body) - self.assertSetEqual(smtplib_mock.lastdest, set([TEST_USER_ADMIN_EMAIL])) - self.assertEqual(smtplib_mock.lastsender, envelope_from) - self.assertIn('From: %s' % envelope_from, smtplib_mock.lastmsg) - self.assertIn('Subject: %s' % subject, smtplib_mock.lastmsg) - self.assertIn(body, smtplib_mock.lastmsg) - self.assertIn(html_body, smtplib_mock.lastmsg) + assert smtplib_mock.lastdest == set([TEST_USER_ADMIN_EMAIL]) + assert smtplib_mock.lastsender == envelope_from + assert 'From: %s' % envelope_from in smtplib_mock.lastmsg + assert 'Subject: %s' % subject in smtplib_mock.lastmsg + assert body in smtplib_mock.lastmsg + assert html_body in smtplib_mock.lastmsg def test_send_mail_with_author(self): mailserver = 'smtp.mailserver.org' @@ -107,12 +107,12 @@ with mock.patch('kallithea.lib.celerylib.tasks.config', config_mock): kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body, author=author) - self.assertSetEqual(smtplib_mock.lastdest, set(recipients)) - self.assertEqual(smtplib_mock.lastsender, envelope_from) - self.assertIn('From: "Kallithea Admin (no-reply)" <%s>' % envelope_from, smtplib_mock.lastmsg) - self.assertIn('Subject: %s' % subject, smtplib_mock.lastmsg) - self.assertIn(body, smtplib_mock.lastmsg) - self.assertIn(html_body, smtplib_mock.lastmsg) + assert smtplib_mock.lastdest == set(recipients) + assert smtplib_mock.lastsender == envelope_from + assert 'From: "Kallithea Admin (no-reply)" <%s>' % envelope_from in smtplib_mock.lastmsg + assert 'Subject: %s' % subject in smtplib_mock.lastmsg + assert body in smtplib_mock.lastmsg + assert html_body in smtplib_mock.lastmsg def test_send_mail_with_author_full_mail_from(self): mailserver = 'smtp.mailserver.org' @@ -131,12 +131,12 @@ with mock.patch('kallithea.lib.celerylib.tasks.config', config_mock): kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body, author=author) - self.assertSetEqual(smtplib_mock.lastdest, set(recipients)) - self.assertEqual(smtplib_mock.lastsender, envelope_from) - self.assertIn('From: "Kallithea Admin (no-reply)" <%s>' % envelope_addr, smtplib_mock.lastmsg) - self.assertIn('Subject: %s' % subject, smtplib_mock.lastmsg) - self.assertIn(body, smtplib_mock.lastmsg) - self.assertIn(html_body, smtplib_mock.lastmsg) + assert smtplib_mock.lastdest == set(recipients) + assert smtplib_mock.lastsender == envelope_from + assert 'From: "Kallithea Admin (no-reply)" <%s>' % envelope_addr in smtplib_mock.lastmsg + assert 'Subject: %s' % subject in smtplib_mock.lastmsg + assert body in smtplib_mock.lastmsg + assert html_body in smtplib_mock.lastmsg def test_send_mail_extra_headers(self): mailserver = 'smtp.mailserver.org' @@ -156,12 +156,12 @@ kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body, author=author, headers=headers) - self.assertSetEqual(smtplib_mock.lastdest, set(recipients)) - self.assertEqual(smtplib_mock.lastsender, envelope_from) - self.assertIn(r'From: "foo (fubar) \"baz\" (no-reply)" <%s>' % envelope_from, smtplib_mock.lastmsg) - self.assertIn('Subject: %s' % subject, smtplib_mock.lastmsg) - self.assertIn(body, smtplib_mock.lastmsg) - self.assertIn(html_body, smtplib_mock.lastmsg) - self.assertIn('Extra: yes', smtplib_mock.lastmsg) + assert smtplib_mock.lastdest == set(recipients) + assert smtplib_mock.lastsender == envelope_from + assert r'From: "foo (fubar) \"baz\" (no-reply)" <%s>' % envelope_from in smtplib_mock.lastmsg + assert 'Subject: %s' % subject in smtplib_mock.lastmsg + assert body in smtplib_mock.lastmsg + assert html_body in smtplib_mock.lastmsg + assert 'Extra: yes' in smtplib_mock.lastmsg # verify that headers dict hasn't mutated by send_email - self.assertDictEqual(headers, {'extra': 'yes'}) + assert headers == {'extra': 'yes'}
--- a/kallithea/tests/other/test_validators.py Fri May 06 21:21:50 2016 +0200 +++ b/kallithea/tests/other/test_validators.py Sun May 08 21:17:09 2016 +0200 @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import formencode +import pytest import tempfile from kallithea.tests import * @@ -21,49 +22,59 @@ def test_Message_extractor(self): validator = v.ValidUsername() - self.assertRaises(formencode.Invalid, validator.to_python, 'default') + with pytest.raises(formencode.Invalid): + validator.to_python('default') class StateObj(object): pass - self.assertRaises(formencode.Invalid, - validator.to_python, 'default', StateObj) + with pytest.raises(formencode.Invalid): + validator.to_python('default', StateObj) def test_ValidUsername(self): validator = v.ValidUsername() - self.assertRaises(formencode.Invalid, validator.to_python, 'default') - self.assertRaises(formencode.Invalid, validator.to_python, 'new_user') - self.assertRaises(formencode.Invalid, validator.to_python, '.,') - self.assertRaises(formencode.Invalid, validator.to_python, - TEST_USER_ADMIN_LOGIN) - self.assertEqual('test', validator.to_python('test')) + with pytest.raises(formencode.Invalid): + validator.to_python('default') + with pytest.raises(formencode.Invalid): + validator.to_python('new_user') + with pytest.raises(formencode.Invalid): + validator.to_python('.,') + with pytest.raises(formencode.Invalid): + validator.to_python(TEST_USER_ADMIN_LOGIN) + assert 'test' == validator.to_python('test') validator = v.ValidUsername(edit=True, old_data={'user_id': 1}) def test_ValidRepoUser(self): validator = v.ValidRepoUser() - self.assertRaises(formencode.Invalid, validator.to_python, 'nouser') - self.assertEqual(TEST_USER_ADMIN_LOGIN, - validator.to_python(TEST_USER_ADMIN_LOGIN)) + with pytest.raises(formencode.Invalid): + validator.to_python('nouser') + assert TEST_USER_ADMIN_LOGIN == validator.to_python(TEST_USER_ADMIN_LOGIN) def test_ValidUserGroup(self): validator = v.ValidUserGroup() - self.assertRaises(formencode.Invalid, validator.to_python, u'default') - self.assertRaises(formencode.Invalid, validator.to_python, u'.,') + with pytest.raises(formencode.Invalid): + validator.to_python(u'default') + with pytest.raises(formencode.Invalid): + validator.to_python(u'.,') gr = fixture.create_user_group(u'test') gr2 = fixture.create_user_group(u'tes2') Session().commit() - self.assertRaises(formencode.Invalid, validator.to_python, u'test') + with pytest.raises(formencode.Invalid): + validator.to_python(u'test') assert gr.users_group_id is not None validator = v.ValidUserGroup(edit=True, old_data={'users_group_id': gr2.users_group_id}) - self.assertRaises(formencode.Invalid, validator.to_python, u'test') - self.assertRaises(formencode.Invalid, validator.to_python, u'TesT') - self.assertRaises(formencode.Invalid, validator.to_python, u'TEST') + with pytest.raises(formencode.Invalid): + validator.to_python(u'test') + with pytest.raises(formencode.Invalid): + validator.to_python(u'TesT') + with pytest.raises(formencode.Invalid): + validator.to_python(u'TEST') UserGroupModel().delete(gr) UserGroupModel().delete(gr2) Session().commit() @@ -71,19 +82,19 @@ def test_ValidRepoGroup(self): validator = v.ValidRepoGroup() model = RepoGroupModel() - self.assertRaises(formencode.Invalid, validator.to_python, - {'group_name': HG_REPO, }) + with pytest.raises(formencode.Invalid): + validator.to_python({'group_name': HG_REPO, }) gr = model.create(group_name=u'test_gr', group_description=u'desc', parent=None, just_db=True, owner=TEST_USER_ADMIN_LOGIN) - self.assertRaises(formencode.Invalid, - validator.to_python, {'group_name': gr.group_name, }) + with pytest.raises(formencode.Invalid): + validator.to_python({'group_name': gr.group_name, }) validator = v.ValidRepoGroup(edit=True, old_data={'group_id': gr.group_id}) - self.assertRaises(formencode.Invalid, - validator.to_python, { + with pytest.raises(formencode.Invalid): + validator.to_python({ 'group_name': gr.group_name + 'n', 'group_parent_id': gr.group_id }) @@ -91,29 +102,28 @@ def test_ValidPassword(self): validator = v.ValidPassword() - self.assertEqual('lol', validator.to_python('lol')) - self.assertEqual(None, validator.to_python(None)) - self.assertRaises(formencode.Invalid, validator.to_python, 'ąćżź') + assert 'lol' == validator.to_python('lol') + assert None == validator.to_python(None) + with pytest.raises(formencode.Invalid): + validator.to_python('ąćżź') def test_ValidPasswordsMatch(self): validator = v.ValidPasswordsMatch('new_password', 'password_confirmation') - self.assertRaises(formencode.Invalid, - validator.to_python, {'new_password': 'pass', + with pytest.raises(formencode.Invalid): + validator.to_python({'new_password': 'pass', 'password_confirmation': 'pass2'}) - self.assertRaises(formencode.Invalid, - validator.to_python, {'new_password': 'pass', + with pytest.raises(formencode.Invalid): + validator.to_python({'new_password': 'pass', 'password_confirmation': 'pass2'}) - self.assertEqual({'new_password': 'pass', - 'password_confirmation': 'pass'}, - validator.to_python({'new_password': 'pass', - 'password_confirmation': 'pass'})) + assert {'new_password': 'pass', + 'password_confirmation': 'pass'} == validator.to_python({'new_password': 'pass', + 'password_confirmation': 'pass'}) - self.assertEqual({'new_password': 'pass', - 'password_confirmation': 'pass'}, - validator.to_python({'new_password': 'pass', - 'password_confirmation': 'pass'})) + assert {'new_password': 'pass', + 'password_confirmation': 'pass'} == validator.to_python({'new_password': 'pass', + 'password_confirmation': 'pass'}) def test_ValidAuth(self): validator = v.ValidAuth() @@ -125,9 +135,9 @@ 'username': 'err', 'password': 'err', } - self.assertEqual(valid_creds, validator.to_python(valid_creds)) - self.assertRaises(formencode.Invalid, - validator.to_python, invalid_creds) + assert valid_creds == validator.to_python(valid_creds) + with pytest.raises(formencode.Invalid): + validator.to_python(invalid_creds) def test_ValidAuthToken(self): validator = v.ValidAuthToken() @@ -139,18 +149,18 @@ def test_ValidRepoName(self): validator = v.ValidRepoName() - self.assertRaises(formencode.Invalid, - validator.to_python, {'repo_name': ''}) + with pytest.raises(formencode.Invalid): + validator.to_python({'repo_name': ''}) - self.assertRaises(formencode.Invalid, - validator.to_python, {'repo_name': HG_REPO}) + with pytest.raises(formencode.Invalid): + validator.to_python({'repo_name': HG_REPO}) gr = RepoGroupModel().create(group_name=u'group_test', group_description=u'desc', parent=None, owner=TEST_USER_ADMIN_LOGIN) - self.assertRaises(formencode.Invalid, - validator.to_python, {'repo_name': gr.group_name}) + with pytest.raises(formencode.Invalid): + validator.to_python({'repo_name': gr.group_name}) #TODO: write an error case for that ie. create a repo withinh a group # self.assertRaises(formencode.Invalid, @@ -168,7 +178,7 @@ ('/]re po', 're-po')]) def test_SlugifyName(self, name, expected): validator = v.SlugifyName() - self.assertEqual(expected, validator.to_python(name)) + assert expected == validator.to_python(name) def test_ValidCloneUri(self): #TODO: write this one @@ -176,8 +186,9 @@ def test_ValidForkType(self): validator = v.ValidForkType(old_data={'repo_type': 'hg'}) - self.assertEqual('hg', validator.to_python('hg')) - self.assertRaises(formencode.Invalid, validator.to_python, 'git') + assert 'hg' == validator.to_python('hg') + with pytest.raises(formencode.Invalid): + validator.to_python('git') def test_ValidPerms(self): #TODO: write this one @@ -185,45 +196,44 @@ def test_ValidSettings(self): validator = v.ValidSettings() - self.assertEqual({'pass': 'pass'}, - validator.to_python(value={'user': 'test', - 'pass': 'pass'})) + assert {'pass': 'pass'} == validator.to_python(value={'user': 'test', + 'pass': 'pass'}) - self.assertEqual({'user2': 'test', 'pass': 'pass'}, - validator.to_python(value={'user2': 'test', - 'pass': 'pass'})) + assert {'user2': 'test', 'pass': 'pass'} == validator.to_python(value={'user2': 'test', + 'pass': 'pass'}) def test_ValidPath(self): validator = v.ValidPath() - self.assertEqual(tempfile.gettempdir(), - validator.to_python(tempfile.gettempdir())) - self.assertRaises(formencode.Invalid, validator.to_python, - '/no_such_dir') + assert tempfile.gettempdir() == validator.to_python(tempfile.gettempdir()) + with pytest.raises(formencode.Invalid): + validator.to_python('/no_such_dir') def test_UniqSystemEmail(self): validator = v.UniqSystemEmail(old_data={}) - self.assertEqual('mail@python.org', - validator.to_python('MaiL@Python.org')) + assert 'mail@python.org' == validator.to_python('MaiL@Python.org') email = TEST_USER_REGULAR2_EMAIL - self.assertRaises(formencode.Invalid, validator.to_python, email) + with pytest.raises(formencode.Invalid): + validator.to_python(email) def test_ValidSystemEmail(self): validator = v.ValidSystemEmail() email = TEST_USER_REGULAR2_EMAIL - self.assertEqual(email, validator.to_python(email)) - self.assertRaises(formencode.Invalid, validator.to_python, 'err') + assert email == validator.to_python(email) + with pytest.raises(formencode.Invalid): + validator.to_python('err') def test_LdapLibValidator(self): if ldap_lib_installed: validator = v.LdapLibValidator() - self.assertEqual("DN", validator.to_python('DN')) + assert "DN" == validator.to_python('DN') else: validator = v.LdapLibValidator() - self.assertRaises(v.LdapImportError, validator.to_python, 'err') + with pytest.raises(v.LdapImportError): + validator.to_python('err') def test_AttrLoginValidator(self): validator = v.AttrLoginValidator() - self.assertEqual('DN_attr', validator.to_python('DN_attr')) + assert 'DN_attr' == validator.to_python('DN_attr')