|
| 1 | +import unittest |
| 2 | +import importlib |
| 3 | +import inspect |
| 4 | +import pkgutil |
| 5 | +import ravendb |
| 6 | + |
| 7 | +from ravendb.tests.test_base import TestBase |
| 8 | + |
| 9 | + |
| 10 | +class TestPlatformContracts(TestBase): |
| 11 | + def setUp(self): |
| 12 | + super(TestPlatformContracts, self).setUp() |
| 13 | + |
| 14 | + def tearDown(self): |
| 15 | + super(TestPlatformContracts, self).tearDown() |
| 16 | + TestBase.delete_all_topology_files() |
| 17 | + |
| 18 | + def test_all_commands_expose_callable_is_read_request(self): |
| 19 | + from ravendb.http.raven_command import RavenCommand |
| 20 | + |
| 21 | + offenders = [] |
| 22 | + for mod in self._import_all_ravendb_modules(): |
| 23 | + for cls in self._iter_subclasses_in_module(mod, RavenCommand): |
| 24 | + attr = getattr(cls, "is_read_request", None) |
| 25 | + if isinstance(attr, property): |
| 26 | + offenders.append(f"{cls.__module__}.{cls.__qualname__}") |
| 27 | + |
| 28 | + if offenders: |
| 29 | + self.fail("is_read_request must be a method on: " + ", ".join(offenders)) |
| 30 | + |
| 31 | + # private helpers |
| 32 | + def _import_all_ravendb_modules(self): |
| 33 | + for _, modname, _ in pkgutil.walk_packages(ravendb.__path__, ravendb.__name__ + "."): |
| 34 | + if modname.startswith("ravendb.tests."): |
| 35 | + continue |
| 36 | + try: |
| 37 | + yield importlib.import_module(modname) |
| 38 | + except Exception: |
| 39 | + continue |
| 40 | + |
| 41 | + def _iter_subclasses_in_module(self, mod, base_cls): |
| 42 | + for _, obj in inspect.getmembers(mod): |
| 43 | + if inspect.isclass(obj) and issubclass(obj, base_cls) and obj is not base_cls: |
| 44 | + yield obj |
| 45 | + if inspect.isclass(obj): |
| 46 | + for _, inner in inspect.getmembers(obj, inspect.isclass): |
| 47 | + if issubclass(inner, base_cls) and inner is not base_cls: |
| 48 | + yield inner |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + unittest.main |
0 commit comments