Exemplo n.º 1
0
 def congratulate_sales_rep(row: Order, old_row: Order, logic_row: LogicRow):
     if logic_row.ins_upd_dlt == "ins" or True:  # logic engine fills parents for insert
         sales_rep = row.SalesRep  # type : Employee
         if sales_rep is None:
             logic_row.log("no salesrep for this order")
         else:
             logic_row.log(f'Hi, {sales_rep.Manager.FirstName}, congratulate {sales_rep.FirstName} on their new order')
Exemplo n.º 2
0
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 == "":
        # with restored db, cust[ALFKI] has bal 960 & 3 unpaid orders, Order[11011) is 960, unshipped
        test_order.ShippedDate = str(datetime.now())
        print(prt("Shipping order - ShippedDate: ['' -> " + test_order.ShippedDate + "]" +
                  " for customer balance: " + str(pre_cust.Balance) +
                  ", with UnpaidOrderCount: " + str(pre_cust.UnpaidOrderCount)))
    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:
        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
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
Exemplo n.º 4
0
session.commit()

post_cust = session.query(
    models.Customer).filter(models.Customer.Id == "ALFKI").one()

print("\nadd_order, update completed\n\n")
row_prt(new_order, "\nnew Order Result")  # $18 + $38 = $56
if new_order.AmountTotal != 56:
    print("==> ERROR - unexpected AmountTotal: " + str(new_order.AmountTotal) +
          "... expected 56")
row_prt(new_item1, "\nnew Order Detail 1 Result")  # 1 Chai  @ $18
row_prt(new_item2, "\nnew Order Detail 2 Result")  # 2 Chang @ $19 = $38

logic_row = LogicRow(row=post_cust,
                     old_row=pre_cust,
                     ins_upd_dlt="*",
                     nest_level=0,
                     a_session=session,
                     row_sets=None)
if post_cust.Balance == pre_cust.Balance + 56:
    logic_row.log("Correct adjusted Customer Result")
    assert True
else:
    logic_row.log("ERROR - incorrect adjusted Customer Result")
    print("\n--> probable cause: Order customer update not written")
    assert False
if post_cust.OrderCount == pre_cust.OrderCount + 1 and\
    post_cust.UnpaidOrderCount == pre_cust.UnpaidOrderCount + 1:
    pass
else:
    logic_row.log("Error - unexpected OrderCounts")
print("\nadd_order, ran to completion\n\n")
    test_order.RequiredDate = str(datetime.now())
    print(
        prt("Shipping order - RequiredDate: ['' -> " +
            test_order.RequiredDate + "]"))
