def test_serializer(session, taxon): session.add(taxon) session.commit() taxon_json = taxon.jsonify() assert Taxon.jsonify(taxon) == taxon_json # class and instance json are the same assert 'str' in taxon_json assert taxon_json['genus']['id'] == taxon.genus_id
def test_form(session, genus, taxon): # from bauble.forms import form_factory, BaseModelForm from bauble.controllers.taxon import TaxonForm session.add(taxon) session.commit() data = { 'sp': faker.first_name(), 'genus_id': genus.id, 'vernacular_names': [{ 'name': faker.first_name(), 'language': 'Macedamian', '_destroy': True }] } form = TaxonForm(data=data) taxon = Taxon() form.populate_obj(taxon) for key, value in form.data.items(): print('{} - {}'.format(key, value)) db.session.add(taxon) db.session.commit() print('taxon.vernacular_names: ', taxon.vernacular_names) assert len(data['vernacular_names']) == len(taxon.vernacular_names) for vn in taxon.vernacular_names: assert vn.id is not None db.session.refresh(taxon) assert taxon.id is not None
def create(): taxon = Taxon() form = resource.save_request_params(taxon, form=TaxonForm()) process_nested_resource('vernacular_names', 'vernacular_name') # TODO: accept vernacular names for create only if request.prefers_json: return (resource.render_json(taxon, status=201) if not form.errors else resource.render_json_errors(form.errors)) return resource.render_html('new', status=201, taxon=taxon, form=form)
def index_taxon(): taxon = Taxon.query.all() data = Taxon.jsonify(taxon, many=True) return utils.json_response(data)
def taxon(genus): from bauble.models import Taxon return Taxon(sp=faker.first_name(), genus=genus)
def new(): taxon = Taxon() geographies = Geography.query.all() return resource.render_html(taxon=taxon, geographies=geographies, form=TaxonForm)