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_queryable_dicts_to_json_bad_args(self): """Raise a TypeError if any objects lack the lookup_dict method.""" with pytest.raises(TypeError): queryable_dicts_to_json((1, 2, 3)) cn1 = CommonName(name='Foxglove') cn1.index = Index(name='Perennial') cn2 = CommonName(name='Coleus') cn2.index = Index(name='Annual') idx = Index(name='Has no lookup_dict') with pytest.raises(TypeError): queryable_dicts_to_json((cn1, cn2, idx))
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_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_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_queryable_dicts_to_json(self): """Generate a JSON string for looking up Grows With cns/cvs. It can take either, as both have the queryable_dict method. """ gwcn1 = CommonName(name='Foxglove') gwcn1.index = Index(name='Perennial') assert queryable_dicts_to_json([gwcn1]) == \ json.dumps((gwcn1.queryable_dict,)) gwcn2 = CommonName(name='Butterfly Weed') gwcn2.index = Index(name='Perennial') assert queryable_dicts_to_json([gwcn1, gwcn2]) == \ json.dumps((gwcn1.queryable_dict, gwcn2.queryable_dict)) gwcv1 = Cultivar(name='Soulmate') gwcv1.common_name = CommonName(name='Butterfly Weed') gwcv1.common_name.index = Index(name='Perennial') assert queryable_dicts_to_json([gwcv1]) == \ json.dumps((gwcv1.queryable_dict,)) gwcv2 = Cultivar(name='Petra') gwcv2.common_name = CommonName(name='Foxglove') gwcv2.common_name.index = Index(name='Perennial') gwcv2.section = Section(name='Polkadot') assert queryable_dicts_to_json([gwcv1, gwcv2]) == \ json.dumps((gwcv1.queryable_dict, gwcv2.queryable_dict))
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_add_one_with_optionals(self): """Add a common name with optionals to Common Names sheet.""" wb = Workbook() ws = wb.active cnws = CommonNamesWorksheet(ws) cnws.setup() cn = CommonName(name='Foxglove', description='Spotty.', instructions='Just add water!') cn.index = Index(name='Perennial') cn.synonyms_string = 'Digitalis' cnws.add_one(cn) assert cnws.cell(2, cnws.cols['Description']).value == 'Spotty.' assert cnws.cell( 2, cnws.cols['Planting Instructions'] ).value == 'Just add water!' assert cnws.cell(2, cnws.cols['Synonyms']).value == 'Digitalis'
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_add_one_no_optionals(self): """Add a common name (with no optional data) to Common Names sheet.""" messages = StringIO() wb = Workbook() ws = wb.active cnws = CommonNamesWorksheet(ws) cnws.setup() cn = CommonName(name='Foxglove') cn.index = Index(name='Perennial') cnws.add_one(cn, stream=messages) assert cnws.cell(2, cnws.cols['Index']).value == 'Perennial' assert cnws.cell(2, cnws.cols['Common Name']).value == 'Foxglove' assert cnws.cell(2, cnws.cols['Description']).value is None assert cnws.cell(2, cnws.cols['Planting Instructions']).value is None assert cnws.cell(2, cnws.cols['Synonyms']).value is None messages.seek(0) msgs = messages.read() assert ('Adding data from <CommonName "Foxglove"> to row #2 of ' 'common names worksheet.') in msgs
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_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