Example #1
0
def test_registry_target_find_specific():
    reg = Registry()

    class Document(object):
        pass

    class SpecialDocument(Document):
        pass

    def linecount(obj):
        pass

    def special_linecount(obj):
        pass

    reg.register(linecount, [Document], 'line count')
    reg.register(special_linecount, [Document], 'special line count')

    assert reg.component(linecount, [Document()]) == 'line count'
    assert (reg.component(special_linecount,
                          [Document()]) == 'special line count')

    assert reg.component(linecount, [SpecialDocument()]) == 'line count'
    assert (reg.component(special_linecount,
                          [SpecialDocument()]) == 'special line count')
Example #2
0
def test_registry_sources():
    reg = Registry()

    class Document(object):
        pass

    class SpecialDocument(Document):
        pass

    def linecount(obj):
        pass

    reg.register(linecount, [Document], 'document line count')
    reg.register(linecount, [SpecialDocument], 'special document line count')

    assert (reg.component(linecount, [Document()]) == 'document line count')

    assert (reg.component(
        linecount, [SpecialDocument()]) == 'special document line count')

    class AnotherDocument(Document):
        pass

    assert (reg.component(linecount,
                          [AnotherDocument()]) == 'document line count')

    class Other(object):
        pass

    assert reg.component(linecount, [Other()], default=None) is None
Example #3
0
def test_registry_class_lookup():
    reg = Registry()

    class Document(object):
        pass

    class SpecialDocument(Document):
        pass

    linecount = 'linecount'

    reg.register_predicates(linecount, [class_predicate('obj')])
    reg.register_value(linecount, [Document], 'document line count')
    reg.register_value(linecount, [SpecialDocument],
                       'special document line count')

    assert (reg.component(linecount, Document) ==
            'document line count')

    assert (reg.component(linecount, SpecialDocument) ==
            'special document line count')

    class AnotherDocument(Document):
        pass

    assert (reg.component(linecount, AnotherDocument) ==
            'document line count')

    class Other(object):
        pass

    assert reg.component(linecount, Other) is None
Example #4
0
def test_predicate_matcher():
    reg = Registry()

    class Document(object):
        def __init__(self, a, b):
            self.a = a
            self.b = b

    def foo(obj):
        pass

    predicates = [Predicate("a", KeyIndex, lambda doc: doc.a, "A"), Predicate("b", KeyIndex, lambda doc: doc.b, "B")]

    matcher = PredicateMatcher(predicates)
    matcher.register({"a": "A"}, "a = A")
    matcher.register({"b": "B"}, "b = B")
    matcher.register({}, "nothing matches")

    reg.register(foo, [Document], matcher)

    assert reg.component(foo, [Document(a="A", b="C")]) == "a = A"
    assert reg.component(foo, [Document(a="C", b="B")]) == "b = B"
    assert reg.component(foo, [Document(a="A", b="B")]) == "a = A"
    assert reg.component(foo, [Document(a="C", b="C")]) == "nothing matches"

    # we can also override lookup by supplying our own predicates
    assert reg.component(foo, [Document(a="C", b="C")], predicates={"a": "A", "b": "C"}) == "a = A"
    # if we don't supply something in predicates ourselves, a default will
    # be used
    assert reg.component(foo, [Document(a="C", b="C")], predicates={"a": "C"}) == "b = B"
Example #5
0
def test_registry_sources():
    reg = Registry()

    class Document(object):
        pass

    class SpecialDocument(Document):
        pass

    def linecount(obj):
        pass

    reg.register(linecount, [Document], 'document line count')
    reg.register(linecount, [SpecialDocument], 'special document line count')

    assert (reg.component(linecount, [Document()]) ==
            'document line count')

    assert (reg.component(linecount, [SpecialDocument()]) ==
            'special document line count')

    class AnotherDocument(Document):
        pass

    assert (reg.component(linecount, [AnotherDocument()]) ==
            'document line count')

    class Other(object):
        pass

    assert reg.component(linecount, [Other()], default=None) is None
