async def test_a(self): b = var(5) res = await async_sum_with_yield(2, b=b) self.assertIsInstance(res, Observable) self.assertEqual(await aev(res), 7) self.assertEqual(inside, 1) b @= 1 self.assertEqual(await aev(res), 3) self.assertEqual(inside, 1) del res await asyncio.sleep((0.1))
async def test_lambda_validator(self): a = var(-1) with self.assertRaises(EvalError) as cm: res = process_positive_num(a) self.assertIsInstance(cm.exception.__cause__, AssertionError) res2 = process_positive_num(validate_arg(a, lambda x: x > 0)) with self.assertRaises(ArgEvalError) as cm: ev(res2) self.assertIsInstance(cm.exception.__cause__, ValidationError)
async def test_operator_getitem_setitem_delitem(self): a = var() res = a[1] with self.assertRaises(ArgEvalError): ev(res) a @= [1, 2, 3] self.assertEqual(ev(res), 2) a[1] = 'hej' self.assertEqual(ev(res), 'hej') del a[1] self.assertEqual(ev(res), 3)
async def test_iterable(self): global called_times2 called_times2 = 0 a = var(55) res = inc_called_times2(a, ignored_arg=[self.observable1, self.observable2]) self.assertEqual(55, res) self.assertEqual(1, called_times2) self.observable1.__notifier__.notify() self.assertEqual(55, res) self.assertEqual(2, called_times2) self.observable2.__notifier__.notify() self.assertEqual(55, res) self.assertEqual(3, called_times2)
async def test_vars(self): global called_times2 called_times2 = 0 a = var(55) res = inc_called_times2(a, ignored_arg=self.observable1) self.assertEqual(55, res) self.assertEqual(1, called_times2) a @= 10 self.assertEqual(10, res) self.assertEqual(2, called_times2) self.observable1.__notifier__.notify() self.assertEqual(10, res) self.assertEqual(3, called_times2)
async def test_exception_propagation(self): b = var() res = sum_with_yield(2, b=b) self.assertIsInstance(res, Observable) gc.collect() self.assertIsNotNone(ev_exception(res)) self.assertEqual(0, inside) with self.assertRaisesRegex(Exception, r'.*b.*'): ev(res) b @= 5 await asyncio.sleep(0.1) gc.collect() self.assertIsNone(ev_exception(res)) self.assertEqual(7, ev(res)) self.assertEqual(1, inside)
async def test_operator_getitem_and_exception(self): a = var(('a', 'b')) res = a[1] self.assertEqual(ev(res), 'b') a @= ('A', 'B', 'C') self.assertEqual(ev(res), 'B') a @= ('A', ) with self.assertRaises(EvalError): ev(res) a @= 5 with self.assertRaises(EvalError): ev(res) # check if it works again a @= (1, 2, 3) self.assertEqual(ev(res), 2)
async def test_a(self): b = var(5) res = sum_with_yield(2, b=b) self.assertIsInstance(res, Observable) self.assertEqual(ev(res), 7) gc.collect() self.assertEqual(inside, 1) b @= (1) print("w1") self.assertEqual(ev(res), 3) self.assertEqual(inside, 1) # await res.dispose() del b gc.collect() # weak_res = weakref.ref(res) # print(gc.get_referrers(res)) del res gc.collect() await asyncio.sleep(0.5) self.assertEqual(inside, 0) print("finished)")
async def test_vars(self): global called_times called_times = 0 a = var(None) res = inc_called_times(a) ev(res) self.assertEqual(1, called_times) a @= 55 await asyncio.sleep(0.1) ev(res) self.assertEqual(2, called_times) some_observable.__notifier__.notify() await asyncio.sleep(1) ev(res) self.assertEqual(3, called_times) some_observable.__notifier__.notify() await asyncio.sleep(1) ev(res) self.assertEqual(4, called_times)
async def test_var_var(self): a = var(2) b = var(5) res = my_sum(a=a, b=b) self.assertIsInstance(res, Observable) self.assertEqual(ev(res), 7)
async def test_var_val_keyword(self): a = var(2) res = my_sum(a=a, b=5) self.assertIsInstance(res, Observable) self.assertEqual(ev(res), 7)
async def test_var_val_positional(self): a = var(2) res = my_sum(a, 5) self.assertIsInstance(res, Observable) self.assertEqual(ev(res), 7)
async def setUp(self): global called_times2 global some_observable self.var1 = var() some_observable @= 3 called_times2 = 0
async def setUp(self): self.observable1 = var() self.observable2 = var()
# async def test_vars(self): # a = var(None) # b = var(None) # res = none_proof_sum(a, b) # await wait_for_var(res) # self.assertIsNone(raw(res)) # b.set(2) # await wait_for_var(res) # self.assertIsNone(raw(res)) # a.set(3) # await wait_for_var(res) # self.assertEqual(raw(res), 5) called_times = 0 some_observable = var() some_observable2 = var() @reactive(other_deps=[some_observable]) def inc_called_times(a): global called_times called_times += 1 return a class OtherDeps(asynctest.TestCase): async def test_vars(self): global called_times called_times = 0 a = var(None)
async def test_operator_mul_numpy(self): import numpy as np a = var(2) b = np.array([1, 2]) res = a * b assert_array_equal(ev(res), np.array([2, 4]))