def test_add_cultivar_successful_submit_no_stock_and_inactive(self, mock_save, app, db): """Flash messages if cultivar is not in stock and has been dropped.""" bn = BotanicalName() cn = CommonName() idx = Index() db.session.add_all([bn, cn, idx]) bn.name = "Digitalis purpurea" idx.name = "Perennial Flower" cn.name = "Foxglove" cn.index = idx bn.common_names.append(cn) db.session.commit() with app.test_client() as tc: rv = tc.post( url_for("seeds.add_cultivar", cn_id=cn.id), data=dict( botanical_name=str(bn.id), index=str(idx.id), description="Very foxy.", active="", in_stock="", name="Foxy", section="0", thumbnail=(io.BytesIO(b"fawks"), "foxy.jpg"), ), follow_redirects=True, ) assert '"Foxy Foxglove" is not in stock' in str(rv.data) assert '"Foxy Foxglove" is currently inactive' in str(rv.data)
def test_remove_botanical_name_renders_page(self, app, db): """Render seeds/remove_botanical_name.html with valid bn_id.""" bn = BotanicalName() db.session.add(bn) bn.name = "Asclepias incarnata" db.session.commit() with app.test_client() as tc: rv = tc.get(url_for("seeds.remove_botanical_name", bn_id=bn.id)) assert "Remove Botanical Name" in str(rv.data)
def test_init(self, m_sfc): m_sfc.return_value = [(1, "One"), (2, "Two"), (3, "Three")] cn1 = CommonName(name="One") cn1.id = 1 cn2 = CommonName(name="Two") cn2.id = 2 obj = BotanicalName(name="Bot nam") obj.common_names = [cn1, cn2] ebnf = EditBotanicalNameForm(obj=obj) assert ebnf.common_names.data == [1, 2]
def test_add_one_with_synonyms(self): """Add a BotanicalName with synonyms to Botanical Names sheet.""" wb = Workbook() ws = wb.active bnws = BotanicalNamesWorksheet(ws) bnws.setup() bn = BotanicalName(name='Innagada davida') cn = CommonName(name='Rock') cn.index = Index(name='Music') bn.common_names = [cn] bn.synonyms_string = 'Iron butterfly' bnws.add_one(bn) assert bnws.cell(2, bnws.cols['Synonyms']).value == 'Iron butterfly'
def test_select_botanical_name_selected(self, app, db): """Redirect to dest if a botanical name is selected.""" bn = BotanicalName() db.session.add(bn) bn.name = "Asclepias incarnata" db.session.commit() with app.test_client() as tc: rv = tc.post( url_for("seeds.select_botanical_name", dest="seeds.edit_botanical_name"), data=dict(botanical_name=bn.id), ) assert rv.status_code == 302 assert rv.location == url_for("seeds.edit_botanical_name", bn_id=bn.id, _external=True)
def test_remove_botanical_name_verified(self, app, db): """Delete BotanicalName from db if verify_removal checked.""" bn = BotanicalName() db.session.add(bn) bn.name = "Asclepias incarnata" db.session.commit() assert BotanicalName.query.count() == 1 with app.test_client() as tc: tc.post( url_for("seeds.remove_botanical_name", bn_id=bn.id), data=dict(verify_removal=True), follow_redirects=True, ) assert BotanicalName.query.count() == 0
def test_validate_botanical_name_id(self, m_bnq): """Raise error if selected BN is not in selected CN.""" bn = BotanicalName("Digitalis über alles") cn1 = CommonName(name="Fauxglove") cn1.id = 1 cn2 = CommonName(name="Spuriousglove") cn2.id = 2 bn.common_names = [cn1, cn2] m_bnq.return_value = bn self = mock.MagicMock() self.common_name_id.data = 3 field = mock.MagicMock() with pytest.raises(ValidationError): EditCultivarForm.validate_botanical_name_id(self=self, field=field)
def foxy_cultivar(): """Generate a Cultivar object based on Foxy Foxglove.""" cultivar = Cultivar() cultivar.name = "Foxy" cultivar.description = "Not to be confused with that Hendrix song." bn = BotanicalName() bn.name = "Digitalis purpurea" cultivar.botanical_name = bn idx = Index() idx.name = "Perennial Flower" cn = CommonName() cn.name = "Foxglove" cn.index = idx cultivar.common_name = cn return cultivar
def test_remove_botanical_name_not_verified(self, app, db): """Redirect to self and flash message if verify_removal unchecked.""" bn = BotanicalName() bn.name = "Asclepias incarnata" db.session.add(bn) db.session.commit() with app.test_client() as tc: rv = tc.post(url_for("seeds.remove_botanical_name", bn_id=bn.id), data=dict(verify_removal="")) assert rv.status_code == 302 assert rv.location == url_for("seeds.remove_botanical_name", bn_id=bn.id, _external=True) with app.test_client() as tc: rv = tc.post( url_for("seeds.remove_botanical_name", bn_id=bn.id), data=dict(verify_removal=""), follow_redirects=True ) assert "Botanical name was not removed" in str(rv.data)
def test_add_one_no_optionals(self): """Add a BotanicalName object to Botanical Names sheet.""" messages = StringIO() wb = Workbook() ws = wb.active bnws = BotanicalNamesWorksheet(ws) bnws.setup() bn = BotanicalName(name='Innagada davida') cn = CommonName(name='Rock') cn.index = Index(name='Music') bn.common_names = [cn] bnws.add_one(bn, stream=messages) assert bnws.cell( 2, bnws.cols['Common Names (JSON)'] ).value == queryable_dicts_to_json([cn]) assert bnws.cell( 2, bnws.cols['Botanical Name'] ).value == 'Innagada davida' assert bnws.cell(2, bnws.cols['Synonyms']).value is None messages.seek(0) msgs = messages.read() assert ('Adding data from <BotanicalName "Innagada davida"> to row ' '#2 of botanical names worksheet.') in msgs
def test_validate_valid_binomen(self): """Returns true if botanical_name contains a valid binomen.""" assert BotanicalName.validate('Asclepias incarnata') assert BotanicalName.validate('Helianthus anuus') assert BotanicalName.validate('Hydrangea Lacecap Group')
def test_validate_starts_with_lower(self): """The first letter of a botanical name should be uppercase.""" assert not BotanicalName.validate('asclepias incarnata')
def test_validate_upper_in_wrong_place(self): """The only uppercase letter in the first word should be the first.""" assert not BotanicalName.validate('AscLepias incarnata')
def test_validate_not_a_string(self): """Return False when given non-string data.""" assert not BotanicalName.validate(42) assert not BotanicalName.validate(('foo', 'bar')) assert not BotanicalName.validate(dict(foo='bar'))
def test_validate_more_than_two_words(self): """A botanical name is still valid with more than 2 words.""" assert BotanicalName.validate('Brassica oleracea Var.')
def test_repr(self): """Return a string in format <BotanicalName '<botanical_name>'>""" bn = BotanicalName(name='Asclepias incarnata') assert bn.__repr__() == '<BotanicalName "Asclepias incarnata">'