Skip to content

Commit

Permalink
Merge pull request graphql-python#19 from mrmilu/enhancement/test-ref…
Browse files Browse the repository at this point in the history
…actor

Restore pytest and improve coverage
  • Loading branch information
Hispar committed Dec 3, 2018
2 parents 119ccd4 + 55113ce commit e1229d0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 93 deletions.
80 changes: 37 additions & 43 deletions graphene_django/tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,53 @@
# -*- coding: utf-8 -*-
# Python imports
from unittest import TestCase
from py.test import raises

# Django imports

# 3rd Party imports

# App imports
from ..registry import Registry
from ..types import DjangoObjectType
from .models import Reporter


class TestSchema(TestCase):
def test_should_raise_if_no_model(self):
with self.assertRaises(Exception) as excinfo:
class Character1(DjangoObjectType):
pass
def test_should_raise_if_no_model():
with raises(Exception) as excinfo:

exception = excinfo.exception
self.assertRegex(str(exception), "valid Django Model")
class Character1(DjangoObjectType):
pass

def test_should_raise_if_model_is_invalid(self):
with self.assertRaises(Exception) as excinfo:
class Character2(DjangoObjectType):
class Meta:
model = 1
assert "valid Django Model" in str(excinfo.value)

exception = excinfo.exception
self.assertRegex(str(exception), "valid Django Model")

def test_should_map_fields_correctly(self):
class ReporterType2(DjangoObjectType):
def test_should_raise_if_model_is_invalid():
with raises(Exception) as excinfo:

class Character2(DjangoObjectType):
class Meta:
model = Reporter
registry = Registry()
model = 1

fields = list(ReporterType2._meta.fields.keys())
assert "valid Django Model" in str(excinfo.value)

self.assertListEqual(fields[:-2], [
"id",
"first_name",
"last_name",
"email",
"pets",
"a_choice",
"reporter_type",
])

self.assertListEqual(sorted(fields[-2:]), ["articles", "films"])
def test_should_map_fields_correctly():
class ReporterType2(DjangoObjectType):
class Meta:
model = Reporter
registry = Registry()

def test_should_map_only_few_fields(self):
class Reporter2(DjangoObjectType):
class Meta:
model = Reporter
only_fields = ("id", "email")
fields = list(ReporterType2._meta.fields.keys())
assert fields[:-2] == [
"id",
"first_name",
"last_name",
"email",
"pets",
"a_choice",
"reporter_type",
]

assert sorted(fields[-2:]) == ["articles", "films"]


def test_should_map_only_few_fields():
class Reporter2(DjangoObjectType):
class Meta:
model = Reporter
only_fields = ("id", "email")

self.assertListEqual(list(Reporter2._meta.fields.keys()), ["id", "email"])
assert list(Reporter2._meta.fields.keys()) == ["id", "email"]
45 changes: 21 additions & 24 deletions graphene_django/tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
# -*- coding: utf-8 -*-
# Python imports
from unittest import TestCase, mock, skip
# General imports
from mock import patch
from pytest import raises

# Django imports
# App imports
import graphene_django.settings as settings

# 3rd Party imports

# App imports
from ..settings import perform_import, import_from_string, reload_graphene_settings, GrapheneSettings, graphene_settings
def test_perform_import_value_none_return_none():
assert settings.perform_import(None, None) is None


class TestSettings(TestCase):
def test_perform_import_value_none_return_none(self):
self.assertIsNone(perform_import(None, None))
def test_perform_import_value_integer_return_integer():
assert settings.perform_import(1, None) == 1

def test_perform_import_value_integer_return_integer(self):
self.assertEqual(perform_import(1, None), 1)

def test_import_from_string_raise_error(self):
with self.assertRaises(ImportError) as context:
import_from_string('x.x', 'y')
def test_import_from_string_raise_error():
with raises(ImportError) as excinfo:
settings.import_from_string('x.x', 'y')

exception = context.exception
assert "Could not import 'x.x' for Graphene setting 'y'" in str(excinfo.value)

self.assertRegex(exception.msg, "Could not import 'x.x' for Graphene setting 'y'")

@skip
@mock.patch('graphene_django.settings.graphene_settings', None)
def test_reload_graphene_settings(self):
reload_graphene_settings(
setting='GRAPHENE',
value='A'
)
self.assertIsInstance(graphene_settings, GrapheneSettings)
@patch('graphene_django.settings.graphene_settings', None)
def test_reload_graphene_settings():
print(settings.graphene_settings)
settings.reload_graphene_settings(
setting='GRAPHENE',
value=None
)
assert isinstance(settings.graphene_settings, settings.GrapheneSettings)
46 changes: 20 additions & 26 deletions graphene_django/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
# -*- coding: utf-8 -*-
# Python imports
from unittest import TestCase, mock

# Django imports

# 3rd Party imports
# general imports
import mock
from pytest import raises

# App imports
from ..utils import get_model_fields, import_single_dispatch
from .models import Film, Reporter


class TestUtils(TestCase):

def test_get_model_fields_no_duplication(self):
reporter_fields = get_model_fields(Reporter)
reporter_name_set = set([field[0] for field in reporter_fields])
self.assertEqual(len(reporter_fields), len(reporter_name_set))
def test_get_model_fields_no_duplication():
reporter_fields = get_model_fields(Reporter)
reporter_name_set = set([field[0] for field in reporter_fields])
assert len(reporter_fields) == len(reporter_name_set)

film_fields = get_model_fields(Film)
film_name_set = set([field[0] for field in film_fields])
self.assertEqual(len(film_fields), len(film_name_set))
film_fields = get_model_fields(Film)
film_name_set = set([field[0] for field in film_fields])
assert len(film_fields) == len(film_name_set)

def test_import_single_dispatch_raise_Exception(self):
modules = {
'functools': None
}

self.module_patcher = mock.patch.dict('sys.modules', modules)
self.module_patcher.start()
with self.assertRaises(Exception) as context:
single_dispatch = import_single_dispatch()
def test_import_single_dispatch_raise_exception():
modules = {
'functools': None
}

exception = context.exception
module_patcher = mock.patch.dict('sys.modules', modules)
module_patcher.start()
with raises(Exception) as excinfo:
single_dispatch = import_single_dispatch()

self.assertRegex(str(exception), "Please install the 'singledispatch'")
self.module_patcher.stop()
assert "Please install the 'singledispatch'" in str(excinfo.value)
module_patcher.stop()

0 comments on commit e1229d0

Please sign in to comment.