Example #6
0
def test_matcher_inheritance():
    reg = Registry()

    class Document(object):
        def __init__(self, id):
            self.id = id

    class SpecialDocument(Document):
        pass

    def linecount(obj):
        pass

    class DocumentMatcher(Matcher):
        def __call__(self, doc):
            if doc.id == 1:
                return 'normal'
            else:
                return 'special'

    class SpecialDocumentMatcher(Matcher):
        def __call__(self, doc):
            if doc.id == 2:
                return 'extra normal'
            else:
                return None

    reg.register(linecount, [Document], DocumentMatcher())
    reg.register(linecount, [SpecialDocument], SpecialDocumentMatcher())

    assert reg.component(linecount, [Document(1)]) == 'normal'
    assert reg.component(linecount, [Document(2)]) == 'special'
    assert reg.component(linecount, [SpecialDocument(1)]) == 'normal'
    assert reg.component(linecount, [SpecialDocument(2)]) == 'extra normal'
    assert reg.component(linecount, [SpecialDocument(3)]) == 'special'
Example #7
0
def test_registry_target_find_specific():
    reg = Registry()

    class Document(object):
        pass

    class LineCount(object):
        pass

    class SpecialLineCount(LineCount):
        pass

    class SpecialDocument(Document):
        pass

    reg.register(LineCount, (Document,), 'line count')
    reg.register(SpecialLineCount, (Document,), 'special line count')

    assert reg.component(LineCount, (Document(),)) == 'line count'
    assert (reg.component(SpecialLineCount, (Document(),)) ==
            'special line count')

    assert reg.component(LineCount, (SpecialDocument(),)) == 'line count'
    assert (reg.component(SpecialLineCount, (SpecialDocument(),)) ==
            'special line count')
Example #8
0
def test_component_inheritance_old_style_class():
    reg = Registry()
    foo = object()

    class Gamma:
        pass

    class Delta(Gamma):
        pass

    @generic
    def target():
        pass

    reg.register(target, [Gamma], foo)

    gamma = Gamma()
    delta = Delta()

    assert reg.component(target, [gamma]) is foo
    assert target.component(gamma, lookup=reg) is foo

    # inheritance case
    assert reg.component(target, [delta]) is foo
    assert target.component(delta, lookup=reg) is foo
Example #9
0
def test_registry_sources():
    reg = Registry()

    class Document(object):
        pass

    class SpecialDocument(Document):
        pass

    class LineCount(object):
        pass

    reg.register(LineCount, (Document,), 'document line count')
    reg.register(LineCount, (SpecialDocument,), 'special document line count')

    assert (reg.component(LineCount, (Document(),)) ==
            'document line count')

    assert (reg.component(LineCount, (SpecialDocument(),)) ==
            'special document line count')

    class AnotherDocument(Document):
        pass

    assert (reg.component(LineCount, (AnotherDocument(),)) ==
            'document line count')

    class Other(object):
        pass

    assert reg.component(LineCount, (Other(),), default=None) is None
Example #10
0
def test_registry_target_find_specific():
    reg = Registry()

    class Document(object):
        pass

    class SpecialDocument(Document):
        pass

    def linecount(obj):
        pass

    def special_linecount(obj):
        pass

    reg.register_predicates(linecount, [class_predicate('obj')])
    reg.register_value(linecount, [Document], 'line count')
    reg.register_predicates(special_linecount, [class_predicate('obj')])
    reg.register_value(special_linecount, [Document], 'special line count')

    assert reg.component(linecount, Document) == 'line count'
    assert (reg.component(special_linecount, Document) ==
            'special line count')

    assert reg.component(linecount, SpecialDocument) == 'line count'
    assert (reg.component(special_linecount, SpecialDocument) ==
            'special line count')
Example #11
0
def test_registry_target_find_specific():
    reg = Registry()

    class Document(object):
        pass

    class SpecialDocument(Document):
        pass

    def linecount(obj):
        pass

    def special_linecount(obj):
        pass

    reg.register(linecount, [Document], 'line count')
    reg.register(special_linecount, [Document], 'special line count')

    assert reg.component(linecount, [Document()]) == 'line count'
    assert (reg.component(special_linecount, [Document()]) ==
            'special line count')

    assert reg.component(linecount, [SpecialDocument()]) == 'line count'
    assert (reg.component(special_linecount, [SpecialDocument()]) ==
            'special line count')
