def test_run_transfermethod_success_decorated_tm(self): ''' calling run should execute the tm associated function ''' var = 0 def func(param): nonlocal var var += param tm = transfer_methods.transfermethod( f=func, f_params={'param': 5}, schedule=schedules.DummySchedule()) self.assertTrue(isinstance(tm.mid, uuid.UUID)) self.assertEqual(tm._f, func) self.assertNotEqual(tm.schedule, None) self.assertTrue(isinstance(tm.schedule, schedules.DummySchedule)) self.assertEqual(tm.f_params, {'param': 5}) tm(func) self.assertEqual(tm._func_params.keys(), {'param': 5}.keys()) self.assertNotEqual(tm.schedule, None) self.assertTrue(isinstance(tm.schedule, schedules.DummySchedule)) self.assertEqual(tm.schedule.activation_metrics, []) self.assertEqual(tm.schedule.exec_on_load, False) self.assertIsNotNone(getattr(tm, 'f', None)) self.assertTrue(asyncio.iscoroutinefunction(tm.f)) tm_info = tmIndex.get_tm_info(tm.mid) self.assertEqual(tm_info['enabled'], False) self.assertEqual(tm_info['tm'], tm) ts = pd.Timestamp('now', tz='utc') metrics = [] loop.run_until_complete(tm.run(ts, metrics)) self.assertEqual(var, 5)
def test_transfermethod_success_registering_decorated_transfermethod_DummySchedule( self): '''transfermethod object should be able to register a decorated transfer_method with DummySchedule ''' def func(param): pass tm = transfer_methods.transfermethod( f_params={'param': 'param'}, schedule=schedules.DummySchedule(exec_on_load=True)) self.assertTrue(isinstance(tm.mid, uuid.UUID)) self.assertNotEqual(tm.schedule, None) self.assertTrue(isinstance(tm.schedule, schedules.DummySchedule)) self.assertEqual(tm.schedule.exec_on_load, True) self.assertEqual(tm.f_params, {'param': 'param'}) f = tm(func) self.assertEqual(f, func) self.assertEqual(tm._func_params.keys(), {'param': 'param'}.keys()) self.assertNotEqual(tm.schedule, None) self.assertTrue(isinstance(tm.schedule, schedules.DummySchedule)) self.assertEqual(tm.schedule.activation_metrics, []) self.assertEqual(tm.schedule.exec_on_load, True) self.assertIsNotNone(getattr(tm, 'f', None)) self.assertTrue(asyncio.iscoroutinefunction(tm.f)) tm_info = tmIndex.get_tm_info(tm.mid) self.assertEqual(tm_info['enabled'], False) self.assertEqual(tm_info['tm'], tm)
def test_DummySchedule_activation_metrics_modification_not_allowed(self): ''' DummySchedule's activation_metrics parameter cannot be modified ''' sc = schedules.DummySchedule() self.assertEqual(sc.exec_on_load, False) self.assertEqual(sc.activation_metrics, []) with self.assertRaises(AttributeError) as cm: sc.activation_metrics = ['a'] self.assertEqual(str(cm.exception), "can't set attribute")
def test_run_transfermethod_failure_not_decorated_or_binded_function_found( self): ''' calling run should fail if tm has not associated function ''' tm = transfer_methods.transfermethod( f_params={'param': 5}, schedule=schedules.DummySchedule()) self.assertTrue(isinstance(tm.mid, uuid.UUID)) self.assertEqual(tm._f, None) self.assertNotEqual(tm.schedule, None) self.assertTrue(isinstance(tm.schedule, schedules.DummySchedule)) self.assertEqual(tm.f_params, {'param': 5}) ts = pd.Timestamp('now', tz='utc') metrics = [] loop.run_until_complete(tm.run(ts, metrics))
async def test_run_transfermethod_success_binded_tm(self): ''' calling run should execute the tm associated function ''' try: async def enable_tm(mid): return True enable_tm_bck = tmIndex.enable_tm tmIndex.enable_tm = enable_tm var = 0 def func(param): nonlocal var var += param tm = transfer_methods.transfermethod( f=func, f_params={'param': 5}, schedule=schedules.DummySchedule()) self.assertTrue(isinstance(tm.mid, uuid.UUID)) self.assertEqual(tm._f, func) self.assertNotEqual(tm.schedule, None) self.assertTrue(isinstance(tm.schedule, schedules.DummySchedule)) self.assertEqual(tm.f_params, {'param': 5}) await tm.bind() self.assertEqual(tm._func_params.keys(), {'param': 5}.keys()) self.assertNotEqual(tm.schedule, None) self.assertTrue(isinstance(tm.schedule, schedules.DummySchedule)) self.assertEqual(tm.schedule.activation_metrics, []) self.assertEqual(tm.schedule.exec_on_load, False) self.assertIsNotNone(getattr(tm, 'f', None)) self.assertTrue(asyncio.iscoroutinefunction(tm.f)) tm_info = tmIndex.get_tm_info(tm.mid) self.assertEqual(tm_info['enabled'], False) self.assertEqual(tm_info['tm'], tm) ts = pd.Timestamp('now', tz='utc') metrics = [] await tm.run(ts, metrics) self.assertEqual(var, 5) except: raise finally: tmIndex.enable_tm = enable_tm_bck
async def test_bind_transfermethod_success_with_DummySchedule(self): ''' calling bind should succeed with a tm with DummySchedule ''' try: async def enable_tm(mid): return True def func(param): pass enable_tm_bck = tmIndex.enable_tm tmIndex.enable_tm = enable_tm tm = transfer_methods.transfermethod( f=func, f_params={'param': 'param'}, schedule=schedules.DummySchedule()) self.assertTrue(isinstance(tm.mid, uuid.UUID)) self.assertEqual(tm._f, func) self.assertNotEqual(tm.schedule, None) self.assertTrue(isinstance(tm.schedule, schedules.DummySchedule)) self.assertEqual(tm.f_params, {'param': 'param'}) await tm.bind() self.assertEqual(tm._func_params.keys(), {'param': 'param'}.keys()) self.assertNotEqual(tm.schedule, None) self.assertTrue(isinstance(tm.schedule, schedules.DummySchedule)) self.assertEqual(tm.schedule.activation_metrics, []) self.assertEqual(tm.schedule.exec_on_load, False) self.assertIsNotNone(getattr(tm, 'f', None)) self.assertTrue(asyncio.iscoroutinefunction(tm.f)) tm_info = tmIndex.get_tm_info(tm.mid) self.assertEqual(tm_info['enabled'], False) self.assertEqual(tm_info['tm'], tm) except: raise finally: tmIndex.enable_tm = enable_tm_bck
def test_transfer_method_DummySchedule_success(self): ''' creation of a transfermethod object should succeed if schedule is DummySchedule ''' schedule = schedules.DummySchedule() tm = transfer_methods.transfermethod(schedule=schedule) self.assertEqual(tm.schedule, schedule)
def test_creation_DummySchedule_success_exec_on_load_true(self): ''' creating a DummySchedule object should succeed ''' sc = schedules.DummySchedule(exec_on_load=True) self.assertEqual(sc.exec_on_load, True) self.assertEqual(sc.activation_metrics, []) self.assertEqual(sc.meets(), False)
def test_creation_DummySchedule_success_with_defaults(self): ''' creating a DummySchedule object should succeed. exec_on_load should be False by default ''' sc = schedules.DummySchedule() self.assertEqual(sc.exec_on_load, False) self.assertEqual(sc.activation_metrics, []) self.assertEqual(sc.meets(), False)