コード例 #1
0
def test_allocations_view(sqlite_bus):
    sqlite_bus.handle(commands.CreateBatch('b1', 'sku1', 50, None))
    sqlite_bus.handle(commands.CreateBatch('b2', 'sku2', 50, date.today()))
    sqlite_bus.handle(commands.Allocate('o1', 'sku1', 20))
    sqlite_bus.handle(commands.Allocate('o1', 'sku2', 20))

    assert views.allocations('o1', sqlite_bus.uow) == [
        {'sku': 'sku1', 'batchref': 'b1'},
        {'sku': 'sku2', 'batchref': 'b2'},
    ]
コード例 #2
0
def test_allocations_view(sqlite_bus):
    sqlite_bus.handle(commands.CreateBatch('sku1batch', 'sku1', 50, None))
    sqlite_bus.handle(commands.CreateBatch('sku2batch', 'sku2', 50, date.today()))
    sqlite_bus.handle(commands.Allocate('order1', 'sku1', 20))
    sqlite_bus.handle(commands.Allocate('order1', 'sku2', 20))
    # add a spurious batch and order to make sure we're getting the right ones
    sqlite_bus.handle(commands.CreateBatch('sku1batch-later', 'sku1', 50, date.today()))
    sqlite_bus.handle(commands.Allocate('otherorder', 'sku1', 30))
    sqlite_bus.handle(commands.Allocate('otherorder', 'sku2', 10))

    assert views.allocations('order1', sqlite_bus.uow) == [
        {'sku': 'sku1', 'batchref': 'sku1batch'},
        {'sku': 'sku2', 'batchref': 'sku2batch'},
    ]
コード例 #3
0
 def test_commits():
     bus = FakeBus()
     bus.handle([
         commands.CreateBatch("b1", "OMINOUS-MIRROR", 100, None),
         commands.Allocate("o1", "OMINOUS-MIRROR", 10),
     ])
     assert bus.uow.committed
コード例 #4
0
    def test_errors_for_invalid_sku():
        bus = FakeBus()
        bus.handle([commands.CreateBatch("b1", "AREALSKU", 100, None)])

        with pytest.raises(exceptions.InvalidSku,
                           match="Invalid sku NONEXISTENTSKU"):
            bus.handle([commands.Allocate("o1", "NONEXISTENTSKU", 10)])
コード例 #5
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()
コード例 #6
0
 def test_commits():
     bus = FakeBus()
     bus.handle([
         commands.CreateBatch('b1', 'sku1', 100, None),
         commands.Allocate('o1', 'sku1', 10),
     ])
     assert bus.uow.committed
コード例 #7
0
    def test_errors_for_invalid_sku():
        bus = FakeBus()
        bus.handle([commands.CreateBatch('b1', 'actualsku', 100, None)])

        with pytest.raises(exceptions.InvalidSku) as ex:
            bus.handle([commands.Allocate('o1', 'nonexistentsku', 10)])
        assert 'Invalid sku nonexistentsku' in str(ex)
コード例 #8
0
 def test_sends_email_on_out_of_stock_error():
     bus = FakeBus()
     bus.handle(commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None))
     bus.handle(commands.Allocate("o1", "POPULAR-CURTAINS", 10))
     assert bus.dependencies['notifications'].sent['*****@*****.**'] == [
         f"Out of stock for POPULAR-CURTAINS",
     ]
コード例 #9
0
 def test_allocates():
     bus = FakeBus()
     bus.handle([
         commands.CreateBatch("b1", "COMPLICATED-LAMP", 100, None),
         commands.Allocate("o1", "COMPLICATED-LAMP", 10),
     ])
     [batch] = bus.uow.products.get("COMPLICATED-LAMP").batches
     assert batch.available_quantity == 90
コード例 #10
0
 def test_allocates():
     bus = FakeBus()
     bus.handle([
         commands.CreateBatch('b1', 'sku1', 100, None),
         commands.Allocate('o1', 'sku1', 10),
     ])
     [batch] = bus.uow.products.get('sku1').batches
     assert batch.available_quantity == 90
コード例 #11
0
    def test_reallocates_if_necessary():
        bus = FakeBus()
        bus.handle([
            commands.CreateBatch('b1', 'sku1', 50, None),
            commands.CreateBatch('b2', 'sku1', 50, date.today()),
            commands.Allocate('o1', 'sku1', 20),
            commands.Allocate('o2', 'sku1', 20),
        ])
        [batch1, batch2] = bus.uow.products.get(sku='sku1').batches
        assert batch1.available_quantity == 10

        bus.handle([commands.ChangeBatchQuantity('b1', 25)])

        # o1 or o2 will be deallocated, so we'll have 25 - 20 * 1
        assert batch1.available_quantity == 5
        # and 20 will be reallocated to the next batch
        assert batch2.available_quantity == 30
コード例 #12
0
def test_deallocation(sqlite_bus):
    sqlite_bus.handle(commands.CreateBatch('b1', 'sku1', 50, None))
    sqlite_bus.handle(commands.CreateBatch('b2', 'sku1', 50, date.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'},
    ]
コード例 #13
0
ファイル: test_handlers.py プロジェクト: hbeemster/code
    def test_reallocates_if_necessary():
        bus = FakeBus()
        bus.handle([
            commands.CreateBatch("b1", "INDIFFERENT-TABLE", 50, None),
            commands.CreateBatch("b2", "INDIFFERENT-TABLE", 50, date.today()),
            commands.Allocate("o1", "INDIFFERENT-TABLE", 20),
            commands.Allocate("o2", "INDIFFERENT-TABLE", 20),
        ])
        [batch1,
         batch2] = bus.uow.products.get(sku="INDIFFERENT-TABLE").batches
        assert batch1.available_quantity == 10

        bus.handle([commands.ChangeBatchQuantity("b1", 25)])

        # o1 or o2 will be deallocated, so we"ll have 25 - 20 * 1
        assert batch1.available_quantity == 5
        # and 20 will be reallocated to the next batch
        assert batch2.available_quantity == 30
コード例 #14
0
def test_out_of_stock_email(bus):
    sku = random_sku()
    bus.handle(commands.CreateBatch('batch1', sku, 9, None))
    bus.handle(commands.Allocate('order1', sku, 10))
    messages = requests.get(
        f'http://{cfg["host"]}:{cfg["http_port"]}/api/v2/messages').json()
    message = next(m for m in messages['items'] if sku in str(m))
    assert message['Raw']['From'] == '*****@*****.**'
    assert message['Raw']['To'] == ['*****@*****.**']
    assert f'Out of stock for {sku}' in message['Raw']['Data']
コード例 #15
0
 def test_sends_email_on_out_of_stock_error():
     bus = FakeBus()
     bus.handle([
         commands.CreateBatch('b1', 'sku1', 9, None),
         commands.Allocate('o1', 'sku1', 10),
     ])
     assert bus.dependencies['send_mail'].call_args == mock.call(
         '*****@*****.**',
         f'Out of stock for sku1',
     )
コード例 #16
0
ファイル: flask_app.py プロジェクト: hbeemster/code
def allocate_endpoint():
    try:
        command = commands.Allocate(
            request.json['orderid'],
            request.json['sku'],
            request.json['qty'],
        )
        bus.handle([command])
    except exceptions.InvalidSku as e:
        return jsonify({'message': str(e)}), 400

    return 'OK', 202