def test_study_list_and_config(self): SL_SUBMIT_URI = DOMAIN + '/v3/study_list' r = test_http_json_method(SL_SUBMIT_URI, 'GET', expected_status=200, return_bool_data=True) self.assertTrue(r[0]) sl = r[1] DEP_SUBMIT_URI = DOMAIN + '/v3/phylesystem_config' r = test_http_json_method(DEP_SUBMIT_URI, 'GET', expected_status=200, return_bool_data=True) self.assertTrue(r[0]) full_config = r[1] GEN_SUBMIT_URI = DOMAIN + '/v4/study/config' r = test_http_json_method(GEN_SUBMIT_URI, 'GET', expected_status=200, return_bool_data=True) self.assertTrue(r[0]) gen_config = r[1] check_study_list_and_config_response(self, sl, full_config, gen_config) if not sl: return doc_id = sl[0] EXT_SUBMIT_URI = DOMAIN + '/v4/study/external_url/{}'.format(doc_id) r = test_http_json_method(EXT_SUBMIT_URI, 'GET', expected_status=200, return_bool_data=True) self.assertTrue(r[0]) e = r[1] check_external_url_response(self, doc_id, e)
def do_tree_test_get(study_id, tree_id): NEXUS_SUBMIT_URI = DOMAIN + '/v1/study/{}/tree/{}.nex'.format(study_id, tree_id) t = get_response_from_http(NEXUS_SUBMIT_URI, 'GET') nexus = t.content if not nexus.startswith('#NEXUS'): sys.exit('Did not get content starting with #NEXUS') data = {'tip_label': 'ot:ottTaxonName'} NEWICK_SUBMIT_URI = DOMAIN + '/v1/study/{}/tree/{}.tre'.format(study_id, tree_id) r = test_http_json_method(NEWICK_SUBMIT_URI, 'GET', data=data, expected_status=200, return_bool_data=True, is_json=False) if r[0]: assert r[1].startswith('(') assert '[pre-ingroup-marker]' not in r[1] else: sys.exit(1) data['bracket_ingroup'] = True r = test_http_json_method(NEWICK_SUBMIT_URI, 'GET', data=data, expected_status=200, return_bool_data=True, is_json=False) if r[0]: assert r[1].startswith('(') assert '[pre-ingroup-marker]' in r[1] else: sys.exit(1)
def simple_test(path, input, check=None, expected_status=200, is_right=(lambda x:True)): DOMAIN = config('host', 'apihost') url = DOMAIN + path try: # print 'checking', url (win, output) = test_http_json_method(url, verb='POST', data=input, expected_status=expected_status, return_bool_data=True) if not win: # Error message already printed print '** http lose' return 1 if isinstance(output, dict) and 'error' in output: # Should be 400, not 200 print '** error', output return 1 elif check == None: return 0 elif check(output, ''): if is_right(output): return 0 else: print '** result is not right', output return 1 else: return 1 except Exception, e: print '** exception', e traceback.print_exc() return 1
def get_study_list(): sl_uri = DOMAIN + '/v3/study/list' r = test_http_json_method(sl_uri, 'GET', expected_status=200, return_bool_data=True) assert r[0] return r[1]
def test_study_list_and_config(self): UB_SUBMIT_URI = DOMAIN + '/v4/study/unmerged_branches' r = test_http_json_method(UB_SUBMIT_URI, 'GET', expected_status=200, return_bool_data=True) self.assertTrue(r[0]) check_unmerged_response(self, r[1])
def test_index(self): global DOMAIN m = pre_v.match(DOMAIN) if m: DOMAIN = m.group(1) I_SUBMIT_URI = DOMAIN + '/v4/study/config' r = test_http_json_method(I_SUBMIT_URI, 'GET', expected_status=200, return_bool_data=True) self.assertTrue(r[0]) check_config_response(self, r[1])
def find_unincluded_tree_for_study(taboo_list, study_id): A_STUDY_URI = DOMAIN + '/v4/study' + '/' + study_id # Put of empty data should result in 400 r = test_http_json_method(A_STUDY_URI, 'GET', expected_status=200, return_bool_data=True) if not r[0]: sys.exit(1) study_obj = r[1]['data'] for tp in tree_iter_nexson_proxy(study_obj): if (tp.tree_id is not None) and (tp.tree_id not in taboo_list): return tp.tree_id return None
def fetch_existing(new_id): study_id = new_id a_study_url = BASE_STUDY_URI + '/' + study_id data = {'output_nexml2json': '1.0.0'} r = test_http_json_method(a_study_url, "GET", data=data, expected_status=200, return_bool_data=True) assert r[0] edit_resp = r[1] return edit_resp['data'], edit_resp
def do_post_test(orig_study_list, data): global to_del, DATA post_resp = test_http_json_method(BASE_STUDY_URI, 'POST', data=data, expected_status=200, return_bool_data=True) if not post_resp[0]: assert False create_resp = post_resp[1] posted_id = create_resp['resource_id'] to_del.append(posted_id) debug('post created {}'.format(posted_id)) assert posted_id not in orig_study_list # Put of empty data should result in 400 resp = test_http_json_method(BASE_STUDY_URI + '/' + posted_id, 'PUT', DATA, expected_status=400) assert resp test_mod_of_existing(posted_id) return fetch_existing(posted_id)[0]
def check_node_info(parameters): DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v3/tree_of_life/node_info' test, result, _ = test_http_json_method(SUBMIT_URI, "POST", data=parameters, expected_status=200, return_bool_data=True) if not test: sys.exit(1) node_id_key = u'node_id' check_node_result(result, node_id_key) # check_node_expectation(parameters, result, node_id_key, ...name...) valid_keys = [node_id_key, u'taxon', u'nearest_taxon', u'synth_id'] check_result_keys(result, valid_keys) sys.exit(exit_status[0])
def fetch_current_synth_study2tree_mapping(): SUBMIT_URI = DOMAIN + '/v3/trees_in_synth' r = test_http_json_method(SUBMIT_URI, 'GET', expected_status=200, return_bool_data=True) if not r[0]: sys.stderr.write("Note that the test trees_in_synth.py will fail if your test collections repo does not have " \ "collections that have the same IDs as the collections currently used in synthesis.\n") sys.exit(1) decisions_list = r[1]['decisions'] m = {} for d in decisions_list: m.setdefault(d["studyID"], []).append(d["treeID"]) return m
def test_mod_of_existing(new_id): study_id = new_id a_study_url = BASE_STUDY_URI + '/' + study_id study_obj, edit_resp = fetch_existing(new_id) starting_commit_sha = edit_resp['sha'] # refresh a timestamp so that the test generates a commit study_obj['nexml']['^bogus_timestamp'] = datetime.datetime.utcnow().isoformat() data = {'nexson': study_obj, 'auth_token': auth_token, 'starting_commit_SHA': starting_commit_sha, } r = test_http_json_method(a_study_url, 'PUT', data=data, expected_status=200, return_bool_data=True) assert r[0]
def test_mod_of_existing(new_id): study_id = new_id a_study_url = BASE_STUDY_URI + '/' + study_id study_obj, edit_resp = fetch_existing(new_id) starting_commit_sha = edit_resp['sha'] # refresh a timestamp so that the test generates a commit study_obj['nexml']['^bogus_timestamp'] = datetime.datetime.utcnow( ).isoformat() data = { 'nexson': study_obj, 'auth_token': auth_token, 'starting_commit_SHA': starting_commit_sha, } r = test_http_json_method(a_study_url, 'PUT', data=data, expected_status=200, return_bool_data=True) assert r[0]
def simple_test(path, input, check=None, expected_status=200, is_right=(lambda x: True)): DOMAIN = config('host', 'apihost') url = DOMAIN + path try: # print 'checking', url (win, output) = test_http_json_method(url, verb='POST', data=input, expected_status=expected_status, return_bool_data=True) if not win: # Error message already printed print '** http lose' return 1 if isinstance(output, dict) and 'error' in output: # Should be 400, not 200 print '** error', output return 1 elif check == None: return 0 elif check(output, ''): if is_right(output): return 0 else: print '** result is not right', output return 1 else: return 1 except Exception, e: print '** exception', e traceback.print_exc() return 1
#!/usr/bin/env python from opentreetesting import test_http_json_method, writable_api_host_and_oauth_or_exit from peyotl import convert_nexson_format import datetime import codecs import json import sys import os DOMAIN, auth_token = writable_api_host_and_oauth_or_exit(__file__) study_id = 12 URL = DOMAIN + '/phylesystem/v1/study/%s' % study_id r = test_http_json_method(URL, 'GET', expected_status=200, return_bool_data=True, headers={ 'content-type': 'text/plain', 'accept': 'text/plain' }, is_json=True) if not r[0]: sys.exit(1) starting_commit_SHA = r[1]['branch2sha']['master'] SUBMIT_URI = DOMAIN + '/phylesystem/v1/study/%s' % study_id fn = 'data/{s}.json'.format(s=study_id) inpf = codecs.open(fn, 'rU', encoding='utf-8') n = json.load(inpf) # refresh a timestamp so that the test generates a commit m = n['nexml']['meta'] short_list = [i for i in m if i.get('@property') == 'bogus_timestamp'] if short_list:
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v3/amendments/list_all' r = test_http_json_method(SUBMIT_URI, 'GET', expected_status=200, return_bool_data=True) if not r[0]: sys.exit(1) #print r[1]
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v3/tnrs/contexts' test, result = test_http_json_method(SUBMIT_URI, "POST", expected_status=200, return_bool_data=True) if not test: sys.exit(1) if not (u'PLANTS' in result and u'ANIMALS' in result and u'FUNGI' in result and u'LIFE' in result and u'MICROBES' in result): errstr = 'Missing key in context listing \n {} \n' sys.stderr.write(errstr.format(result)) sys.exit(1) if u'Archaea' not in result[u'MICROBES']: sys.stderr.write('Archaea not in context MICROBES\n') sys.exit(1) if u'Arachnides' not in result[u'ANIMALS']: # spelling? sys.stderr.write('Arachnides not in context Animals\n') sys.exit(1)
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') if len(sys.argv) > 1: study_id = sys.argv[1] else: study_id = 12 SUBMIT_URI = DOMAIN + '/v1/study/%s' % study_id data = { 'auth_token': os.environ.get('GITHUB_OAUTH_TOKEN', 'bogus_token'), } if test_http_json_method(SUBMIT_URI, 'DELETE',data=data, expected_status=200): sys.exit(0) sys.exit(1)
posted_id = create_resp['resource_id'] to_del.append(posted_id) debug('post created {}'.format(posted_id)) assert posted_id not in orig_study_list # Put of empty data should result in 400 resp = test_http_json_method(BASE_STUDY_URI + '/' + posted_id, 'PUT', DATA, expected_status=400) assert resp test_mod_of_existing(posted_id) return fetch_existing(posted_id)[0] # Can't PUT w/o an ID if not test_http_json_method(BASE_STUDY_URI, 'PUT', DATA, expected_status=404): sys.exit(1) rc = 1 try: orig_study_list = get_study_list() # empty POST do_post_test(orig_study_list, DATA) # POST via DOI doi_data = dict(DATA) doi_data["import_method"] = "import-method-PUBLICATION_DOI" doi = "10.3732/ajb.0800060" doi_data["publication_DOI"] = doi new_study = do_post_test(orig_study_list, doi_data) assert doi in str(new_study) rc = 0 finally:
#!/usr/bin/env python from opentreetesting import test_http_json_method, writable_api_host_and_oauth_or_exit import datetime import codecs import json import sys import os DOMAIN, auth_token = writable_api_host_and_oauth_or_exit(__file__) # this makes it easier to test concurrent pushes to different branches study_id = "12" URL = DOMAIN + '/phylesystem/v1/study/%s' % study_id r = test_http_json_method(URL, 'GET', expected_status=200, return_bool_data=True, headers={'content-type':'text/plain', 'accept':'text/plain'}, is_json=True) if not r[0]: sys.exit(1) starting_commit_SHA = r[1]['branch2sha']['master'] study_id="12" SUBMIT_URI = DOMAIN + '/phylesystem/v1/study/{s}'.format(s=study_id) fn = 'data/{s}.json'.format(s=study_id) inpf = codecs.open(fn, 'rU', encoding='utf-8') n = json.load(inpf) # refresh a timestamp so that the test generates a commit m = n['nexml']['meta'] short_list = [i for i in m if i.get('@property') == 'bogus_timestamp'] if short_list: el = short_list[0]
posted_id = create_resp['resource_id'] to_del.append(posted_id) debug('post created {}'.format(posted_id)) assert posted_id not in orig_study_list # Put of empty data should result in 400 resp = test_http_json_method(BASE_STUDY_URI + '/' + posted_id, 'PUT', DATA, expected_status=400) assert resp test_mod_of_existing(posted_id) return fetch_existing(posted_id)[0] # Can't PUT w/o an ID if not test_http_json_method(BASE_STUDY_URI, 'PUT', DATA, expected_status=404): sys.exit(1) rc = 1 try: orig_study_list = get_study_list() # empty POST do_post_test(orig_study_list, DATA) # POST via DOI doi_data = dict(DATA) doi_data["import_method"] = "import-method-PUBLICATION_DOI" doi = "10.3732/ajb.0800060" doi_data["publication_DOI"] = doi new_study = do_post_test(orig_study_list, doi_data) assert doi in str(new_study) rc = 0
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v2/taxonomy/taxon' test, result = test_http_json_method(SUBMIT_URI, "POST", data={ "ott_id": 515698, "include_lineage": "true" }, expected_status=200, return_bool_data=True) if not test: sys.exit(1) if u'ot:ottId' not in result: sys.stderr.write('ot:ottId not found in result') sys.exit(1) if result[u'ot:ottId'] != 515698: sys.stderr.write('Incorrect ottid in returned taxon {}', format(result[u'ot:ottId'])) sys.exit(1) if u'taxonomic_lineage' not in result: errstr = 'No lineage returned when expected in taxon report: {}' sys.stderr.write(errstr.format(result)) sys.exit(1)
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v3/taxonomy/mrca' # added 3826 from asterales test, _ = test_http_json_method(SUBMIT_URI, "POST", data={"ott_ids": [5551856, 821970, 770319]}, expected_status=400, return_bool_data=True) if not test: sys.exit(1)
def check_about(parameters): DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v3/tree_of_life/about' test, result, _ = test_http_json_method(SUBMIT_URI, "POST", data=parameters, expected_status=200, return_bool_data=True) if not test: sys.exit(1) """ Doc says: "synth_id" : "opentree4.1", "root_node_id" : "ott93302", # in v2 this was an integer "num_source_trees" : 484, "date_completed" : "2016-02-09 18:14:43", "taxonomy_version" : "2.9draft12", "num_tips" : 2424255, "num_source_studies" : 478, "filtered_flags" : [ "major_rank_conflict", "major_rank_conflict_inherited", "environmental", "unclassified_inherited", "unclassified", "viral", "barren", "not_otu", "incertae_sedis", "incertae_sedis_inherited", "extinct_inherited", "extinct", "hidden", "unplaced", "unplaced_inherited", "was_container", "inconsistent", "hybrid", "merged" ], "root_ott_id" : 93302 "root_taxon_name" : "cellular organisms", maybe: "root_taxon" : { ... } """ code = [0] def lose(): code[0] = 1 def check_result_type(name, typo): value = result.get(name) if not isinstance(value, typo): if value == None: sys.stderr.write('{} missing\n'.format(name)) print result.keys() else: sys.stderr.write('{} is not a {}\n'.format(name, typo)) lose() return None return value check_result_type(u'root_node_id', unicode) num_source_trees = check_result_type(u'num_source_trees', int) if num_source_trees != None and num_source_trees < 2: sys.stderr.write('not very many source trees: {}\n'.format(num_source_trees)) lose() check_result_type(u'date_completed', unicode) num_tips = check_result_type(u'num_tips', int) if num_tips != None and num_tips < 3: sys.stderr.write('not very many tips: {}\n'.format(num_tips)) lose() check_result_type(u'taxonomy_version', unicode) num_source_trees = check_result_type(u'num_source_trees', int) if num_source_trees != None and num_source_trees < 2: sys.stderr.write('not very trees: {}\n'.format(num_source_trees)) lose() num_source_studies = check_result_type(u'num_source_studies', int) if num_source_studies != None and num_source_studies < 2: sys.stderr.write('not very studies: {}\n'.format(num_source_studies)) lose() check_result_type(u'filtered_flags', list) if u'root_ott_id' in result: check_result_type(u'root_ott_id', int) check_result_type(u'root_taxon_name', unicode) source_list = parameters.get(u'source_list') if source_list == None or not source_list: if u'sources' in result: sys.stderr.write('source_list false but there are sources in result: {}\n'.format(source_list)) lose() else: sources = check_result_type(u'sources', list) metas = check_result_type(u'source_id_map', dict) if sources != None: for source in sources: if not isinstance(source, unicode): sys.stderr.write('ill-formed source: {} should be a string\n'.format(source)) lose() break if metas != None: for label in metas.keys(): meta = metas[label] if label == u'taxonomy': True elif (isinstance(meta, dict) and u'study_id' in meta and u'tree_id' in meta): True else: sys.stderr.write('ill-formed meta: {} should be {}"study_id": ..., "tree_id": ...{}\n'.format(meta, '{', '}')) lose() break sys.exit(code[0])
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') if test_http_json_method(DOMAIN, 'GET', expected_status=200): sys.exit(0) sys.exit(1)
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v1/study/10/tree/tree3.nex' data = {'tip_label': 'ot:ottTaxonName'} r = test_http_json_method(SUBMIT_URI, 'GET', data=data, expected_status=200, return_bool_data=True, is_json=False) if r[0]: print r[1] sys.exit(0) sys.exit(1)
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v2/taxonomy/taxon' TEST_NAME = u'Alseuosmia banksii' test, result, _ = test_http_json_method(SUBMIT_URI, "POST", data={"ott_id": 901642}, expected_status=200, return_bool_data=True) if not test: sys.exit(1) if u'ot:ottTaxonName' not in result: sys.stderr.write('ot:ottTaxonName not returned in result\n') sys.exit(1) taxonName = result[u'ot:ottTaxonName'] if taxonName != TEST_NAME: errstr = 'Expected taxon name {} but not found in \n{}\n' sys.stderr.write(errstr.format(TEST_NAME, taxonName)) sys.exit(1)
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v2/taxonomy/taxon' test, result = test_http_json_method(SUBMIT_URI, "POST", data={ "ott_id": 1066581, "list_terminal_descendants": "true" }, expected_status=200, return_bool_data=True) if not test: sys.exit(1) if u'terminal_descendants' not in result: sys.stderr.write('No terminal_descendants found in result\n') sys.exit(1) descendants = result[u'terminal_descendants'] if not set([490099, 1066590]).issubset(set(descendants)): sys.stderr.write( "Bos taurus (490099) and Bos primigenius (1066590) not returned as descendants of Bos (1066581)\n" ) sys.exit(1)
import codecs import json import sys import os DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v1/study/1003' inpf = codecs.open('../nexson-validator/tests/single/input/1003.json', 'rU', encoding='utf-8') n = json.load(inpf) # refresh a timestamp so that the test generates a commit m = n['nexml']['meta'] short_list = [i for i in m if i.get('@property') == 'bogus_timestamp'] if short_list: el = short_list[0] else: el = {'@property': 'bogus_timestamp', '@xsi:type': 'nex:LiteralMeta'} m.append(el) el['$'] = datetime.datetime.utcnow().isoformat() data = { 'nexson': n, 'auth_token': os.environ.get('GITHUB_OAUTH_TOKEN', 'bogus_token'), 'author_name': "Some Dude", 'author_email': "*****@*****.**", } if test_http_json_method(SUBMIT_URI, 'PUT', data=data, expected_status=200): sys.exit(0) sys.exit(1)
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v2/tnrs/match_names' TEST_LIST = ["Hylobates"] TEST_IDS = [166552] test, result = test_http_json_method(SUBMIT_URI, "POST", data={"names":TEST_LIST, "context_name": 'Mammals'}, expected_status=200, return_bool_data=True) if not test: sys.exit(1) if set(TEST_LIST) != set(result[u'matched_name_ids']): errstr = "Failed to match, submitted: {}, returned {}\n" sys.stderr.write(errstr.format(TEST_LIST,result[u'matched_name_ids'])) sys.exit(1) MATCH_LIST = result['results'] for match in MATCH_LIST: m = match[u'matches'][0] if m[u'matched_name'] not in TEST_LIST: errstr = "bad match return {}, expected one of {}\n" sys.stderr.write(errstr.format(m[u'matched_name'],str(TEST_LIST))) sys.exit(1) if m[u'ot:ottId'] not in TEST_IDS: errstr = "bad match return {}, expected one of {}\n" sys.stderr.write(errstr.format(m[u'ot:ottId'],str(TEST_IDS))) sys.exit(1)
def check_about(parameters): DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v3/tree_of_life/about' test, result, _ = test_http_json_method(SUBMIT_URI, "POST", data=parameters, expected_status=200, return_bool_data=True) if not test: sys.exit(1) """ Doc says: "synth_id" : "opentree4.1", "root_node_id" : "ott93302", # in v2 this was an integer "num_source_trees" : 484, "date_completed" : "2016-02-09 18:14:43", "taxonomy_version" : "2.9draft12", "num_tips" : 2424255, "num_source_studies" : 478, "filtered_flags" : [ "major_rank_conflict", "major_rank_conflict_inherited", "environmental", "unclassified_inherited", "unclassified", "viral", "barren", "not_otu", "incertae_sedis", "incertae_sedis_inherited", "extinct_inherited", "extinct", "hidden", "unplaced", "unplaced_inherited", "was_container", "inconsistent", "hybrid", "merged" ], "root_ott_id" : 93302 "root_taxon_name" : "cellular organisms", maybe: "root_taxon" : { ... } """ code = [0] def lose(): code[0] = 1 def check_result_type(name, typo): value = result.get(name) if not isinstance(value, typo): if value == None: sys.stderr.write('{} missing\n'.format(name)) print result.keys() else: sys.stderr.write('{} is not a {}\n'.format(name, typo)) lose() return None return value check_result_type(u'root_node_id', unicode) num_source_trees = check_result_type(u'num_source_trees', int) if num_source_trees != None and num_source_trees < 2: sys.stderr.write( 'not very many source trees: {}\n'.format(num_source_trees)) lose() check_result_type(u'date_completed', unicode) num_tips = check_result_type(u'num_tips', int) if num_tips != None and num_tips < 3: sys.stderr.write('not very many tips: {}\n'.format(num_tips)) lose() check_result_type(u'taxonomy_version', unicode) num_source_trees = check_result_type(u'num_source_trees', int) if num_source_trees != None and num_source_trees < 2: sys.stderr.write('not very trees: {}\n'.format(num_source_trees)) lose() num_source_studies = check_result_type(u'num_source_studies', int) if num_source_studies != None and num_source_studies < 2: sys.stderr.write('not very studies: {}\n'.format(num_source_studies)) lose() check_result_type(u'filtered_flags', list) if u'root_ott_id' in result: check_result_type(u'root_ott_id', int) check_result_type(u'root_taxon_name', unicode) source_list = parameters.get(u'source_list') if source_list == None or not source_list: if u'sources' in result: sys.stderr.write( 'source_list false but there are sources in result: {}\n'. format(source_list)) lose() else: sources = check_result_type(u'sources', list) metas = check_result_type(u'source_id_map', dict) if sources != None: for source in sources: if not isinstance(source, unicode): sys.stderr.write( 'ill-formed source: {} should be a string\n'.format( source)) lose() break if metas != None: for label in metas.keys(): meta = metas[label] if label == u'taxonomy': True elif (isinstance(meta, dict) and u'study_id' in meta and u'tree_id' in meta): True else: sys.stderr.write( 'ill-formed meta: {} should be {}"study_id": ..., "tree_id": ...{}\n' .format(meta, '{', '}')) lose() break sys.exit(code[0])
#!/usr/bin/env python import sys, os, re from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v2/tnrs/autocomplete_name' SEARCHNAME = "Endoxyla" test, result = test_http_json_method(SUBMIT_URI, "POST", data={ "name": SEARCHNAME, "context_name": "All life" }, expected_status=200, return_bool_data=True) if not test: sys.exit(1) if result == []: sys.stderr.write("Empty list returned\n") sys.exit(1) NAMECHECK = re.compile(SEARCHNAME) for item in result: uname = item[u'unique_name'] if uname is None: sys.stderr.write( "returned taxon record had no unique_name specified\n") sys.exit(1) elif not re.search(NAMECHECK, uname): failstr = "unique name: {} of taxon record does not contain search name: {}\n" sys.stderr.write(failstr.format(uname, SEARCHNAME)) sys.exit(1) sys.exit(0)
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v2/tnrs/infer_context' NAMESLIST = ["Pan","H**o","Mus musculus","Upupa epops"] test, result = test_http_json_method(SUBMIT_URI, "POST", data={"names":NAMESLIST}, expected_status=200, return_bool_data=True) if not test: sys.exit(1) if u'context_name' not in result: sys.stderr.write('No context_name returned in result.\n') sys.exit(1) if result[u'context_name'] != u'Tetrapods': errstr = 'Expected context = Tetrapods, returned {}\n' sys.stderr.write(errstr.format(result[u'context_name'])) sys.exit(1) if result[u'ambiguous_names'] != []: errstr = 'Expected no ambiguous_names, but found {}\n' sys.stderr.write(errstr.format(result[u'ambiguous_names'])) sys.exit(1)
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, writable_api_host_and_oauth_or_exit DOMAIN, auth_token = writable_api_host_and_oauth_or_exit(__file__) study = '10' SUBMIT_URI = DOMAIN + '/phylesystem/v1/study/' + study data = {'output_nexml2json':'1.2'} r = test_http_json_method(SUBMIT_URI, 'GET', data=data, expected_status=200, return_bool_data=True) d = r[1]['data'] c = d['nexml'].get('^ot:testCount', 0) if isinstance(c, list): c = c[0] c = c + 1 d['nexml']['^ot:testCount'] = c starting_commit_SHA = r[1]['sha'] data = { 'nexson' : d, 'auth_token': auth_token, 'starting_commit_SHA': starting_commit_SHA, } r2 = test_http_json_method(SUBMIT_URI, 'PUT', data=data, expected_status=200, return_bool_data=True) PUSH_URI = DOMAIN + '/phylesystem/push/v1/' + study r3 = test_http_json_method(PUSH_URI,
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config("host", "apihost") CONTROLLER = DOMAIN + "/v3/studies" SUBMIT_URI = CONTROLLER + "/find_studies" ################################################ # find studies curated by user, using verbose = False # study can have multiple curators p = {"verbose": False, "property": "ot:curatorName", "value": "Romina Gazis"} r = test_http_json_method(SUBMIT_URI, "POST", data=p, expected_status=200, return_bool_data=True) # structure of r is (true/false,json-results,true/false) assert r[0] is True json_result = r[1] print json_result assert len(json_result) > 0 # should return only study_Id top_level_key = json_result.keys()[0] assert top_level_key == "matched_studies" assert json_result[top_level_key][0].keys() == ["ot:studyId"] ################################################ # test error when value != string p = {"verbose": False, "property": "ot:curatorName", "value": 100} r = test_http_json_method(SUBMIT_URI, "POST", data=p, expected_status=400, return_bool_data=False) # structure of r is (true/false,json-results,true/false)
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v3/taxonomy/mrca' # added 3826 from asterales test, _ = test_http_json_method(SUBMIT_URI, "POST", data={"ott_ids":[5551856,821970,770319]}, expected_status=400, return_bool_data=True) if not test: sys.exit(1)
tree_id = find_unincluded_tree_for_study([], study_id) if tree_id is not None: return study_id, tree_id for study_id in check_later: tree_id = find_unincluded_tree_for_study(study2tree_list[study_id], study_id) if tree_id is not None: return study_id, tree_id return None, None # Get a mapping that reveals which trees are currently included orig_study_to_tree_list = fetch_current_synth_study2tree_mapping() # Find a (study_id, tree_id) pair that is not currently included SL_URI = DOMAIN + '/v3/study_list' r = test_http_json_method(SL_URI, 'GET', expected_status=200, return_bool_data=True) if not r[0]: sys.exit(1) study_list = r[1] study_id, tree_id = find_unincluded_study_tree_pair(orig_study_to_tree_list, study_list) if study_id is None: sys.stderr.write('Full {} not completed because all trees are in synth collection.\n'.format(sys.argv[0])) print(study_id, tree_id) # See if we can add the tree SUBMIT_URI = DOMAIN + '/v4/include_tree_in_synth' change_synth_data = {'auth_token': auth_token, 'study_id': study_id, 'tree_id': tree_id, } r = test_http_json_method(SUBMIT_URI, 'POST', data=change_synth_data, expected_status=200,
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/phylesystem/v1/study/10' data = {'output_nexml2json':'x0.0.0'} if test_http_json_method(SUBMIT_URI, 'GET', data=data, expected_status=400): sys.exit(0) sys.exit(1)
#!/usr/bin/env python import sys from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') if '/api.opentree' in DOMAIN: study = 'ot_134' else: study = 'pg_90' SUBMIT_URI = '{d}/v1/study/{s}/file'.format(d=DOMAIN, s=study) r = test_http_json_method(SUBMIT_URI, 'GET', expected_status=200, return_bool_data=True, is_json=True) if r[0]: fid = r[1][0]['id'] else: sys.exit(1) NEW_URL = SUBMIT_URI + '/bogusid' x = test_http_json_method(NEW_URL, 'GET', expected_status=404) NEW_URL = SUBMIT_URI + '/' + fid r = test_http_json_method(NEW_URL, 'GET', expected_status=200, return_bool_data=True, headers={ 'content-type': 'text/plain', 'accept': 'text/plain' }, is_json=False)
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v2/taxonomy/taxon' test, result = test_http_json_method(SUBMIT_URI, "POST", data={"ott_id":515698, "include_lineage":"true"}, expected_status=200, return_bool_data=True) if not test: sys.exit(1) if u'ot:ottId' not in result: sys.stderr.write('ot:ottId not found in result') sys.exit(1) if result[u'ot:ottId'] != 515698: sys.stderr.write('Incorrect ottid in returned taxon {}',format(result[u'ot:ottId'])) sys.exit(1) if u'taxonomic_lineage' not in result: errstr = 'No lineage returned when expected in taxon report: {}' sys.stderr.write(errstr.format(result)) sys.exit(1)
DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v1/study/' inpf = codecs.open('data/10.json', 'rU', encoding='utf-8') n = json.load(inpf) # refresh a timestamp so that the test generates a commit m = n['nexml']['^bogus_timestamp'] = datetime.datetime.utcnow().isoformat() data = { 'nexson': n, 'auth_token': os.environ.get('GITHUB_OAUTH_TOKEN', 'bogus_token'), 'cc0_agreement': 'true', } r = test_http_json_method(SUBMIT_URI, 'POST', data=data, expected_status=200, return_bool_data=True) if not r[0]: sys.exit(1) resp = r[1] starting_commit_SHA = resp['sha'] c_id = resp['resource_id'] print c_id DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v1/study/%s' % c_id data = { 'auth_token': os.environ.get('GITHUB_OAUTH_TOKEN', 'bogus_token'), 'starting_commit_SHA': starting_commit_SHA, }
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v2/taxonomy/lica' test, result = test_http_json_method(SUBMIT_URI, "POST", data={"ott_ids":[901642, 55033]}, expected_status=200, return_bool_data=True) if not test: sys.exit(1) if u'lica' not in result: sys.stderr.write('No lica found in returned: \n{}'.format(result)) sys.exit(1) lica = result[u'lica'] if u'ot:ottId' not in lica: sys.stderr.write('No ottId found in returned: \n{}'.format(result)) sys.exit(1) expectedId = 637370 if lica[u'ot:ottId'] != expectedId: errstr = 'Expected {} , found {}\n' sys.stderr.write(errstr.format(expectedId,lica[u'ot:ottId'])) sys.exit(1)
DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v1/study/' inpf = codecs.open('data/10.json', 'rU', encoding='utf-8') n = json.load(inpf) # refresh a timestamp so that the test generates a commit m = n['nexml']['^bogus_timestamp'] = datetime.datetime.utcnow().isoformat() data = { 'nexson' : n, 'auth_token': os.environ.get('GITHUB_OAUTH_TOKEN', 'bogus_token'), 'cc0_agreement' : 'true', } r = test_http_json_method(SUBMIT_URI, 'POST', data=data, expected_status=200, return_bool_data=True) if not r[0]: sys.exit(1) resp = r[1] starting_commit_SHA = resp['sha'] c_id = resp['resource_id'] print c_id DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v1/study/%s' % c_id data = { 'auth_token': os.environ.get('GITHUB_OAUTH_TOKEN', 'bogus_token'), 'starting_commit_SHA': starting_commit_SHA, }
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') # study 1 does not exist SUBMIT_URI = DOMAIN + '/v1/study/1' if test_http_json_method(SUBMIT_URI, 'GET', expected_status=404): sys.exit(0) sys.exit(1)
#!/usr/bin/env python import sys, os from opentreetesting import test_http_json_method, config DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v2/taxonomy/taxon' TEST_NAME = u'Alseuosmia banksii' test, result, _ = test_http_json_method(SUBMIT_URI, "POST", data={"ott_id":901642}, expected_status=200, return_bool_data=True) if not test: sys.exit(1) if u'ot:ottTaxonName' not in result: sys.stderr.write('ot:ottTaxonName not returned in result\n') sys.exit(1) taxonName = result[u'ot:ottTaxonName'] if taxonName != TEST_NAME: errstr = 'Expected taxon name {} but not found in \n{}\n' sys.stderr.write(errstr.format(TEST_NAME,taxonName)) sys.exit(1)
#!/usr/bin/env python from opentreetesting import test_http_json_method, writable_api_host_and_oauth_or_exit import datetime import codecs import json import sys import os DOMAIN, auth_token = writable_api_host_and_oauth_or_exit(__file__) study_id = '10' SUBMIT_URI = DOMAIN + '/phylesystem/v1/study/' + study_id data = {'output_nexml2json': '1.0.0'} r = test_http_json_method(SUBMIT_URI, 'GET', data=data, expected_status=200, return_bool_data=True) if not r[0]: sys.exit(0) resp = r[1] starting_commit_SHA = resp['sha'] SUBMIT_URI = DOMAIN + '/phylesystem/v1/study/{s}'.format(s=study_id) n = resp['data'] # refresh a timestamp so that the test generates a commit m = n['nexml']['^bogus_timestamp'] = datetime.datetime.utcnow().isoformat() data = { 'nexson': n, 'auth_token': auth_token, 'starting_commit_SHA': starting_commit_SHA, } r = test_http_json_method(SUBMIT_URI, 'PUT',
import datetime import codecs import copy import json import sys import os study_id = '99' DOMAIN = config('host', 'apihost') SUBMIT_URI = DOMAIN + '/v1/study/{s}'.format(s=study_id) #A full integration test, with GET, PUT, POST, MERGE and a merge conflict, #test get and save sha data = {'output_nexml2json': '1.2'} r = test_http_json_method(SUBMIT_URI, 'GET', data=data, expected_status=200, return_bool_data=True) assert (r[0] == True) start_sha = r[1]["sha"] blob = r[1]["data"] assert (start_sha) acurr_obj = blob zcurr_obj = copy.deepcopy(blob) # PUT edits to a study that should merge to master starting_commit_SHA = start_sha ac = acurr_obj['nexml'].get("^acount", 0) ac += 1