Skip to content
This repository was archived by the owner on Aug 19, 2022. It is now read-only.

Commit ab84160

Browse files
author
Thiago C. D'Ávila
authored
Merge pull request #49 from staticdev/test-refactor
Testing workflow and session
2 parents 5331845 + cf5a470 commit ab84160

File tree

10 files changed

+182
-13
lines changed

10 files changed

+182
-13
lines changed

.github/workflows/tests.yml

+58-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
name: Tests
22

3-
on: [push, pull_request]
3+
on:
4+
- push
5+
- pull_request
46

57
jobs:
68
tests:
7-
name: Tests (${{ matrix.python-version }}, ${{ matrix.os }})
9+
name: ${{ matrix.session }} ${{ matrix.python-version }} / ${{ matrix.os }}
810
runs-on: ${{ matrix.os }}
911
strategy:
1012
fail-fast: false
1113
matrix:
1214
include:
13-
- { python-version: 3.8, os: ubuntu-latest }
14-
# - { python-version: 3.8, os: windows-latest }
15-
- { python-version: 3.8, os: macos-latest }
16-
- { python-version: 3.7, os: ubuntu-latest }
15+
- { python-version: 3.8, os: ubuntu-latest, session: "pre-commit" }
16+
- { python-version: 3.8, os: ubuntu-latest, session: "safety" }
17+
- { python-version: 3.8, os: ubuntu-latest, session: "mypy-3.8" }
18+
- { python-version: 3.7, os: ubuntu-latest, session: "mypy-3.7" }
19+
- { python-version: 3.8, os: ubuntu-latest, session: "tests-3.8" }
20+
- { python-version: 3.7, os: ubuntu-latest, session: "tests-3.7" }
21+
# - { python-version: 3.8, os: windows-latest, session: "tests-3.8" }
22+
- { python-version: 3.8, os: macos-latest, session: "tests-3.8" }
23+
# - { python-version: 3.8, os: ubuntu-latest, session: "docs" }
24+
25+
env:
26+
NOXSESSION: ${{ matrix.session }}
27+
1728
steps:
1829
- name: Check out the repository
19-
uses: actions/[email protected].0
30+
uses: actions/[email protected].1
2031

2132
- name: Set up Python ${{ matrix.python-version }}
2233
uses: actions/setup-python@v2
@@ -38,6 +49,46 @@ jobs:
3849
pip install --constraint=.github/workflows/constraints.txt nox
3950
nox --version
4051
52+
- name: Compute pre-commit cache key
53+
if: matrix.session == 'pre-commit'
54+
id: pre-commit-cache
55+
shell: python
56+
run: |
57+
import hashlib
58+
import sys
59+
60+
python = "py{}.{}".format(*sys.version_info[:2])
61+
payload = sys.version.encode() + sys.executable.encode()
62+
digest = hashlib.sha256(payload).hexdigest()
63+
result = "${{ runner.os }}-{}-{}-pre-commit".format(python, digest[:8])
64+
65+
print("::set-output name=result::{}".format(result))
66+
67+
- name: Restore pre-commit cache
68+
uses: actions/[email protected]
69+
if: matrix.session == 'pre-commit'
70+
with:
71+
path: ~/.cache/pre-commit
72+
key: ${{ steps.pre-commit-cache.outputs.result }}-${{ hashFiles('.pre-commit-config.yaml') }}
73+
restore-keys: |
74+
${{ steps.pre-commit-cache.outputs.result }}-
75+
4176
- name: Run Nox
4277
run: |
4378
nox --force-color
79+
80+
# - name: Upload documentation
81+
# if: matrix.session == 'docs'
82+
# uses: actions/upload-artifact@v2
83+
# with:
84+
# name: docs
85+
# path: docs/_build
86+
87+
- name: Create coverage report
88+
if: always() && matrix.session == 'tests'
89+
run: |
90+
nox --force-color --session=coverage -- xml
91+
92+
- name: Upload coverage report
93+
if: always() && matrix.session == 'tests'
94+
uses: codecov/[email protected]

