Ejemplo n.º 1
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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
def test_search(setup):

    user = setup.user

    family_name = api.get_random_name()
    family = api.create_resource('/family', {'family': family_name}, user=user)

    family2 = api.create_resource('/family', {'family': api.get_random_name()}, user=user)

    # return $http({method: 'GET', url: globals.apiRoot + '/search', params: {'q': value}})
    #         .then(callback);

    response = requests.get(api.api_root + "/search", params={'q': family_name},
                            headers=get_headers(), auth=(user.email, user.access_token))

    assert response.status_code == 200

    response_json = json.loads(response.text)
    assert 'families' in response_json
    families = response_json['families']
    assert len(families) == 1
    assert families[0]['id'] == family['id']

    api.delete_resource('/family/{}'.format(family['id']), user=user)
    api.delete_resource('/family/{}'.format(family2['id']), user=user)
Ejemplo n.º 4
0
def test_distribution(setup):

    # create a taxon taxon
    taxon = Taxon(genus=setup.genus, sp=api.get_random_name())
    setup.session.add(taxon)

    geography = Geography(name="Test")
    setup.session.add(geography)
    setup.session.commit()

    dist_route = "/taxon/{}/distributions".format(taxon.id)

    # get a geogrpahy
    # geography = setup.session.query(Geography).get(1)
    # assert geography is not None

    # add the geography to the taxon
    # create a vernacular name using the route
    dist_json = api.create_resource(dist_route, geography.json(), user=setup.user)

    # check the geography is in the distribution list
    dists = api.get_resource(dist_route, user=setup.user)
    assert isinstance(dists, list)
    assert geography.id in [dist["id"] for dist in dists]

    # delete the name via the route
    api.delete_resource(dist_route + "/{}".format(dist_json["id"]), user=setup.user)

    setup.session.delete(taxon)
    setup.session.commit()
Ejemplo n.º 5
0
def test_names(setup):
    # create a taxon taxon
    taxon = Taxon(genus=setup.genus, sp=api.get_random_name())
    setup.session.add(taxon)
    setup.session.commit()

    names_route = "/taxon/{}/names".format(taxon.id)

    # create a vernacular name using the route
    name_json = api.create_resource(
        names_route, {"name": api.get_random_name(), "language": api.get_random_name()}, user=setup.user
    )

    assert "id" in name_json

    # check the name is in the list of names
    names = api.get_resource(names_route, user=setup.user)
    assert isinstance(names, list)
    assert name_json["id"] in [name["id"] for name in names]

    # delete the name via the route
    api.delete_resource(names_route + "/{}".format(name_json["id"]), user=setup.user)

    setup.session.delete(taxon)
    setup.session.commit()
Ejemplo n.º 6
0
def test_parser(setup):
    """
    Test the bauble.search.SearchParser
    """
    user = setup.user
    family = api.create_resource('/family', {'family': api.get_random_name()}, user=user)
    parser = bauble.search.SearchParser()
    parser.statement.parseString(family['family'])
    api.delete_resource('/family/{}'.format(family['id']), user)
Ejemplo n.º 7
0
def test_delete(setup):
    session = setup.session

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

    api.delete_resource('/report/{}'.format(report.id), setup.user)

    report_id = report.id
    session.expunge(report)
    assert session.query(Report).get(report_id) is None
Ejemplo n.º 8
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)
Ejemplo n.º 9
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)
Ejemplo n.º 10
0
def test_organization(setup):

    org_json = api.create_resource("/organization", {
        'name': 'Test BG'
    }, user=setup.user)

    # make sure we can get the organization resource
    org_json = api.get_resource('/organization/{}'.format(org_json['id']), user=setup.user)
    owner_id = org_json['owners'][0]
    user_json = api.get_resource('/user/{}'.format(owner_id), user=setup.user)
    assert user_json['id'] == owner_id

    # add another user to the organization
    #user2_data = organization.owners[0].json()
    user2_data = setup.user.json()
    user2_data['username'] = api.get_random_name()
    user2_data['email'] = api.get_random_name()
    user2_data['organization'] = org_json
    user2 = api.create_resource("/user", user2_data, user=setup.user)
    assert user2 is not None

    # TODO delete all the tables and schema for the organization
    api.delete_resource('/organization/{}'.format(org_json['id']), user=setup.user)
Ejemplo n.º 11
0
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'])
Ejemplo n.º 12
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)
Ejemplo n.º 13
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)