def test_request_delete_most_detailed_shh(two_sets_four_versions_sqlite): db = two_sets_four_versions_sqlite session = db.session version1 = session.query(SequelaSetVersion).get(1) seqhh11 = session.query(SequelaHierarchyHistory).get([1, 11]) parent = version1.get_hierarchy_rows(seqhh11.parent_id)[0] siblings = parent.children number_of_siblings = len(siblings) request = {'sequela_hierarchy_history': [ {'sequela_id': 11, 'sequela_set_version_id': 1, 'is_delete': True}]} handler = RequestHandler(session) handler.process_request(request) session.commit() siblings = parent.children assert seqhh11 not in siblings assert number_of_siblings == len(parent.children) + 1 assert session.query(SequelaHierarchyHistory).get((1, 11)) is None
def test_request_delete_aggregate_shh(two_sets_four_versions_sqlite): db = two_sets_four_versions_sqlite session = db.session version2 = session.query(SequelaSetVersion).get(2) seqhh3 = session.query(SequelaHierarchyHistory).get([2, 3]) children = seqhh3.children parent = version2.get_hierarchy_rows(seqhh3.parent_id)[0] siblings = parent.children children_len_before = len(children) siblings_len_before = len(siblings) request = {'sequela_hierarchy_history': [ {'sequela_set_version_id': 2, 'sequela_id': 3, 'is_delete': True}]} handler = RequestHandler(session) handler.process_request(request) session.commit() siblings = parent.children assert seqhh3 not in siblings assert len(siblings) == siblings_len_before - 1 + children_len_before assert session.query(SequelaHierarchyHistory).get((2, 3)) is None
def test_request_modify_shh(two_sets_four_versions_sqlite): db = two_sets_four_versions_sqlite session = db.session seqhh4 = session.query(SequelaHierarchyHistory).get([2, 4]) old_cause = seqhh4.cause_id old_me = seqhh4.modelable_entity_id old_hs = seqhh4.healthstate_id request = {'sequela_hierarchy_history': [ {'sequela_set_version_id': 2, 'sequela_id': 4, 'cause_id': 419, 'modelable_entity_id': 841, 'healthstate_id': 1968}]} handler = RequestHandler(session) handler.process_request(request) session.commit() assert old_cause != seqhh4.cause_id assert old_me != seqhh4.modelable_entity_id assert old_hs != seqhh4.healthstate_id assert seqhh4.cause_id == 419 assert seqhh4.modelable_entity_id == 841 assert seqhh4.healthstate_id == 1968
def test_add_single_version(two_sets_four_versions_sqlite): db = two_sets_four_versions_sqlite session = db.session versions = session.query(SequelaSetVersion).all() num_ver_before = len(versions) version_to_add = { 'sequela_set_version': { 'sequela_set_id': 1, 'sequela_set_version_id': None, 'sequela_set_version': 'dummy', 'sequela_set_version_description': 'new', 'sequela_set_version_justification': 'we want it' } } handler = RequestHandler(session) handler.process_request(version_to_add) versions = session.query(SequelaSetVersion).all() # there were 4 versions, we added one assert len(versions) == num_ver_before + 1 # set 1 had 2 versions previously, now 3 set_1 = session.query(SequelaSet).get(1) assert len(set_1.fk_sequela_set_version_id.all()) == 3 # version 5 should have the name 'dummy' version_5 = session.query(SequelaSetVersion).get(5) assert version_5.sequela_set_version == 'dummy'
def test_modify_hierarchy_history(two_sets_four_versions_sqlite): """ Take the first sequela_set_version (id: 1) and alter the hierarchy so the parent sequela_1 is modified from children [11, 12, 13, 14] to [11, 12] and sequela_2 is modified from children [21, 22, 23, 24] to [21, 22]. This should cause sequela_ids 13, 14, 23, 24 to have their parent and path to top parent attributes changed to their own id. """ db = two_sets_four_versions_sqlite session = db.session version_1 = session.query(SequelaSetVersion).get(1) seq_1 = session.query(SequelaHierarchyHistory).filter( SequelaHierarchyHistory.sequela_id == 1).first() seq_1_children = seq_1.children num_seq_1_children_before = len(seq_1_children) seq_2 = session.query(SequelaHierarchyHistory).filter( SequelaHierarchyHistory.sequela_id == 2).first() seq_2_children = seq_2.children num_seq_2_children_before = len(seq_2_children) request = { 'sequela_hierarchy_history': [{ 'sequela_set_version_id': 1, 'sequela_id': 1, 'sequela_name': 'test sequela 1', 'cause_id': 294, 'modelable_entity_id': 1109, 'healthstate_id': 1, 'children': [11, 21] }, { 'sequela_set_version_id': 1, 'sequela_id': 2, 'sequela_name': 'test sequela 2', 'cause_id': 295, 'modelable_entity_id': 1110, 'healthstate_id': 2, 'children': [12, 22] }] } handler = RequestHandler(session) handler.process_request(request) # We've modified the old hierarchies to take away two children assert (len(seq_1_children) == len(seq_2_children) == num_seq_1_children_before - 2) # Since sequela 13, 14, 23, 24 have been removed from their parents, # each of them should now have the global parent as their parent id adj_children_ids = [13, 14, 23, 24] adjusted_children = version_1.fk_sequela_hierarchy_history.filter( SequelaHierarchyHistory.sequela_id.in_(adj_children_ids)).all() assert all(child.parent_id == 0 for child in adjusted_children)
def test_request_delete_sequela_set(two_sets_four_versions_sqlite): db = two_sets_four_versions_sqlite session = db.session set1 = session.query(SequelaSet).get(1) request = {'sequela_set': {'sequela_set_id': 1, 'is_delete': True}} handler = RequestHandler(session) handler.process_request(request) session.commit() assert set1.last_updated_action == 'DELETE'
def test_add_rei(two_sets_four_versions_sqlite): db = two_sets_four_versions_sqlite session = db.session num_seqs_before = len(session.query(Sequela).all()) request = { 'sequela': [{ 'sequela_id': None, 'sequela_name': 'this is my name', 'sequela_rei_history': [{ 'sequela_set_version_id': 1, 'rei_id': 82 }] }], 'sequela_rei_history': [{ 'sequela_id': 3, 'sequela_set_version_id': 1, 'rei_id': 86 }, { 'sequela_id': 3, 'sequela_set_version_id': 1, 'rei_id': 87 }] } handler = RequestHandler(session) handler.process_request(request) version = session.query(SequelaSetVersion).get(1) version_reis = version.fk_sequela_rei_history # version 1 should now have 3 rei rows assert len(version_reis.all()) == 3 # version 1, sequela 3 should have 2 rei rows seq3 = version_reis.filter(SequelaReiHistory.sequela_id == 3).all() assert len(seq3) == 2 # there should be one new sequela num_seqs_after = len(session.query(Sequela).all()) assert num_seqs_after == num_seqs_before + 1 # the new seq should have 1 rei row new_seq_id = session.query(Sequela).filter( Sequela.sequela_name == 'this is my name').one().sequela_id new_seq_rei = version_reis.filter( SequelaReiHistory.sequela_id == new_seq_id).all() assert len(new_seq_rei) == 1
def test_request_delete_one_sequela(two_sets_four_versions_sqlite): db = two_sets_four_versions_sqlite session = db.session seq2 = session.query(Sequela).get(2) request = {'sequela': [{'sequela_id': 2, 'is_delete': True}]} handler = RequestHandler(session) handler.process_request(request) session.commit() assert seq2.last_updated_action == 'DELETE' assert seq2.active_end is not None
def test_request_modify_sequela(two_sets_four_versions_sqlite): db = two_sets_four_versions_sqlite session = db.session seq11 = session.query(Sequela).get(11) old_name = seq11.sequela_name request = {'sequela': [{'sequela_id': 11, 'sequela_name': old_name + 'NEW'}]} handler = RequestHandler(session) handler.process_request(request) session.commit() assert seq11.sequela_name == old_name + 'NEW'
def test_add_single_hierarchy(two_sets_four_versions_sqlite): db = two_sets_four_versions_sqlite session = db.session version_2 = session.query(SequelaSetVersion).get(2) num_version_2_shh_before = len( version_2.fk_sequela_hierarchy_history.all()) sequela = session.query(Sequela).all() num_seq_before = len(sequela) max_sequela_id = max([seq.sequela_id for seq in sequela]) shh_to_add = { 'sequela': [{ 'sequela_id': 61, 'sequela_name': 'new sequela 1', 'sequela_hierarchy_history': { 'sequela_set_version_id': 2, 'cause_id': 295, 'modelable_entity_id': 1109, 'healthstate_id': 1 } }, { 'sequela_id': None, 'sequela_name': 'new sequela', 'sequela_hierarchy_history': { 'sequela_set_version_id': 2, 'cause_id': 295, 'modelable_entity_id': 1110, 'healthstate_id': 2 } }] } handler = RequestHandler(session) handler.process_request(shh_to_add) version_2 = session.query(SequelaSetVersion).get(2) # there were 12 shh rows associated with version 2, we added two assert len(version_2.fk_sequela_hierarchy_history.all()) == ( num_version_2_shh_before + 2) # one of the two new shh rows is also a new sequela assert len(session.query(Sequela).all()) == num_seq_before + 1 # the name of the last sequela added should be 'new_sequela' test_seq = session.query(Sequela).get(max_sequela_id + 1) assert test_seq.sequela_name == 'new sequela'
def test_request_delete_two_sequela(two_sets_four_versions_sqlite): db = two_sets_four_versions_sqlite session = db.session seqs_to_del = session.query(Sequela).filter(Sequela.sequela_id.in_([2, 4])) request = {'sequela': []} for seq in seqs_to_del: request['sequela'].append({'sequela_id': seq.sequela_id, 'is_delete': True}) handler = RequestHandler(session) handler.process_request(request) session.commit() for seq in seqs_to_del: assert seq.last_updated_action == 'DELETE' assert seq.active_end is not None
def test_request_modify_sequela_set(two_sets_four_versions_sqlite): db = two_sets_four_versions_sqlite session = db.session set1 = session.query(SequelaSet).get(1) set2 = session.query(SequelaSet).get(2) old_name1 = set1.sequela_set_name old_name2 = set2.sequela_set_name request = {'sequela_set': [ {'sequela_set_id': 1, 'sequela_set_name': old_name2 + 'SWAPPED'}, {'sequela_set_id': 2, 'sequela_set_name': old_name1 + 'SWAPPED'}]} handler = RequestHandler(session) handler.process_request(request) session.commit() assert old_name1 + 'SWAPPED' == set2.sequela_set_name assert old_name2 + 'SWAPPED' == set1.sequela_set_name
def test_request_modify_sequela_set_version(two_sets_four_versions_sqlite): db = two_sets_four_versions_sqlite session = db.session version1 = session.query(SequelaSetVersion).get(1) old_desc = version1.sequela_set_version_description old_just = version1.sequela_set_version_justification request = {'sequela_set_version': { 'sequela_set_id': 1, 'sequela_set_version_id': 1, 'sequela_set_version_description': 'Changing this thang', 'sequela_set_version_justification': 'BECAUSE'}} handler = RequestHandler(session) handler.process_request(request) session.commit() assert version1.sequela_set_version_description != old_desc assert version1.sequela_set_version_justification != old_just assert version1.sequela_set_version_description == 'Changing this thang' assert version1.sequela_set_version_justification == 'BECAUSE'
def test_add_single_hierachy(two_sets_four_versions_sqlite): db = two_sets_four_versions_sqlite session = db.session version_1 = session.query(SequelaSetVersion).get(1) # create new aggregate # assign both most detailed 3 and 4 to new aggregate new_aggregate = { 'sequela': [{ 'sequela_id': None, 'sequela_name': 'aggregate of id 3 and 4', 'sequela_hierarchy_history': { 'sequela_set_version_id': 1, 'children': [3, 4], 'cause_id': 294, 'modelable_entity_id': 1109, 'healthstate_id': 4 } }] } handler = RequestHandler(session) handler.process_request(new_aggregate) all_sequela = version_1.fk_sequela_hierarchy_history.all() new_agg_id = max([seq.sequela_id for seq in all_sequela]) new_agg = session.query(SequelaHierarchyHistory).get([1, new_agg_id]) children = new_agg.children assert len(children) == 2 seq_3_parent = session.query(SequelaHierarchyHistory).get([1, 3]).parent_id seq_4_parent = session.query(SequelaHierarchyHistory).get([1, 4]).parent_id assert seq_3_parent == seq_4_parent == new_agg_id
from glob import glob import os import pandas as pd from epic_db.database import config from epic_db.models import * from epic_db.requests import RequestHandler eng = ('mysql://{user}:{password}@modeling-mortality-db.ihme.washington.edu:' '3306/epic?charset=utf8&use_unicode=1'.format( user=os.environ.get('EPIC_DB_USER'), password=os.environ.get('EPIC_DB_PASS'))) config.create_engine(eng) session = config.Session() # Operations on SequelaSetVersion 16 v16 = session.query(SequelaSetVersion).get(16) # Create request here request = {} # Send request through the handler handler = RequestHandler(session) handler.process_request(request) # commit changes session.commit()
def test_multiple(two_sets_four_versions_sqlite): db = two_sets_four_versions_sqlite session = db.session set_1 = session.query(SequelaSet).get(1) num_set_1_versions_before = len(set_1.fk_sequela_set_version_id.all()) old_seqs = session.query(Sequela).all() old_seq_ids = [seq.sequela_id for seq in old_seqs] to_add = { 'sequela_set_version': { 'sequela_set_id': 1, 'sequela_set_version_id': None, 'sequela_set_version': 'new version', 'sequela_set_version_description': 'a new version to use', 'sequela_set_version_justification': 'cuz we wants it', 'sequela': [{ 'sequela_id': 61, 'sequela_name': 'new sequela 1', 'sequela_hierarchy_history': { 'cause_id': 295, 'modelable_entity_id': 1109, 'healthstate_id': 1 } }, { 'sequela_id': None, 'sequela_name': 'new sequela 2', 'sequela_hierarchy_history': { 'cause_id': 295, 'modelable_entity_id': 1110, 'healthstate_id': 2 } }] } } handler = RequestHandler(session) handler.process_request(to_add) # we added one version to set_1 assert len( set_1.fk_sequela_set_version_id.all()) == (num_set_1_versions_before + 1) # get that version by name new_version = session.query(SequelaSetVersion).filter( SequelaSetVersion.sequela_set_version == 'new version').one() new_shh = new_version.fk_sequela_hierarchy_history all_sequela = session.query(Sequela).all() # that version should have 2 shh rows associated with it assert len(new_shh.all()) == 2 # one of those two new versions should have old sequela_id 61 assert len( new_shh.filter(SequelaHierarchyHistory.sequela_id == 61).all()) == 1 # we added one new sequela assert len(all_sequela) == (len(old_seqs) + 1) # one of the two we added should also have an shh row in the new version new_seq_ids = [ seq.sequela_id for seq in all_sequela if seq.sequela_id not in old_seq_ids ] new_shh_new_seq = [ shh.sequela_id for shh in new_shh if shh.sequela_id in new_seq_ids ] assert len(new_shh_new_seq) == 1