Beispiel #1
0
    def test_reallocates_if_necessary(self):
        batchref1 = 'b1'
        batchref2 = 'b2'
        sku = 'FACA-DOISGUMES'

        uow = FakeUnitOfWork(None)
        event_history = [
            commands.CreateBatch(batchref1, sku, 50, None),
            commands.CreateBatch(batchref2, sku, 50, date.today()),
            commands.Allocate('o1', sku, 21),
            commands.Allocate('o2', sku, 20),
        ]

        for e in event_history:
            MessageBus().handle(e, uow)

        [batch1, batch2] = uow.products.get(sku=sku).batches
        assert batch1.available_quantity == 9
        assert batch2.available_quantity == 50

        MessageBus().handle(commands.ChangeBatchQuantity(batchref1, 25), uow)
        # order1 or order2 will be deallocated, so we'll have 25 - 21
        assert batch1.available_quantity == 5
        # and 20 will be reallocated to the next batch
        assert batch2.available_quantity == 29
Beispiel #2
0
def test_allocations_view(sqlite_bus):

    orderid1 = random_orderid('1')
    other_orderid1 = random_orderid('other-1')
    sku1, sku2 = random_sku('1'), random_sku('2')

    batch1, batch2, other_batch1 = random_batchref('1'), random_batchref(
        '2'), random_batchref('other-1')
    sqlite_bus.handle(commands.CreateBatch(batch1, sku1, 50, None))
    sqlite_bus.handle(commands.CreateBatch(batch2, sku2, 50, None))
    sqlite_bus.handle(commands.Allocate(orderid1, sku1, 20))
    sqlite_bus.handle(commands.Allocate(orderid1, sku2, 20))

    sqlite_bus.handle(commands.CreateBatch(other_batch1, sku1, 50, today))

    sqlite_bus.handle(commands.Allocate(other_orderid1, sku1, 30))
    sqlite_bus.handle(commands.Allocate(other_orderid1, sku2, 10))

    assert views.allocations(orderid1, sqlite_bus.uow) == [
        {
            'sku': sku1,
            'batchref': batch1
        },
        {
            'sku': sku2,
            'batchref': batch2
        },
    ]
Beispiel #3
0
    def test_returns_allocation(self, fake_bus):
        batch = 'b1'
        sku = 'COMPLICATED-LAMP'

        #uow = FakeUnitOfWork(None)
        #MessageBus().handle(commands.CreateBatch(batch, sku, 100, None), uow)
        #result = MessageBus().handle(commands.Allocate('o1', sku, 10), uow)
        fake_bus.handle(commands.CreateBatch(batch, sku, 100, None))
        result = fake_bus.handle(commands.Allocate('o1', sku, 0))
        assert result == [batch]
Beispiel #4
0
 def change_batch_quantity(self, ref: str, qty: int):
     batch = next(b for b in self.batches if b.reference == ref)
     batch._purchased_quantity = qty
     while batch.available_quantity < 0:
         line = batch.deallocate_one()
         logging.info('desalocou ')
         if not hasattr(self, 'events'):
             self.events = []
         self.events.append(
             commands.Allocate(line.orderid, line.sku, line.qty))
Beispiel #5
0
def test_sends_email_on_out_of_stock_error():
    fake_notifs = FakeNotifications()
    bus = bootstrap.bootstrap(start_orm=False,
                              uow=FakeUnitOfWork(None),
                              notifications=fake_notifs,
                              publish=lambda *args: None)
    bus.handle(commands.CreateBatch('b1', 'POPULAR-CURTAINS', 9, None))
    bus.handle(commands.Allocate('o1', 'POPULAR-CURTAINS', 10))
    assert fake_notifs.sent['*****@*****.**'] == [
        f'Out of stock for POPULAR-CURTAINS',
    ]
Beispiel #6
0
def allocate_endpoint():
    try:
        #command = commands.Allocate.from_json(request.json)

        command = commands.Allocate(
            request.json['orderid'],
            request.json['sku'],
            request.json['qty'],
        )
        results = MessageBus().handle(
            command, unit_of_work.ProductSqlAlchemyUnitOfWork(get_session))
        batchref = results.pop(0)
    except (OutOfStock, InvalidSku, Exception) as e:
        return jsonify({'message': str(e)}), 400

    return jsonify({'batchref': batchref}), 202
Beispiel #7
0
def reallocate(event: events.Deallocated, uow: unit_of_work.AbstractUnitOfWork):
    with uow:
        product = uow.products.get(sku=event.sku)
        product.events.append(commands.Allocate(**asdict(event)))
        uow.commit()