2016-06-17 19:29:38 +03:00
|
|
|
from py.test import raises
|
|
|
|
|
|
|
|
from ..types import DjangoObjectType
|
2016-06-20 00:30:33 +03:00
|
|
|
from ..registry import Registry
|
2016-06-17 19:29:38 +03:00
|
|
|
|
|
|
|
from .models import Reporter
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_raise_if_no_model():
|
|
|
|
with raises(Exception) as excinfo:
|
|
|
|
class Character1(DjangoObjectType):
|
|
|
|
pass
|
2016-06-20 00:30:33 +03:00
|
|
|
assert 'valid Django Model' in str(excinfo.value)
|
2016-06-17 19:29:38 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_raise_if_model_is_invalid():
|
|
|
|
with raises(Exception) as excinfo:
|
|
|
|
class Character2(DjangoObjectType):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = 1
|
2016-06-20 00:30:33 +03:00
|
|
|
assert 'valid Django Model' in str(excinfo.value)
|
2016-06-17 19:29:38 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_map_fields_correctly():
|
|
|
|
class ReporterType2(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2016-06-20 00:30:33 +03:00
|
|
|
registry = Registry()
|
2016-08-14 20:57:08 +03:00
|
|
|
assert list(ReporterType2._meta.fields.keys()) == ['id', 'first_name', 'last_name', 'email', 'pets', 'a_choice', 'articles', 'films']
|
2016-06-17 19:29:38 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_map_only_few_fields():
|
|
|
|
class Reporter2(DjangoObjectType):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2016-08-14 20:57:08 +03:00
|
|
|
only_fields = ('id', 'email')
|
2016-06-20 00:30:33 +03:00
|
|
|
|
2016-08-14 20:57:08 +03:00
|
|
|
assert list(Reporter2._meta.fields.keys()) == ['id', 'email']
|