Exemplo n.º 1
0
def test_genus_json(setup):
    session = setup.session

    family = Family(family=api.get_random_name())
    genus_name = api.get_random_name()
    genus = Genus(family=family, genus=genus_name)
    note = GenusNote(genus=genus, note="this is a test")
    syn = GenusSynonym(genus=genus, synonym=genus)

    session.add_all([family, genus, note, syn])
    session.commit()

    genus_json = genus.json()
    assert 'id' in genus_json
    assert genus_json['id'] == genus.id
    assert 'genus' in genus_json
    assert 'str' in genus_json
    assert 'qualifier' in genus_json

    note_json = note.json()
    assert 'id' in note_json
    assert 'genus_id' in note_json
    assert note_json['genus_id'] == genus.id

    syn_json = syn.json()
    assert 'id' in syn_json
    assert syn_json['genus_id'] == genus.id
    assert syn_json['synonym_id'] == genus.id

    session.delete(genus)
    session.commit()
    session.close()
Exemplo n.º 2
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)
Exemplo n.º 3
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()
Exemplo n.º 4
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)
Exemplo n.º 5
0
def test_embed(setup):
    session = setup.session
    user = setup.user

    family = Family(family=api.get_random_name())
    family2 = Family(family=api.get_random_name())
    family.synonyms.append(family2)
    session.add_all([family, family2])
    session.commit()

    response = requests.get('{}/family/{}?embed=synonyms&embed=notes'.format(api.api_root, family.id), auth=(user.email, user.access_token))
    # response_json = api.get_resource('/family/{}'.format(family.id),
    #                                  {'embed': ['synonyms', 'notes']}, user=user)
    assert response.status_code == 200
    response_json = response.json()
Exemplo n.º 6
0
def test_family_json(setup):
    session = setup.session

    family_name = api.get_random_name()
    family = Family(family=family_name)
    note = FamilyNote(family=family, note="this is a test")
    syn = FamilySynonym(family=family, synonym=family)

    session.add_all([family, note, syn])
    session.commit()

    family_json = family.json()
    assert 'id' in family_json
    assert family_json['id'] == family.id

    family_json = family.json()
    assert 'family' in family_json
    assert 'str' in family_json
    assert 'qualifier' in family_json

    note_json = note.json()
    assert 'id' in note_json
    assert note_json['note'] == note.note
    assert 'family_id' in note_json
    assert note_json['family_id'] == family.id

    syn_json = syn.json()
    assert syn_json['family_id'] == family.id
    assert syn_json['synonym_id'] == family.id

    session.delete(family)
    session.commit()
    session.close()
Exemplo n.º 7
0
def test_auth(user, session):

    # set the password explicity so we know what it is
    password = get_random_name()
    user.password = password
    session.add(user)
    session.commit()

    # test /login
    response = requests.get(api_root + "/login", auth=(user.email, password))
    assert response.status_code == 200
    assert "application/json" in response.headers['content-type']
    user_json = response.json()
    assert len(user_json['access_token']) == 32

    # test /logout
    user = session.merge(user)
    session.refresh(user)
    assert len(user.access_token) == 32
    assert isinstance(user.access_token_expiration, datetime)
    response = requests.get(api_root + "/logout", auth=(user.email, user.access_token))
    assert response.status_code == 200
    session.refresh(user)
    assert user.access_token is None
    assert user.access_token_expiration is None
Exemplo n.º 8
0
def test_json(setup):
    session = setup.session

    taxon = Taxon(genus=setup.genus, sp=api.get_random_name())

    note = TaxonNote(taxon=taxon, note="this is a test")
    syn = TaxonSynonym(taxon=taxon, synonym=taxon)

    session.add_all([taxon, note, syn])
    session.commit()

    taxon_json = taxon.json()
    assert "id" in taxon_json
    assert "str" in taxon_json
    assert taxon_json["genus_id"] == setup.genus.id

    taxon_json = taxon.json()
    assert "str" in taxon_json
    # add all deph=2 fields

    note_json = note.json()
    assert "id" in note_json
    assert "taxon_id" in note_json
    assert note_json["taxon_id"] == taxon.id

    syn_json = syn.json()
    assert "id" in syn_json
    assert syn_json["taxon_id"] == taxon.id
    assert syn_json["synonym_id"] == taxon.id

    session.delete(taxon)
    session.commit()
    session.close()