Example #12
0
def test_clear():
    reg = Registry()

    def linecount(obj):
        pass

    reg.register(linecount, [], 'once')
    assert reg.component(linecount, []) == 'once'
    reg.clear()
    with pytest.raises(ComponentLookupError):
        reg.component(linecount, [])
Example #13
0
def test_clear():
    reg = Registry()

    def linecount(obj):
        pass

    reg.register(linecount, [], 'once')
    assert reg.component(linecount, []) == 'once'
    reg.clear()
    with pytest.raises(ComponentLookupError):
        reg.component(linecount, [])
Example #14
0
def test_clear():
    reg = Registry()

    def linecount():
        pass

    reg.register_predicates(linecount, [])
    reg.register_value(linecount, (), 'once')
    assert reg.component(linecount, ()) == 'once'
    reg.clear()
    reg.register_predicates(linecount, [])
    assert reg.component(linecount, ()) is None
Example #15
0
def test_component_not_found():
    reg = Registry()

    with py.test.raises(ComponentLookupError):
        reg.component(ITarget, []) is None
    assert reg.component(ITarget, [], 'default') == 'default'

    alpha = Alpha()
    with py.test.raises(ComponentLookupError):
        assert reg.component(ITarget, [alpha])
    assert reg.component(ITarget, [], 'default') == 'default'

    assert ITarget.component(alpha, lookup=reg, default='default') == 'default'
    with py.test.raises(ComponentLookupError):
        ITarget.component(alpha, lookup=reg)
Example #16
0
def test_matcher():
    reg = Registry()

    class Document(object):
        def __init__(self, id):
            self.id = id

    class LineCount(object):
        pass

    class SpecialLineCount(LineCount):
        pass

    class Matcher(IMatcher):
        def __init__(self, func, value):
            self.func = func
            self.value = value

        def __call__(self, *args, **kw):
            if self.func(*args, **kw):
                return self.value
            else:
                return None

    reg.register(LineCount, (Document,),
                 Matcher(lambda doc: doc.id == 1, 'line count'))

    reg.register(SpecialLineCount, (Document,),
                 Matcher(lambda doc: doc.id != 1, 'special line count'))

    assert reg.component(LineCount, (Document(1),)) == 'line count'
    assert reg.component(LineCount, (Document(2),)) == 'special line count'
Example #17
0
def test_matcher_predicates():
    reg = Registry()

    class Document(object):
        pass

    class SpecialDocument(Document):
        pass

    def linecount(obj):
        pass

    count = {
        'predicates': 0,
        'called': 0
        }

    class DocumentMatcher(Matcher):
        def predicates(self, doc):
            count['predicates'] += 1
            return dict(precalculated=100)

        def __call__(self, doc, precalculated):
            count['called'] += 1
            return 'value: %s' % precalculated

    class SpecialDocumentMatcher(Matcher):
        def predicates(self, doc):
            count['predicates'] += 1
            return dict(precalculated=100)

        def __call__(self, doc, precalculated):
            count['called'] += 1
            return None

    reg.register(linecount, [Document],
                 DocumentMatcher())
    reg.register(linecount, [SpecialDocument],
                 SpecialDocumentMatcher())

    assert reg.component(linecount, [Document()]) == 'value: 100'
    assert count['called'] == 1
    assert count['predicates'] == 1
    assert reg.component(linecount, [SpecialDocument()]) == 'value: 100'
    # called two more times, but predicates only called once
    assert count['called'] == 3
    assert count['predicates'] == 2
Example #18
0
def test_component_two_sources():
    reg = Registry()
    foo = object()
    reg.register(ITarget, (IAlpha, IBeta), foo)
    alpha = Alpha()
    beta = Beta()
    assert reg.component(ITarget, [alpha, beta]) is foo
    assert ITarget.component(alpha, beta, lookup=reg) is foo
Example #19
0
def test_component_class_based_registration():
    reg = Registry()
    foo = object()
    reg.register(ITarget, (Alpha,), foo)

    alpha = Alpha()
    assert reg.component(ITarget, [alpha]) is foo
    assert ITarget.component(alpha, lookup=reg) is foo
