def test_remove_index_verified(self, app, db): """Remove Index from db if verify_removal is checked.""" idx = Index() idx2 = Index() db.session.add_all([idx, idx2]) idx.name = "Annual Flower" idx2.name = "Herb" db.session.commit() assert idx in Index.query.all() with app.test_client() as tc: tc.post( url_for("seeds.remove_index", idx_id=idx.id), data=dict(verify_removal=True, move_to=idx2.id), follow_redirects=True, ) assert idx not in Index.query.all()
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_cultivar_bad_slugs(self, app, db): """Return 404 if any slug given does not correspond to a db entry.""" app.config["SHOW_CULTIVAR_PAGES"] = True idx = Index() cn = CommonName() cultivar = Cultivar() db.session.add_all([idx, cn, cultivar]) idx.name = "Perennial Flower" cn.name = "Foxglove" cultivar.name = "Foxy" cultivar.common_name = cn cultivar.description = "Like that Hendrix song." db.session.commit() with app.test_client() as tc: rv = tc.get(url_for("seeds.cultivar", idx_slug=idx.slug, cn_slug=cn.slug, cv_slug="no-biscuit")) assert rv.status_code == 404 with app.test_client() as tc: rv = tc.get(url_for("seeds.cultivar", idx_slug="no_biscuit", cn_slug=cn.slug, cv_slug=cultivar.slug)) assert rv.status_code == 404 with app.test_client() as tc: rv = tc.get(url_for("seeds.cultivar", idx_slug=idx.slug, cn_slug="no-biscuit", cv_slug=cultivar.slug)) assert rv.status_code == 404 with app.test_client() as tc: rv = tc.get(url_for("seeds.cultivar", idx_slug="no-biscuit", cn_slug="no-biscuit", cv_slug="no-biscuit")) assert rv.status_code == 404
def test__eq__(self): """Return True if all columns are the same value.""" idx1 = Index() idx2 = Index() idx1.id = 42 assert idx1 != idx2 idx2.id = 42 assert idx1 == idx2 idx1.position = 3 idx1.name = 'Annual' idx1.slug = 'annual' idx1.description = 'Not built to last.' assert idx1 != idx2 idx2.position = 3 idx2.name = 'Annual' idx2.slug = 'annual' idx2.description = 'Not built to last.' assert idx1 == idx2
def test_remove_index_not_verified(self, app, db): """Redirect to self if verify_removal not checked.""" idx = Index() idx2 = Index() db.session.add_all([idx, idx2]) idx.name = "Annual Flower" idx2.name = "Herb" db.session.commit() with app.test_client() as tc: rv = tc.post(url_for("seeds.remove_index", idx_id=idx.id), data=dict(verify_removal="", move_to=idx2.id)) assert rv.location == url_for("seeds.remove_index", idx_id=idx.id, _external=True) with app.test_client() as tc: rv = tc.post( url_for("seeds.remove_index", idx_id=idx.id), data=dict(verify_removal="", move_to=idx2.id), follow_redirects=True, ) assert "Index was not removed" in str(rv.data)
def test_edit_index_renders_page(self, app, db): """Render the page for editing a index given valid idx_id.""" idx = Index() db.session.add(idx) idx.name = "Vegetable" db.session.commit() with app.test_client() as tc: rv = tc.get(url_for("seeds.edit_index", idx_id=idx.id), follow_redirects=True) assert "Edit Index" in str(rv.data)
def test_select_index_success(self, app, db): """Redirect to dest with idx_id selected by form.""" idx = Index() db.session.add(idx) idx.name = "Annual Flower" db.session.commit() with app.test_client() as tc: rv = tc.post(url_for("seeds.select_index", dest="seeds.edit_index"), data=dict(index=idx.id)) assert rv.status_code == 302 assert rv.location == url_for("seeds.edit_index", idx_id=idx.id, _external=True)
def test_index_with_valid_slug(self, app, db): """Return valid page given a valid index slug.""" idx = Index() db.session.add(idx) idx.name = "Annual Flower" idx.description = "Not really built to last." db.session.commit() with app.test_client() as tc: rv = tc.get(url_for("seeds.index", idx_slug=idx.slug), follow_redirects=True) assert "Annual Flower" in str(rv.data)
def test_dict__to_from_dict__(self, m_q): """An Index.dict_ fed to Index.from_dict_ creates identical Index.""" m_q.get.return_value = None idx1 = Index() idx1.id = 42 idx1.position = 3 idx1.name = 'Annual' idx1.slug = 'annual' idx1.description = 'Not built to last.' d = idx1.dict_ assert Index.from_dict_(d) == idx1
def test_common_name_bad_slugs(self, app, db): """Give a 404 page if given malformed cn_slug and idx_slug.""" cn = CommonName() idx = Index() db.session.add_all([cn, idx]) cn.name = "Foxglove" idx.name = "Perennial Flower" db.session.commit() with app.test_client() as tc: rv = tc.get(url_for("seeds.common_name", idx_slug="pewennial-flower", cn_slug="fawksglove")) assert rv.status_code == 404
def test_cv_slugs_not_in_cultivar(self, app, db): """Return 404 if slugs return db entries, but entry not in cultivar.""" app.config["SHOW_CULTIVAR_PAGES"] = True idx1 = Index() idx2 = Index() cn1 = CommonName() cn2 = CommonName() cultivar = Cultivar() db.session.add_all([idx1, idx2, cn1, cn2, cultivar]) idx1.name = "Perennial Flower" idx2.name = "Long Hair" cn1.name = "Foxglove" cn2.name = "Persian" cultivar.name = "Foxy" cultivar.common_name = cn1 db.session.commit() with app.test_client() as tc: rv = tc.get(url_for("seeds.cultivar", idx_slug=idx1.slug, cn_slug=cn2.slug, cv_slug=cultivar.slug)) assert rv.status_code == 404 with app.test_client() as tc: rv = tc.get(url_for("seeds.cultivar", idx_slug=idx2.slug, cn_slug=cn1.slug, cv_slug=cultivar.slug)) assert rv.status_code == 404
def test_common_name_renders_page(self, app, db): """Render page with common name info given valid slugs.""" cn = CommonName() idx = Index() db.session.add_all([cn, idx]) cn.name = "Foxglove" cn.description = "Do foxes really wear these?" idx.name = "Perennial Flower" cn.index = idx db.session.commit() with app.test_client() as tc: rv = tc.get(url_for("seeds.common_name", idx_slug=idx.slug, cn_slug=cn.slug)) assert "Do foxes really wear these?" in str(rv.data)
def test_from_dict__index_exists(self, m_q): """Do not allow from_dict_ to create an Index w/ id already in use.""" old_idx = Index() old_idx.id = 42 m_q.get.return_value = old_idx idx = Index() idx.id = 42 idx.position = 3 idx.name = 'Annual' idx.slug = 'annual' idx.description = 'Not built to last.' d = idx.dict_ with pytest.raises(ValueError): Index.from_dict_(d)
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__repr__(self): """Return string formatted <Index '<index>'>""" index = Index() index.name = 'vegetable' assert index.__repr__() == '<Index "vegetable">'
def test_plural(self): """Return plural version of ._name.""" index = Index() index.name = 'Annual Flower' assert index.plural == 'Annual Flowers'
def test_header(self): """Return '<._name> Seeds'""" index = Index() index.name = 'Annual Flower' assert index.header == 'Annual Flower Seeds'