예제 #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
예제 #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
        },
    ]
예제 #3
0
    def test_for_new_product(self, fake_bus):
        batch = 'b1'
        sku = 'CRUNCHY-ARMCHAIR'

        #uow = FakeUnitOfWork(None)
        #MessageBus().handle(commands.CreateBatch(batch, sku, 100, None), uow)
        fake_bus.handle(commands.CreateBatch(batch, sku, 100, None))
        assert fake_bus.uow.products.get(sku=sku).get_batch(batch) is not None
        assert fake_bus.uow.committed
예제 #4
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]
예제 #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',
    ]
예제 #6
0
    def test_changes_available_quantity(self):
        batchref = 'b1'
        sku = 'COMPLICATED-LAMP'

        uow = FakeUnitOfWork(None)
        MessageBus().handle(commands.CreateBatch(batchref, sku, 100, None),
                            uow)

        [batch] = uow.products.get(sku=sku).batches
        assert batch.available_quantity == 100

        MessageBus().handle(commands.ChangeBatchQuantity(batchref, 50), uow)
        assert batch.available_quantity == 50
예제 #7
0
def batches():
    #orm.start_mappers()
    try:
        eta = request.json['eta']
        if eta is not None:
            eta = datetime.fromisoformat(eta).date()
        command = commands.CreateBatch(request.json['ref'],
                                       request.json['sku'],
                                       request.json['qty'], eta)
        # uow = unit_of_work.ProductSqlAlchemyUnitOfWork(get_session)
        # bus.handle(command, uow)
        bus.handle(command)
        return 'OK', 201
    except Exception as e:
        return jsonify({'message': str(e)}), 400