def test_works_with_cache(tmpdir): """ Tests if the cache properly wraps functions to allow inspection.""" cache = JobLibCache(str(tmpdir), verbose=1) explorer = Explorer(cache=cache) @explorer.provider def c(a, b=1): return a + b z = explorer.get_variable('c', a=2) assert z == 2 + 1 z = explorer.get_variable('c', a=2, b=2) assert z == 2 + 2
def test_provider_and_opts(): explorer = Explorer() # Simple call @explorer.provider def sq(sum): return sum*sum @explorer.provider def sum(x, y=12): return x+y z = explorer.get_variable('sq', x=10) assert z == (10+12)**2 z = explorer.get_variable('sq', x=10, y=8) assert z == (10+8)**2
def test_resolve_call(): explorer = Explorer() # Simple call @explorer.add_function(provides='version') def version(): return 12 ver = explorer.get_variable('version') assert ver == 12 # Complex call @explorer.add_function(provides=('surname','middlename'), requires='name') def surname(name): return {'John': ('Doe', None), 'Martin':('King', 'Luther')}[name] @explorer.add_function(provides='age', requires=('name',)) def age(name): return {'John': 12, 'Martin': 15}[name] @explorer.add_function(provides=('person', ), requires=('name','age','surname')) def make_person(name, surname, age): return Person(name, surname, age) person = explorer.get_variable('person', name='John') print(person) assert person.age == 12 assert person.surname == 'Doe' person = explorer.get_variable('person', name='Martin') print(person) assert person.age == 15 middle = explorer.get_variable('middlename', name='Martin') assert middle == 'Luther' people = explorer.map_variable('person', name=['Martin', 'John']) assert len(people) == 2 assert people[0].name == 'Martin' assert people[0].age == 15
def test_add_with_provider(): explorer = Explorer() @explorer.provider def y(x, m): return x*2 + m calls_z = 0 @explorer.provider(cache=False) def z(y): nonlocal calls_z calls_z += 1 return calls_z + y zv = explorer.get_variable('z', x=1, m=5) assert zv == 1*2+1+5 zs = explorer.map_variable('z', x=[1, 2, 3], m=[2]) print(zs) assert all(zs == [6, 9, 12])