def test_retrieving_batches(session): 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 test_get_by_batchref(sqlite_session): repo = repository.SqlAlchemyRepository(sqlite_session) b1 = model.Batch('b1', 'sku1', 100) b2 = model.Batch('b2', 'sku1', 100) b3 = model.Batch('b3', 'sku2', 100) p1 = model.Product('sku1', [b1, b2]) p2 = model.Product('sku2', [b3]) repo.add(p1) repo.add(p2) assert repo.get_by_batcheref('b1') == p1 assert repo.get_by_batcheref('b3') == p2
def test_retrieving_batches(session): 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, '2019-03-28')" ) expected = [ model.Batch("batch1", "sku1", 100, None), model.Batch("batch2", "sku2", 200, date(2019, 3, 28)), ] 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: uow.batches.add(model.Batch(ref, sku, qty, eta)) uow.commit()
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 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_saving_batches(session): 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 == [(batch.reference, batch.sku, batch._purchased_quantity, batch.eta)]
def test_saving_batches(session): 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): 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 add_batch(ref: str, sku: str, qty: int, eta: Optional[date], uow: unit_of_work.AbstractUnitOfWork): with uow: product = uow.products.get(sku=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_repository_can_save_a_batch(session): 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 test_records_out_of_stock_event_if_cannot_allocat(): batch = model.Batch('b1', '椅子', 10, eta=today) product = model.Product('椅子', batches=[batch]) product.allocate(model.OrderLine('o1', '椅子', 10)) allocation = product.allocate(model.OrderLine( 'o2', '椅子', 1, )) assert product.events[-1] == events.OutOfStock(sku='椅子') assert allocation is None
def add_batch( message: commands.CreateBatch, uow: unit_of_work.AbstractUnitOfWork ): sku = message.sku batch = model.Batch( message.reference, message.sku, message.qty, message.eta ) with uow: product = uow.products.get(sku=sku) if product is None: product = model.Product(sku, []) uow.products.add(product) product.batches.append(batch) uow.commit()
def test_updating_a_batch(session): order1 = model.OrderLine(ORDER_1, BENCH, 10) order2 = model.OrderLine('order2', BENCH, 20) batch = model.Batch(BATCH_1, BENCH, 100, eta=None) batch.allocate(order1) repo = repository.SQLAlchemyRepository(session) repo.add(batch) session.commit() batch.allocate(order2) repo.add(batch) session.commit() assert get_allocations(session, BATCH_1) == {ORDER_1, 'order2'}
def test_repository_can_retrieve_a_batch_with_allocations(session): orderline_id = insert_order_line(session) batch1_id = insert_batch(session, BATCH_1) insert_batch(session, BATCH_2) insert_allocation(session, orderline_id, batch1_id) repo = repository.SQLAlchemyRepository(session) retrieved = repo.get(BATCH_1) # tests the read side expected = model.Batch(BATCH_1, SOFA, HUNDRED, eta=None) assert retrieved == expected # tests object equality assert retrieved.sku == expected.sku assert retrieved._qty == expected._qty assert retrieved._allocations == { model.OrderLine(ORDER_1, SOFA, TWELVE), }
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), }
def test_repository_can_retrieve_a_batch_with_allocations(session): orderline_id = insert_order_line(session) batch1_id = insert_product_batch(session, "batch1") insert_batch(session, "batch2") insert_allocation(session, orderline_id, batch1_id) repo = repository.SqlAlchemyRepository(session) retrieved = repo.get("GENERIC-SOFA") batch = model.Batch("batch1", "GENERIC-SOFA", 100, eta=None) expected = model.Product("GENERIC-SOFA", [batch]) assert retrieved == expected assert retrieved.batches[ 0]._purchased_quantity == batch._purchased_quantity assert retrieved.batches[0]._allocations == { model.OrderLine("order1", "GENERIC-SOFA", 12), }
def test_repository_can_save_a_batch(session): batch = model.Batch(BATCH_1, SOAP, HUNDRED, eta=None) repo = repository.SQLAlchemyRepository(session) repo.add(batch) # method under test # https://docs.sqlalchemy.org/en/13/orm/session_basics.html#committing session.commit() # separate from repository (intentional), it # flushes pending changes and commits the current transaction. # verify the data is saved rows = list( # https://docs.sqlalchemy.org/en/13/orm/session_api.html#sqlalchemy.orm.session.Session.execute session.execute( # Execute a SQL expression construct or string statement within the current transaction 'SELECT ref, sku, _qty, eta ' \ 'FROM "batches"' ) ) assert rows == [(BATCH_1, SOAP, HUNDRED, None)]
def for_batch(ref, sku, qty, eta=None): """Factory for making a Repository with a Batch.""" repo = FakeRepository([model.Batch(ref, sku, qty, eta)]) return repo
import pytest from allocation.adapters import repository from allocation.domain import model <<<<<<< HEAD pytestmark = pytest.mark.usefixtures('mappers') ======= pytestmark = pytest.mark.usefixtures("mappers") >>>>>>> upstream/master def test_get_by_batchref(sqlite_session_factory): session = sqlite_session_factory() repo = repository.SqlAlchemyRepository(session) <<<<<<< HEAD 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 ======= 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)
def for_batch(reference, sku, qty, eta=None): batch = model.Batch(reference, sku, qty, eta) return FakeRepository([model.Product(sku, [batch])])