Best Python code snippet using tavern
test_smache.py
Source:test_smache.py  
...7    redis_con.flushall()8    yield9def setup_module(module):10    global smache, score, h, f, with_raw11    DummyA.unsubscribe_all()12    DummyB.unsubscribe_all()13    DummyC.unsubscribe_all()14    smache = Smache(scheduler=InProcessScheduler(),15                    write_through=False)16    @smache.sources(DummyB, DummyC)17    @smache.computed(DummyA)18    def score(a):19        return a.value + 5 + 1020    @smache.computed(DummyB, DummyC)21    def h(b, c):22        return b.value + c.value23    @smache.computed(DummyA, DummyB, DummyC)24    def f(a, b, c):25        return a.value * h(b, c)26    @smache.computed(DummyA, DummyB, Raw)27    def with_raw(a, b, static_value):28        return (a.value + b.value) * static_value29def teardown_module(module):30    DummyA.unsubscribe_all()31    DummyB.unsubscribe_all()32    DummyC.unsubscribe_all()33def test_cache():34    ax = DummyA(1, 10)35    bx = DummyB(2, 2)36    cx = DummyC(3, 3)37    assert f(ax, bx, cx) == 5038    assert h(bx, cx) == 539    assert score(ax)40    assert smache.is_fun_fresh(score, ax) == True41    assert smache.is_fun_fresh(f, ax, bx, cx) == True42    assert smache.is_fun_fresh(h, bx, cx) == True43    DummyB.update(0, {})44    assert smache.is_fun_fresh(score, ax) == False45    assert smache.is_fun_fresh(f, ax, bx, cx) == True46    assert smache.is_fun_fresh(h, bx, cx) == True...test_relations.py
Source:test_relations.py  
...10    DummyC.set_data({'20': {'value': -1}, '30': {'value': -2}})11    yield12def setup_module(module):13    global smache, f, h14    DummyA.unsubscribe_all()15    DummyB.unsubscribe_all()16    DummyC.unsubscribe_all()17    smache = Smache(scheduler=InProcessScheduler(), write_through=False)18    @smache.relations((DummyB, lambda b: DummyC.find('30')))19    @smache.computed(DummyA, DummyC, Raw, Raw)20    def f(a, dummyc, c, d):21        b = DummyB.find('2')22        return a.value * b.value23    @smache.relations((DummyB, lambda b: [DummyA.find('1'), DummyA.find('2')]))24    @smache.computed(DummyA, Raw, Raw)25    def h(a, c, d):26        b = DummyB.find('2')27        return a.value * b.value28def teardown_module(module):29    DummyA.unsubscribe_all()30    DummyB.unsubscribe_all()31    DummyC.unsubscribe_all()32def test_computed_function_are_updated_when_relations_are():33    ax = DummyA('1', 10)34    ax2 = DummyA('2', 500)35    cx = DummyC('20', 500)36    cx2 = DummyC('30', 500)37    assert f(ax, cx, 5, 10) == 20038    assert f(ax2, cx2, 5, 10) == 1000039    DummyC.update('30', {'value': 30})40    assert smache.is_fun_fresh(f, ax, cx, 5, 10) == True41    assert smache.is_fun_fresh(f, ax2, cx2, 5, 10) == False42def test_relations_with_list():43    ax = DummyA('1', 10)44    ax2 = DummyA('2', 500)45    assert h(ax, 5, 10) == 200...event_handler.py
Source:event_handler.py  
...14        if predicate in self._event_handlers:15            self._event_handlers[predicate].discard(subscriber)16            if len(self._event_handlers[predicate]) == 0:17                del self._event_handlers[predicate]18    def unsubscribe_all(self, subscriber: EventSubscriber):19        predicates_for_removal = []20        for predicate, subscribers in self._event_handlers.items():21            subscribers.discard(subscriber)22            if len(subscribers) == 0:23                predicates_for_removal.append(predicate)24        for predicate in predicates_for_removal:25            del self._event_handlers[predicate]26    def publish(self, event: DomainEvent):27        matching_handlers = set()28        for event_predicate, handlers in self._event_handlers.items():29            if event_predicate(event):30                matching_handlers.update(handlers)31        for handler in matching_handlers:32            handler(event)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
