2022-11-14 13:56:18 +03:00
|
|
|
from pytest import raises
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
from ..registry import Registry
|
2016-09-18 03:09:56 +03:00
|
|
|
from ..types import DjangoObjectType
|
2016-09-18 02:29:00 +03:00
|
|
|
from .models import Reporter
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_raise_if_no_model():
|
|
|
|
with raises(Exception) as excinfo:
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
class Character1(DjangoObjectType):
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2018-07-20 02:51:33 +03:00
|
|
|
|
|
|
|
assert "valid Django Model" in str(excinfo.value)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_raise_if_model_is_invalid():
|
|
|
|
with raises(Exception) as excinfo:
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
class Character2(DjangoObjectType):
|
2016-09-18 02:29:00 +03:00
|
|
|
class Meta:
|
|
|
|
model = 1
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2018-07-20 02:51:33 +03:00
|
|
|
|
|
|
|
assert "valid Django Model" in str(excinfo.value)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_map_fields_correctly():
|
|
|
|
class ReporterType2(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
|
|
|
registry = Registry()
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2016-09-18 03:28:41 +03:00
|
|
|
fields = list(ReporterType2._meta.fields.keys())
|
|
|
|
assert fields[:-2] == [
|
2018-07-20 02:51:33 +03:00
|
|
|
"id",
|
|
|
|
"first_name",
|
|
|
|
"last_name",
|
|
|
|
"email",
|
|
|
|
"pets",
|
|
|
|
"a_choice",
|
|
|
|
"reporter_type",
|
2016-09-18 03:28:41 +03:00
|
|
|
]
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
assert sorted(fields[-2:]) == ["articles", "films"]
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_map_only_few_fields():
|
|
|
|
class Reporter2(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2019-07-09 16:03:11 +03:00
|
|
|
fields = ("id", "email")
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
assert list(Reporter2._meta.fields.keys()) == ["id", "email"]
|