def test_postponed(db): cache = Cache(db, write='on_exit') sess = Session([cache]) with sess: sess.eval(analysis(calcs()), task_filter=lambda t: t.label[0] != '/') assert len(cache._objects) == 23 assert not cache._objects
def test_pickled(db): with Session([Cache(db)], warn=False) as sess: get_object() with Session([Cache(db)]) as sess: sess.eval(get_object()) with Session([Cache(db)]) as sess: assert type(get_object().value) is object
def test_fibonacci2(): with Session() as sess: sess.eval(fib(10)) n_tasks = len(sess._tasks) with Session() as sess: assert sess.eval([fib(5), fib(10)]) == [5, 55] assert n_tasks == len(sess._tasks)
def test_db(db, mocker): sess = Session([Cache(db)]) with sess: sess.eval(analysis(calcs())) mocker.patch.object(sess, 'run_task') with sess: assert sess.eval(analysis(calcs())) == 20 assert not sess.run_task.called
def test_stderr(): @Rule def f(): return run_shell('echo 5 1>&2', stderr=subprocess.PIPE) with Session() as sess: assert int(sess.eval(f()[1])) == 5
def test_exception(): def handler(task, exc): if isinstance(exc, subprocess.CalledProcessError): return True with Session([Parallel()]) as sess: sess.eval(analysis(calcs('x')), exception_handler=handler)
def test_postponed(tmpdir): fmngr = FileManager(tmpdir, eager=False) with Session([fmngr]) as sess: sess.eval(calcs2(), task_filter=lambda t: t.label[0] != '/') assert len(list(Path(tmpdir).glob('**'))) == 1 fmngr.store_cache() assert len(list(Path(tmpdir).glob('**'))) == 7
def test_hashing(tmpdir): def run(calcs, fmngr=None): with Session([fmngr] if fmngr else []) as sess: sess.run_task(calcs()) fut = calcs().future_result() sess.eval(calcs()) task = fut.resolve()[0][1].task output = task.resolve().resolve()['STDOUT'] return task, output fmngr = FileManager(tmpdir) without_fmngr = run(calcs) with_fmngr = run(calcs, fmngr) with Session([fmngr]) as sess: task = dir_task( File.from_str('script', '#!/bin/bash\nexpr $(cat input) "*" 2; true'), [File.from_str('data', str(0)), [Path('input'), 'data']], ) sess.run_task(task) alt_input = task, task.resolve().resolve()['STDOUT'] alt_input = run(calcs2, fmngr) assert len(fmngr._cache) == 12 assert len(list(Path(tmpdir).glob('*/*'))) == 12 assert isinstance(without_fmngr[1], HashedFile) assert isinstance(with_fmngr[1], HashedFile) assert isinstance(alt_input[1], HashedFile) assert without_fmngr[0].hashid == with_fmngr[0].hashid assert without_fmngr[0].hashid == alt_input[0].hashid assert without_fmngr[1].hashid == with_fmngr[1].hashid assert without_fmngr[1].hashid == alt_input[1].hashid
def test_alt_input2(datafile, tmpdir): with Session([FileManager(tmpdir)]) as sess: task = dir_task( File.from_str('script', '#!/bin/bash\nexpr $(cat input) "*" 2; true'), [File.from_path('data'), [Path('input'), 'data']], ) assert int(sess.run_task(task).value['STDOUT'].read_text()) == 4
def test_object(): @Rule def get_object(): return object() with Session() as sess: sess.eval(get_object())
def test_partial_eval(): with Session(warn=False) as sess: main = multi(5) sess.run_task(main) tasks = main.future_result().resolve() sess.run_task(tasks[3]) assert total(main).call() == 3
def test_error(tmpdir): with Session([TmpdirManager(tmpdir)]) as sess: try: sess.eval(dir_task(File.from_str('script', '#!/bin/bash\nxxx'), [])) except subprocess.CalledProcessError: pass assert len(list(Path(tmpdir).glob('*/*'))) == 3
def run(calcs, fmngr=None): with Session([fmngr] if fmngr else []) as sess: sess.run_task(calcs()) fut = calcs().future_result() sess.eval(calcs()) task = fut.resolve()[0][1].task output = task.resolve().resolve()['STDOUT'] return task, output
def test_local_storage(): @Rule def f(): return Session.active().running_task.storage['test'] with Session() as sess: f().storage['test'] = 3 assert sess.run_task(f()).value == 3
def test_dependency_cycle(): @Rule def f(x): return f(x) with pytest.raises(MonaError): with Session() as sess: sess.eval(f(1))
def test_process_error(): @Rule def f(): return run_shell('<', stderr=subprocess.PIPE) with pytest.raises(subprocess.CalledProcessError): with Session() as sess: sess.eval(f())
def test_missing_file(tmpdir): fmngr = FileManager(tmpdir) with pytest.raises(FilesError): with Session([fmngr]) as sess: sess.run_task(calcs()) shutil.rmtree(tmpdir) fmngr._cache.clear() sess.eval(calcs())
def test_fibonacci3(): @Rule def fib(n): if n < 2: return n return total([fib(n - 1), fib(n - 2)]) with Session() as sess: assert sess.eval(fib(10)) == 55
def test_recursion(): @Rule def recurse(i): if i < 5: return recurse(i + 1) return i with Session() as sess: assert sess.eval(recurse(0)) == 5
def test_fibonacci4(): @Rule def fib(n): if n < 2: return [[n]] return [[add(fib(n - 1)[0][0], fib(n - 2)[0][0])]] with Session() as sess: assert sess.eval(fib(10)[0][0]) == 55
def test_returned_done_future(): @Rule def f(x): if x < 0: return x return f(-x) with Session() as sess: sess.eval(f(-4)) assert sess.eval(f(4)) == -4
def test_identical_futures(): @Rule def f(x, y): x, y = x[0], y[0] m = min(x, y) if m < 0: return [0] return [f([m], [max(x, y) - 1])[0]] with Session() as sess: expr = f([f([1], [1])[0]], [f([1], [1])[0]])[0] assert sess.eval(expr, depth=True) == 0
def test_access(tmpdir): fmngr = FileManager(tmpdir) with Session([fmngr]) as sess: sess.run_task(calcs()) fut = calcs().future_result() sess.eval(calcs()) task = fut.resolve()[0][1].task output = task.resolve().resolve()['STDOUT'] assert int(output.value.read_text()) == 0 shutil.rmtree(tmpdir) fmngr._cache.clear() with pytest.raises(FilesError): output.value.read_text()
def test_db_files(db, tmpdir, mocker): sess = Session([Cache(db), FileManager(tmpdir)]) with sess: sess.eval(analysis(calcs2())) mocker.patch.object(sess, 'run_task') with sess: sess.eval(analysis(calcs2())) assert not sess.run_task.called
def test_python(): with Session() as sess: assert sess.eval(python()).read_text().rstrip() == 'coverage'
def test_graphviz(): with Session() as sess: sess.eval(identity(multi(5))) dot = sess.dot_graph(format='svg') assert len(dot.source.split('\n')) == 20
def f(): return Session.active().running_task.storage['test']
def test_fibonacci(): with Session() as sess: assert sess.eval(fib(10)) == 55
def test_calc(): with Session() as sess: sess.run_task(calcs()) assert sess.eval(analysis(calcs())) == 20
def test_invalid_file(): with pytest.raises(InvalidInput): with Session() as sess: sess.eval(dir_task('', {}))