def test_get_linked_items_w_type_in_no_children(auth, mocker):
    mocker.patch('functions.script_utils.get_metadata',
                 side_effect=[{
                     'status': 'current'
                 }, {
                     '@type': ['Publication']
                 }])
    iids = scu.get_linked_items(auth, 'test_id')
    assert iids['test_id'] == 'Publication'
def test_get_linked_items_w_no_type(auth, mocker):
    mocker.patch('functions.script_utils.get_metadata',
                 side_effect=[{
                     'status': 'released'
                 }, {
                     'field': 'value'
                 }])
    iids = scu.get_linked_items(auth, 'test_id')
    assert not iids
Example #3
0
def main():  # pragma: no cover
    args = get_args()
    try:
        auth = get_authentication_with_server(args.key, args.env)
    except Exception:
        print("Authentication failed")
        sys.exit(1)
    itemids = scu.get_item_ids_from_args(args.input, auth, args.search)
    taggable = scu.get_types_that_can_have_field(auth, 'tags')
    if args.types2exclude is not None:
        # remove explicitly provide types not to tag
        taggable = [t for t in taggable if t not in args.types2exclude]

    seen = [
    ]  # only need to add tag once so this keeps track of what's been seen
    to_patch = {}  # keep track of those to patch
    # main loop through the top level item ids
    for itemid in itemids:
        items2tag = {}
        if args.taglinked:
            # need to get linked items and tag them
            linked = scu.get_linked_items(auth, itemid, {})
            items2tag = scu.filter_dict_by_value(linked,
                                                 taggable,
                                                 include=True)
        else:
            # only want to tag provided items
            itype = scu.get_item_type(auth, itemid)
            if itype in taggable:
                items2tag = {itemid: itype}
        for i, t in items2tag.items():
            if i not in seen:
                seen.append(i)
                item = get_metadata(i, auth)
                if not scu.has_field_value(item, 'tags', args.tag):
                    # not already tagged with this tag so make a patch and add 2 dict
                    to_patch[i] = make_tag_patch(item, args.tag)

    # now do the patching or reporting
    for pid, patch in to_patch.items():
        if args.dbupdate:
            pres = patch_metadata(patch, pid, auth)
            print(pres['status'])
        else:
            print("DRY RUN: patch ", pid, " with ", patch)
def test_get_linked_items_w_no_linked_foundids(auth, mocker):
    goodids = ['test_id']
    badids = ['6256801c-9c6e-4563-a97a-a295fccf5f07']
    resp1 = {
        'status': 'released',
        '@type': ['Biosample', 'Item'],
        'biosource': '7256801c-9c6e-4563-a97a-a295fccf5f07',
        'attachment': '6256801c-9c6e-4563-a97a-a295fccf5f07'
    }
    found = {}
    mocker.patch('functions.script_utils.get_metadata',
                 side_effect=[resp1, {
                     '@type': ['Biosample']
                 }])
    mocker.patch('functions.script_utils.find_uuids', return_value=None)
    iids = scu.get_linked_items(auth, 'test_id', found)
    for i in iids:
        assert i in goodids
        assert i not in badids
def test_get_linked_item_ids_w_recursive(auth, mocker):
    goodids = [
        'test_id', '7256801c-9c6e-4563-a97a-a295fccf5f07',
        '5256801c-9c6e-4563-a97a-a295fccf5f07'
    ]
    badids = ['6256801c-9c6e-4563-a97a-a295fccf5f07']
    resp1 = {
        'status': 'released',
        '@type': ['Biosample', 'Item'],
        'biosource': '7256801c-9c6e-4563-a97a-a295fccf5f07',
        'attachment': '6256801c-9c6e-4563-a97a-a295fccf5f07'
    }
    resp1 = OrderedDict(sorted(resp1.items(), key=lambda t: t[0]))
    resp2 = {
        'status': 'released',
        '@type': ['Biosource', 'Item'],
        'biosource_vendor': '5256801c-9c6e-4563-a97a-a295fccf5f07'
    }
    resp2 = OrderedDict(sorted(resp2.items(), key=lambda t: t[0]))
    resp3 = {
        'status': 'released',
    }
    mocker.patch('functions.script_utils.get_metadata',
                 side_effect=[
                     resp1, {
                         '@type': ['Biosample']
                     }, resp2, {
                         '@type': ['Biosource']
                     }, resp3, {
                         '@type': ['Vendor']
                     }
                 ])
    mocker.patch('functions.script_utils.find_uuids',
                 side_effect=[
                     None, ['7256801c-9c6e-4563-a97a-a295fccf5f07'], None,
                     None, ['5256801c-9c6e-4563-a97a-a295fccf5f07'], None, None
                 ])
    iids = scu.get_linked_items(auth, 'test_id')
    for i in iids:
        assert i in goodids
        assert i not in badids
def main():
    args = get_args(sys.argv[1:])
    try:
        auth = get_authentication_with_server(args.key, args.env)
    except Exception:
        print("Authentication failed")
        sys.exit(1)
    itemids = scu.get_item_ids_from_args(args.input, auth, args.search)
    excluded_types = get_excluded(args.types2exclude, args.types2include)
    no_child = ['Publication', 'Lab', 'User', 'Award']  # default no_childs
    if args.no_children:
        no_child.extend(args.no_children)
        no_child = list(set(no_child))

    all_linked_ids = []
    # main loop through the top level item ids
    for itemid in itemids:
        linked = scu.get_linked_items(auth, itemid, {})
        if excluded_types is not None:
            linked = scu.filter_dict_by_value(linked,
                                              excluded_types,
                                              include=False)
        ll = [(k, linked[k]) for k in sorted(linked, key=linked.get)]
        for i, t in ll:
            suff = ''
            if i == itemid:
                suff = '\tINPUT'
            if is_released(i, auth):
                suff = '\tRELEASED' + suff
                if not args.include_released:
                    print(i, '\t', t, '\tSKIPPING', suff)
                    continue
            if i not in all_linked_ids:
                all_linked_ids.append(i)
            else:
                suff = suff + '\tSEEN'
            print(i, '\t', t, suff)
    for a in all_linked_ids:
        print(a)
def test_get_linked_items_w_error_status(auth, mocker):
    mocker.patch('functions.script_utils.get_metadata',
                 return_value={'status': 'error'})
    iids = scu.get_linked_items(auth, 'test_id')
    assert not iids
def test_get_linked_items_w_item_in_found(auth):
    itemid = 'itemid'
    found_items = {itemid: 1}
    iid = scu.get_linked_items(auth, itemid, found_items)
    assert itemid in iid