def test__eq__(self):
     """A `CommonName` is equal to another if relevant columns match."""
     cn1 = CommonName()
     cn2 = CommonName()
     idx = Index()
     idx.id = 1
     x = CommonName()
     y = CommonName()
     z = CommonName()
     x.id = 24
     y.id = 25
     z.id = 26
     cv1, cv2, cv3 = Cultivar(), Cultivar(), Cultivar()
     cn1.id = 42
     cn1.index = idx
     cn1.name = 'Annual'
     cn1.slug = 'annual'
     cn1.description = 'Not built to last.'
     cn1.instructions = 'Plant them.'
     cn1.gw_common_names = [x, y, z]
     cn1.gw_cultivars = [cv1, cv2, cv3]
     cn1.visible = True
     assert cn1 != cn2
     cn2.id = 42
     cn2.index = idx
     cn2.name = 'Annual'
     cn2.slug = 'annual'
     cn2.description = 'Not built to last.'
     cn2.instructions = 'Plant them.'
     cn2.gw_common_names = [x, y, z]
     cn2.gw_cultivars = [cv1, cv2, cv3]
     cn2.visible = True
     assert cn1 == cn2
 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_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_queryable_dict(self):
     """Return a dict containing the name and index of a CommonName."""
     cn = CommonName()
     assert cn.queryable_dict == {'Common Name': None, 'Index': None}
     cn.name = 'Foxglove'
     assert cn.queryable_dict == {'Common Name': 'Foxglove', 'Index': None}
     cn.index = Index(name='Perennial')
     assert cn.queryable_dict == {'Common Name': 'Foxglove',
                                  'Index': 'Perennial'}
 def test_remove_common_name_verified(self, app, db):
     """Delete CommonName from db on successful submit."""
     cn = CommonName()
     cn2 = CommonName()
     idx = Index(name="Perennial")
     db.session.add_all([idx, cn, cn2])
     cn.name = "Coleus"
     cn.index = idx
     cn2.name = "Kingus"
     cn2.index = idx
     db.session.commit()
     assert cn in CommonName.query.all()
     with app.test_client() as tc:
         tc.post(
             url_for("seeds.remove_common_name", cn_id=cn.id),
             data=dict(verify_removal=True, move_to=cn2.id),
             follow_redirects=True,
         )
     assert cn not in CommonName.query.all()
 def test_remove_common_name_renders_page(self, app, db):
     """Render seeds/remove_common_name.html given valid cn_id."""
     cn = CommonName()
     db.session.add(cn)
     cn.name = "Coleus"
     db.session.commit()
     with app.test_client() as tc:
         rv = tc.get(url_for("seeds.remove_common_name", cn_id=cn.id))
     assert rv.status_code == 200
     assert "Remove Common Name" in str(rv.data)
 def test_remove_common_name_not_verified(self, app, db):
     """Redirect to self with flash if verify_removal not checked."""
     cn = CommonName()
     cn2 = CommonName()
     db.session.add_all([cn, cn2])
     cn.name = "Coleus"
     cn2.name = "Kingus"
     db.session.commit()
     assert cn in CommonName.query.all()
     with app.test_client() as tc:
         rv = tc.post(url_for("seeds.remove_common_name", cn_id=cn.id), data=dict(verify_removal="", move_to=cn2.id))
     assert rv.location == url_for("seeds.remove_common_name", cn_id=cn.id, _external=True)
     with app.test_client() as tc:
         rv = tc.post(
             url_for("seeds.remove_common_name", cn_id=cn.id),
             data=dict(verify_removal="", move_to=cn2.id),
             follow_redirects=True,
         )
     assert "Common name was not removed" in str(rv.data)
     assert cn in CommonName.query.all()
 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_select_common_name_success(self, app, db):
     """Redirect to dest with cn_id selected by form."""
     cn = CommonName()
     db.session.add(cn)
     cn.name = "Coleus"
     db.session.commit()
     with app.test_client() as tc:
         rv = tc.post(
             url_for("seeds.select_common_name", dest="seeds.edit_common_name"), data=dict(common_name=cn.id)
         )
     assert rv.status_code == 302
     assert rv.location == url_for("seeds.edit_common_name", cn_id=cn.id, _external=True)
 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 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_dict__to_from_dict_existing_cn(self, m_q):
     """Do not create `CommonName` if id already exists in db."""
     old_cn = CommonName()
     old_cn.id = 42
     m_q.get.return_value = old_cn
     cn = CommonName()
     idx = Index()
     idx.id = 1
     cn.id = 42
     cn.index = idx
     cn.name = 'Annual'
     cn.slug = 'annual'
     cn.description = 'Not built to last.'
     cn.instructions = 'Plant them.'
     cn.visible = True
     d = cn.dict_
     with pytest.raises(ValueError):
         CommonName.from_dict_(d)
    def test_dict__to_from_dict_(self, m_iq, m_cq):
        """Create new CommonName equal to CN.dict_

        Note:

        grows_with is excluded because that must be handled by a different
        function.
        """
        m_cq.get.return_value = None
        cn = CommonName()
        idx = Index()
        m_iq.get.return_value = idx
        idx.id = 1
        cn.id = 42
        cn.index = idx
        cn.name = 'Annual'
        cn.slug = 'annual'
        cn.description = 'Not built to last.'
        cn.instructions = 'Plant them.'
        cn.visible = True
        d = cn.dict_
        assert CommonName.from_dict_(d)
 def test_header(self):
     """Return '<arranged_name> Seeds'."""
     cn = CommonName()
     cn.name = 'Foxglove'
     assert cn.header == 'Foxglove Seeds'