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()
def post_taxon(): if not request.json: bottle.abort(400, 'The request doesn\'t contain a request body') # create a copy of the request data with only the columns data = {col: request.json[col] for col in request.json.keys() if col in taxon_mutable} # if there isn't a genus_id look for a genus relation on the request data if not 'genus_id' in data and 'genus' in request.json and isinstance(request.json['genus'], dict) and 'id' in request.json['genus']: data['genus_id'] = request.json['genus']['id'] # make a copy of the data for only those fields that are columns taxon = Taxon(**data) request.session.add(taxon) request.session.commit() response.status = 201 return taxon.json()