def toggle_order_shipped(): """ toggle Shipped Date, to trigger * balance adjustment * cascade to OrderDetails * and Product adjustment also test join """ pre_cust = session.query( models.Customer).filter(models.Customer.Id == "ALFKI").one() session.expunge(pre_cust) print("") test_order = session.query(models.Order).filter( models.Order.Id == 11011).join(models.Employee).one() if test_order.ShippedDate is None or test_order.ShippedDate == "": test_order.ShippedDate = str(datetime.now()) print( prt("Shipping order - ShippedDate: ['' -> " + test_order.ShippedDate + "]")) else: test_order.ShippedDate = None print(prt("Returning order - ShippedDate: [ -> None]")) insp = inspect(test_order) session.commit() print("") post_cust = session.query( models.Customer).filter(models.Customer.Id == "ALFKI").one() logic_row = LogicRow(row=post_cust, old_row=pre_cust, ins_upd_dlt="*", nest_level=0, a_session=session, row_sets=None) if abs(post_cust.Balance - pre_cust.Balance) == 960: logic_row.log("Correct adjusted Customer Result") assert True else: logic_row.log(post_cust, "ERROR - incorrect adjusted Customer Result") assert False if post_cust.Balance == 0: pass else: logic_row.log("ERROR - balance should be 0") assert False if post_cust.UnpaidOrderCount == 2 and pre_cust.UnpaidOrderCount == 3: pass else: logic_row.log("Error - UnpaidOrderCount should be 2") assert False
def toggle_order_shipped(): """ toggle Shipped Date, to trigger balance adjustment """ """ also test join. session.query(Customer).join(Invoice).filter(Invoice.amount == 8500).all() """ pre_cust = session.query( models.Customer).filter(models.Customer.Id == "ALFKI").one() session.expunge(pre_cust) print("") test_order = session.query(models.Order).filter( models.Order.Id == 11011).join(models.Employee).one() if test_order.ShippedDate is None or test_order.ShippedDate == "": test_order.ShippedDate = str(datetime.now()) print( prt("Shipping order - ShippedDate: ['' -> " + test_order.ShippedDate + "]")) else: test_order.ShippedDate = None print(prt("Returning order - ShippedDate: [ -> None]")) insp = inspect(test_order) session.commit() print("") post_cust = session.query( models.Customer).filter(models.Customer.Id == "ALFKI").one() logic_row = LogicRow(row=pre_cust, old_row=post_cust, ins_upd_dlt="*", nest_level=0, a_session=session, row_cache=None) if abs(post_cust.Balance - pre_cust.Balance) == 960: logic_row.log("Correct adjusted Customer Result") assert True else: row_prt(post_cust, "\nERROR - incorrect adjusted Customer Result") print("\n--> probable cause: Order customer update not written") row_prt(pre_cust, "\npre_alfki") assert False
# OrderDetails - https://docs.sqlalchemy.org/en/13/orm/backref.html bad_item1 = models.OrderDetail(ProductId=1, Amount=0, Quantity=1, UnitPrice=18, Discount=0) bad_order.OrderDetailList.append(bad_item1) bad_item2 = models.OrderDetail(ProductId=2, Amount=0, Quantity=20000, UnitPrice=18, Discount=0) bad_order.OrderDetailList.append(bad_item2) did_fail_as_expected = False try: session.commit() except: session.rollback() did_fail_as_expected = True if not did_fail_as_expected: raise Exception("huge order expected to fail, but succeeded") else: print("\n" + prt( "huge order failed credit check as expected. Now trying valid order, should succeed..." )) new_order = models.Order(AmountTotal=0, CustomerId="ALFKI", ShipCity="Richmond", EmployeeId=6,