Exemplo n.º 9
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()
Exemplo n.º 10
0
def test_password(session):
    username = api.get_random_name()
    email = username + '@bauble.io'
    password = api.get_random_name()

    user = User(email=email, username=username, password=password)
    session.add(user)
    session.commit()

    # test the password isn't stored in plain text
    assert user._password != password

    # test that we can compare the password against a plain test password
    assert user.password == password

    session.delete(user)
    session.commit()
Exemplo n.º 11
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)
Exemplo n.º 12
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)
Exemplo n.º 13
0
def setup(request, organization, session):
    setup.organization = session.merge(organization)
    setup.user = setup.organization.owners[0]
    setup.session = session
    db.set_session_schema(session, setup.organization.pg_schema)

    setup.family = Family(family=api.get_random_name())
    setup.genus = Genus(family=setup.family, genus=api.get_random_name())
    setup.session.add_all([setup.family, setup.genus])
    setup.session.commit()

    def cleanup():
        setup.session.delete(setup.genus)
        setup.session.delete(setup.family)
        setup.session.commit()

    request.addfinalizer(cleanup)

    return setup
Exemplo n.º 14
0
def test_get(setup):
    session = setup.session

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

    # a list request should work
    response = api.get_resource('/report/{}'.format(report.id), user=setup.user)
    assert response["id"] == report.id
Exemplo n.º 15
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)
Exemplo n.º 16
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
Exemplo n.º 17
0
def test_plant_json(setup):

    session = setup.session

    family = Family(family=api.get_random_name())
    genus_name = api.get_random_name()
    genus = Genus(family=family, genus=genus_name)
    taxon = Taxon(genus=genus, sp=api.get_random_name())
    acc = Accession(taxon=taxon, code=api.get_random_name())
    location = Location(code=api.get_random_name()[0:9])
    plant = Plant(accession=acc, code=api.get_random_name()[0:5],
                  quantity=1, location=location)

    note = PlantNote(plant=plant, note="this is a test")

    all_objs = [family, genus, taxon, note, acc, plant, location]
    session.add_all(all_objs)
    session.commit()

    plant_json = plant.json()
    assert 'id' in plant_json
    assert plant_json['id'] == plant.id
    assert 'str' in plant_json

    note_json = note.json()
    assert 'id' in note_json
    assert 'plant_id' in note_json
    assert note_json['plant_id'] == plant.id

    map(lambda o: session.delete(o), all_objs)
    session.commit()
    session.close()
Exemplo n.º 18
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)
Exemplo n.º 19
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)
Exemplo n.º 20
0
def test_index(setup):
    reports = []

    for num in range(0, 5):
        reports.append(Report(name=api.get_random_name()))
    setup.session.add_all(reports)
    setup.session.commit()

    # a list request should work
    response = api.get_resource('/report', user=setup.user)
    assert isinstance(response, list)

    for report in reports:
        setup.session.delete(report)
    setup.session.commit()
Exemplo n.º 21
0
def test_synonyms(setup):
    session = setup.session
    family = Family(family=api.get_random_name())
    family2 = Family(family=api.get_random_name())
    session.add_all([family, family2])
    session.commit()

    family.synonyms.append(family2)
    session.commit()
    assert family2 in family.synonyms

    # make sure the FamilySynonym was created
    count = session.query(FamilySynonym).filter_by(family_id=family.id,
                                                   synonym_id=family2.id).count()
    assert count == 1

    family.synonyms.remove(family2)
    session.commit()
    assert family2 not in family.synonyms

    # make sure the FamilySynonym was removed
    count = session.query(FamilySynonym).filter_by(family_id=family.id,
                                                   synonym_id=family2.id).count()
    assert count == 0

    # make sure that if a family is deleted all its synonyms get deleted
    family.synonyms.append(family2)
    session.commit()
    count = session.query(FamilySynonym).filter_by(family_id=family.id,
                                                   synonym_id=family2.id).count()
    assert count == 1
    session.delete(family)
    session.commit()
    count = session.query(FamilySynonym).filter_by(family_id=family.id,
                                                   synonym_id=family2.id).count()
    assert count == 0
Exemplo n.º 22
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()
Exemplo n.º 23
0
def test_location_json(setup):

    session = setup.session

    code = api.get_random_name()[0:9]
    location = Location(code=code)

    session.add(location)
    session.commit()

    location_json = location.json()
    assert 'id' in location_json
    assert location_json['id'] == location.id
    assert 'code' in location_json
    assert 'str' in location_json

    session.delete(location)
    session.commit()
    session.close()
