def test_orderline_mapper_can_load_lines(session: 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), ] assert session.query(model.OrderLine).all() == expected
def test_returns_allocation(): line = model.OrderLine("o1", "COMPLICATED-LAMP", 10) batch = model.Batch("b1", "COMPLICATED-LAMP", 100, eta=None) repo = FakeRepository([batch]) result = services.allocate(line, repo, FakeSession()) assert result == "b1"
def test_orderline_mapper_can_save_lines(session: 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_bookmark_mapper_can_load_bookmarks(session): session.execute( "INSERT INTO bookmarks (id, title, url, notes, date_added) VALUES " '(1,"google","https://www.google.com/","google website", "2021-03-24 03:56:33.961691"),' '(2,"google1","https://www.google1.com/","google1 website", "2021-03-24 03:56:33.961691"),' '(3,"google2","https://www.google2.com/","google2 website", "2021-03-24 03:56:33.961691"),' ) expected = [ model.OrderLine(1, "google", "https://www.google.com/", "google website", "2021-03-24 03:56:33.961691"), model.OrderLine(2, "google1", "https://www.google1.com/", "google1 website", "2021-03-24 03:56:33.961691"), model.OrderLine(3, "google2", "https://www.google2.com/", "google2 website", "2021-03-24 03:56:33.961691"), ] assert session.query(model.Bookmark).all() == expected
def test_commits(): line = model.OrderLine('o1', 'OMINOUS-MIRROR', 10) batch = model.Batch('b1', 'OMINOUS-MIRROR', 100, eta=None) repo = FakeRepository([batch]) session = FakeSession() services.allocate(line, repo, session) assert session.committed is True
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(services.InvalidSku, match="Invalid sku NONEXISTENTSKU"): services.allocate(line, 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 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 assert retrieved.sku == expected.sku assert retrieved._purchased_quantity == expected._purchased_quantity assert retrieved._allocations == { model.OrderLine("order1", "GENERIC-SOFA", 12), }
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) # checks that the types match, and that the reference is the same assert retrieved == expected # Batch.__eq__ only compares reference # explicitly check on its major attributes, including ._allocations, which is a Python set of OrderLine value objects. assert retrieved.sku == expected.sku assert retrieved._purchased_quantity == expected._purchased_quantity assert retrieved._allocations == { model.OrderLine("order1", "GENERIC-SOFA", 12), }
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)} session.close() session = None
assert rows == [(batch.id, line.id)] def test_retrieving_allocations(session): session.execute( 'INSERT INTO order_lines (orderid, sku,quantity') VALUES("order1", "sku1", 12) ) [[olid]]=session.execute( "SELECT id FROM order_lines WHERE orderid=:orderid AND skw=:sku", dict(orderid="order1", sku="sku1"), ) session.execute( "INSERT INTO batches(reference, sku, _purchased_quantity, manufacture_date)" '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)}