Skip to content

Commit

Permalink
mitigate runtests.py fail when GDAL library is not installed #945 #821
Browse files Browse the repository at this point in the history
 #775 #777

will coventiently make ``./runtests.py`` brush over system library
requirements to help contributors run the test suite locally without
installing extra stuff.

full tox run will require it though.
  • Loading branch information
tfranzel committed Feb 25, 2023
1 parent 21068e0 commit 4fde93e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest

PYTEST_ARGS = {
'default': ['tests'],
'default': ['tests', '--allow-skip-extra-system-req'],
'fast': ['tests', '-q'],
}

Expand Down
12 changes: 12 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,15 @@ def get_response_schema(operation, status=None, content_type='application/json')

def get_request_schema(operation, content_type='application/json'):
return operation['requestBody']['content'][content_type]['schema']


def is_gis_installed():
# only load GIS if library is installed. This is required for the GIS test to work
from django.core.exceptions import ImproperlyConfigured

try:
from django.contrib.gis.gdal import gdal_version # noqa: F401
except ImproperlyConfigured:
return False
else:
return True
18 changes: 17 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import pytest
from django.core import management

from tests import is_gis_installed


def pytest_configure(config):
from django.conf import settings
Expand All @@ -20,13 +22,16 @@ def pytest_configure(config):
'allauth.account',
'oauth2_provider',
'django_filters',
'rest_framework_gis',
# this is not strictly required and when added django-polymorphic
# currently breaks the whole Django/DRF upstream testing.
# 'polymorphic',
# 'rest_framework_jwt',
]

# only load GIS if library is installed. This is required for the GIS test to work
if is_gis_installed():
contrib_apps.append('rest_framework_gis')

base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

settings.configure(
Expand Down Expand Up @@ -118,18 +123,29 @@ def pytest_addoption(parser):
default=False,
help="run contrib tests but allow them to fail"
)
parser.addoption(
"--allow-skip-extra-system-req",
action="store_true",
default=False,
help=""
)


def pytest_collection_modifyitems(config, items):
skip_missing_contrib = pytest.mark.skip(reason="skip tests for missing contrib package")
allow_contrib_fail = pytest.mark.xfail(reason="contrib test were allowed to fail")
allow_sys_requirement_fail = pytest.mark.xfail(reason="may fail due to missing system library")

for item in items:
for marker in item.own_markers:
if marker.name == 'contrib' and config.getoption("--skip-missing-contrib"):
if not all([module_available(module_str) for module_str in marker.args]): # pragma: no cover
item.add_marker(skip_missing_contrib)
if marker.name == 'contrib' and config.getoption("--allow-contrib-fail"):
item.add_marker(allow_contrib_fail)
if marker.name == 'system_requirement_fulfilled' and config.getoption("--allow-skip-extra-system-req"):
if not marker.args[0]:
item.add_marker(allow_sys_requirement_fail)


@pytest.fixture()
Expand Down
4 changes: 3 additions & 1 deletion tests/contrib/test_rest_framework_gis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
from rest_framework import mixins, routers, serializers, viewsets

from drf_spectacular.utils import extend_schema_serializer
from tests import assert_schema, generate_schema
from tests import assert_schema, generate_schema, is_gis_installed


@pytest.mark.contrib('rest_framework_gis')
@pytest.mark.system_requirement_fulfilled(is_gis_installed())
@pytest.mark.skipif(DRF_VERSION < '3.12', reason='DRF pagination schema broken')
@mock.patch('drf_spectacular.settings.spectacular_settings.ENUM_NAME_OVERRIDES', {})
def test_rest_framework_gis(no_warnings, clear_caches):
Expand Down Expand Up @@ -90,6 +91,7 @@ class PlainViewset(mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.Ge


@pytest.mark.contrib('rest_framework_gis', 'django_filter')
@pytest.mark.system_requirement_fulfilled(is_gis_installed())
@mock.patch('drf_spectacular.settings.spectacular_settings.ENUM_NAME_OVERRIDES', {})
def test_geo_filter_set(no_warnings):
from django.contrib.gis.db.models import PointField
Expand Down
5 changes: 5 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ exclude_lines =
if __name__ == .__main__.:
if TYPE_CHECKING:

[pytest]
markers =
contrib: marks upstream package dependency
system_requirement_fulfilled: marks system library dependency.

[flake8]
ignore =
# line break before binary operator
Expand Down

0 comments on commit 4fde93e

Please sign in to comment.