def test_route(setup): """ Test the server properly handle /taxon resources """ user = setup.user source_detail = SourceDetail(name=test.get_random_name(), description="the description") setup.session.add(source_detail) setup.session.commit() # TODO: test that POST with taxon and taxon_id both work, same with source_detail # create an accession first_accession = test.create_resource('/accession', { 'taxon_id': setup.taxon.id, 'code': test.get_random_name(), 'source': { 'id': source_detail.id, 'sources_code': test.get_random_name(), 'collection': { "locale": test.get_random_name() }, 'propagation': { 'prop_type': 'UnrootedCutting', 'media': "Fafard 3B" } }, # TODO: setting the plant propagation id would require us to create a second # accession, plant and plant propagation record and set that record # here 'plant_propagation': {} }, user) # create another accession and use the first as a synonym data = {'taxon_id': setup.taxon.id, 'code': test.get_random_name()} notes = [{'user': '******', 'category': 'test', 'date': '1/1/2001', 'note': 'test note'}, {'user': '******', 'category': 'test', 'date': '2/2/2001', 'note': 'test note2'}], synonyms = [{'synonym': first_accession}] second_accession = test.create_resource('/accession', data, user) assert 'id' in second_accession # created # update the accession second_accession['accession'] = test.get_random_name() second_accession['source'] = first_accession['source'] second_id = second_accession['id'] second_accession = test.update_resource('/accession/' + str(second_id), second_accession, user) assert second_accession['id'] == second_id # make sure they have the same ref after the update # get the accession first_accession = test.get_resource('/accession/' + str(first_accession['id']), user=user) # query for taxa accessions = test.query_resource('/accession', q=second_accession['code'], user=user) assert second_accession['id'] in [accession['id'] for accession in accessions] # delete the created resources test.delete_resource('/accession/' + str(first_accession['id']), user) test.delete_resource('/accession/' + str(second_accession['id']), user)
def test_server(setup): """ Test the server properly handle /location resources """ user = setup.user # create a location location = api.create_resource('/location', {'code': api.get_random_name()[0:9]}, user) assert 'id' in location # created location_id = location['id'] location['code'] = api.get_random_name()[0:9] location = api.update_resource('/location/{}'.format(location_id), location, user) assert location['id'] == location_id # get the location location = api.get_resource('/location/{}'.format(location['id']), user=user) # query for locations locations = api.query_resource('/location', q=location['code'], user=user) assert location['id'] in [location['id'] for location in locations] # delete the created resources api.delete_resource('/location/{}'.format(location['id']), user)
def xtest_resource(session): """ Test the server properly /family resources """ return db.set_session_schema(session, session.merge(organization).pg_schema) families = session.query(Family) # create a family family first_family = api.create_resource('/family', {'family': api.get_random_name()}) # get the family first_family = api.get_resource(first_family['ref']) # query for families response_json = api.query_resource('/family', q=second_family['family']) second_family = response_json[0] # we're assuming there's only one assert second_family['ref'] == second_ref # delete the created resources api.delete_resource(first_family['ref']) api.delete_resource(second_family['ref'])
def test_server(setup): """ Test the server properly handle /genus resources """ user = setup.user family = api.create_resource('/family', {'family': api.get_random_name()}, user) # create a genus first_genus = api.create_resource('/genus', {'genus': api.get_random_name(), 'family': family}, user) # create another genus and use the first as a synonym data = {'genus': api.get_random_name(), 'family': family, 'notes': [{'user': '******', 'category': 'test', 'date': '2001-1-1', 'note': 'test note'}, {'user': '******', 'category': 'test', 'date': '2002-2-2', 'note': 'test note2'}], 'synonyms': [first_genus] #'synonyms': [{'synonym': first_genus}] } second_genus = api.create_resource('/genus', data, user) assert 'id' in second_genus # created # update the genus second_genus['genus'] = api.get_random_name() second_id = second_genus['id'] second_genus = api.update_resource('/genus/' + str(second_id), second_genus, user=user) assert second_genus['id'] == second_id # make sure they have the same id after the update # get the genus first_genus = api.get_resource('/genus/' + str(first_genus['id']), user=user) # query for genera and make sure the second genus is in the results genera = api.query_resource('/genus', q=second_genus['genus'], user=user) # TODO: ** shouldn't len(genera) be 1 since the name should be unique #assert second_genus['ref'] in [genus['ref'] for genus in genera] assert second_genus['id'] in [genus['id'] for genus in genera] # test getting the genus relative to its family # ** TODO: now we just embed the relation in the /genera/:id # ** request....need to create a test to make sure it's happening # genera = api.get_resource('/family/' + str(family['id']) + "/genera", user=user) # assert first_genus['id'] in [genus['id'] for genus in genera] # test getting a family with its genera relations # ** TODO: now we just embed the relation in the /genera/:id # ** request....need to create a test to make sure it's happening #response_json = api.query_resource('/family', q=family['family'], relations="genera,notes", user=user) #families = response_json # TODO: *** i don't know if we still support returning relations like this...do # we need to # print(families[0]['genera']) # assert first_genus['ref'] in [genus['ref'] for genus in families[0]['genera']] # count the number of genera on a family # TODO: ** count is temporarily disabled # count = api.count_resource(family['ref'] + "/genera") # assert count == "2" # delete the created resources api.delete_resource('/genus/' + str(first_genus['id']), user) api.delete_resource('/genus/' + str(second_genus['id']), user) api.delete_resource('/family/' + str(family['id']), user)
def test_server(setup): """ Test the server properly handle /taxon resources """ user = setup.user family = api.create_resource('/family', {'family': api.get_random_name()}, user) genus = api.create_resource('/genus', {'genus': api.get_random_name(), 'family': family}, user) taxon = api.create_resource('/taxon', {'genus': genus, 'sp': api.get_random_name()}, user) accession = api.create_resource('/accession', {'taxon': taxon, 'code': api.get_random_name()}, user) location = api.create_resource('/location', {'code': api.get_random_name()[0:9]}, user) location2 = api.create_resource('/location', {'code': api.get_random_name()[0:9]}, user) plant = api.create_resource('/plant', { 'accession': accession, 'location': location, 'code': api.get_random_name()[0:5], 'quantity': 10}, user) # update the plant, add 2 assert 'id' in plant # created plant_id = plant['id'] plant['code'] = api.get_random_name()[0:5] plant['quantity'] = plant['quantity'] + 2 plant['change'] = { #'reason': 'No reason really.', 'date': datetime.now().isoformat() } plant = api.update_resource(plant_ref(plant_id), plant, user) assert plant['id'] == plant_id plant = api.get_resource(plant_ref(plant['id']), user=user) assert isinstance(plant['changes'], list) assert plant['changes'][-1]['quantity'] == 2 assert plant['changes'][-1]['from_location_id'] == location['id'] assert plant['changes'][-1]['to_location_id'] is None # update the plant and remove 3 plant['quantity'] = plant['quantity'] - 3 plant['change'] = { #'reason': 'No reason really.', 'date': datetime.now().isoformat() } plant = api.update_resource(plant_ref(plant_id), plant, user) assert plant['id'] == plant_id plant = api.get_resource(plant_ref(plant['id']), user=user) assert isinstance(plant['changes'], list) assert plant['changes'][-1]['quantity'] == -3 assert plant['changes'][-1]['from_location_id'] == location['id'] assert plant['changes'][-1]['to_location_id'] is None # move some of the plants to a new location change_quantity = plant['quantity'] - 4 plant['quantity'] = change_quantity plant['location_id'] = location2['id'] plant['change'] = { 'reason': 'No reason really.', 'date': datetime.now().isoformat() } plant = api.update_resource(plant_ref(plant_id), plant, user) assert plant['id'] == plant_id plant = api.get_resource(plant_ref(plant['id']), user=user) assert isinstance(plant['changes'], list) assert plant['changes'][-1]['quantity'] == change_quantity assert plant['changes'][-1]['from_location_id'] == location['id'] assert plant['changes'][-1]['to_location_id'] == location2['id'] # query for plants plants = api.query_resource('/plant', q=plant['code'], user=user) assert plant['id'] in [plant['id'] for plant in plants] # delete the created resources api.delete_resource('/plant/' + str(plant['id']), user) api.delete_resource('/location/' + str(location['id']), user) api.delete_resource('/location/' + str(location2['id']), user) api.delete_resource('/accession/' + str(accession['id']), user) api.delete_resource('/taxon/' + str(taxon['id']), user) api.delete_resource('/genus/' + str(genus['id']), user) api.delete_resource('/family/' + str(family['id']), user)