Exemplo n.º 24
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()
Exemplo n.º 25
0
def test_index(setup):
    session = setup.session
    user = setup.user

    families = []
    for num in range(0, 5):
        families.append(Family(family=api.get_random_name()))
    session.add_all(families)
    session.commit()

    # a list request should work
    response = api.get_resource('/family', user=user)
    assert isinstance(response, list)

    # a list request with a filter should work
    filter = json.dumps({'family': families[0].family[0:4] + "%"})
    response = api.get_resource('/family?filter={}'.format(filter), user=user)
    assert isinstance(response, list)
    assert response[0]['family'] == families[0].family

    for family in families:
        session.delete(family)
    session.commit()
Exemplo n.º 26
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'])
Exemplo n.º 27
0
def test_invitation(invitation_setup):
    user = invitation_setup.user
    org = invitation_setup.organization
    session = invitation_setup.session

    invite_email = '{}@bauble.io'.format(api.get_random_name())

    assert len(org.invitations) == 0
    response = requests.post(api.api_root + "/organization/{}/invite".format(org.id),
                             headers={'content-type': 'application/json'},
                             auth=(user.email, user.access_token),
                             data=json.dumps({
                                 'email': invite_email  # doesn't get sent
                             }))
    assert response.status_code == 200, response.text

    session.refresh(org)
    assert len(org.invitations) == 1
    invitation = org.invitations[0]
    assert isinstance(invitation.token, str)
    assert isinstance(invitation.token_expiration, datetime)

    response = requests.post(api.api_root + "/invitations/{}".format(invitation.token),
                             headers={'content-type': 'application/json'},
                             data=json.dumps({
                                 'password': '******'
                             }))
    response_json = response.json()
    assert response.status_code == 200, response.text
    assert response_json['email'] == invite_email
    assert response_json['access_token'] is not None
    session.refresh(invitation)
    assert invitation.accepted is True

    invited_user = session.query(User).filter_by(email=invite_email).one()
    session.delete(invited_user)
    session.commit()
Exemplo n.º 28
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)
Exemplo n.º 29
0
def test_json(setup):

    session = setup.session
    acc = Accession(taxon=setup.taxon, code=test.get_random_name())
    source = Source(accession=acc, sources_code=test.get_random_name())
    source.source_detail = SourceDetail(name=test.get_random_name(), description="the description")
    source.collection = Collection(locale=test.get_random_name())
    source.propagation = Propagation(prop_type='Seed')
    source.propagation.seed = PropSeed(nseeds=100, date_sown="2011/1/1")

    # TODO: plant propagations require a plant.id
    # source.plant_propagation = PlantPropagation(plant_id=)
    # source.plant_propagation.propagation = PlantPropagation(prop_type='Seed')
    # source.plant_propagation.seed = PropSeed(nseeds=100, date_sown="1/1/11")

    verification = Verification(accession=acc, verifier=test.get_random_name(),
                                date="2011/1/1", level=1, taxon=setup.taxon,
                                prev_taxon=setup.taxon)
    voucher = Voucher(accession=acc, herbarium=test.get_random_name(),
                      code=test.get_random_name())

    note = AccessionNote(accession=acc, note="this is a test")

    all_objs = [note, acc, source]
    session.add_all(all_objs)
    session.commit()

    acc_json = acc.json()
    assert 'id' in acc_json
    assert acc_json['id'] == acc.id
    assert 'str' in acc_json
    assert acc_json['taxon_id'] == setup.taxon.id

    note_json = note.json()
    assert 'id' in note_json
    assert 'accession_id' in note_json
    assert note_json['accession_id'] == acc.id

    source_json = source.json()
    assert 'id' in source_json

    # now switch the source propagation to UnrootedCuttings....

    # if we don't delete explicity then delete-orphan doesn't happen
    # on the PropSeed...last tested with SA 0.8.2pp
    session.delete(source.propagation)
    source.propagation = Propagation(prop_type='UnrootedCutting')
    source.propagation.cutting = PropCutting()

    # source.plant_propagation = Propagation(prop_type='UnrootedCutting')
    # source.plant_propagation.cutting = PropCutting()

    source_json = source.json()


    ver_json = verification.json()


    voucher_json = voucher.json()

    for obj in all_objs:
        session.delete(obj)
    session.commit()
    session.close()
Exemplo n.º 30
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)