def test_edit_packet_submission_no_changes(self, app, db): """Flash a message if no changes are made in a form submission.""" cultivar = Cultivar() packet = Packet() db.session.add(packet, cultivar) packet.price = Decimal("2.99") packet.quantity = Quantity(value=100, units="seeds") packet.sku = "8675309" cultivar.name = "Foxy" cultivar.common_name = CommonName(name="Foxglove") cultivar.packets.append(packet) db.session.commit() with app.test_client() as tc: rv = tc.post( url_for("seeds.edit_packet", pkt_id=packet.id), data=dict( id=packet.id, price=packet.price, qty_val=str(packet.quantity.value), units=packet.quantity.units, sku=packet.sku, ), follow_redirects=True, ) assert "No changes to" in str(rv.data)
def test_edit_packet_submission_change_inputs(self, app, db): """Change packet and flash message if new values present in inputs.""" packet = Packet() db.session.add(packet) packet.price = Decimal("1.99") packet.quantity = Quantity(value=100, units="seeds") packet.sku = "8675309" packet.cultivar = Cultivar(name="Foxy") db.session.commit() with app.test_client() as tc: tc.post( url_for("seeds.edit_packet", pkt_id=packet.id), data=dict( id=packet.id, cultivar_id=packet.cultivar.id, price="2.99", qty_val="2.5", units="grams", sku="BOUT350", ), follow_redirects=True, ) assert packet.price == Decimal("2.99") assert packet.quantity.value == Decimal("2.5") assert packet.quantity.units == "grams" assert packet.sku == "BOUT350"
def test_info_getter(self): """Return a string containing onformation on the packet.""" pk = Packet() pk.sku = '8675309' pk.price = '3.50' assert pk.info == 'SKU #8675309: $3.50 for None None' pk.quantity = Quantity(100, 'seeds') assert pk.info == 'SKU #8675309: $3.50 for 100 seeds'
def test_remove_packet_renders_page(self, app, db): """Render form page given a valid packet id.""" packet = Packet() db.session.add(packet) packet.price = Decimal("1.99") packet.quantity = Quantity(value=100, units="seeds") packet.sku = "8675309" db.session.commit() with app.test_client() as tc: rv = tc.get(url_for("seeds.remove_packet", pkt_id=packet.id)) assert "Remove Packet" in str(rv.data)
def test_remove_packet_submission_verified(self, app, db): """Delete packet and flash a message if verify_removal is checked.""" packet = Packet() db.session.add(packet) packet.price = Decimal("1.99") packet.quantity = Quantity(value=100, units="seeds") packet.sku = "8675309" db.session.commit() with app.test_client() as tc: tc.post( url_for("seeds.remove_packet", pkt_id=packet.id), data=dict(verify_removal=True), follow_redirects=True ) assert Packet.query.count() == 0
def test_edit_packet_renders_page(self, app, db): """Render form page with valid pkt_id and no post data.""" cultivar = Cultivar() packet = Packet() db.session.add_all([packet, cultivar]) packet.price = Decimal("2.99") packet.quantity = Quantity(value=100, units="seeds") packet.sku = "8675309" cultivar.name = "Foxy" cultivar.common_name = CommonName(name="Foxglove") cultivar.packets.append(packet) db.session.commit() with app.test_client() as tc: rv = tc.get(url_for("seeds.edit_packet", pkt_id=packet.id)) assert "Edit Packet" in str(rv.data)
def test_select_packet_valid_submission(self, app, db): """Redirect to dest given valid selection.""" cultivar = Cultivar() packet = Packet() db.session.add_all([cultivar, packet]) cultivar.name = "Foxy" cultivar.common_name = CommonName(name="Foxglove") cultivar.packets.append(packet) packet.price = Decimal("1.99") packet.quantity = Quantity(value=100, units="seeds") packet.sku = "8675309" db.session.commit() with app.test_client() as tc: rv = tc.post(url_for("seeds.select_packet", dest="seeds.edit_packet"), data=dict(packet=packet.id)) assert rv.location == url_for("seeds.edit_packet", pkt_id=packet.id, _external=True)
def test_remove_packet_submission_no_changes(self, app, db): """Redirect and flash a message if verify_removal unchecked.""" packet = Packet() db.session.add(packet) packet.price = Decimal("1.99") packet.quantity = Quantity(value=100, units="seeds") packet.sku = "8675309" db.session.commit() with app.test_client() as tc: rv = tc.post(url_for("seeds.remove_packet", pkt_id=packet.id), data=dict(verify_removal=None)) assert rv.location in url_for("seeds.remove_packet", pkt_id=packet.id, _external=True) with app.test_client() as tc: rv = tc.post( url_for("seeds.remove_packet", pkt_id=packet.id), data=dict(verify_removal=None), follow_redirects=True ) assert "Packet was not removed" in str(rv.data)
def save_row_to_db(self, row, stream=sys.stdout): """Save a row from the Packets sheet to the database. Args: row: The number of the row to save. stream: Optional IO stream to print messages to. Returns: bool: `True` if changes have been made, `False` if not. """ cultivar_json = self.cell(row, self.cols['Cultivar (JSON)']).value cv_dict = json.loads(cultivar_json) sku = self.cell(row, self.cols['SKU']).value price = self.cell(row, self.cols['Price']).value quantity = self.cell(row, self.cols['Quantity']).value units = self.cell(row, self.cols['Units']).value print('-- BEGIN editing/creating Packet with the SKU \'{0}\' from row ' '#{1}. --'.format(sku, row), file=stream) edited = False pkt = Packet.query.filter(Packet.sku == sku).one_or_none() if pkt: print('The Packet with SKU \'{0}\' has been loaded from the ' 'database.'.format(pkt.sku), file=stream) else: edited = True qty = Quantity.from_queryable_values(value=quantity, units=units) if not qty: qty = Quantity(value=quantity, units=units) pkt = Packet(sku=sku, price=price, quantity=qty) db.session.add(pkt) pkt.cultivar = Cultivar.get_or_create( name=dbify(cv_dict['Cultivar Name']), common_name=dbify(cv_dict['Common Name']), index=dbify(cv_dict['Index']), stream=stream ) print('The Packet with SKU \'{0}\' does not yet exist, so it has ' 'been created.'.format(pkt.sku), file=stream) if price != str(pkt.price): edited = True pkt.price = price print('The price for Packet SKU \'{0}\' has been set to: ${1}.' .format(pkt.sku, pkt.price), file=stream) qty = Quantity.from_queryable_values(value=quantity, units=units) if not qty: qty = Quantity(value=quantity, units=units) if qty is not pkt.quantity: edited = True pkt.quantity = qty print('The quantity for the Packet SKU \'{0}\' has been set to: ' '{1} {2}'.format(pkt.sku, qty.value, qty.units), file=stream) if edited: db.session.flush() print('Changes to the Packet \'{0}\' have been flushed to ' 'the database.'.format(pkt.info), file=stream) else: print('No changes were made to the Packet \'{0}\'.' .format(pkt.info), file=stream) print('-- END editing/creating Packet with SKU \'{0}\' from row #{1}. ' '--'.format(pkt.sku, row), file=stream) return edited