Skip to content
This repository has been archived by the owner on Mar 30, 2020. It is now read-only.

Commit

Permalink
new tests structure
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium committed Jan 3, 2018
1 parent 3cbaba5 commit fdeacb4
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 197 deletions.
14 changes: 8 additions & 6 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
django.setup()


import djburger # noQA

from tests.controllers import * # noQA
from tests.parsers import * # noQA
from tests.renderers import * # noQA
from tests.side_validators import * # noQA
from tests.validators import * # noQA
from tests.views import * # noQA

from tests.django import * # noQA
from tests.django_and_other import * # noQA
from tests.main import * # noQA
from tests.marshmallow import * # noQA
from tests.pyschemes import * # noQA
from tests.rest import * # noQA


if __name__ == '__main__':
Expand Down
5 changes: 5 additions & 0 deletions tests/django/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .controllers import *
from .parsers import *
from .renderers import *
from .validators import *
from .views import *
6 changes: 2 additions & 4 deletions tests/controllers.py → tests/django/controllers.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# built-in
from __main__ import unittest
from __main__ import unittest, djburger
# external
from django.contrib.auth.models import Group
from django.http import HttpResponse
from django.test import RequestFactory
# project
import djburger


class TestControllers(unittest.TestCase):
class DjangoControllersTest(unittest.TestCase):

def test_objects_controllers(self):
# PREPARE
Expand Down
6 changes: 2 additions & 4 deletions tests/parsers.py → tests/django/parsers.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# built-in
import json
from __main__ import unittest
from __main__ import unittest, djburger
# external
import bson
from django.test import RequestFactory
# project
import djburger


class TestParsers(unittest.TestCase):
class DjangoParsersTest(unittest.TestCase):

def test_default_parser(self):
factory = RequestFactory()
Expand Down
6 changes: 2 additions & 4 deletions tests/renderers.py → tests/django/renderers.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
# built-in
import json
from __main__ import unittest
from __main__ import unittest, djburger
# external
import bson
from django.core.exceptions import ValidationError
import yaml
# project
import djburger


# import only after django.setup()
from rest_framework import renderers as rest_framework_renderers # noQA


class TestRenderers(unittest.TestCase):
class DjangoRenderersTest(unittest.TestCase):

def test_json_renderer(self):
with self.subTest(src_text='str'):
Expand Down
48 changes: 48 additions & 0 deletions tests/django/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# built-in
from __main__ import unittest, djburger
# external
from django.contrib.auth.models import Group


class DjangoFormValidatorsTest(unittest.TestCase):

@classmethod
def setUpClass(cls):
Group.objects.filter(name='TEST_IT').delete()
Group.objects.filter(name='TEST_IT_2').delete()

cls.obj = Group.objects.create(name='TEST_IT')
Group.objects.create(name='TEST_IT_2')
cls.qs = Group.objects.all()

@classmethod
def tearDownClass(cls):
Group.objects.get(name='TEST_IT').delete()
Group.objects.get(name='TEST_IT_2').delete()

def base_validator(self):
class Base(djburger.v.b.Form):
name = djburger.f.CharField(max_length=20)
mail = djburger.f.EmailField()
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 wrapper_validator(self):
class Base(djburger.f.Form):
name = djburger.f.CharField(max_length=20)
mail = djburger.f.EmailField()
Wrapped = djburger.v.w.Form(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())
2 changes: 1 addition & 1 deletion tests/views.py → tests/django/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import djburger # noQA


class TestViews(unittest.TestCase):
class DjangoViewsTest(unittest.TestCase):

def test_controller(self):
class Base(djburger.ViewBase):
Expand Down
59 changes: 59 additions & 0 deletions tests/django_and_other.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# built-in
from __main__ import unittest, djburger
# external
from django.contrib.auth.models import Group
import marshmallow
from pyschemes import Scheme as PySchemes
import rest_framework


class DjangoOtherValidatorsTest(unittest.TestCase):

@classmethod
def setUpClass(cls):
Group.objects.filter(name='TEST_IT').delete()
Group.objects.filter(name='TEST_IT_2').delete()

cls.obj = Group.objects.create(name='TEST_IT')
Group.objects.create(name='TEST_IT_2')
cls.qs = Group.objects.all()

