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

Change test runner to pytest #73

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ include AUTHORS
include CHANGELOG.md
include LICENSE
include README.md
include run_tests.py
include django_enumfield/py.typed
global-exclude *.py[cod] __pycache__ *.so
12 changes: 4 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: test
test:
python setup.py test
pytest $(test)

.PHONY: flake8
flake8:
Expand All @@ -12,15 +12,15 @@ mypy:

.PHONY: isort
isort:
isort -rc django_enumfield run_tests.py setup.py
isort -rc django_enumfield setup.py

.PHONY: black
black:
black django_enumfield run_tests.py setup.py
black django_enumfield setup.py

.PHONY: black-check
black-check:
black --check django_enumfield run_tests.py setup.py
black --check django_enumfield setup.py

.PHONY: checks
checks: mypy flake8 black-check
Expand All @@ -36,10 +36,6 @@ install:
develop:
python setup.py develop

.PHONY: coverage
coverage:
coverage run --include=django_enumfield/* setup.py test

.PHONY: clean
clean:
rm -rf build dist .tox/ *.egg *.egg-info .coverage* .eggs
Expand Down
5 changes: 3 additions & 2 deletions django_enumfield/enum.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import absolute_import

import logging
import enum
from typing import Any, List, Optional, Sequence, Tuple, TypeVar, Union, cast, Mapping
import logging
from typing import Any, List, Mapping, Optional, Sequence, Tuple, TypeVar, Union, cast

from django.utils.encoding import force_str

try:
Expand Down
62 changes: 62 additions & 0 deletions django_enumfield/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import os
from pathlib import Path

import django
import pytest

SECRET_KEY = "secret"
INSTALLED_APPS = [
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.admin",
"django.contrib.sessions",
"django.contrib.messages",
"django_enumfield",
"django_enumfield.tests",
]
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"APP_DIRS": True,
"OPTIONS": {
"debug": True, # Raise template errors
},
}
]
DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}}
ROOT_URLCONF = "django_enumfield.tests.urls"
DEBUG = True
MIDDLEWARE = [
"django.middleware.common.CommonMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
]


def pytest_configure(config):
from django.conf import settings

settings.configure(
SECRET_KEY=SECRET_KEY,
INSTALLED_APPS=INSTALLED_APPS,
TEMPLATES=TEMPLATES,
DATABASES=DATABASES,
ROOT_URLCONF=ROOT_URLCONF,
DEBUG=DEBUG,
MIDDLEWARE=MIDDLEWARE,
)

django.setup()


@pytest.fixture(scope="session", autouse=True)
def delete_migrations():
migrations_dir = Path(__file__).parent / "migrations"
if migrations_dir.exists() and migrations_dir.is_dir():
os.system("rm -r " + str(migrations_dir))

yield

if migrations_dir.exists() and migrations_dir.is_dir():
os.system("rm -r " + str(migrations_dir))
12 changes: 9 additions & 3 deletions django_enumfield/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
from django_enumfield.enum import Enum


class BaseModel(models.Model):
class Meta:
app_label = "tests"
abstract = True


class LampState(Enum):
OFF = 0
ON = 1

__default__ = OFF


class Lamp(models.Model):
class Lamp(BaseModel):
state = EnumField(LampState, verbose_name="stately_state")


Expand Down Expand Up @@ -41,7 +47,7 @@ class PersonStatusDefault(Enum):
__default__ = UNBORN


class Person(models.Model):
class Person(BaseModel):
example = models.CharField(max_length=100, default="foo")
status = EnumField(PersonStatus, default=PersonStatus.ALIVE)

Expand Down Expand Up @@ -78,7 +84,7 @@ def get_default_beer_label():
return LabelBeer.JUPILER


class Beer(models.Model):
class Beer(BaseModel):
style = EnumField(BeerStyle)
state = EnumField(BeerState, null=True, blank=True)
label = EnumField(LabelBeer, default=get_default_beer_label)
12 changes: 0 additions & 12 deletions django_enumfield/tests/test_settings.py

This file was deleted.

2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins =
mypy_django_plugin.main

[mypy.plugins.django-stubs]
django_settings_module = django_enumfield.tests.test_settings
django_settings_module = django_enumfield.tests.conftest

[mypy-*.migrations.*]
ignore_errors = True
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[build-system]
requires = ["setuptools>=57.0.0", "wheel"]
build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]
python_files = "tests.py test_*.py *_tests.py"
testpaths = ["django_enumfield"]
pythonpath = "django_enumfield"
addopts = "--cov --no-cov-on-fail"
107 changes: 0 additions & 107 deletions run_tests.py

This file was deleted.

25 changes: 18 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from distutils.command.install import INSTALL_SCHEMES
from distutils.command.install_data import install_data
from io import open
from pathlib import Path

try:
from setuptools import setup
Expand Down Expand Up @@ -51,7 +52,10 @@ def fullsplit(path, result=None):
elif filenames:
data_files.append([dirpath, [os.path.join(dirpath, f) for f in filenames]])

version = __import__("django_enumfield").__version__
root_init = Path(__file__).resolve().parent / "django_enumfield" / "__init__.py"
init_globals = {}
exec(root_init.read_text(), init_globals)
version = init_globals["__version__"]

with open(os.path.join(root_dir, "README.md"), encoding="utf-8") as f:
description = f.read()
Expand Down Expand Up @@ -91,17 +95,24 @@ def fullsplit(path, result=None):
data_files=data_files,
packages=packages,
include_package_data=True,
tests_require=[
"Django",
"djangorestframework",
],
install_requires=["Django>=2.2"],
python_requires=">=3.7",
zip_safe=False,
test_suite="run_tests.main",
extras_require={
"test": [
"djangorestframework",
"pytest",
"pytest-cov",
"pytest-django",
],
"dev": [
"Django",
"djangorestframework",
"pytest",
"pytest-cov",
"pytest-django",
"black",
"isort",
"Django",
"mypy",
"django-stubs",
"djangorestframework-stubs",
Expand Down
8 changes: 3 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ envlist =
[testenv]
whitelist_externals = make
deps=
.[test]
django22: Django>=2.2,<2.3
django30: Django>=3.0b1,<3.1
django31: Django>=3.1,<3.2
Expand All @@ -21,17 +22,14 @@ basepython = python3.7
passenv = TOXENV CI
whitelist_externals = make
deps =
.[test]
Django>=2.2,<2.3
coverage==4.5.2
flake8
python-coveralls
mypy
django-stubs
djangorestframework-stubs
black
setenv =
COVERALLS_REPO_TOKEN=LdECqqwg7eelQx9w8gvooUZCFIaCqGZCv
commands =
make checks coverage
coverage report
coveralls --ignore-errors
make checks