Example #20
0
def test_register_twice_without_sources():
    reg = Registry()

    def linecount(obj):
        pass

    reg.register(linecount, [], 'once')
    reg.register(linecount, [], 'twice')
    assert reg.component(linecount, []) == 'twice'
Example #21
0
def test_register_twice_without_sources():
    reg = Registry()

    def linecount(obj):
        pass

    reg.register(linecount, [], 'once')
    reg.register(linecount, [], 'twice')
    assert reg.component(linecount, []) == 'twice'
Example #22
0
def test_component_to_itself():
    reg = Registry()
    alpha = Alpha()

    foo = object()

    reg.register(IAlpha, [IAlpha], foo)

    assert reg.component(IAlpha, [alpha]) is foo
    assert IAlpha.component(alpha, lookup=reg) is foo
Example #23
0
def test_component_not_found():
    reg = Registry()

    @generic
    def target(obj):
        pass

    with pytest.raises(ComponentLookupError):
        reg.component(target, []) is None
    assert reg.component(target, [], 'default') == 'default'

    alpha = Alpha()
    with pytest.raises(ComponentLookupError):
        assert reg.component(target, [alpha])
    assert reg.component(target, [], 'default') == 'default'

    assert target.component(alpha, lookup=reg, default='default') == 'default'
    with pytest.raises(ComponentLookupError):
        target.component(alpha, lookup=reg)
Example #24
0
def test_component_not_found():
    reg = Registry()

    @generic
    def target(obj):
        pass

    with pytest.raises(ComponentLookupError):
        reg.component(target, []) is None
    assert reg.component(target, [], 'default') == 'default'

    alpha = Alpha()
    with pytest.raises(ComponentLookupError):
        assert reg.component(target, [alpha])
    assert reg.component(target, [], 'default') == 'default'

    assert target.component(alpha, lookup=reg, default='default') == 'default'
    with pytest.raises(ComponentLookupError):
        target.component(alpha, lookup=reg)
Example #25
0
def test_matcher_predicates():
    reg = Registry()

    class Document(object):
        pass

    class SpecialDocument(Document):
        pass

    def linecount(obj):
        pass

    count = {'predicates': 0, 'called': 0}

    class DocumentMatcher(Matcher):
        def predicates(self, doc):
            count['predicates'] += 1
            return dict(precalculated=100)

        def __call__(self, doc, precalculated):
            count['called'] += 1
            return 'value: %s' % precalculated

    class SpecialDocumentMatcher(Matcher):
        def predicates(self, doc):
            count['predicates'] += 1
            return dict(precalculated=100)

        def __call__(self, doc, precalculated):
            count['called'] += 1
            return None

    reg.register(linecount, [Document], DocumentMatcher())
    reg.register(linecount, [SpecialDocument], SpecialDocumentMatcher())

    assert reg.component(linecount, [Document()]) == 'value: 100'
    assert count['called'] == 1
    assert count['predicates'] == 1
    assert reg.component(linecount, [SpecialDocument()]) == 'value: 100'
    # called two more times, but predicates only called once
    assert count['called'] == 3
    assert count['predicates'] == 2
Example #26
0
def test_component_no_source():
    reg = Registry()
    foo = object()

    @generic
    def target():
        pass

    reg.register(target, (), foo)
    assert reg.component(target, []) is foo
    assert target.component(lookup=reg) is foo
Example #27
0
def test_registry_no_sources():
    reg = Registry()

    class Animal(object):
        pass

    def something():
        pass

    reg.register(something, (), 'elephant')
    assert reg.component(something, ()) == 'elephant'
Example #28
0
def test_registry_no_sources():
    reg = Registry()

    class Animal(object):
        pass

    class Elephant(Animal):
        pass

    reg.register(Elephant, (), 'elephant')
    assert reg.component(Animal, ()) == 'elephant'
Example #29
0
def test_component_no_source():
    reg = Registry()
    foo = object()

    @generic
    def target():
        pass

    reg.register(target, (), foo)
    assert reg.component(target, []) is foo
    assert target.component(lookup=reg) is foo
Example #30
0
def test_registry_no_sources():
    reg = Registry()

    class Animal(object):
        pass

    def something():
        pass

    reg.register(something, (), 'elephant')
    assert reg.component(something, ()) == 'elephant'
