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

Use pytest #231

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
1c831f6
Fix Python formatting in releng.models
jelly May 22, 2020
1a16df1
Add PGP key to the Release model
jelly May 22, 2020
326b84e
mirrors: add conftest for mirrors
jelly Mar 8, 2019
407de39
mirrors: tests: convert test_models to pytest
jelly Mar 8, 2019
7a13e2e
mirrors: switch to pytest
jelly Mar 8, 2019
bb6a5f1
mirrors: tests: remove duplicate tests
jelly Mar 8, 2019
e5547f3
mirrors: convert mirrorstatus to pytest
jelly Mar 8, 2019
58cf8b1
mirrors: tests: port to pytest
jelly Mar 8, 2019
591bcf4
mirrors: tests: port to pytest
jelly Mar 8, 2019
858322c
mirrors: convert to pytest
jelly Mar 9, 2019
fca832f
mirrors: convert to pytest
jelly Mar 9, 2019
fce216e
Remove unrequired function
jelly Mar 9, 2019
14d5bfc
mirrors: ipv6 does not exists everywhere :(
jelly Mar 9, 2019
5855cda
releng: convert to pytest fixtures
jelly Mar 9, 2019
2df9544
main: convert tests to pytest
jelly Mar 9, 2019
a620fed
visualize: convert to pytest
jelly Mar 9, 2019
ac05d53
public: port to pytest
jelly Mar 9, 2019
7500b1e
news: port to pytest
jelly Mar 9, 2019
6710663
packages: port to pytest
jelly Mar 9, 2019
8245939
todolists: port to pytest
jelly Mar 9, 2019
6e7829d
packages; port to pytest
jelly Mar 9, 2019
ba55137
devel: port to pytest
jelly Mar 9, 2019
9cf6c4c
Add pytest integration files
jelly Jul 29, 2019
04835d4
Add pylint disable for import
jelly Aug 4, 2019
0f30d71
Update tests
jelly Aug 4, 2019
aaaaab9
disable coverage reporting for the django debug toolbar
jelly Aug 4, 2019
60667cc
todolists: increase coverage for todolist models
jelly Aug 4, 2019
df32e88
mirrors: add test for "smart" protocol
jelly Aug 4, 2019
4bb9496
public: add logged in index view
jelly Aug 4, 2019
40325d1
Add test for non existing dependencies
jelly Aug 4, 2019
9de5aff
conftest: remove unused import
jelly Aug 4, 2019
cbe2caa
Fix typo in makefile
jelly Aug 5, 2019
f6c4674
Hardcore settins for testing
jelly Aug 5, 2019
932e43b
Also install pytest-django
jelly Aug 5, 2019
c747659
reroll options
jelly Aug 5, 2019
3621de4
ok wtf
jelly Aug 5, 2019
1d54e2e
forgot pytest -cov
jelly Aug 5, 2019
91140ad
Add requirements_test.txt file for test depenendencies
jelly Aug 6, 2019
84a37ec
port todolists to pytest
jelly Aug 6, 2019
08a3962
Remove unused test function
jelly May 23, 2020
8b21e12
Port planet tests to pytest
jelly May 23, 2020
3d4827c
Port remainder to pytest
jelly May 23, 2020
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
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ omit =
**/tests/*.py
env*
settings.py
conftest.py
local_settings.py
/home/travis/virtualenv*
manage.py
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ env/

# coverage
.coverage
htmlcov
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ language: python
python:
- "3.7"
cache: pip
install: "pip install -r requirements.txt && pip install coveralls pylint"
install: "pip install -r requirements.txt && pip install -r requirements_test.txt"

script:
- python manage.py collectstatic --noinput
- coverage run --rcfile .coveragerc manage.py test
- pylint devel main mirrors news packages releng templates todolists visualize *.py
- make collectstatic
- make lint
- make coverage

after_success:
- coveralls
32 changes: 32 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
PYTHON>=python
PYTEST?=pytest
PYTEST_OPTIONS+=
PYTEST_INPUT?=.
PYTEST_COVERAGE_OPTIONS+=--cov-report=term-missing --cov-report=html:htmlcov --cov=.
PYTEST_PDB?=0
PYTEST_PDB_OPTIONS?=--pdb --pdbcls=IPython.terminal.debugger:TerminalPdb


ifeq (${PYTEST_PDB},1)
PYTEST_OPTIONS+= ${PYTEST_PDB_OPTIONS}
else
test-pdb: PYTEST_OPTIONS+= ${PYTEST_PDB_OPTIONS}
endif
test-pdb: test


.PHONY: test lint coverage

lint:
pylint devel main mirrors news packages releng templates todolists visualize *.py

collectstatic:
python manage.py collectstatic --noinput

test: test-py

test-py coverage:
DJANGO_SETTINGS_MODULE=settings ${PYTEST} ${PYTEST_OPTIONS} ${PYTEST_COVERAGE_OPTIONS} ${PYTEST_INPUT}

open-coverage: coverage
${BROWSER} htmlcov/index.html
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ packages, you will probably want the following:
./manage.py loaddata main/fixtures/*.json
./manage.py loaddata devel/fixtures/*.json
./manage.py loaddata mirrors/fixtures/*.json
./manage.py loaddata releng/fixtures/*.json

6. Use the following commands to start a service instance

Expand Down Expand Up @@ -96,14 +95,17 @@ In local\_settings.py add entries to set EMAIL\_HOST to 'localhost' and EMAIL\_P

# Running tests and coverage

Install the test dependencies:

pip install -r requirements_text.txt

To the unittests execute the following commands:

./manage.py collectstatic --noinput
./manage.py test

Running coverage:

pip install coverage
coverage run --omit='env*' --source='.' manage.py test
coverage report

Expand Down
60 changes: 60 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import pytest

from django.contrib.auth.models import Group
from django.core.management import call_command

from devel.models import UserProfile


USERNAME = 'joeuser'
FIRSTNAME = 'Joe'
LASTNAME = 'User'
EMAIL = 'user1@example.com'


@pytest.fixture
def arches(db):
call_command('loaddata', 'main/fixtures/arches.json')


@pytest.fixture
def repos(db):
call_command('loaddata', 'main/fixtures/repos.json')


@pytest.fixture
def package(db, arches, repos):
# TODO: convert to create_package with standard parameters
call_command('loaddata', 'main/fixtures/package.json')


@pytest.fixture
def groups(db):
call_command('loaddata', 'main/fixtures/groups.json')


@pytest.fixture
def staff_groups(db):
call_command('loaddata', 'devel/fixtures/staff_groups.json')


@pytest.fixture
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, user, userprofile, groups):
user.groups.add(Group.objects.get(name='Developers'))
client.login(username=USERNAME, password=USERNAME)
return client
48 changes: 10 additions & 38 deletions devel/tests/test_devel.py
Original file line number Diff line number Diff line change
@@ -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
51 changes: 25 additions & 26 deletions devel/tests/test_pgp_import.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from unittest.mock import patch

import pytest

from django.core.management import call_command
from django.core.management.base import CommandError
from django.test import TransactionTestCase


CREATED = 1541685162
Expand All @@ -19,28 +20,26 @@
]


class PGPImportTest(TransactionTestCase):
fixtures = ['main/fixtures/arches.json', 'main/fixtures/repos.json']

def test_pgp_import_error(self):
with self.assertRaises(CommandError) as e:
call_command('pgp_import')
self.assertIn('keyring_path', str(e.exception))

@patch('devel.management.commands.pgp_import.call_gpg')
def test_pgp_import_garbage_data(self, mock_call_gpg):
mock_call_gpg.return_value = 'barf'
with patch('devel.management.commands.pgp_import.logger') as logger:
call_command('pgp_import', '/tmp')
logger.info.assert_called()
logger.info.assert_any_call('created %d, updated %d signatures', 0, 0)
logger.info.assert_any_call('created %d, updated %d keys', 0, 0)

@patch('devel.management.commands.pgp_import.call_gpg')
def test_pgp_import(self, mock_call_gpg):
mock_call_gpg.return_value = '\n'.join(SIG_DATA)
with patch('devel.management.commands.pgp_import.logger') as logger:
call_command('pgp_import', '/tmp')
logger.info.assert_called()
logger.info.assert_any_call('created %d, updated %d signatures', 0, 0)
logger.info.assert_any_call('created %d, updated %d keys', 1, 0)
def test_pgp_import_error(arches, repos):
with pytest.raises(CommandError) as e:
call_command('pgp_import')
assert 'keyring_path' in str(e.value)


@patch('devel.management.commands.pgp_import.call_gpg')
def test_pgp_import_garbage_data(mock_call_gpg, arches, repos):
mock_call_gpg.return_value = 'barf'
with patch('devel.management.commands.pgp_import.logger') as logger:
call_command('pgp_import', '/tmp')
logger.info.assert_called()
logger.info.assert_any_call('created %d, updated %d signatures', 0, 0)
logger.info.assert_any_call('created %d, updated %d keys', 0, 0)

@patch('devel.management.commands.pgp_import.call_gpg')
def test_pgp_import(mock_call_gpg, arches, repos):
mock_call_gpg.return_value = '\n'.join(SIG_DATA)
with patch('devel.management.commands.pgp_import.logger') as logger:
call_command('pgp_import', '/tmp')
logger.info.assert_called()
logger.info.assert_any_call('created %d, updated %d signatures', 0, 0)
logger.info.assert_any_call('created %d, updated %d keys', 1, 0)
13 changes: 4 additions & 9 deletions devel/tests/test_rematch_developers.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
from unittest.mock import patch


from django.core.management import call_command
from django.test import TransactionTestCase


class RematchDeveloperTest(TransactionTestCase):
fixtures = ['main/fixtures/arches.json', 'main/fixtures/repos.json']

def test_rematch_developers(self):
with patch('devel.management.commands.rematch_developers.logger') as logger:
call_command('rematch_developers')
logger.info.assert_called()
def test_rematch_developers(arches, repos):
with patch('devel.management.commands.rematch_developers.logger') as logger:
call_command('rematch_developers')
logger.info.assert_called()
Loading