# HG changeset patch # User Thomas De Schampheleire # Date 2016-04-26 20:00:39 # Node ID be1d20bfd2ddffd837c844d8aa1b428433025ee8 # Parent 91b89797a775ccd68c9710967532d4813779b317 pytest migration: model: convert all tests to TestControllerPytest The model tests were based on BaseTestCase which does not exist currently for pytest-style tests. Nevertheless, there seems to be no advantage of directly subclassing BaseTestCase over subclassing TestControllerPytest. Thus, keep things simple and use TestControllerPytest. diff --git a/kallithea/tests/models/test_changeset_status.py b/kallithea/tests/models/test_changeset_status.py --- a/kallithea/tests/models/test_changeset_status.py +++ b/kallithea/tests/models/test_changeset_status.py @@ -7,12 +7,12 @@ class CSM(object): # ChangesetStatusMock def __init__(self, status): self.status = status -class TestChangesetStatusCalculation(BaseTestCase): +class TestChangesetStatusCalculation(TestControllerPytest): - def setUp(self): + def setup_method(self, method): self.m = ChangesetStatusModel() - @parameterized.expand([ + @parametrize('name,expected_result,statuses', [ ('empty list', CS.STATUS_UNDER_REVIEW, []), ('approve', CS.STATUS_APPROVED, [CSM(CS.STATUS_APPROVED)]), ('approve2', CS.STATUS_APPROVED, [CSM(CS.STATUS_APPROVED), CSM(CS.STATUS_APPROVED)]), diff --git a/kallithea/tests/models/test_diff_parsers.py b/kallithea/tests/models/test_diff_parsers.py --- a/kallithea/tests/models/test_diff_parsers.py +++ b/kallithea/tests/models/test_diff_parsers.py @@ -271,9 +271,9 @@ DIFF_FIXTURES = { } -class DiffLibTest(BaseTestCase): +class TestDiffLib(TestControllerPytest): - @parameterized.expand([(x,) for x in DIFF_FIXTURES]) + @parametrize('diff_fixture', DIFF_FIXTURES) def test_diff(self, diff_fixture): diff = fixture.load_resource(diff_fixture, strip=False) vcs = 'hg' diff --git a/kallithea/tests/models/test_notifications.py b/kallithea/tests/models/test_notifications.py --- a/kallithea/tests/models/test_notifications.py +++ b/kallithea/tests/models/test_notifications.py @@ -7,9 +7,9 @@ from kallithea.model.meta import Session from kallithea.model.notification import NotificationModel -class TestNotifications(BaseTestCase): +class TestNotifications(TestControllerPytest): - def __init__(self, methodName='runTest'): + def setup_method(self, method): Session.remove() self.u1 = UserModel().create_or_update(username=u'u1', password=u'qweqwe', @@ -32,9 +32,6 @@ class TestNotifications(BaseTestCase): Session().commit() self.u3 = self.u3.user_id - super(TestNotifications, self).__init__(methodName=methodName) - - def setUp(self): remove_all_notifications() self.assertEqual([], Notification.query().all()) self.assertEqual([], UserNotification.query().all()) diff --git a/kallithea/tests/models/test_permissions.py b/kallithea/tests/models/test_permissions.py --- a/kallithea/tests/models/test_permissions.py +++ b/kallithea/tests/models/test_permissions.py @@ -15,18 +15,16 @@ from kallithea.model.permission import P fixture = Fixture() -class TestPermissions(BaseTestCase): - def __init__(self, methodName='runTest'): - super(TestPermissions, self).__init__(methodName=methodName) +class TestPermissions(TestControllerPytest): @classmethod - def setUpClass(cls): + def setup_class(cls): #recreate default user to get a clean start PermissionModel().create_default_permissions(user=User.DEFAULT_USER, force=True) Session().commit() - def setUp(self): + def setup_method(self, method): self.u1 = UserModel().create_or_update( username=u'u1', password=u'qweqwe', email=u'u1@example.com', firstname=u'u1', lastname=u'u1' @@ -46,7 +44,7 @@ class TestPermissions(BaseTestCase): ) Session().commit() - def tearDown(self): + def teardown_method(self, method): if hasattr(self, 'test_repo'): RepoModel().delete(repo=self.test_repo) @@ -716,7 +714,7 @@ class TestPermissions(BaseTestCase): PermissionModel().create_default_permissions(user=self.u1) self._test_def_perm_equal(user=self.u1) - @parameterized.expand([ + @parametrize('perm,modify_to', [ ('repository.read', 'repository.none'), ('group.read', 'group.none'), ('usergroup.read', 'usergroup.none'), diff --git a/kallithea/tests/models/test_repo_groups.py b/kallithea/tests/models/test_repo_groups.py --- a/kallithea/tests/models/test_repo_groups.py +++ b/kallithea/tests/models/test_repo_groups.py @@ -33,14 +33,14 @@ def _update_repo(name, **kwargs): return r -class TestRepoGroups(BaseTestCase): +class TestRepoGroups(TestControllerPytest): - def setUp(self): + def setup_method(self, method): self.g1 = fixture.create_repo_group(u'test1', skip_if_exists=True) self.g2 = fixture.create_repo_group(u'test2', skip_if_exists=True) self.g3 = fixture.create_repo_group(u'test3', skip_if_exists=True) - def tearDown(self): + def teardown_method(self, method): Session.remove() def __check_path(self, *path): diff --git a/kallithea/tests/models/test_repos.py b/kallithea/tests/models/test_repos.py --- a/kallithea/tests/models/test_repos.py +++ b/kallithea/tests/models/test_repos.py @@ -9,12 +9,9 @@ from kallithea.lib.exceptions import Att fixture = Fixture() -class TestRepos(BaseTestCase): +class TestRepos(TestControllerPytest): - def setUp(self): - pass - - def tearDown(self): + def teardown_method(self, method): Session.remove() def test_remove_repo(self): @@ -34,6 +31,10 @@ class TestRepos(BaseTestCase): Session().commit() self.assertRaises(AttachedForksError, lambda: RepoModel().delete(repo=repo)) + # cleanup + RepoModel().delete(repo=u'test-repo-fork-1') + RepoModel().delete(repo=u'test-repo-1') + Session().commit() def test_remove_repo_delete_forks(self): repo = fixture.create_repo(name=u'test-repo-1') diff --git a/kallithea/tests/models/test_user_groups.py b/kallithea/tests/models/test_user_groups.py --- a/kallithea/tests/models/test_user_groups.py +++ b/kallithea/tests/models/test_user_groups.py @@ -1,6 +1,6 @@ from kallithea.model.db import User -from kallithea.tests import BaseTestCase, parameterized, TEST_USER_REGULAR_LOGIN +from kallithea.tests import * from kallithea.tests.fixture import Fixture from kallithea.model.user_group import UserGroupModel @@ -10,15 +10,15 @@ from kallithea.model.meta import Session fixture = Fixture() -class TestUserGroups(BaseTestCase): +class TestUserGroups(TestControllerPytest): - def tearDown(self): + def teardown_method(self, method): # delete all groups for gr in UserGroupModel.get_all(): fixture.destroy_user_group(gr) Session().commit() - @parameterized.expand([ + @parametrize('pre_existing,regular_should_be,external_should_be,groups,expected', [ ([], [], [], [], []), ([], [u'regular'], [], [], [u'regular']), # no changes of regular ([u'some_other'], [], [], [u'some_other'], []), # not added to regular group diff --git a/kallithea/tests/models/test_users.py b/kallithea/tests/models/test_users.py --- a/kallithea/tests/models/test_users.py +++ b/kallithea/tests/models/test_users.py @@ -11,12 +11,13 @@ from kallithea.tests.fixture import Fixt fixture = Fixture() -class TestUser(BaseTestCase): - def __init__(self, methodName='runTest'): +class TestUser(TestControllerPytest): + + @classmethod + def setup_class(cls): Session.remove() - super(TestUser, self).__init__(methodName=methodName) - def tearDown(self): + def teardown_method(self, method): Session.remove() def test_create_and_remove(self): @@ -98,18 +99,15 @@ class TestUser(BaseTestCase): Session().commit() -class TestUsers(BaseTestCase): +class TestUsers(TestControllerPytest): - def __init__(self, methodName='runTest'): - super(TestUsers, self).__init__(methodName=methodName) - - def setUp(self): + def setup_method(self, method): self.u1 = UserModel().create_or_update(username=u'u1', password=u'qweqwe', email=u'u1@example.com', firstname=u'u1', lastname=u'u1') - def tearDown(self): + def teardown_method(self, method): perm = Permission.query().all() for p in perm: UserModel().revoke_perm(self.u1, p)