def test_has_field_value_check_for_field_and_value(eset_json): fields_w_values = { "schema_version": "2", "accession": "4DNES4GSP9S4", "aliases": "ren:HG00512_repset" } for f, v in fields_w_values.items(): assert scu.has_field_value(eset_json, f, v)
def make_tag_patch(item, tag): if not scu.has_field_value(item, 'tags', tag): # not already tagged with this tag so make a patch and add 2 dict if item.get('tags'): tags = item['tags'] tags.append(tag) else: tags = [tag] return {'tags': tags} return None
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_has_field_value_value_not_found_as_string(eset_json): not_found = 'unfound' assert not scu.has_field_value(eset_json, 'status', not_found)
def test_has_field_value_value_not_found_in_list(eset_json): not_found = 'unfound' assert not scu.has_field_value(eset_json, 'experiments_in_set', not_found)
def test_has_field_value_check_for_field_w_item_no_dict(): f = "b" v = "/labs/david-gilbert-lab/" assert not scu.has_field_value('blah', f, v, True)
def test_has_field_value_check_for_field_w_item_not_found(bs_embed_json): f = "lab" v = "/labs/nobodys-lab/" assert not scu.has_field_value(bs_embed_json, f, v, True)
def test_has_field_value_check_for_field_w_item(bs_embed_json): f = "lab" v = "/labs/david-gilbert-lab/" assert scu.has_field_value(bs_embed_json, f, v, True)
def test_has_field_value_no_it_doesnt(eset_json): fieldnames = ['biosample', 'blah', 'bio_rep_no'] for f in fieldnames: assert not scu.has_field_value(eset_json, f)
def test_has_field_value_check_for_field_only(eset_json): fieldnames = ['schema_version', 'award', 'alternate_accessions'] for f in fieldnames: assert scu.has_field_value(eset_json, f)