def test_rule_no_activity_in_24h_not_fired(self, db_conn, config, statements): current_ts = datetime.now().astimezone() ago_12h = current_ts - timedelta(hours=12) with db_conn.xact(): statements['create_user'](1, 'john', 'smith', 'someprog') statements['add_event']('1', ago_12h, 'steps', '1') rule_engine = rules.RuleEngine(config) rule_engine.load_rules() rule_engine.execute_rule('inactivity') alerts = statements.get('get_alerts_for_user').rows(1) assert len(list(alerts)) == 0
def test_rule_activity_endorsement_fired(self, db_conn, config, statements): current_ts = datetime.now().astimezone() with db_conn.xact(): statements['create_user'](1, 'john', 'smith', 'someprog') for delta_hours in range(3,0,-1): statements['add_event']('1', current_ts - timedelta(hours=delta_hours), 'medication', 'done') rule_engine = rules.RuleEngine(config) rule_engine.load_rules() rule_engine.execute_rule('activity-endorsement') alerts = statements.get('get_alerts_for_user').rows(1) alerts_as_list = list(alerts) assert len(alerts_as_list) == 0
def test_rule_no_medication_in_3d_not_fired(self, db_conn, config, statements): current_ts = datetime.now().astimezone() ago_1d = current_ts - timedelta(days=1) with db_conn.xact(): statements['create_user'](1, 'john', 'smith', 'someprog') statements['add_event']('1', ago_1d, 'medication', 'done') rule_engine = rules.RuleEngine(config) rule_engine.load_rules() rule_engine.execute_rule('missing-medication') alerts = statements.get('get_alerts_for_user').rows(1) alerts_as_list = list(alerts) assert len(alerts_as_list) == 0
def test_rule_pro_deterioration_not_fired(self, db_conn, config, statements): current_ts = datetime.now().astimezone() with db_conn.xact(): statements['create_user'](1, 'john', 'smith', 'someprog') statements['add_event']('1', current_ts - timedelta(days=2), 'PRO', '15 - Mid') statements['add_event']('1', current_ts - timedelta(days=1), 'PRO', '15 - Mid') statements['add_event']('1', current_ts - timedelta(days=0), 'PRO', '10 - Low') rule_engine = rules.RuleEngine(config) rule_engine.load_rules() rule_engine.execute_rule('pro-deterioration') alerts = statements.get('get_alerts_for_user').rows(1) alerts_as_list = list(alerts) assert len(alerts_as_list) == 0
def init_rule_engine(): global rule_engine config = { 'connectParams': { 'user': os.environ.get('USER'), 'password': os.environ.get('PASSWORD'), 'database': os.environ.get('DATABASE'), 'unix': os.environ.get('UNIX_SOCKET') }, 'schema': os.environ.get('SCHEMA') } if not config['schema']: config['schema'] = 'public' rule_engine = rules.RuleEngine(config)
def test_rule_no_duplicates_config_interval(self, db_conn, config, statements): current_ts = datetime.now().astimezone() with db_conn.xact(): statements['create_user'](1, 'john', 'smith', 'someprog') statements['add_event']('1', current_ts - timedelta(hours=36), 'steps', '1') statements['create_alert'](1, current_ts - timedelta(hours=1), 'inactivity', 'some message') custom_config = copy.deepcopy(config) # default is 12 hours, - override with '30 minutes' custom_config['duplicateInterval'] = '30 minutes' rule_engine = rules.RuleEngine(custom_config) rule_engine.load_rules() rule_engine.execute_rule('inactivity') alerts = statements.get('get_alerts_for_user').rows(1) alerts_as_list = list(alerts) assert len(alerts_as_list) == 2
def test_rule_no_duplicates_12h(self, db_conn, config, statements): current_ts = datetime.now().astimezone() with db_conn.xact(): statements['create_user'](1, 'john', 'smith', 'someprog') statements['add_event']('1', current_ts - timedelta(hours=36), 'steps', '1') statements['create_alert'](1, current_ts - timedelta(hours=7), 'inactivity', 'some message') rule_engine = rules.RuleEngine(config) rule_engine.load_rules() rule_engine.execute_rule('inactivity') alerts = statements.get('get_alerts_for_user').rows(1) alerts_as_list = list(alerts) assert len(alerts_as_list) == 1 # user_id == 1 assert alerts_as_list[0][1] == 1 # rule_id == 'inactivity' assert alerts_as_list[0][3] == 'inactivity'