def test_orderline_mapper_can_load_lines(session): session.execute('INSERT INTO order_lines (orderid, sku, qty) VALUES' '("order1", "RED-CHAIR", 12),' '("order1", "RED-TABLE", 13),' '("order2", "BLUE-LIPSTICK", 14)') expected = [ model.OrderLine("order1", "RED-CHAIR", 12), model.OrderLine("order1", "RED-TABLE", 13), model.OrderLine("order2", "BLUE-LIPSTICK", 14), ]
def test_retrieving_allocations(session): session.execute( 'INSERT INTO order_lines (orderid, sku, qty) VALUES ("order1", "sku1", 12)' ) [[olid]] = session.execute( 'SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku', dict(orderid='order1', sku='sku1') ) session.execute( 'INSERT INTO batches (reference, sku, _purchased_quantity, eta)' ' VALUES ("batch1", "sku1", 100, null)' ) [[bid]] = session.execute( 'SELECT id FROM batches WHERE reference=:ref AND sku=:sku', dict(ref='batch1', sku='sku1') ) session.execute( 'INSERT INTO allocations (orderline_id, batch_id) VALUES (:olid, :bid)', dict(olid=olid, bid=bid) ) batch = session.query(model.Batch).one() assert batch._allocations == { model.OrderLine("order1", "sku1", 12) }
def test_orderline_mapper_can_save_lines(session): new_line = model.OrderLine("order1", "DECORATIVE-WIDGET", 12) session.add(new_line) session.commit() rows = list(session.execute('SELECT orderid, sku, qty FROM "order_lines"')) assert rows == [("order1", "DECORATIVE-WIDGET", 12)]
def test_error_for_invalid_sku(): line = model.OrderLine("o1", "NONEXISTENTSKU", 10) batch = model.Batch("b1", "AREALSKU", 100, eta=None) repo = FakeRepository([batch]) with pytest.raises(handlers.InvalidSku, match="Invalid name of SKU: NONEXISTENTSKU"): handlers.allocate("o1", "NONEXISTENTSKU", 10, repo, FakeSession())
def test_saving_allocations(session): batch = model.Batch('batch1', 'sku1', 100, eta=None) line = model.OrderLine('order1', 'sku1', 10) batch.allocate(line) session.add(batch) session.commit() rows = list(session.execute('SELECT orderline_id, batch_id FROM "allocations"')) assert rows == [(batch.id, line.id)]
def test_commits(): line = model.OrderLine("o1", "OMINOUS-MIRROR", 10) batch = model.Batch("b1", "OMINOUS-MIRROR", 100, eta=None) repo = FakeRepository([batch]) session = FakeSession() handlers.allocate("o1", "OMINOUS-MIRROR", 10, repo, session) assert session.committed is True
def test_uow_can_retrieve_a_batch_and_allocate_to_it(session_factory): session = session_factory() insert_batch(session, 'batch1', 'HIPSTER-WORKBENCH', 100, None) session.commit() uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory) with uow: batch = uow.batches.get(reference='batch1') line = model.OrderLine('o1', 'HIPSTER-WORKBENCH', 10) batch.allocate(line) uow.commit() batchref = get_allocated_batch_ref(session, 'o1', 'HIPSTER-WORKBENCH') assert batchref == 'batch1'
def allocate_endpoint(): session = get_session() repo = repository.SqlAlchemyRepository(session) line = model.OrderLine( request.json['orderid'], request.json['sku'], request.json['qty'], ) try: batchref = services.allocate(line, repo, session) except (model.OutOfStock, services.InvalidSku) as e: return jsonify({'message': str(e)}), 400 return jsonify({'batchref': batchref}), 201
def test_repository_can_retrieve_a_batch_with_allocations(session): orderline_id = insert_order_line(session) batch1_id = insert_batch(session, "batch1") insert_batch(session, "batch2") insert_allocation(session, orderline_id, batch1_id) repo = repository.SqlAlchemyRepository(session) retrieved = repo.get("batch1") expected = model.Batch("batch1", "GENERIC-SOFA", 100, eta=None) assert retrieved == expected # Batch.__eq__ only compares reference assert retrieved.sku == expected.sku assert retrieved._purchased_quantity == expected._purchased_quantity assert retrieved._allocations == { model.OrderLine("order1", "GENERIC-SOFA", 12), }