codecov.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
comment: false
2+
coverage:
3+
status:
4+
project:
5+
default:
6+
target: "0"
7+
patch:
8+
default:
9+
target: "0"

noxfile.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
package = "django_sorting_bootstrap"
1212
python_versions = ["3.8", "3.7"]
13-
nox.options.sessions = "pre-commit", "safety", "mypy" # , "tests"
13+
nox.options.sessions = "pre-commit", "safety", "mypy", "tests"
1414
locations = "src", "tests", "noxfile.py"
1515

1616

@@ -138,9 +138,11 @@ def mypy(session: Session) -> None:
138138
def tests(session: Session) -> None:
139139
"""Run the test suite."""
140140
install_package(session)
141-
install(session, "coverage[toml]", "pytest")
142-
session.run("coverage", "run", "--parallel", "-m", "pytest", *session.posargs)
143-
session.notify("coverage")
141+
install(session, "coverage[toml]", "pytest", "pytest-django")
142+
try:
143+
session.run("coverage", "run", "--parallel", "-m", "pytest", *session.posargs)
144+
finally:
145+
session.notify("coverage")
144146

145147

146148
@nox.session

poetry.lock

+21-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+11
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ codecov = "^2.1.3"
4040
reorder-python-imports = "^2.3.1"
4141
pre-commit = "^2.4.0"
4242
coverage = {extras = ["toml"], version = "^5.1"}
43+
pytest-django = "^3.9.0"
44+
45+
[tool.coverage.paths]
46+
source = ["src", "*/site-packages"]
47+
48+
[tool.coverage.run]
49+
branch = true
50+
source = ["django_sorting_bootstrap"]
51+
52+
[tool.coverage.report]
53+
show_missing = true
4354

4455
[build-system]
4556
requires = ["poetry>=0.12"]

pytest.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
DJANGO_SETTINGS_MODULE = tests.testapp.settings

tests/manage.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.settings")
9+
try:
10+
from django.core.management import execute_from_command_line
11+
except ImportError as exc:
12+
raise ImportError(
13+
"Couldn't import Django. Are you sure it's installed and "
14+
"available on your PYTHONPATH environment variable? Did you "
15+
"forget to activate a virtual environment?"
16+
) from exc
17+
execute_from_command_line(sys.argv)
18+
19+
20+
if __name__ == "__main__":
21+
main()

tests/test_views.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""Views test module."""
2+
from django.test import TestCase
3+
4+
import django_sorting_bootstrap.views
5+
6+
7+
class SimpleChangeListTests(TestCase):
8+
def test_create(self):
9+
django_sorting_bootstrap.views.SimpleChangeList(
10+
"request", "model", "list_display"
11+
)

tests/testapp/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Test app for the django-pagination-bootstrap package."""

tests/testapp/settings.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Django test settings."""
2+
import os
3+
4+
SECRET_KEY = "$%ffv#zpca%a#bdxtl&)&=5k20egnwcwjdg665r-lsr+s5zdw#"
5+
6+
APP_NAME = "django_sorting_bootstrap"
7+
8+
DEBUG = True
9+
10+
DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3"}}
11+
12+
USE_TZ = True
13+
14+
SITE_ID = 1
15+
16+
INSTALLED_APPS = (
17+
"django.contrib.auth",
18+
"django.contrib.contenttypes",
19+
"django.contrib.sessions",
20+
"django.contrib.sites",
21+
APP_NAME,
22+
)
23+
24+
TEMPLATES = [
25+
{
26+
"BACKEND": "django.template.backends.django.DjangoTemplates",
27+
"DIRS": [os.path.join(os.path.dirname(__file__), "templates")],
28+
"OPTIONS": {
29+
"context_processors": [
30+
"django.template.context_processors.debug",
31+
"django.template.context_processors.request",
32+
"django.contrib.auth.context_processors.auth",
33+
"django.template.context_processors.i18n",
34+
"django.template.context_processors.media",
35+
],
36+
"loaders": [
37+
"django.template.loaders.filesystem.Loader",
38+
"django.template.loaders.app_directories.Loader",
39+
],
40+
},
41+
}
42+
]

0 commit comments

Comments
 (0)