def test_realocates_batch_if_nessesary_when_available_quantity_reduces( messagebus, ): history = [ commands.CreateBatch(reference="batch1", sku="INDIFFERENT-TABLE", qty=100), commands.CreateBatch( reference="batch2", sku="INDIFFERENT-TABLE", qty=100, eta=date.today(), ), commands.Allocate(orderid="order1", sku="INDIFFERENT-TABLE", qty=50), commands.Allocate(orderid="order2", sku="INDIFFERENT-TABLE", qty=50), ] for message in history: messagebus.handle(message) [batch1, batch2] = messagebus.uow.products.get("INDIFFERENT-TABLE").batches assert batch1.available_quantity == 0 assert batch2.available_quantity == 100 message = commands.ChangeBatchQuantity(reference="batch1", qty=55) messagebus.handle(message) assert batch1.available_quantity == 5 assert batch2.available_quantity == 50
def test_deallocation(sqlite_bus): sqlite_bus.handle(commands.CreateBatch('b1', 'sku1', 50, None)) sqlite_bus.handle(commands.CreateBatch('b2', 'sku1', 50, today)) sqlite_bus.handle(commands.Allocate('o1', 'sku1', 40)) sqlite_bus.handle(commands.ChangeBatchQuantity('b1', 10)) assert views.allocations('o1') == [{'sku': 'sku1', 'batchref': 'b2'}]
def test_changes_avaliable_qty(self): bus = bootstrap_test_app() bus.handle(commands.CreateBatch('批次1', '板凳', 70)) [batch] = bus.uow.products.get(sku='板凳').batches bus.handle(commands.ChangeBatchQuantity('批次1', 50)) assert batch.available_quantity == 50
def test_changes_available_quantity(self): bus = bootstrap_test_app() bus.handle(commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None)) [batch] = bus.uow.products.get(sku="ADORABLE-SETTEE").batches assert batch.available_quantity == 100 bus.handle(commands.ChangeBatchQuantity("batch1", 50)) assert batch.available_quantity == 50
def test_deallocation(sqlite_bus): sqlite_bus.handle(commands.CreateBatch("b1", "sku1", 50, None)) sqlite_bus.handle(commands.CreateBatch("b2", "sku1", 50, today)) sqlite_bus.handle(commands.Allocate("o1", "sku1", 40)) sqlite_bus.handle(commands.ChangeBatchQuantity("b1", 10)) assert views.allocations("o1", sqlite_bus.uow) == [ {"sku": "sku1", "batchref": "b2"}, ]
def test_deallocation(sqlite_session_factory): uow = unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory) messagebus.handle(commands.CreateBatch("b1", "sku1", 50, None), uow) messagebus.handle(commands.CreateBatch("b2", "sku1", 50, today), uow) messagebus.handle(commands.Allocate("o1", "sku1", 40), uow) messagebus.handle(commands.ChangeBatchQuantity("b1", 10), uow) assert views.allocations("o1", uow) == [ {"sku": "sku1", "batchref": "b2"}, ]
def test_changes_available_quantity(self): uow = FakeUnitOfWork() messagebus.handle( commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None), uow) [batch] = uow.products.get(sku="ADORABLE-SETTEE").batches assert batch.available_quantity == 100 messagebus.handle(commands.ChangeBatchQuantity("batch1", 50), uow) assert batch.available_quantity == 50
def test_deallocation(sqlite_session_factory): uow = unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory) messagebus.handle(commands.CreateBatch('b1', 'sku1', 50, None), uow) messagebus.handle(commands.CreateBatch('b2', 'sku1', 50, today), uow) messagebus.handle(commands.Allocate('o1', 'sku1', 40), uow) messagebus.handle(commands.ChangeBatchQuantity('b1', 10), uow) assert views.allocations('o1', uow) == [ { 'sku': 'sku1', 'batchref': 'b2' }, ]
def test_changes_available_quantity(messagebus): message = commands.CreateBatch(reference="batch1", sku="ADORABLE-SETTEE", qty=100) messagebus.handle(message) [batch] = messagebus.uow.products.get("ADORABLE-SETTEE").batches assert batch.available_quantity == 100 message = commands.ChangeBatchQuantity(reference="batch1", qty=50) messagebus.handle(message) assert batch.available_quantity == 50
def test_reallocates_if_necessary(self): bus = bootstrap_test_app() event_history = [ commands.CreateBatch('批次1', '桌子', 50), commands.CreateBatch('批次2', '桌子', 50, date.today()), commands.Allocate('订单1', '桌子', 20), commands.Allocate('订单2', '桌子', 20), ] for e in event_history: bus.handle(e) [batch1, batch2] = bus.uow.products.get(sku='桌子').batches assert batch1.available_quantity == 10 # 下了两单 现在库存10 assert batch2.available_quantity == 50 # 这个不变 # bus.handle( # commands.ChangeBatchQuantity('批次1', 25), uow) bus.handle(commands.ChangeBatchQuantity('批次1', 25)) assert batch1.available_quantity == 5 # 批次1泡水了 库存25 所以要退下的单子给批次2 assert batch2.available_quantity == 30 # 批次2 减少20
def test_reallocates_if_necessary(self): uow = FakeUnitOfWork() history = [ commands.CreateBatch("batch1", "INDIFFERENT-TABLE", 50, None), commands.CreateBatch("batch2", "INDIFFERENT-TABLE", 50, date.today()), commands.Allocate("order1", "INDIFFERENT-TABLE", 20), commands.Allocate("order2", "INDIFFERENT-TABLE", 20), ] for msg in history: messagebus.handle(msg, uow) [batch1, batch2] = uow.products.get(sku="INDIFFERENT-TABLE").batches assert batch1.available_quantity == 10 assert batch2.available_quantity == 50 messagebus.handle(commands.ChangeBatchQuantity("batch1", 25), uow) # order1 or order2 will be deallocated, so we'll have 25 - 20 assert batch1.available_quantity == 5 # and 20 will be reallocated to the next batch assert batch2.available_quantity == 30
def test_deallocation(messagebus, random_orderid): orderid = random_orderid() messagebus.handle(commands.CreateBatch("sku1batch", "sku1", 50, None)) messagebus.handle(commands.CreateBatch("sku2batch", "sku1", 50, today)) messagebus.handle(commands.Allocate(orderid, "sku1", 20)) assert views.allocations(orderid, messagebus.uow) == [ { "sku": "sku1", "qty": 20, "batchref": "sku1batch" }, ] messagebus.handle(commands.ChangeBatchQuantity("sku1batch", 10)) assert views.allocations(orderid, messagebus.uow) == [ { "sku": "sku1", "qty": 20, "batchref": "sku2batch" }, ]
def test_reallocates_if_necessary(self): bus = bootstrap_test_app() history = [ commands.CreateBatch("batch1", "INDIFFERENT-TABLE", 50, None), commands.CreateBatch("batch2", "INDIFFERENT-TABLE", 50, date.today()), commands.Allocate("order1", "INDIFFERENT-TABLE", 20), commands.Allocate("order2", "INDIFFERENT-TABLE", 20), ] for msg in history: bus.handle(msg) [batch1, batch2] = bus.uow.products.get(sku="INDIFFERENT-TABLE").batches assert batch1.available_quantity == 10 assert batch2.available_quantity == 50 bus.handle(commands.ChangeBatchQuantity("batch1", 25)) # order1 or order2 will be deallocated, so we"ll have 25 - 20 assert batch1.available_quantity == 5 # and 20 will be reallocated to the next batch assert batch2.available_quantity == 30
def test_reallocates_if_necessary(self): bus = bootstrap_test_app() event_history = [ commands.CreateBatch('batch1', 'INDIFFERENT-TABLE', 50, None), commands.CreateBatch('batch2', 'INDIFFERENT-TABLE', 50, date.today()), commands.Allocate('order1', 'INDIFFERENT-TABLE', 20), commands.Allocate('order2', 'INDIFFERENT-TABLE', 20) ] for e in event_history: bus.handle(e) [batch1, batch2] = bus.uow.products.get(sku='INDIFFERENT-TABLE').batches assert batch1.available_quantity == 10 assert batch2.available_quantity == 50 bus.handle(commands.ChangeBatchQuantity('batch1', 25)) # order 1 and order2 will be deallocated, so we'll have 25 - 20 assert batch1.available_quantity == 5 # and 20 will be reallocated to the next batch assert batch2.available_quantity == 30
def handle_change_batch_quantity(message, messagebus): data = json.loads(message["data"]) command = commands.ChangeBatchQuantity(data["batchref"], data["qty"]) messagebus.handle(message=command)
def handle_change_batch_quantity(m, bus): logger.info("handling %s", m) data = json.loads(m["data"]) cmd = commands.ChangeBatchQuantity(ref=data["batchref"], qty=data["qty"]) bus.handle(cmd)
def handle_change_batch_quantity(m): logging.debug('handling %s', m) data = json.loads(m['data']) cmd = commands.ChangeBatchQuantity(ref=data['batchref'], qty=data['qty']) messagebus.handle(cmd, uow=unit_of_work.SqlAlchemyUnitOfWork())
def handle_change_batch_quantity(m, bus): logger.debug(f'handling {m}') data = json.loads(m['data']) cmd = commands.ChangeBatchQuantity(ref=data['batchref'], qty=data['qty']) bus.handle(cmd)
def handle_change_batch_quantity(m, bus): logger.debug('handing %s', m) data = json.loads(m['data']) command = commands.ChangeBatchQuantity(ref=data['batchref'], qty=data['qty']) bus.handle(command)
def main(): logger.info("Redis pubsub starting") bus = bootstrap.bootstrap() pubsub = r.pubsub(ignore_subscribe_messages=True) pubsub.subscribe("change_batch_quantity") >>>>>>> upstream/master for m in pubsub.listen(): handle_change_batch_quantity(m, bus) def handle_change_batch_quantity(m, bus): <<<<<<< HEAD logger.info('handling %s', m) data = json.loads(m['data']) cmd = commands.ChangeBatchQuantity(ref=data['batchref'], qty=data['qty']) bus.handle(cmd) if __name__ == '__main__': ======= logger.info("handling %s", m) data = json.loads(m["data"]) cmd = commands.ChangeBatchQuantity(ref=data["batchref"], qty=data["qty"]) bus.handle(cmd) if __name__ == "__main__": >>>>>>> upstream/master main()
def handle_change_batch_quantity(m): logging.debug("handling %s", m) data = json.loads(m["data"]) cmd = commands.ChangeBatchQuantity(ref=data["batchref"], qty=data["qty"]) messagebus.handle(cmd, uow=unit_of_work.SqlAlchemyUnitOfWork())