1
"""test the inspection registry system."""
3
from sqlalchemy.testing import eq_, assert_raises_message
4
from sqlalchemy import exc, util
5
from sqlalchemy import inspection, inspect
6
from sqlalchemy.testing import fixtures
8
class TestFixture(object):
11
class TestInspection(fixtures.TestBase):
14
for type_ in list(inspection._registrars):
15
if issubclass(type_, TestFixture):
16
del inspection._registrars[type_]
18
def test_def_insp(self):
19
class SomeFoo(TestFixture):
22
@inspection._inspects(SomeFoo)
23
def insp_somefoo(subject):
24
return {"insp":subject}
27
insp = inspect(somefoo)
28
assert insp["insp"] is somefoo
30
def test_no_inspect(self):
31
class SomeFoo(TestFixture):
34
assert_raises_message(
35
exc.NoInspectionAvailable,
36
"No inspection system is available for object of type ",
40
def test_class_insp(self):
41
class SomeFoo(TestFixture):
44
class SomeFooInspect(object):
45
def __init__(self, target):
47
SomeFooInspect = inspection._inspects(SomeFoo)(SomeFooInspect)
50
insp = inspect(somefoo)
51
assert isinstance(insp, SomeFooInspect)
52
assert insp.target is somefoo
54
def test_hierarchy_insp(self):
55
class SomeFoo(TestFixture):
58
class SomeSubFoo(SomeFoo):
61
@inspection._inspects(SomeFoo)
62
def insp_somefoo(subject):
65
@inspection._inspects(SomeSubFoo)
66
def insp_somesubfoo(subject):
70
eq_(inspect(SomeFoo()), 1)
71
eq_(inspect(SomeSubFoo()), 2)