From fbc3023b5dfb641797397ed579ac7270830ad77a Mon Sep 17 00:00:00 2001 From: orsinium Date: Wed, 3 Jan 2018 19:35:42 +0500 Subject: [PATCH] tox improvements --- djburger/utils.py | 2 + run_tests.py | 35 ++++++---- tests/{django => django_tests}/__init__.py | 0 tests/{django => django_tests}/controllers.py | 0 tests/{django => django_tests}/parsers.py | 0 tests/{django => django_tests}/renderers.py | 0 tests/{django => django_tests}/validators.py | 0 tests/{django => django_tests}/views.py | 0 tests/{djside.py => djside_tests.py} | 0 tests/{main.py => main_tests.py} | 0 tests/marshmallow.py | 36 ---------- tests/{rest.py => rest_tests.py} | 0 tests/{pyschemes.py => side_tests.py} | 33 +++++++++ tox.ini | 70 ++++++------------- 14 files changed, 77 insertions(+), 99 deletions(-) rename tests/{django => django_tests}/__init__.py (100%) rename tests/{django => django_tests}/controllers.py (100%) rename tests/{django => django_tests}/parsers.py (100%) rename tests/{django => django_tests}/renderers.py (100%) rename tests/{django => django_tests}/validators.py (100%) rename tests/{django => django_tests}/views.py (100%) rename tests/{djside.py => djside_tests.py} (100%) rename tests/{main.py => main_tests.py} (100%) delete mode 100644 tests/marshmallow.py rename tests/{rest.py => rest_tests.py} (100%) rename tests/{pyschemes.py => side_tests.py} (53%) diff --git a/djburger/utils.py b/djburger/utils.py index 5d33b6d..eb3f574 100644 --- a/djburger/utils.py +++ b/djburger/utils.py @@ -23,6 +23,8 @@ def safe_model_to_dict(model): + if not is_django_installed: + return model if isinstance(model, Model): return model_to_dict(model) return model diff --git a/run_tests.py b/run_tests.py index c26f8c8..5539f37 100644 --- a/run_tests.py +++ b/run_tests.py @@ -9,11 +9,14 @@ import unittest2 as unittest -testing_type = sys.argv[1] if len(sys.argv) > 1 else '' +testing_type = ' '.join(sys.argv) if len(sys.argv) > 1 else '' -if not testing_type or testing_type in ('django', 'djside'): +try: import django +except ImportError: + pass +else: sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/example') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings") django.setup() @@ -22,17 +25,23 @@ import djburger # noQA -from tests import main -if not testing_type or testing_type == 'django': - from tests import django -if not testing_type or testing_type == 'djside': - from tests import djside -if not testing_type or testing_type == 'rest': - from tests import rest -if not testing_type or testing_type == 'marshmallow': - from tests import marshmallow -if not testing_type or testing_type == 'pyschemes': - from tests import pyschemes +if not testing_type: + from tests.main_tests import * + from tests.django_tests import * + from tests.djside_tests import * + from tests.rest_tests import * + from tests.side_tests import * +else: + if 'main_tests' in testing_type: + from tests import main_tests + if 'django_tests' in testing_type: + from tests import django_tests + if 'djside_tests' in testing_type: + from tests import djside_tests + if 'rest_tests' in testing_type: + from tests import rest_tests + if ' side_tests' in testing_type or testing_type.startswith('side_tests'): + from tests import side_tests if __name__ == '__main__': diff --git a/tests/django/__init__.py b/tests/django_tests/__init__.py similarity index 100% rename from tests/django/__init__.py rename to tests/django_tests/__init__.py diff --git a/tests/django/controllers.py b/tests/django_tests/controllers.py similarity index 100% rename from tests/django/controllers.py rename to tests/django_tests/controllers.py diff --git a/tests/django/parsers.py b/tests/django_tests/parsers.py similarity index 100% rename from tests/django/parsers.py rename to tests/django_tests/parsers.py diff --git a/tests/django/renderers.py b/tests/django_tests/renderers.py similarity index 100% rename from tests/django/renderers.py rename to tests/django_tests/renderers.py diff --git a/tests/django/validators.py b/tests/django_tests/validators.py similarity index 100% rename from tests/django/validators.py rename to tests/django_tests/validators.py diff --git a/tests/django/views.py b/tests/django_tests/views.py similarity index 100% rename from tests/django/views.py rename to tests/django_tests/views.py diff --git a/tests/djside.py b/tests/djside_tests.py similarity index 100% rename from tests/djside.py rename to tests/djside_tests.py diff --git a/tests/main.py b/tests/main_tests.py similarity index 100% rename from tests/main.py rename to tests/main_tests.py diff --git a/tests/marshmallow.py b/tests/marshmallow.py deleted file mode 100644 index 273da39..0000000 --- a/tests/marshmallow.py +++ /dev/null @@ -1,36 +0,0 @@ -# built-in -from __main__ import unittest, djburger -# external -import marshmallow - - -class MarshmallowValidatorsTest(unittest.TestCase): - - def test_base_validator(self): - # BASE - class Base(djburger.v.b.Marshmallow): - name = marshmallow.fields.Str() - mail = marshmallow.fields.Email() - with self.subTest(src_text='base pass'): - data = {'name': 'John Doe', 'mail': 'test@gmail.com'} - v = Base(request=None, data=data) - self.assertTrue(v.is_valid()) - with self.subTest(src_text='base not pass'): - data = {'name': 'John Doe', 'mail': 'test.gmail.com'} - v = Base(request=None, data=data) - self.assertFalse(v.is_valid()) - - def test_wrapper_validator(self): - class Base(marshmallow.Schema): - name = marshmallow.fields.Str() - mail = marshmallow.fields.Email() - - Wrapped = djburger.v.w.Marshmallow(Base) # noQA - with self.subTest(src_text='base pass'): - data = {'name': 'John Doe', 'mail': 'test@gmail.com'} - v = Wrapped(request=None, data=data) - self.assertTrue(v.is_valid()) - with self.subTest(src_text='base not pass'): - data = {'name': 'John Doe', 'mail': 'test.gmail.com'} - v = Wrapped(request=None, data=data) - self.assertFalse(v.is_valid()) diff --git a/tests/rest.py b/tests/rest_tests.py similarity index 100% rename from tests/rest.py rename to tests/rest_tests.py diff --git a/tests/pyschemes.py b/tests/side_tests.py similarity index 53% rename from tests/pyschemes.py rename to tests/side_tests.py index 0df9fec..eb76d61 100644 --- a/tests/pyschemes.py +++ b/tests/side_tests.py @@ -1,9 +1,42 @@ # built-in from __main__ import unittest, djburger # external +import marshmallow from pyschemes import Scheme as PySchemes +class MarshmallowValidatorsTest(unittest.TestCase): + + def test_base_validator(self): + # BASE + class Base(djburger.v.b.Marshmallow): + name = marshmallow.fields.Str() + mail = marshmallow.fields.Email() + with self.subTest(src_text='base pass'): + data = {'name': 'John Doe', 'mail': 'test@gmail.com'} + v = Base(request=None, data=data) + self.assertTrue(v.is_valid()) + with self.subTest(src_text='base not pass'): + data = {'name': 'John Doe', 'mail': 'test.gmail.com'} + v = Base(request=None, data=data) + self.assertFalse(v.is_valid()) + + def test_wrapper_validator(self): + class Base(marshmallow.Schema): + name = marshmallow.fields.Str() + mail = marshmallow.fields.Email() + + Wrapped = djburger.v.w.Marshmallow(Base) # noQA + with self.subTest(src_text='base pass'): + data = {'name': 'John Doe', 'mail': 'test@gmail.com'} + v = Wrapped(request=None, data=data) + self.assertTrue(v.is_valid()) + with self.subTest(src_text='base not pass'): + data = {'name': 'John Doe', 'mail': 'test.gmail.com'} + v = Wrapped(request=None, data=data) + self.assertFalse(v.is_valid()) + + class PySchemesValidatorsTest(unittest.TestCase): def test_base_validator(self): diff --git a/tox.ini b/tox.ini index ee171b2..2cfd5cf 100644 --- a/tox.ini +++ b/tox.ini @@ -1,71 +1,41 @@ [tox] envlist = - # main - py{2,3}-main, - # django - py2-django-django{18,110,111}, - py3-django-django{18,110,111,20,master}, - # djside - py2-djside-django{18,110,111}, - py3-djside-django{18,110,111,20,master}, - # side - py2-side-django{18,110,111}, - py3-side-django{18,110,111,20,master}, - # other + py{2,3}-{MAIN,SIDE}, + py2-django{18,110,111}-{DJSIDE,DJANGO}, + py3-django{18,110,111,20,master}-{DJSIDE,DJANGO}, flake8, -[testenv:main] -commands = python run_tests.py main -envdir = {toxworkdir}/venvs/{envname} -setenv = - PYTHONDONTWRITEBYTECODE=1 - PYTHONWARNINGS=once -deps = - py2: unittest2 - -rrequirements/main.txt - - -[testenv:django] -commands = python run_tests.py django +[testenv] +commands = + MAIN: python run_tests.py main_tests + SIDE: python run_tests.py side_tests + DJANGO: python run_tests.py django_tests + DJSIDE: python run_tests.py djside_tests rest_tests envdir = {toxworkdir}/venvs/{envname} setenv = PYTHONDONTWRITEBYTECODE=1 PYTHONWARNINGS=once deps = + # required + six # django django18: Django>=1.8,<1.9 django110: Django>=1.10,<1.11 django111: Django>=1.11,<1.12 django20: Django>=2.0,<2.1 djangomaster: https://github.com/django/django/archive/master.tar.gz - # testing - py2: unittest2 - -rrequirements/main.txt - - -[testenv:djside] -commands = python run_tests.py djside -envdir = {toxworkdir}/venvs/{envname} -setenv = - PYTHONDONTWRITEBYTECODE=1 - PYTHONWARNINGS=once -deps = + # rest django{110,111,20,master}: djangorestframework django18: djangorestframework>=3.5,<3.6 + # other + DJANGO: bson + {DJANGO,DJSIDE,SIDE}: marshmallow + {DJANGO,DJSIDE,SIDE}: pyschemes + DJANGO: PyYAML + DJANGO: tablib + # testing py2: unittest2 - -rrequirements/testing-side.txt - - -[testenv:side] -commands = python run_tests.py marshmallow pyschemes -envdir = {toxworkdir}/venvs/{envname} -setenv = - PYTHONDONTWRITEBYTECODE=1 - PYTHONWARNINGS=once -deps = - py2: unittest2 - -rrequirements/testing-side.txt [testenv:flake8] @@ -74,5 +44,5 @@ skip_install = True deps = flake8 commands = - flake8 --exclude=migrations {toxinidir}/djburger + flake8 {toxinidir}/djburger