def test_for_existing_product(): bus = FakeBus() bus.handle(commands.CreateBatch("b1", "GARISH-RUG", 100, None)) bus.handle(commands.CreateBatch("b2", "GARISH-RUG", 99, None)) assert "b2" in [ b.reference for b in bus.uow.products.get("GARISH-RUG").batches ]
def test_for_existing_product(): bus = FakeBus() bus.handle([ commands.CreateBatch('b1', 'sku1', 100, None), commands.CreateBatch('b2', 'sku1', 99, None), ]) assert 'b2' in [ b.reference for b in bus.uow.products.get('sku1').batches ]
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'}, ]
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'}, ]
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'}, ]
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)
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", ]
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)])
def test_commits(): bus = FakeBus() bus.handle([ commands.CreateBatch("b1", "OMINOUS-MIRROR", 100, None), commands.Allocate("o1", "OMINOUS-MIRROR", 10), ]) assert bus.uow.committed
def test_commits(): bus = FakeBus() bus.handle([ commands.CreateBatch('b1', 'sku1', 100, None), commands.Allocate('o1', 'sku1', 10), ]) assert bus.uow.committed
def test_changes_available_quantity(): bus = FakeBus() bus.handle([commands.CreateBatch("b1", "ADORABLE-SETTEE", 100, None)]) [batch] = bus.uow.products.get(sku="ADORABLE-SETTEE").batches assert batch.available_quantity == 100 bus.handle([commands.ChangeBatchQuantity("b1", 50)]) assert batch.available_quantity == 50
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
def test_changes_available_quantity(): bus = FakeBus() bus.handle([commands.CreateBatch('b1', 'sku1', 100, None)]) [batch] = bus.uow.products.get(sku='sku1').batches assert batch.available_quantity == 100 bus.handle([commands.ChangeBatchQuantity('b1', 50)]) assert batch.available_quantity == 50
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
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
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
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']
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', )
def add_batch(): 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, ) bus.handle([command]) return 'OK', 201
def test_for_new_product(): bus = FakeBus() bus.handle([commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None)]) assert bus.uow.products.get("CRUNCHY-ARMCHAIR") is not None assert bus.uow.committed
def test_for_new_product(): bus = FakeBus() bus.handle([commands.CreateBatch('b1', 'sku1', 100, None)]) assert bus.uow.products.get('sku1') is not None assert bus.uow.committed