Example #31
0
def test_register_twice_with_sources():
    reg = Registry()

    class Document(object):
        pass

    def linecount(obj):
        pass

    reg.register(linecount, [Document], 'document line count')
    reg.register(linecount, [Document], 'another line count')
    assert reg.component(linecount, [Document()]) == 'another line count'
Example #32
0
def test_register_twice_with_sources():
    reg = Registry()

    class Document(object):
        pass

    def linecount(obj):
        pass

    reg.register(linecount, [Document], 'document line count')
    reg.register(linecount, [Document], 'another line count')
    assert reg.component(linecount, [Document()]) == 'another line count'
Example #33
0
def test_component_two_sources():
    reg = Registry()
    foo = object()

    @generic
    def target():
        pass

    reg.register(target, (IAlpha, IBeta), foo)
    alpha = Alpha()
    beta = Beta()
    assert reg.component(target, [alpha, beta]) is foo
    assert target.component(alpha, beta, lookup=reg) is foo
Example #34
0
def test_component_one_source():
    reg = Registry()
    foo = object()

    @generic
    def target():
        pass

    reg.register(target, [Alpha], foo)

    alpha = Alpha()
    assert reg.component(target, [alpha]) is foo
    assert target.component(alpha, lookup=reg) is foo
Example #35
0
def test_component_one_source():
    reg = Registry()
    foo = object()

    @generic
    def target():
        pass

    reg.register(target, [Alpha], foo)

    alpha = Alpha()
    assert reg.component(target, [alpha]) is foo
    assert target.component(alpha, lookup=reg) is foo
Example #36
0
def test_predicate_matcher():
    reg = Registry()

    class Document(object):
        def __init__(self, a, b):
            self.a = a
            self.b = b

    def foo(obj):
        pass

    predicates = [
        Predicate('a', KeyIndex, lambda doc: doc.a, 'A'),
        Predicate('b', KeyIndex, lambda doc: doc.b, 'B')
    ]

    matcher = PredicateMatcher(predicates)
    matcher.register({'a': 'A'}, 'a = A')
    matcher.register({'b': 'B'}, 'b = B')
    matcher.register({}, 'nothing matches')

    reg.register(foo, [Document], matcher)

    assert reg.component(foo, [Document(a='A', b='C')]) == 'a = A'
    assert reg.component(foo, [Document(a='C', b='B')]) == 'b = B'
    assert reg.component(foo, [Document(a='A', b='B')]) == 'a = A'
    assert reg.component(foo, [Document(a='C', b='C')]) == 'nothing matches'

    # we can also override lookup by supplying our own predicates
    assert reg.component(foo, [Document(a='C', b='C')],
                         predicates={
                             'a': 'A',
                             'b': 'C'
                         }) == 'a = A'
    # if we don't supply something in predicates ourselves, a default will
    # be used
    assert reg.component(foo, [Document(a='C', b='C')],
                         predicates={'a': 'C'}) == 'b = B'
Example #37
0
def test_registry_target_find_subclass():
    reg = Registry()

    class Document(object):
        pass

    class Animal(object):
        pass

    class Elephant(Animal):
        pass

    reg.register(Elephant, (Document,), 'elephant')
    assert reg.component(Animal, (Document(),)) == 'elephant'
Example #38
0
def test_matcher_inheritance():
    reg = Registry()

    class Document(object):
        def __init__(self, id):
            self.id = id

    class SpecialDocument(Document):
        pass

    def linecount(obj):
        pass

    class DocumentMatcher(Matcher):
        def __call__(self, doc):
            if doc.id == 1:
                return 'normal'
            else:
                return 'special'

    class SpecialDocumentMatcher(Matcher):
        def __call__(self, doc):
            if doc.id == 2:
                return 'extra normal'
            else:
                return None

    reg.register(linecount, [Document],
                 DocumentMatcher())
    reg.register(linecount, [SpecialDocument],
                 SpecialDocumentMatcher())

    assert reg.component(linecount, [Document(1)]) == 'normal'
    assert reg.component(linecount, [Document(2)]) == 'special'
    assert reg.component(linecount, [SpecialDocument(1)]) == 'normal'
    assert reg.component(linecount, [SpecialDocument(2)]) == 'extra normal'
    assert reg.component(linecount, [SpecialDocument(3)]) == 'special'
