def test_get_by_batchref(sqlite_session_factory): session = sqlite_session_factory() repo = repository.SqlAlchemyRepository(session) b1 = model.Batch(ref='b1', sku='sku1', qty=100, eta=None) b2 = model.Batch(ref='b2', sku='sku1', qty=100, eta=None) b3 = model.Batch(ref='b3', sku='sku2', qty=100, eta=None) p1 = model.Product(sku='sku1', batches=[b1, b2]) p2 = model.Product(sku='sku2', batches=[b3]) repo.add(p1) repo.add(p2) assert repo.get_by_batchref('b2') == p1 assert repo.get_by_batchref('b3') == p2
def test_retrieving_batches(session_factory): session = session_factory session.execute( 'INSERT INTO batches (reference, sku, _purchased_quantity, eta)' ' VALUES ("batch1", "sku1", 100, null)') session.execute( 'INSERT INTO batches (reference, sku, _purchased_quantity, eta)' ' VALUES ("batch2", "sku2", 200, "2011-04-11")') expected = [ model.Batch("batch1", "sku1", 100, eta=None), model.Batch("batch2", "sku2", 200, eta=date(2011, 4, 11)), ] assert session.query(model.Batch).all() == expected
def add_batch(ref: str, sku: str, qty: int, eta: Optional[date], uow: unit_of_work.AbstractUnitOfWork): with uow: product = uow.products.get(sku) if product is None: product = model.Product(sku, batches=[]) uow.products.add(product) product.batches.append(model.Batch(ref, sku, qty, eta)) uow.commit()
def test_saving_batches(session_factory): session = session_factory batch = model.Batch('batch1', 'sku1', 100, eta=None) session.add(batch) session.commit() rows = list( session.execute( 'SELECT reference, sku, _purchased_quantity, eta FROM "batches"')) assert rows == [('batch1', 'sku1', 100, None)]
def test_saving_allocations(session_factory): session = session_factory 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_repository_can_save_a_batch(session_factory): session = session_factory batch = model.Batch("batch1", "RUSTY-SOAPDISH", 100, eta=None) repo = repository.SqlAlchemyRepository(session) repo.add(batch) session.commit() rows = list( session.execute( 'SELECT reference, sku, _purchased_quantity, eta FROM "batches"')) assert rows == [("batch1", "RUSTY-SOAPDISH", 100, None)]
def add_batch( cmd: commands.CreateBatch, uow: unit_of_work.AbstractUnitOfWork ): with uow: product = uow.products.get(sku=cmd.sku) if product is None: product = model.Product(cmd.sku, batches=[]) uow.products.add(product) product.batches.append(model.Batch( cmd.ref, cmd.sku, cmd.qty, cmd.eta )) uow.commit()
def test_repository_can_retrieve_a_batch_with_allocations(session_factory): session = session_factory orderline_id = insert_order_line(session) batch1_id = insert_batch(session, "batch1") insert_batch(session, "batch2") insert_allocation(session, orderline_id, batch1_id) retrieved = repository.SqlAlchemyRepository(session).get("batch1") #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), }
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 add_batch(ref: str, sku: str, qty: int, eta: Optional[date], uow: unit_of_work.AbstractUnitOfWork): with uow: uow.batches.add(model.Batch(ref, sku, qty, eta)) uow.commit()