Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
def test_patch(setup):
    session = setup.session

    report = Report(name=api.get_random_name())
    session.add(report)
    session.commit()

    report.name += "_patched"

    # should fail with a 400 response if there is no request body
    response = api.update_resource('/report/{}'.format(report.id), report, setup.user)

    data = json.dumps(report.json())
    api.update_resource('/report/{}'.format(report.id), data, setup.user)
    session.delete(report)
    session.commit()
Esempio n. 4
0
def test_patch(setup):
    session = setup.session
    user = setup.user

    family = Family(family=api.get_random_name())
    session.add(family)
    session.commit()

    family.family = family.family + "_patched"

    # should fail with a 400 response if there is no request body
    response = requests.request('PATCH', '{}/family/{}'.format(api.api_root, family.id),
                                auth=(user.email, user.access_token),
                                headers = {'content-type': 'application/json'})
    assert response.status_code == 400, response.body

    data = json.dumps(family.json())
    api.update_resource('/family/{}'.format(family.id), data, user)
    session.delete(family)
    session.commit()
Esempio n. 5
0
def test_route(setup):
    """
    Test the /family resource.
    """
    session = setup.session
    user = setup.user

    # create a family family
    first_family = api.create_resource('/family', {'family': api.get_random_name()}, user)

    # create another family and use the first as a synonym
    data = {'family': api.get_random_name(),
            'notes': [{'user': '******', 'category': 'test', 'date': '2001/1/1', 'note': 'test note'},
                      {'user': '******', 'category': 'test', 'date': '2002/2/2', 'note': 'test note2'}],
            'synonyms': [first_family]
            }

    second_family = api.create_resource('/family', data, user)
    assert 'id' in second_family  # created

    # update the family
    second_family['family'] = api.get_random_name()
    second_id = second_family['id']
    second_family = api.update_resource('/family/{}'.format(second_id), second_family, user)
    assert second_family['id'] == second_id  # make sure they have the same ref after the update

    # get the family
    first_family = api.get_resource('/family/{}'.format(first_family['id']), user=user)

    # query for families
    #response_json = api.query_resource('/family', q=second_family['family'], user=user)
    filter_by = json.dumps({'family': second_family['family'][0:4] + '%'})
    response_json = api.get_resource('/family', {'filter': filter_by}, user=user)
    assert len(response_json) == 1
    second_family = response_json[0]  # we're assuming there's only one
    assert second_family['id'] == second_id

    # make sure filtering against properties that aren't columns doesn't work
    filter_by = json.dumps({'something': 'test'})
    response_json = api.get_resource('/family', {'filter': filter_by}, user=user)
    assert len(response_json) > 1

    # delete the created resources
    api.delete_resource('/family/{}'.format(first_family['id']), user)
    api.delete_resource('/family/{}'.format(second_family['id']), user)
Esempio n. 6
0
def test_route(setup):
    """
    Test the server properly handle /taxon resources
    """

    # create a taxon taxon
    first_taxon = api.create_resource(
        "/taxon", {"sp": api.get_random_name(), "genus_id": setup.genus.id}, user=setup.user
    )

    second_taxon = api.create_resource(
        "/taxon", {"sp": api.get_random_name(), "genus_id": setup.genus.id}, user=setup.user
    )

    assert "id" in second_taxon  # created

    # update the taxon
    second_taxon["sp"] = api.get_random_name()
    second_id = second_taxon["id"]
    second_taxon = api.update_resource("/taxon/" + str(second_id), second_taxon, user=setup.user)
    assert second_taxon["id"] == second_id  # make sure they have the same ref after the update

    # get the taxon
    first_taxon = api.get_resource("/taxon/" + str(first_taxon["id"]), user=setup.user)

    # query for taxa
    # print('data[sp]: ' + str(data['sp']))
    # print('second_taxon', second_taxon)
    # response_json = api.query_resource('/taxon', q=data['sp'])
    # print(response_json)
    # second_taxon = response_json['results'][0]  # we're assuming there's only one
    # assert second_taxon['ref'] == second_ref

    # test getting the taxon relative to its family
    # ** TODO: now we just embed the relation in the /taxon/:id
    # ** request....need to create a test to make sure it's happening
    # taxa = api.get_resource('/family/{}/genera/taxa'.format(family['id']), user=user)
    # assert first_taxon['id'] in [taxon['id'] for taxon in taxa]

    # delete the created resources
    api.delete_resource("/taxon/" + str(first_taxon["id"]), user=setup.user)
    api.delete_resource("/taxon/" + str(second_taxon["id"]), user=setup.user)
Esempio n. 7
0
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)
Esempio n. 8
0
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)