def test_predicate_raises_exception(self): r = collections.OrderedDict() def raiser(): raise Exception("hello") core._add_input(r, 'a-a', 2, predicates=[raiser]) core._add_input(r, 'a-a', 1) with self.assertRaises(core.AbortExecution): core._resolve_item(r['a_a'])
def test_process_input_function(self): r = collections.OrderedDict() f = lambda: 2 core._add_input(r, 'a', lambda: f) i = core._resolve_item(r['a']) v = core._process_item(i, TestProcessItem.context_fn) self.assertEqual(v, 2)
def test_process_compute_with_dependencies(self): r = collections.OrderedDict() core._add_input(r, 'a', 1) f1 = lambda c: c add_compute_helper(r, 'b', f1, ['a']) i = core._resolve_item(r['b']) v = core._process_item(i, TestProcessItem.context_fn) self.assertEqual(v, ['a'])
def test_single_compute(self): r = collections.OrderedDict() core._add_input(r, 'a-a', 1) f1 = lambda c: c.a_a + 1 add_compute_helper(r, 'b-b', f1, ['a-a']) core._check_reactor(r) x = core._resolve_item(r['b_b']) self.assertIsInstance(x, core.ResolvedItem) self.assertEqual(x.type_, core.Type.COMPUTE) self.assertEqual(x.key, 'b_b') self.assertEqual(x.name, 'b-b') self.assertEqual(x.item, f1) self.assertEqual(x.dependents, set()) self.assertEqual(x.dependencies, ['a_a']) self.assertEqual(x.persistent, True) # also check that a-a got the dependent x = core._resolve_item(r['a_a']) self.assertEqual(x.type_, core.Type.INPUT) self.assertEqual(x.dependents, {'b_b'})
def test_process_raises_abort_execution(self): r = collections.OrderedDict() def f(): raise core.AbortExecution("aborted execution") core._add_input(r, 'a', f) i = core._resolve_item(r['a']) with self.assertRaises(core.AbortExecution) as e: core._process_item(i, TestProcessItem.context_fn) self.assertEqual(str(e.exception), "aborted execution")
def test_predicate_picks_correct_depenencies_and_persistent(self): r = collections.OrderedDict() core._add_input(r, 'a', 1) core._add_input(r, 'b', 2) f1 = lambda c: c.a_a + 1 add_compute_helper(r, 'c', f1, ['a'], persistent=False) f2 = lambda c: c.a_a + 2 add_compute_helper(r, 'c', f2, ['b'], predicates=[lambda: False]) x = core._resolve_item(r['c']) self.assertEqual(x.item, f1) self.assertEqual(x.dependencies, ['a']) self.assertEqual(x.persistent, False)
def test_single_input(self): r = collections.OrderedDict() core._add_input(r, 'a-a', 1) x = core._resolve_item(r['a_a']) self.assertIsInstance(x, core.ResolvedItem) self.assertEqual(x.type_, core.Type.INPUT) self.assertEqual(x.key, 'a_a') self.assertEqual(x.name, 'a-a') self.assertEqual(x.item, 1) self.assertEqual(x.dependents, set()) self.assertEqual(x.dependencies, []) self.assertEqual(x.persistent, True)
def test_resolve_to_predicate_true_change_order(self): r = collections.OrderedDict() core._add_input(r, 'a-a', 2, predicates=[lambda: True]) core._add_input(r, 'a-a', 1) x = core._resolve_item(r['a_a']) self.assertIsInstance(x, core.ResolvedItem) self.assertEqual(x.type_, core.Type.INPUT) self.assertEqual(x.key, 'a_a') self.assertEqual(x.name, 'a-a') self.assertEqual(x.item, 2) self.assertEqual(x.dependents, set()) self.assertEqual(x.dependencies, []) self.assertEqual(x.persistent, True)
def test_process_raises_other_exception(self): r = collections.OrderedDict() class CustomException(Exception): pass def f(): raise CustomException("aborted custom") core._add_input(r, 'a', f) i = core._resolve_item(r['a']) with self.assertRaises(core.AbortExecution) as e: core._process_item(i, TestProcessItem.context_fn) self.assertTrue("aborted custom" in str(e.exception))
def test_process_input_value(self): r = collections.OrderedDict() core._add_input(r, 'a', 1) i = core._resolve_item(r['a']) v = core._process_item(i, TestProcessItem.context_fn) self.assertEqual(v, 1)
def test_predicate_only_that_is_false_returns_none(self): r = collections.OrderedDict() core._add_input(r, 'a-a', 2, predicates=[lambda: False]) self.assertIsNone(core._resolve_item(r['a_a']))