else:
    test_order.RequiredDate = None
    print(prt("Returning order - RequiredDate: [ -> 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_sets=None)

if abs(post_cust.Balance - pre_cust.Balance) == 0:
    logic_row.log("Correct non-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

print("\nupd_order_shipped, ran to completion")
Exemplo n.º 6
0
def temp_debug(a_session, bug_explore, row_cache):
    """
    see description in nw/trans_tests/upd_order_reuse
    """
    for each_instance in a_session.dirty:
        table_name = each_instance.__tablename__
        if table_name.startswith("OrderDetail"):
            bug_explore[0] = each_instance
        else:
            bug_explore[1] = each_instance
    order_detail_first = False  # true triggers defer
    if order_detail_first:
        each_instance = bug_explore[0]
        old_row = get_old_row(each_instance)
        logic_row = LogicRow(row=each_instance,
                             old_row=old_row,
                             ins_upd_dlt="upd",
                             nest_level=0,
                             a_session=a_session,
                             row_sets=row_cache)
        logic_row.update(reason="client")
        each_instance = bug_explore[1]
        old_row = get_old_row(each_instance)
        logic_row = LogicRow(row=each_instance,
                             old_row=old_row,
                             ins_upd_dlt="upd",
                             nest_level=0,
                             a_session=a_session,
                             row_sets=row_cache)
        logic_row.update(reason="client")
    else:
        each_instance = bug_explore[1]
        old_row = get_old_row(each_instance)
        logic_row = LogicRow(row=each_instance,
                             old_row=old_row,
                             ins_upd_dlt="upd",
                             nest_level=0,
                             a_session=a_session,
                             row_sets=row_cache)
        logic_row.update(reason="client")
        each_instance = bug_explore[0]
        old_row = get_old_row(each_instance)
        logic_row = LogicRow(row=each_instance,
                             old_row=old_row,
                             ins_upd_dlt="upd",
                             nest_level=0,
                             a_session=a_session,
                             row_sets=row_cache)
        logic_row.update(reason="client")
Exemplo n.º 7
0
def before_flush(a_session: session, a_flush_context, an_instances):
    """
    Logic Execution processes LogicRows: row and old_row

    Note old_row is critical for:
        * user logic (did the value change?  by how much?)
        * performance / pruning (skip rules iff no dependent values change)
        * performance / optimization (1 row adjustments, not expensive select sum/count)
    """
    """
    Logic Phase
    """
    python_rules.logic_logger.debug(
        "Logic Phase:\t\tROW LOGIC (sqlalchemy before_flush)\t\t\t")
    # print("\n***************** sqlalchemy calls python_rules\n")

    row_sets = RowSets()  # type : RowSet
    for each_instance in a_session.dirty:
        row_sets.add_submitted(each_instance)

    bug_explore = None  # None to disable, [None, None] to enable
    if bug_explore is not None:  # temp hack - order rows to explore bug (upd_order_reuse)
        temp_debug(a_session, bug_explore, row_sets)
    else:
        for each_instance in a_session.dirty:
            table_name = each_instance.__tablename__
            old_row = get_old_row(each_instance)
            logic_row = LogicRow(row=each_instance,
                                 old_row=old_row,
                                 ins_upd_dlt="upd",
                                 nest_level=0,
                                 a_session=a_session,
                                 row_sets=row_sets)
            logic_row.update(reason="client")

    for each_instance in a_session.new:
        table_name = each_instance.__tablename__
        logic_row = LogicRow(row=each_instance,
                             old_row=None,
                             ins_upd_dlt="ins",
                             nest_level=0,
                             a_session=a_session,
                             row_sets=row_sets)
        logic_row.insert(reason="client")

    for each_instance in a_session.deleted:
        table_name = each_instance.__tablename__
        logic_row = LogicRow(row=each_instance,
                             old_row=None,
                             ins_upd_dlt="dlt",
                             nest_level=0,
                             a_session=a_session,
                             row_sets=row_sets)
        logic_row.delete(reason="client")
    """
    Commit Logic Phase
    """
    python_rules.logic_logger.debug(
        "Logic Phase:\t\tCOMMIT   \t\t\t\t\t\t\t\t\t")
    for each_logic_row_key in row_sets.processed_rows:
        each_logic_row = row_sets.processed_rows[each_logic_row_key]
        python_rules.engine_logger.debug("visit: " + each_logic_row.__str__())
        commit_row_events = rule_bank_withdraw.rules_of_class(
            each_logic_row, CommitRowEvent)
        for each_row_event in commit_row_events:
            each_logic_row.log("Commit Event")
            each_row_event.execute(each_logic_row)
    """
    Proceed with sqlalchemy flush processing
    """
    python_rules.logic_logger.debug(
        "Logic Phase:\t\tFLUSH   (sqlalchemy flush processing       \t")
Exemplo n.º 8
0
    else:
        logic_row.log("Product adjusted improperly on ship order")
        assert False


toggle_order_shipped()
pre_adjusted_product = session.query(
    models.Product).filter(models.Product.Id == 58).one()
session.expunge(pre_adjusted_product)
print("\ndlt_order, shipped... now delete")
delete_cust = session.query(
    models.Customer).filter(models.Customer.Id == "ALFKI").one()
session.delete(delete_cust)
session.commit()

post_adjusted_product = session.query(
    models.Product).filter(models.Product.Id == 58).one()
logic_row = LogicRow(row=post_adjusted_product,
                     old_row=pre_adjusted_product,
                     ins_upd_dlt="*",
                     nest_level=0,
                     a_session=session,
                     row_sets=None)
if post_adjusted_product.UnitsShipped == pre_adjusted_product.UnitsShipped - 40:
    logic_row.log("Product adjusted properly on delete customer")
else:
    logic_row.log("Product adjusted improperly on delete customer")
    assert False

print("\ndlt_order, shipped... deleted - check log")
Exemplo n.º 9
0
""" 
Illustrate re-use with a number of changes:
    1 - reassign the order to a different customer
    2 - change an OrderDetail (eg, "I'll buy 1 WidgetSet, not 5 Widgets")
        a. A different Product
        b. A different Quantity
"""

pre_alfki = session.query(
    models.Customer).filter(models.Customer.Id == "ALFKI").one()
pre_anatr = session.query(
    models.Customer).filter(models.Customer.Id == "ANATR").one()

logic_row = LogicRow(row=pre_alfki,
                     old_row=pre_alfki,
                     ins_upd_dlt="*",
                     nest_level=0,
                     a_session=session,
                     row_sets=None)
logic_row.log("starting")

logic_row = LogicRow(row=pre_anatr,
                     old_row=pre_anatr,
                     ins_upd_dlt="*",
                     nest_level=0,
                     a_session=session,
                     row_sets=None)
logic_row.log("starting")

pre_order = session.query(
    models.Order).filter(models.Order.Id == 11011).one()  # type : Order
logic_row = LogicRow(row=pre_order,