Skip to content

Commit

Permalink
Fix bug 1455246 - Improve pytest environment. (#972)
Browse files Browse the repository at this point in the history
* Fix bug 1455246 - Improve pytest environment.

This commit moves the pytest configuration into a more discoverable file (pytest.ini), and changes it to run tests in a more standard manner. Running pytest now runs all django tests in the pontoon folder (and not tests in /tests) and reuses the database between runs. It also enables passing files and folders to the pytest command.

* Moved batch tests back to pontoon.

* Moved teams tests back to pontoon.

* Fixed teams tests.

* Moved machinery tests back to pontoon.

* Fix machinery tests.

* Moved tags tests back to pontoon.

* Moved some fixture files.

* Migrated the site_matrix fixture over, moved it to a dedicated test app.

* Load base fixtures for all tests.

* Fix tags tests.

* Run migrations every time, as migrations create data that is required by some tests.

* Remove django test runnner.

* Fixed PEP8 violations, added a missing init file.

* Moved base tests back to pontoon.

* Fix base tests.

* Moved checks tests back to pontoon.

* Fix checks tests.

* Refactor fixtures to avoid code duplication.

* Removed remains of old pytest directory.

* Use factories from test app.

* Moved final checks test to pontoon.

* Address review comments.
  • Loading branch information
adngdb committed Jun 4, 2018
1 parent e28a473 commit 30c81b0
Show file tree
Hide file tree
Showing 104 changed files with 4,749 additions and 4,372 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Expand Up @@ -38,8 +38,7 @@ script:
- ./node_modules/.bin/webpack
- python manage.py collectstatic -v0 --noinput
- npm test
- python manage.py test --with-coverage
- py.test --cov-append --cov-report=term --cov=. -v
- py.test --cov-append --cov-report=term --cov=. -v --create-db --migrations
- ./node_modules/.bin/eslint .
- codecov

Expand Down
10 changes: 2 additions & 8 deletions docker/run_tests.sh
Expand Up @@ -17,7 +17,6 @@ echo ""
echo "--------------------------------------------------------------------------------------------"
echo "Linting Python code"
$PYLAMA pontoon
$PYLAMA tests

echo ""
echo "--------------------------------------------------------------------------------------------"
Expand All @@ -36,10 +35,5 @@ $NPM test

echo ""
echo "--------------------------------------------------------------------------------------------"
echo "Running Python tests with django"
$PYTHON manage.py test

echo ""
echo "--------------------------------------------------------------------------------------------"
echo "Running Python tests with pytest"
$PYTEST
echo "Running Python tests"
$PYTEST --cov-append --cov-report=term --cov=.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,23 +1,21 @@
import pytest

from pontoon.base.models import (
PermissionChangelog
)
from pontoon.base.forms import (
LocalePermsForm,
ProjectLocalePermsForm
)
from pontoon.base.models import PermissionChangelog


@pytest.mark.django_db
def test_locale_perms_form_log_no_changes(user0, locale0):
def test_locale_perms_form_log_no_changes(user_a, locale_a):
form = LocalePermsForm(
{
'translators': [],
'managers': []
},
instance=locale0,
user=user0
instance=locale_a,
user=user_a
)
assert form.is_valid()

Expand All @@ -27,13 +25,13 @@ def test_locale_perms_form_log_no_changes(user0, locale0):


@pytest.mark.django_db
def test_project_locale_perms_form_log_no_changes(user0, locale0):
def test_project_locale_perms_form_log_no_changes(user_a, locale_a):
form = ProjectLocalePermsForm(
{
'translators': [],
},
instance=locale0,
user=user0
instance=locale_a,
user=user_a,
)
assert form.is_valid()

Expand All @@ -43,15 +41,17 @@ def test_project_locale_perms_form_log_no_changes(user0, locale0):


@pytest.mark.django_db
def test_locale_perms_form_log(locale0, user0, user1, userX, assert_permissionchangelog):
def test_locale_perms_form_log(
locale_a, user_a, user_b, user_c, assert_permissionchangelog
):
# Add new users to groups
form = LocalePermsForm(
{
'translators': [userX.pk],
'managers': [user1.pk]
'translators': [user_c.pk],
'managers': [user_b.pk],
},
instance=locale0,
user=user0
instance=locale_a,
user=user_a,
)

assert form.is_valid()
Expand All @@ -62,60 +62,64 @@ def test_locale_perms_form_log(locale0, user0, user1, userX, assert_permissionch
assert_permissionchangelog(
changelog_entry0,
'added',
user0,
userX,
locale0.translators_group
user_a,
user_c,
locale_a.translators_group,
)

assert_permissionchangelog(
changelog_entry1,
'added',
user0,
user1,
locale0.managers_group
user_a,
user_b,
locale_a.managers_group,
)

# Remove items from groups
form = LocalePermsForm(
{
'translators': [],
'managers': []
'managers': [],
},
instance=locale0,
user=user0
instance=locale_a,
user=user_a,
)

assert form.is_valid()
form.save()

changelog_entry3, changelog_entry2 = PermissionChangelog.objects.order_by('-pk')[:2]
changelog_entry3, changelog_entry2 = (
PermissionChangelog.objects.order_by('-pk')[:2]
)

assert_permissionchangelog(
changelog_entry2,
'removed',
user0,
userX,
locale0.translators_group
user_a,
user_c,
locale_a.translators_group,
)

assert_permissionchangelog(
changelog_entry3,
'removed',
user0,
user1,
locale0.managers_group
user_a,
user_b,
locale_a.managers_group,
)


@pytest.mark.django_db
def test_project_locale_perms_form_log(locale0, user0, user1, userX, assert_permissionchangelog):
def test_project_locale_perms_form_log(
locale_a, user_a, user_b, user_c, assert_permissionchangelog
):
# Add new users to groups
form = ProjectLocalePermsForm(
{
'translators': [userX.pk],
'translators': [user_c.pk],
},
instance=locale0,
user=user0
instance=locale_a,
user=user_a,
)

assert form.is_valid()
Expand All @@ -126,19 +130,19 @@ def test_project_locale_perms_form_log(locale0, user0, user1, userX, assert_perm
assert_permissionchangelog(
changelog_entry0,
'added',
user0,
userX,
locale0.translators_group
user_a,
user_c,
locale_a.translators_group,
)

# Remove items from groups
form = ProjectLocalePermsForm(
{
'translators': [],
'managers': []
'managers': [],
},
instance=locale0,
user=user0
instance=locale_a,
user=user_a,
)

assert form.is_valid()
Expand All @@ -149,7 +153,7 @@ def test_project_locale_perms_form_log(locale0, user0, user1, userX, assert_perm
assert_permissionchangelog(
changelog_entry1,
'removed',
user0,
userX,
locale0.translators_group
user_a,
user_c,
locale_a.translators_group,
)
File renamed without changes.

0 comments on commit 30c81b0

Please sign in to comment.