1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
from pytest import raises
from graphene import ObjectType, String
from ..module_loading import import_string, lazy_import
def test_import_string():
MyString = import_string("graphene.String")
assert MyString == String
MyObjectTypeMeta = import_string("graphene.ObjectType", "__doc__")
assert MyObjectTypeMeta == ObjectType.__doc__
def test_import_string_module():
with raises(Exception) as exc_info:
import_string("graphenea")
assert str(exc_info.value) == "graphenea doesn't look like a module path"
def test_import_string_class():
with raises(Exception) as exc_info:
import_string("graphene.Stringa")
assert (
str(exc_info.value)
== 'Module "graphene" does not define a "Stringa" attribute/class'
)
def test_import_string_attributes():
with raises(Exception) as exc_info:
import_string("graphene.String", "length")
assert (
str(exc_info.value)
== 'Module "graphene" does not define a "length" attribute inside attribute/class '
'"String"'
)
with raises(Exception) as exc_info:
import_string("graphene.ObjectType", "__class__.length")
assert (
str(exc_info.value)
== 'Module "graphene" does not define a "__class__.length" attribute inside '
'attribute/class "ObjectType"'
)
with raises(Exception) as exc_info:
import_string("graphene.ObjectType", "__classa__.__base__")
assert (
str(exc_info.value)
== 'Module "graphene" does not define a "__classa__" attribute inside attribute/class '
'"ObjectType"'
)
def test_lazy_import():
f = lazy_import("graphene.String")
MyString = f()
assert MyString == String
f = lazy_import("graphene.ObjectType", "__doc__")
MyObjectTypeMeta = f()
assert MyObjectTypeMeta == ObjectType.__doc__
|