Example #39
0
def test_component_inheritance():
    reg = Registry()
    foo = object()

    class Gamma(object):
        pass

    class Delta(Gamma):
        pass

    reg.register(ITarget, [Gamma], foo)

    delta = Delta()

    assert reg.component(ITarget, [delta]) is foo
    assert ITarget.component(delta, lookup=reg) is foo
Example #40
0
def test_component_inheritance():
    reg = Registry()
    foo = object()

    class Gamma(object):
        pass

    class Delta(Gamma):
        pass

    @generic
    def target():
        pass

    reg.register(target, [Gamma], foo)

    delta = Delta()

    assert reg.component(target, [delta]) is foo
    assert target.component(delta, lookup=reg) is foo
Example #41
0
def test_matcher():
    reg = Registry()

    class Document(object):
        def __init__(self, id):
            self.id = id

    def linecount(obj):
        pass

    class MyMatcher(Matcher):
        def __call__(self, doc):
            if doc.id == 1:
                return 'normal'
            else:
                return 'special'

    reg.register(linecount, [Document], MyMatcher())

    assert reg.component(linecount, [Document(1)]) == 'normal'
    assert reg.component(linecount, [Document(2)]) == 'special'
Example #42
0
def test_matcher():
    reg = Registry()

    class Document(object):
        def __init__(self, id):
            self.id = id

    def linecount(obj):
        pass

    class MyMatcher(Matcher):
        def __call__(self, doc):
            if doc.id == 1:
                return 'normal'
            else:
                return 'special'

    reg.register(linecount, [Document],
                 MyMatcher())

    assert reg.component(linecount, [Document(1)]) == 'normal'
    assert reg.component(linecount, [Document(2)]) == 'special'
Example #43
0
def test_registry():
    r = Registry()

    class Foo(object):
        pass

    class FooSub(Foo):
        pass

    def view(self, request):
        raise NotImplementedError()

    def get_model(self):
        return self

    def get_name(request):
        return request.name

    def get_request_method(request):
        return request.request_method

    def model_fallback(self, request):
        return "Model fallback"

    def name_fallback(self, request):
        return "Name fallback"

    def request_method_fallback(self, request):
        return "Request method fallback"

    r.register_callable_predicates(view, [
        match_instance('model', get_model, model_fallback),
        match_key('name', get_name, name_fallback),
        match_key('request_method', get_request_method, request_method_fallback)])

    def foo_default(self, request):
        return "foo default"

    def foo_post(self, request):
        return "foo default post"

    def foo_edit(self, request):
        return "foo edit"

    r.register_value(view, (Foo, '', 'GET'), foo_default)
    r.register_value(view, (Foo, '', 'POST'), foo_post)
    r.register_value(view, (Foo, 'edit', 'POST'), foo_edit)

    assert r.component(view, (Foo, '', 'GET')) is foo_default
    assert r.component(view, (Foo, '', 'POST')) is foo_post
    assert r.component(view, (Foo, 'edit', 'POST')) is foo_edit
    assert r.component(view, (FooSub, '', 'GET')) is foo_default
    assert r.component(view, (FooSub, '', 'POST')) is foo_post

    l = r.lookup()

    class Request(object):
        def __init__(self, name, request_method):
            self.name = name
            self.request_method = request_method

    assert l.call(view, Foo(), Request('', 'GET')) == 'foo default'
    assert l.call(view, FooSub(), Request('', 'GET')) == 'foo default'
    assert l.call(view, FooSub(), Request('edit', 'POST')) == 'foo edit'

    class Bar(object):
        pass

    assert l.call(view, Bar(), Request('', 'GET')) == 'Model fallback'
    assert l.call(view, Foo(), Request('dummy', 'GET')) == 'Name fallback'
    assert l.call(view, Foo(), Request('', 'PUT')) == 'Request method fallback'
    assert l.call(view, FooSub(), Request('dummy', 'GET')) == 'Name fallback'
Example #44
0
def test_component_no_source():
    reg = Registry()
    foo = object()
    reg.register(ITarget, (), foo)
    assert reg.component(ITarget, []) is foo
    assert ITarget.component(lookup=reg) is foo