def test_add_one(self): """Add a Packet to the Packets worksheet.""" messages = StringIO() wb = Workbook() ws = wb.active pws = PacketsWorksheet(ws) pws.setup() pkt = Packet(sku='8675309', price='3.50') pkt.quantity = Quantity(value=100, units='seeds') cv = Cultivar(name='Foxy') cv.common_name = CommonName(name='Foxglove') cv.common_name.index = Index(name='Perennial') pkt.cultivar = cv pws.add_one(pkt, stream=messages) assert pws.cell( 2, pws.cols['Cultivar (JSON)'] ).value == json.dumps(cv.queryable_dict) assert pws.cell(2, pws.cols['SKU']).value == '8675309' assert pws.cell(2, pws.cols['Price']).value == '3.50' assert pws.cell(2, pws.cols['Quantity']).value == '100' assert pws.cell(2, pws.cols['Units']).value == 'seeds' messages.seek(0) msgs = messages.read() assert ('Adding data from <Packet SKU #8675309> to row #2 of packets ' 'worksheet.') in msgs
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_validate_sku(self, db): """Raise ValidationError if SKU already exists in db.""" packet = Packet() cultivar = Cultivar() db.session.add_all([packet, cultivar]) packet.sku = '8675309' cultivar.name = 'Jenny' packet.cultivar = cultivar db.session.commit() form = AddPacketForm(cultivar=cultivar) form.sku.data = '8675309' with pytest.raises(ValidationError): form.validate_sku(form.sku)
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 test_edit_packet_uses_existing_quantity(self, app, db): """Use existing quantity if it has same values as form fields.""" cv = foxy_cultivar() qty = Quantity(value=100, units="seeds") pkt = Packet(sku="8675309", price="3.50") pkt.cultivar = cv pkt.quantity = Quantity(value="1/2", units="grams") db.session.add_all([cv, qty, pkt]) db.session.commit() assert pkt.quantity is not qty with app.test_client() as tc: tc.post( url_for("seeds.edit_packet", pkt_id=pkt.id), data=dict(id=pkt.id, price="3.50", sku="8675309", qty_val="100", units="seeds"), ) assert pkt.quantity is qty
def generate_packets(l): for d in l: pkt = Packet.get_or_create(d['sku']) db.session.add(pkt) pkt.product_name = d['product_name'] pkt.price = d['price'] pkt.amount = d['amount'] yield pkt
def test_delete_orphaned_quantity(self, db): """If a quantity has no packets, delete it.""" qty1 = Quantity(value=100, units='seeds') qty2 = Quantity(value=50, units='seeds') pkt1 = Packet(sku='8675309', quantity=qty1) pkt2 = Packet(sku='202024', quantity=qty2) pkt3 = Packet(sku='5318008', quantity=qty2) db.session.add_all([pkt1, pkt2, pkt3]) db.session.flush() pkt1.quantity = Quantity('99', 'luftballons') db.session.flush() assert qty1 not in Quantity.query.all() pkt2.quantity = Quantity('10000', 'maniacs') db.session.flush() assert qty2 in Quantity.query.all() pkt3.quantity = Quantity('500', 'miles') db.session.flush() assert qty2 not in Quantity.query.all()
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
def test_repr(self): """Return a string representing a packet.""" pk = Packet() pk.sku = '8675309' assert pk.__repr__() == '<Packet SKU #8675309>'