@classmethod
def tearDownClass(cls):
Group.objects.get(name='TEST_IT').delete()
Group.objects.get(name='TEST_IT_2').delete()

def pyschemes(self):
with self.subTest(src_text='pyschemes'):
v = djburger.v.c.PySchemes(
{'name': str, 'id': int},
policy='drop'
)
v = v(request=None, data=self.obj)
self.assertTrue(v.is_valid())

def marshmallow(self):
with self.subTest(src_text='marshmallow base'):
class Base(djburger.v.b.Marshmallow):
name = marshmallow.fields.Str()
v = Base(request=None, data=self.obj)
self.assertTrue(v.is_valid())
with self.subTest(src_text='marshmallow wrapper'):
class Base(marshmallow.Schema):
name = marshmallow.fields.Str()
Wrapped = djburger.v.w.Marshmallow(Base) # noQA
v = Wrapped(request=None, data=self.obj)
self.assertTrue(v.is_valid())

def rest(self):
with self.subTest(src_text='rest framework base'):
class Base(djburger.v.b.RESTFramework):
name = rest_framework.serializers.CharField(max_length=20)
v = Base(request=None, data=self.obj)
self.assertTrue(v.is_valid())
with self.subTest(src_text='rest framework wrapper'):
class Base(rest_framework.serializers.Serializer):
name = rest_framework.serializers.CharField(max_length=20)
Wrapped = djburger.v.w.RESTFramework(Base) # noQA
v = Wrapped(request=None, data=self.obj)
self.assertTrue(v.is_valid())
5 changes: 2 additions & 3 deletions tests/validators.py → tests/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from __main__ import unittest
import djburger
from __main__ import unittest, djburger


class TestValidators(unittest.TestCase):
class MainValidatorsTest(unittest.TestCase):

def test_type_validator(self):
# BASE
Expand Down
36 changes: 36 additions & 0 deletions tests/marshmallow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# built-in
from __main__ import unittest, djburger
# external
import marshmallow


class MarshmallowValidatorsTest(unittest.TestCase):

def 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 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())
39 changes: 39 additions & 0 deletions tests/pyschemes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# built-in
from __main__ import unittest, djburger
# external
from pyschemes import Scheme as PySchemes


class PySchemesValidatorsTest(unittest.TestCase):

def base_validator(self):
# BASE
with self.subTest(src_text='base pass'):
v = djburger.v.c.PySchemes([str, 2, int])
v = v(request=None, data=['3', 2, 4])
v.is_valid()
self.assertTrue(v.is_valid())
with self.subTest(src_text='base not pass'):
v = djburger.v.c.PySchemes([str, 2, int])
v = v(request=None, data=[1, 2, 4])
self.assertFalse(v.is_valid())
with self.subTest(src_text='base int data'):
v = djburger.v.c.PySchemes(int)
v = v(request=None, data=3)
v.is_valid()
self.assertEqual(v.cleaned_data, 3)

def wrapper_validator(self):
with self.subTest(src_text='base pass'):
v = djburger.v.w.PySchemes(PySchemes([str, 2, int]))
v = v(request=None, data=['3', 2, 4])
self.assertTrue(v.is_valid())
with self.subTest(src_text='base not pass'):
v = djburger.v.w.PySchemes(PySchemes([str, 2, int]))
v = v(request=None, data=[1, 2, 4])
self.assertFalse(v.is_valid())
with self.subTest(src_text='base int data'):
v = djburger.v.w.PySchemes(PySchemes(int))
v = v(request=None, data=3)
v.is_valid()
self.assertEqual(v.cleaned_data, 3)
34 changes: 34 additions & 0 deletions tests/rest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# built-in
from __main__ import unittest, djburger
# external
import rest_framework


class RestValidatorsTest(unittest.TestCase):
def base_validator(self):
# BASE
class Base(djburger.v.b.RESTFramework):
name = rest_framework.serializers.CharField(max_length=20)
mail = rest_framework.serializers.EmailField()
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 wrapper_validator(self):
class Base(rest_framework.serializers.Serializer):
name = rest_framework.serializers.CharField(max_length=20)
mail = rest_framework.serializers.EmailField()
Wrapped = djburger.v.w.RESTFramework(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())
Loading

0 comments on commit fdeacb4

Please sign in to comment.