From 3d4827c0232766a26267b73b602c571ae7acfeda Mon Sep 17 00:00:00 2001 From: Jelle van der Waa Date: Sat, 23 May 2020 23:09:55 +0200 Subject: [PATCH] Port remainder to pytest --- conftest.py | 30 +- devel/tests/test_devel.py | 48 +--- devel/tests/test_reporead.py | 161 ++++++----- devel/tests/test_reports.py | 125 ++++---- devel/tests/test_user.py | 300 +++++++++++--------- mirrors/tests/conftest.py | 21 +- mirrors/tests/test_mirrorrsync.py | 39 ++- packages/tests/test_adoptorphan_packages.py | 18 +- packages/tests/test_unflag.py | 22 +- todolists/tests/test_models.py | 4 +- 10 files changed, 391 insertions(+), 377 deletions(-) diff --git a/conftest.py b/conftest.py index 704a8477..064f0e17 100644 --- a/conftest.py +++ b/conftest.py @@ -23,12 +23,9 @@ def repos(db): @pytest.fixture -def package(db): - # TODO(jelle): create own parameter based version - from main.models import Package - print(list(Package.objects.all())) +def package(db, arches, repos): + # TODO: convert to create_package with standard parameters call_command('loaddata', 'main/fixtures/package.json') - print(list(Package.objects.all())) @pytest.fixture @@ -41,22 +38,23 @@ def staff_groups(db): call_command('loaddata', 'devel/fixtures/staff_groups.json') -# TODO: test with non-admin user fixture @pytest.fixture -def admin_user_profile(admin_user, arches, repos): - profile = UserProfile.objects.create(user=admin_user, - public_email="public@archlinux.org") +def user(django_user_model): + user = django_user_model.objects.create_user(username=USERNAME, password=USERNAME) + yield user + user.delete() + + +@pytest.fixture +def userprofile(user): + profile = UserProfile.objects.create(user=user, + public_email=f'{user.username}@archlinux.org') yield profile profile.delete() @pytest.fixture -def user_client(client, django_user_model, groups): - user = django_user_model.objects.create_user(username=USERNAME, password=USERNAME) - profile = UserProfile.objects.create(user=user, - public_email="{}@archlinux.org".format(user.username)) +def user_client(client, user, userprofile, groups): user.groups.add(Group.objects.get(name='Developers')) client.login(username=USERNAME, password=USERNAME) - yield client - profile.delete() - user.delete() + return client diff --git a/devel/tests/test_devel.py b/devel/tests/test_devel.py index c8a28c1a..6835456b 100644 --- a/devel/tests/test_devel.py +++ b/devel/tests/test_devel.py @@ -1,42 +1,14 @@ -from django.test import TransactionTestCase -from django.contrib.auth.models import User, Group -from devel.models import UserProfile +def test_clock(user_client): + response = user_client.get('/devel/clock/') + assert response.status_code == 200 -class DevelView(TransactionTestCase): - fixtures = ['main/fixtures/arches.json', 'main/fixtures/repos.json', - 'main/fixtures/package.json'] +def test_profile(user_client): + response = user_client.get('/devel/profile/') + assert response.status_code == 200 + # Test changing - def setUp(self): - password = 'test' - self.user = User.objects.create_superuser('admin', - 'admin@archlinux.org', - password) - for name in ['Developers', 'Retired Developers']: - Group.objects.create(name=name) - self.user.groups.add(Group.objects.get(name='Developers')) - self.user.save() - self.profile = UserProfile.objects.create(user=self.user, - public_email="{}@awesome.com".format(self.user.username)) - self.client.post('/login/', { - 'username': self.user.username, - 'password': password - }) - def tearDown(self): - self.profile.delete() - self.user.delete() - Group.objects.all().delete() - - def test_clock(self): - response = self.client.get('/devel/clock/') - self.assertEqual(response.status_code, 200) - - def test_profile(self): - response = self.client.get('/devel/profile/') - self.assertEqual(response.status_code, 200) - # Test changing - - def test_stats(self): - response = self.client.get('/devel/stats/') - self.assertEqual(response.status_code, 200) +def test_stats(user_client): + response = user_client.get('/devel/stats/') + assert response.status_code == 200 diff --git a/devel/tests/test_reporead.py b/devel/tests/test_reporead.py index 0ffe3a17..d6eefd86 100644 --- a/devel/tests/test_reporead.py +++ b/devel/tests/test_reporead.py @@ -2,10 +2,10 @@ from unittest.mock import patch from datetime import datetime +import pytest from django.core.management import call_command from django.core.management.base import CommandError -from django.test import TransactionTestCase from django.utils import timezone @@ -13,82 +13,95 @@ from packages.models import FlagRequest -# Django's TestCase is wrapped in transaction, therefore use TransactionTestCase -class RepoReadTest(TransactionTestCase): - fixtures = ['main/fixtures/arches.json', 'main/fixtures/repos.json'] +@pytest.fixture +def create_pkg(arches, repos): + packages = [] - def create_pkg(self, repo=None, pkgver='4.10.1', pkgrel='1'): + def _create_pkg(repo=None, pkgver='4.10.1', pkgrel='1'): if not repo: repo = Repo.objects.get(name__iexact='core') arch = Arch.objects.get(name__iexact='any') now = datetime.now(tz=timezone.utc) - return Package.objects.create(arch=arch, repo=repo, pkgname='systemd', - pkgbase='systemd', pkgver=pkgver, - pkgrel=pkgrel, pkgdesc='Linux kernel', - compressed_size=10, installed_size=20, - last_update=now, created=now) - - def test_invalid_args(self): - with self.assertRaises(CommandError) as e: - call_command('reporead') - self.assertIn('missing arch and file.', str(e.exception)) - - with self.assertRaises(CommandError) as e: - call_command('reporead', 'x86_64') - self.assertIn('Package database file is required.', str(e.exception)) - - with self.assertRaises(CommandError) as e: - call_command('reporead', 'x86_64', 'nothing.db.tar.gz') - self.assertIn('Specified package database file does not exist.', str(e.exception)) - - def test_invalid_arch(self): - with self.assertRaises(CommandError) as e: - call_command('reporead', 'armv64', 'devel/fixtures/core.db.tar.gz') - self.assertEqual('Specified architecture armv64 is not currently known.', str(e.exception)) - - def test_read_packages(self): - with patch('devel.management.commands.reporead.logger') as logger: - call_command('reporead', 'x86_64', 'devel/fixtures/core.db.tar.gz') - logger.info.assert_called() - - # Verify contents - with tarfile.open('devel/fixtures/core.db.tar.gz') as tar: - files = [name.replace('core.db/', '') for name in tar.getnames() if name != 'core.db' and not 'desc' in name] - - packages = Package.objects.all() - import_packages = ["{}-{}-{}".format(pkg.pkgname, pkg.pkgver, pkg.pkgrel) for pkg in packages] - self.assertCountEqual(files, import_packages) - - def test_flagoutofdate(self): - pkg = self.create_pkg() - FlagRequest.objects.create(pkgbase=pkg.pkgbase, repo=pkg.repo, - pkgver=pkg.pkgver, epoch=pkg.epoch, - ip_address='1.1.1.1') - - with patch('devel.management.commands.reporead.logger') as logger: - call_command('reporead', 'x86_64', 'devel/fixtures/core.db.tar.gz') - logger.info.assert_called() - - self.assertEqual(len(FlagRequest.objects.all()), 0) - - def test_flagoutofdate_staging(self): - staging = Repo.objects.get(name__iexact='staging') - - pkg = self.create_pkg() - staging_pkg = self.create_pkg(repo=staging, pkgrel='2') - - FlagRequest.objects.create(pkgbase=pkg.pkgbase, repo=pkg.repo, - pkgver=pkg.pkgver, epoch=pkg.epoch, - ip_address='1.1.1.1') - FlagRequest.objects.create(pkgbase=staging_pkg.pkgbase, repo=staging_pkg.repo, - pkgver=staging_pkg.pkgver, epoch=staging_pkg.epoch, - ip_address='1.1.1.1') - - with patch('devel.management.commands.reporead.logger') as logger: - call_command('reporead', 'x86_64', 'devel/fixtures/core.db.tar.gz') - logger.info.assert_called() - - objects = FlagRequest.objects.all() - self.assertEqual(len(objects), 1) - self.assertEqual(objects[0].pkgver, staging_pkg.pkgver) + package = Package.objects.create(arch=arch, repo=repo, pkgname='systemd', + pkgbase='systemd', pkgver=pkgver, + pkgrel=pkgrel, pkgdesc='Linux kernel', + compressed_size=10, installed_size=20, + last_update=now, created=now) + packages.append(package) + return package + + yield _create_pkg + + for package in packages: + package.delete() + + +def test_invalid_args(): + with pytest.raises(CommandError) as excinfo: + call_command('reporead') + assert 'missing arch and file.' in str(excinfo) + + with pytest.raises(CommandError) as excinfo: + call_command('reporead', 'x86_64') + assert 'Package database file is required.' in str(excinfo) + + with pytest.raises(CommandError) as excinfo: + call_command('reporead', 'x86_64', 'nothing.db.tar.gz') + assert 'Specified package database file does not exist.' in str(excinfo) + + +def test_invalid_arch(transactional_db, arches, repos): + with pytest.raises(CommandError) as excinfo: + call_command('reporead', 'armv64', 'devel/fixtures/core.db.tar.gz') + assert 'Specified architecture armv64 is not currently known.' in str(excinfo) + + +# TODO: create pacman repo db with a pytest fixture +def test_read_packages(transactional_db, arches, repos): + with patch('devel.management.commands.reporead.logger') as logger: + call_command('reporead', 'x86_64', 'devel/fixtures/core.db.tar.gz') + logger.info.assert_called() + + # Verify contents + with tarfile.open('devel/fixtures/core.db.tar.gz') as tar: + files = [name.replace('core.db/', '') for name in tar.getnames() if name != 'core.db' and not 'desc' in name] + + packages = Package.objects.all() + import_packages = ["{}-{}-{}".format(pkg.pkgname, pkg.pkgver, pkg.pkgrel) for pkg in packages] + assert len(files) == len(import_packages) + + +def test_flagoutofdate(transactional_db, create_pkg): + pkg = create_pkg() + FlagRequest.objects.create(pkgbase=pkg.pkgbase, repo=pkg.repo, + pkgver=pkg.pkgver, epoch=pkg.epoch, + ip_address='1.1.1.1') + + with patch('devel.management.commands.reporead.logger') as logger: + call_command('reporead', 'x86_64', 'devel/fixtures/core.db.tar.gz') + logger.info.assert_called() + + assert not len(FlagRequest.objects.all()) + + +def test_flagoutofdate_staging(transactional_db, create_pkg): + staging = Repo.objects.get(name__iexact='staging') + + pkg = create_pkg() + staging_pkg = create_pkg(repo=staging, pkgrel='2') + + FlagRequest.objects.create(pkgbase=pkg.pkgbase, repo=pkg.repo, + pkgver=pkg.pkgver, epoch=pkg.epoch, + ip_address='1.1.1.1') + FlagRequest.objects.create(pkgbase=staging_pkg.pkgbase, repo=staging_pkg.repo, + pkgver=staging_pkg.pkgver, epoch=staging_pkg.epoch, + ip_address='1.1.1.1') + + with patch('devel.management.commands.reporead.logger') as logger: + call_command('reporead', 'x86_64', 'devel/fixtures/core.db.tar.gz') + logger.info.assert_called() + + objects = FlagRequest.objects.all() + assert len(objects) == 1 + assert objects[0].pkgver == staging_pkg.pkgver diff --git a/devel/tests/test_reports.py b/devel/tests/test_reports.py index 1fea1c4c..0e37f519 100644 --- a/devel/tests/test_reports.py +++ b/devel/tests/test_reports.py @@ -1,64 +1,61 @@ -from django.test import TransactionTestCase -from django.contrib.auth.models import User - - -class DeveloperReport(TransactionTestCase): - fixtures = ['main/fixtures/arches.json', 'main/fixtures/repos.json', - 'main/fixtures/package.json'] - - def setUp(self): - password = 'test' - self.user = User.objects.create_superuser('admin', - 'admin@archlinux.org', - password) - self.client.post('/login/', { - 'username': self.user.username, - 'password': password - }) - - def tearDown(self): - self.user.delete() - - def test_overview(self): - response = self.client.get('/devel/') - self.assertEqual(response.status_code, 200) - - def test_reports_old(self): - response = self.client.get('/devel/reports/old', follow=True) - self.assertEqual(response.status_code, 200) - - def test_reports_outofdate(self): - response = self.client.get('/devel/reports/long-out-of-date', follow=True) - self.assertEqual(response.status_code, 200) - - def test_reports_big(self): - response = self.client.get('/devel/reports/big', follow=True) - self.assertEqual(response.status_code, 200) - - def test_reports_badcompression(self): - response = self.client.get('/devel/reports/badcompression', follow=True) - self.assertEqual(response.status_code, 200) - - def test_reports_uncompressed_man(self): - response = self.client.get('/devel/reports/uncompressed-man', follow=True) - self.assertEqual(response.status_code, 200) - - def test_reports_uncompressed_info(self): - response = self.client.get('/devel/reports/uncompressed-info', follow=True) - self.assertEqual(response.status_code, 200) - - def test_reports_unneeded_orphans(self): - response = self.client.get('/devel/reports/unneeded-orphans', follow=True) - self.assertEqual(response.status_code, 200) - - def test_reports_mismatched_signature(self): - response = self.client.get('/devel/reports/mismatched-signature', follow=True) - self.assertEqual(response.status_code, 200) - - def test_reports_signature_time(self): - response = self.client.get('/devel/reports/signature-time', follow=True) - self.assertEqual(response.status_code, 200) - - def test_non_existing_dependencies(self): - response = self.client.get('/devel/reports/non-existing-dependencies', follow=True) - self.assertEqual(response.status_code, 200) +import pytest + + +@pytest.fixture +def report_client(user_client, arches, repos, package): + return user_client + + +def test_overview(report_client): + response = report_client.get('/devel/') + assert response.status_code == 200 + + +def test_reports_old(report_client): + response = report_client.get('/devel/reports/old', follow=True) + assert response.status_code == 200 + + +def test_reports_outofdate(report_client): + response = report_client.get('/devel/reports/long-out-of-date', follow=True) + assert response.status_code == 200 + + +def test_reports_big(report_client): + response = report_client.get('/devel/reports/big', follow=True) + assert response.status_code == 200 + + +def test_reports_badcompression(report_client): + response = report_client.get('/devel/reports/badcompression', follow=True) + assert response.status_code == 200 + + +def test_reports_uncompressed_man(report_client): + response = report_client.get('/devel/reports/uncompressed-man', follow=True) + assert response.status_code == 200 + + +def test_reports_uncompressed_info(report_client): + response = report_client.get('/devel/reports/uncompressed-info', follow=True) + assert response.status_code == 200 + + +def test_reports_unneeded_orphans(report_client): + response = report_client.get('/devel/reports/unneeded-orphans', follow=True) + assert response.status_code == 200 + + +def test_reports_mismatched_signature(report_client): + response = report_client.get('/devel/reports/mismatched-signature', follow=True) + assert response.status_code == 200 + + +def test_reports_signature_time(report_client): + response = report_client.get('/devel/reports/signature-time', follow=True) + assert response.status_code == 200 + + +def test_non_existing_dependencies(report_client): + response = report_client.get('/devel/reports/non-existing-dependencies', follow=True) + assert response.status_code == 200 diff --git a/devel/tests/test_user.py b/devel/tests/test_user.py index 91c0b1e6..6f434824 100644 --- a/devel/tests/test_user.py +++ b/devel/tests/test_user.py @@ -1,135 +1,177 @@ -from django.contrib.auth.models import User -from django.test import TestCase +import pytest + +from django.contrib.auth.models import User from devel.utils import UserFinder from devel.models import UserProfile -class DevelTest(TestCase): - def test_index(self): - response = self.client.get('/devel/') - self.assertEqual(response.status_code, 302) - self.assertEqual(response.has_header('Location'), True) - self.assertEqual(response['location'], - '/login/?next=/devel/') - - def test_profile(self): - response = self.client.get('/devel/profile/') - self.assertEqual(response.status_code, 302) - self.assertEqual(response.has_header('Location'), True) - self.assertEqual(response['location'], - '/login/?next=/devel/profile/') - - def test_newuser(self): - response = self.client.get('/devel/newuser/') - self.assertEqual(response.status_code, 302) - self.assertEqual(response.has_header('Location'), True) - self.assertEqual(response['location'], - '/login/?next=/devel/newuser/') - - def test_mirrors(self): - response = self.client.get('/mirrors/') - self.assertEqual(response.status_code, 200) - - def test_admin_log(self): - User.objects.create_superuser('admin', 'admin@archlinux.org', 'admin') - response = self.client.post('/login/', {'username': 'admin', 'password': 'admin'}) - response = self.client.get('/devel/admin_log', follow=True) - self.assertEqual(response.status_code, 200) - -class FindUserTest(TestCase): - - def setUp(self): - self.finder = UserFinder() - - self.user1 = User.objects.create(username="joeuser", first_name="Joe", - last_name="User", email="user1@example.com") - self.user2 = User.objects.create(username="john", first_name="John", - last_name="", email="user2@example.com") - self.user3 = User.objects.create(username="bjones", first_name="Bob", - last_name="Jones", email="user3@example.com") - - for user in (self.user1, self.user2, self.user3): - email_addr = "%s@awesome.com" % user.username - UserProfile.objects.create(user=user, public_email=email_addr) - - self.user4 = User.objects.create(username="tim1", first_name="Tim", - last_name="One", email="tim@example.com") - self.user5 = User.objects.create(username="tim2", first_name="Tim", - last_name="Two", email="timtwo@example.com") - - def test_not_matching(self): - self.assertIsNone(self.finder.find(None)) - self.assertIsNone(self.finder.find("")) - self.assertIsNone(self.finder.find("Bogus")) - self.assertIsNone(self.finder.find("Bogus ")) - self.assertIsNone(self.finder.find("")) - self.assertIsNone(self.finder.find("bogus@example.com")) - self.assertIsNone(self.finder.find("Unknown Packager")) - - def test_by_email(self): - self.assertEqual(self.user1, - self.finder.find("XXX YYY ")) - self.assertEqual(self.user2, - self.finder.find("YYY ZZZ ")) - - def test_by_profile_email(self): - self.assertEqual(self.user1, - self.finder.find("XXX ")) - self.assertEqual(self.user2, - self.finder.find("YYY ")) - self.assertEqual(self.user3, - self.finder.find("ZZZ ")) - - def test_by_name(self): - self.assertEqual(self.user1, - self.finder.find("Joe User ")) - self.assertEqual(self.user1, - self.finder.find("Joe User")) - self.assertEqual(self.user2, - self.finder.find("John ")) - self.assertEqual(self.user2, - self.finder.find("John")) - self.assertEqual(self.user3, - self.finder.find("Bob Jones ")) - - def test_by_invalid(self): - self.assertEqual(self.user1, - self.finder.find("Joe User ")) - self.assertEqual(self.user3, - self.finder.find("Bob Jones ")) - - def test_ambiguous(self): - self.assertEqual(self.user4, - self.finder.find("Tim One ")) - self.assertEqual(self.user5, - self.finder.find("Tim Two ")) - self.assertIsNone(self.finder.find("Tim ")) - - def test_find_by_username(self): - self.assertEqual(self.finder.find_by_username(None), None) - self.assertEqual(self.finder.find_by_username('noone'), None) - self.assertEqual(self.finder.find_by_username(self.user1.username), self.user1) - # Test cache - self.assertEqual(self.finder.find_by_username(self.user1.username), self.user1) - - def test_find_by_email(self): - self.assertEqual(self.finder.find_by_email(None), None) - self.assertEqual(self.finder.find_by_email('bar@bar.com'), None) - self.assertEqual(self.finder.find_by_email(self.user1.email), self.user1) - # Test cache - self.assertEqual(self.finder.find_by_email(self.user1.email), self.user1) + +def test_index(client): + response = client.get('/devel/') + assert response.status_code == 302 + assert response.has_header('Location') + assert response['location'] == '/login/?next=/devel/' + + +def test_profile(client): + response = client.get('/devel/profile/') + assert response.status_code == 302 + assert response.has_header('Location') + assert response['location'] == '/login/?next=/devel/profile/' + + +def test_newuser(client): + response = client.get('/devel/newuser/') + assert response.status_code == 302 + assert response.has_header('Location') + assert response['location'] == '/login/?next=/devel/newuser/' + + +def test_mirrors(client, db): + response = client.get('/mirrors/') + assert response.status_code == 200 + + +def test_admin_log(admin_client): + response = admin_client.get('/devel/admin_log', follow=True) + assert response.status_code == 200 + + +@pytest.fixture +def user1(django_user_model): + user1 = django_user_model.objects.create(username="joeuser", first_name="Joe", + last_name="User", email="user1@example.com") + email_addr = "%s@awesome.com" % user1.username + profile = UserProfile.objects.create(user=user1, public_email=email_addr) + yield user1 + profile.delete() + user1.delete() + + +@pytest.fixture +def create_user(django_user_model): + users = [] + + def _create_user(username, firstname, lastname="", email="", public_email=""): + user = User.objects.create(username=username, first_name=firstname, + last_name=lastname, email=email) + profile = None + + if public_email: + profile = UserProfile.objects.create(user=user, public_email=public_email) + + users.append((user, profile)) + return user + + yield _create_user + + for user, prof in users: + if prof: + prof.delete() + user.delete() + + +@pytest.fixture() +def users(create_user): + create_user('joeuser', 'Joe', 'User', 'user1@example.com', 'joeuser@awesome.com') + create_user('john', 'John', '', 'user2@example.com', 'john@awesome.com') + create_user('bjones', 'Bob', 'Jones', 'user3@example.com', 'bjones@awesome.com') + + create_user('tin1', 'Tim', 'One', 'tim@example.com') + create_user('tin2', 'Tim', 'Two', 'timtwo@example.com') + + +@pytest.fixture() +def finder(): + return UserFinder() + + +def test_not_matching(finder, users): + + assert not finder.find(None) + assert not finder.find("") + assert not finder.find("Bogus") + assert not finder.find("Bogus ") + assert not finder.find("") + assert not finder.find("bogus@example.com") + assert not finder.find("Unknown Packager") + + +def test_by_email(finder, create_user): + user1 = create_user('joeuser', 'Joe', 'User', 'user1@example.com', 'joeuser@awesome.com') + user2 = create_user('john', 'John', '', 'user2@example.com', 'john@example.com') + + assert user1 == finder.find("XXX YYY ") + assert user2 == finder.find("YYY ZZZ ") + + +def test_by_profile_email(finder, create_user): + user1 = create_user('joeuser', 'Joe', 'User', 'user1@example.com', 'joeuser@awesome.com') + user2 = create_user('john', 'John', '', 'user2@example.com', 'john@awesome.com') + user3 = create_user('bjones', 'Bob', 'Jones', 'user3@example.com', 'bjones@awesome.com') + + assert user1 == finder.find("XXX ") + assert user2 == finder.find("YYY ") + assert user3 == finder.find("ZZZ ") + + +def test_by_name(finder, create_user): + user1 = create_user('joeuser', 'Joe', 'User', 'user1@example.com', 'joeuser@awesome.com') + user2 = create_user('john', 'John', '', 'user2@example.com', 'john@awesome.com') + user3 = create_user('bjones', 'Bob', 'Jones', 'user3@example.com', 'bjones@awesome.com') + + assert user1 == finder.find("Joe User ") + assert user1 == finder.find("Joe User") + assert user2 == finder.find("John ") + assert user2 == finder.find("John") + assert user3 == finder.find("Bob Jones ") + + +def test_by_invalid(finder, create_user): + user1 = create_user('joeuser', 'Joe', 'User', 'user1@example.com', 'joeuser@awesome.com') + + assert user1 == finder.find("Joe User ") + assert user3 == finder.find("Bob Jones ") + + +def test_ambiguous(finder, create_user): + user4 = create_user('tin1', 'Tim', 'One', 'tim@example.com') + user5 = create_user('tin2', 'Tim', 'Two', 'timtwo@example.com') + assert user4 == finder.find("Tim One ") + assert user5 == finder.find("Tim Two ") + assert not finder.find("Tim ") + + +def test_find_by_username(finder, create_user): + user1 = create_user('joeuser', 'Joe', 'User', 'user1@example.com', 'joeuser@awesome.com') + + assert not finder.find_by_username(None) + assert not finder.find_by_username('noone') + assert user1 == finder.find_by_username(user1.username) + # Test cache + assert user1 == finder.find_by_username(user1.username) + + +def test_find_by_email(finder, create_user): + user1 = create_user('joeuser', 'Joe', 'User', 'user1@example.com', 'joeuser@awesome.com') + + assert not finder.find_by_email(None) + assert not finder.find_by_email('bar@bar.com') + assert user1 == finder.find_by_username(user1.username) + # Test cache + assert user1 == finder.find_by_username(user1.username) # vim: set ts=4 sw=4 et: diff --git a/mirrors/tests/conftest.py b/mirrors/tests/conftest.py index 72bf5947..24028d8d 100644 --- a/mirrors/tests/conftest.py +++ b/mirrors/tests/conftest.py @@ -14,10 +14,10 @@ @pytest.fixture def mirror(db, name=NAME, admin_email=ADMIN_EMAIL): - mirror = Mirror.objects.create(name=name, - admin_email=admin_email) - yield mirror - mirror.delete() + mirror = Mirror.objects.create(name=name, + admin_email=admin_email) + yield mirror + mirror.delete() @pytest.fixture @@ -39,17 +39,18 @@ def mirrorprotocol(db, protocol=PROTOCOL): @pytest.fixture def mirrorurl(db, mirror, mirrorprotocol, country=COUNTRY, url=URL): - mirror_url = MirrorUrl.objects.create(url=url, - protocol=mirrorprotocol, - mirror=mirror, - country=country) - yield mirror_url - mirror_url.delete() + mirror_url = MirrorUrl.objects.create(url=url, + protocol=mirrorprotocol, + mirror=mirror, + country=country) + yield mirror_url + mirror_url.delete() @pytest.fixture def create_mirrorurl(db, mirror, mirrorprotocol): mirrors = [] + def _create_mirrorurl(country=COUNTRY, url=URL): mirror_url = MirrorUrl.objects.create(url=url, protocol=mirrorprotocol, diff --git a/mirrors/tests/test_mirrorrsync.py b/mirrors/tests/test_mirrorrsync.py index 31dc325d..9f18fedd 100644 --- a/mirrors/tests/test_mirrorrsync.py +++ b/mirrors/tests/test_mirrorrsync.py @@ -1,30 +1,23 @@ -from django.test import TransactionTestCase +import pytest -from mirrors.models import MirrorRsync, Mirror +from mirrors.models import MirrorRsync -TEST_IPV6 = "2a0b:4342:1a31:410::" -TEST_IPV4 = "8.8.8.8" +def test_invalid(transactional_db, mirror): + with pytest.raises(ValueError) as excinfo: + MirrorRsync.objects.create(ip="8.8.8.8.8", mirror=mirror) + assert 'IPv4 Address with more than 4 bytes' in str(excinfo) -class MirrorRsyncTest(TransactionTestCase): - def setUp(self): - self.mirror = Mirror.objects.create(name='rmirror', - admin_email='foo@bar.com') - def tearDown(self): - self.mirror.delete() +def test_ipv6(transactional_db, mirror): + ipv6 = "2a0b:4342:1a31:410::" + mirrorrsync = MirrorRsync.objects.create(ip=ipv6, mirror=mirror) + assert str(mirrorrsync) == ipv6 + mirrorrsync.delete() - def test_ipv6(self): - mirrorrsync = MirrorRsync.objects.create(ip=TEST_IPV6, mirror=self.mirror) - self.assertEqual(str(mirrorrsync), TEST_IPV6) - mirrorrsync.delete() - def test_ipv4(self): - mirrorrsync = MirrorRsync.objects.create(ip=TEST_IPV4, mirror=self.mirror) - self.assertEqual(str(mirrorrsync), TEST_IPV4) - mirrorrsync.delete() - - def test_invalid(self): - with self.assertRaises(ValueError) as e: - MirrorRsync.objects.create(ip="8.8.8.8.8", mirror=self.mirror) - self.assertIn('IPv4 Address with more than 4 bytes', str(e.exception)) +def test_ipv4(transactional_db, mirror): + ipv4 = "8.8.8.8" + mirrorrsync = MirrorRsync.objects.create(ip=ipv4, mirror=mirror) + assert str(mirrorrsync) == ipv4 + mirrorrsync.delete() diff --git a/packages/tests/test_adoptorphan_packages.py b/packages/tests/test_adoptorphan_packages.py index 1f086049..8d9bda9b 100644 --- a/packages/tests/test_adoptorphan_packages.py +++ b/packages/tests/test_adoptorphan_packages.py @@ -14,33 +14,33 @@ def request(client, pkgid, adopt=True): def test_wrong_request(admin_client, arches, repos, package): - # TODO(jelle): fix + # TODO(jelle): fix pkg = Package.objects.first() response = admin_client.post('/packages/update/', {'pkgid': pkg.id, }, follow=True) assert response.status_code == 200 assert 'Are you trying to adopt or disown' in response.content.decode() -def test_stale_relations(admin_user_profile, admin_client, arches, repos, package): - response = admin_client.get('/packages/stale_relations/') +def test_stale_relations(user_client, arches, repos, package): + response = user_client.get('/packages/stale_relations/') assert response.status_code == 200 -def test_no_permissions(admin_client, admin_user_profile, arches, repos, package): +def test_no_permissions(user_client, arches, repos, package): pkg = Package.objects.first() - response = request(admin_client, pkg.id) + response = request(user_client, pkg.id) assert response.status_code == 200 assert len(PackageRelation.objects.all()) == 0 -def test_adopt_package(admin_client, admin_user_profile, arches, repos, package): - admin_user_profile.allowed_repos.add(Repo.objects.get(name='Core')) +def test_adopt_package(user_client, userprofile, arches, repos, package): + userprofile.allowed_repos.add(Repo.objects.get(name='Core')) pkg = Package.objects.first() - response = request(admin_client, pkg.id) + response = request(user_client, pkg.id) assert response.status_code == 200 assert len(PackageRelation.objects.all()) == 1 - response = request(admin_client, pkg.id, False) + response = request(user_client, pkg.id, False) assert response.status_code == 200 assert len(PackageRelation.objects.all()) == 0 diff --git a/packages/tests/test_unflag.py b/packages/tests/test_unflag.py index 077fbc32..16724bb1 100644 --- a/packages/tests/test_unflag.py +++ b/packages/tests/test_unflag.py @@ -6,29 +6,27 @@ def assert_flag_package(client): } # TODO: hardcoded package name - response = client.post('/packages/core/x86_64/linux/flag/', - data, - follow=True) + response = client.post('/packages/core/x86_64/linux/flag/', data, follow=True) assert response.status_code == 200 -def test_unflag_package_404(admin_user_profile, admin_client): - response = admin_client.get('/packages/core/x86_64/fooobar/unflag/') +def test_unflag_package_404(user_client): + response = user_client.get('/packages/core/x86_64/fooobar/unflag/') assert response.status_code == 404 - response = admin_client.get('/packages/core/x86_64/fooobar/unflag/all/') + response = user_client.get('/packages/core/x86_64/fooobar/unflag/all/') assert response.status_code == 404 -def test_unflag_package(admin_user_profile, admin_client, package): - assert_flag_package(admin_client) - response = admin_client.get('/packages/core/x86_64/linux/unflag/', follow=True) +def test_unflag_package(user_client, package): + assert_flag_package(user_client) + response = user_client.get('/packages/core/x86_64/linux/unflag/', follow=True) assert response.status_code == 200 assert 'Flag linux as out-of-date' in response.content.decode() -def test_unflag_all_package(admin_user_profile, admin_client, package): - assert_flag_package(admin_client) - response = admin_client.get('/packages/core/x86_64/linux/unflag/all/', follow=True) +def test_unflag_all_package(user_client, package): + assert_flag_package(user_client) + response = user_client.get('/packages/core/x86_64/linux/unflag/all/', follow=True) assert response.status_code == 200 assert 'Flag linux as out-of-date' in response.content.decode() diff --git a/todolists/tests/test_models.py b/todolists/tests/test_models.py index db62cc96..61ed458d 100644 --- a/todolists/tests/test_models.py +++ b/todolists/tests/test_models.py @@ -1,10 +1,10 @@ -from todolists.tests.conftest import NAME, RAW +from todolists.tests.conftest import NAME def test_stripped_description(todolist): todolist.description = 'Boost rebuild ' desc = todolist.stripped_description - assert desc.endswith(' ') == False + assert not desc.endswith(' ') def test_get_absolute_url(todolist):