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_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_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_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 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