def test_decorator(self, args, kwargs, exception_message): ext = CommCareExtensions() ext_point = ext.extension_point(lambda: None) with testil.assert_raises(ExtensionError, msg=exception_message): @ext_point.extend(*args, **kwargs) def impl(): pass
def test_late_extension_definition(): ext = CommCareExtensions() @ext.extension_point def ext_point_b(): """testing...""" ext.load_extensions([]) with testil.assert_raises(ExtensionError, msg=re.compile("Late extension definition")): @ext_point_b.extend def impl(): pass
def test_single_value_none(): ext = CommCareExtensions() @ext.extension_point(result_format=ResultFormat.FIRST) def ext_point(): pass testil.eq(ext_point(), None)
def test_invoke_default(): ext = CommCareExtensions() @ext.extension_point(result_format=ResultFormat.FIRST) def ext_point_d(domain): return f'default {domain}' testil.eq(ext_point_d(1), 'default 1') @ext_point_d.extend(domains=[1]) def extend_1(domain): return f"custom {domain}" testil.eq(ext_point_d(1), 'custom 1') testil.eq(ext_point_d(2), 'default 2')
def test_single_value(): ext = CommCareExtensions() @ext.extension_point(result_format=ResultFormat.FIRST) def ext_point_d(): """test""" @ext_point_d.extend def extend_1(): return None @ext_point_d.extend def extend_2(): return 1 testil.eq(ext_point_d(), 1)
def test_flatten_results(): ext = CommCareExtensions() @ext.extension_point(result_format=ResultFormat.FLATTEN) def ext_point_d(): """test""" @ext_point_d.extend def extend_1(): return [1, 2] @ext_point_d.extend def extend_2(): return [3, 4] testil.eq(ext_point_d(), [1, 2, 3, 4])
**Flatten Results** :: @extensions.extension_point(result_format=ResultFormat.FLATTEN) def get_things(...): pass > get_things(...) ["thing2", "thing1", "thing3", "thing4"] **First Result** This will return the first result that is not None. This will only call the extension point implementations until a value is found. :: @extensions.extension_point(result_format=ResultFormat.FIRST) def get_things(...): pass > get_things(...) ["thing2", "thing1"] """ from corehq.extensions.interface import CommCareExtensions, ResultFormat # noqa F401 extension_manager = CommCareExtensions() extension_point = extension_manager.extension_point
import re import testil from corehq.extensions.interface import CommCareExtensions, ExtensionError, ResultFormat from corehq.util.test_utils import generate_cases extensions = CommCareExtensions() extension_point = extensions.extension_point @extension_point def ext_point_a(arg1, domain): pass class DemoExtension: def __init__(self, mock_calls): self.mock_calls = { args: response for args, response in mock_calls.items() } def ext_point_a(self, arg1, domain): return self.mock_calls[(arg1, domain)] demo_extension = DemoExtension({ (1, "d2"): "p1", }) ext_point_a.extend(domains=["d2"])(demo_extension.ext_point_a)