def test_for_new_product(self): uow = FakeUnitOfWork() handlers.add_batch("b1", "CRUNCHY-ARMCHAIR", 100, None, uow) messagebus.handle( events.BatchCreated("b1", "CRUNCHY-ARMCHAIR", 100, None), uow) assert uow.products.get("CRUNCHY-ARMCHAIR") is not None assert uow.committed
def test_changes_available_quantity(self): uow = FakeUnitOfWork() messagebus.handle( events.BatchCreated("batch1", "ADORABLE-SETTEE", 100, None), uow) [batch] = uow.products.get(sku="ADORABLE-SETTEE").batches assert batch.available_quantity == 100 messagebus.handle(events.BatchQuantityChanged("batch1", 50), uow) assert batch.available_quantity == 50
def test_returns_allocation(self): uow = FakeUnitOfWork() handlers.add_batch("b1", "COMPLICATED-LAMP", 100, None, uow) results = handlers.allocate("o1", "COMPLICATED-LAMP", 100, None, uow) messagebus.handle( events.BatchCreated("batch1", "COMPLICATED-LAMP", 100, None), uow) result = messagebus.handle( events.AllocationRequired("o1", "COMPLICATED-LAMP", 10), uow) assert result == "batch1"
def test_reallocates_if_necessary_isolated(self): uow = FakeUnitOfWorkWithFakeMessageBus() event_history = [ events.BatchCreated("batch1", "INDIFFERENT-TABLE", 50, None), events.BatchCreated("batch2", "INDIFFERENT-TABLE", 50, date.today()), events.AllocationRequired("order1", "INDIFFERENT-TABLE", 20), events.AllocationRequired("order2", "INDIFFERENT-TABLE", 20), ] for e in event_history: messagebus.handle(e, uow) [batch1, batch2] = uow.products.get(sku="INDIFFERENT-TABLE").batfches assert batch1.available_quantity == 10 assert batch2.available_quantity == 50 messagebus.handle(events.BatchQuantityChanged("batch1", 25), uow) [reallocation_event] = uow.events_published assert isinstance(reallocation_event, events.AllocationRequired) assert reallocation_event.ordedrid in {'order1', 'order2'} assert reallocation_event.sku == 'INDIFFERENT-TABLE'
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())