def add_batch(): session = get_session() repo = repository.SqlAlchemyRepository(session) eta = request.json['eta'] if eta is not None: eta = datetime.fromisoformat(eta).date() services.add_batch( request.json['ref'], request.json['sku'], request.json['qty'], eta, repo, session ) return 'OK', 201
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_respository_can_save_batch(session): sku = 'RUSTY-SOAPDISH3' batch = Batch('batch1', sku, 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', sku, 100, None)]
def allocate_endpoint(): session = get_session() repo = repository.SqlAlchemyRepository(session) try: batchref = services.allocate( request.json['orderid'], request.json['sku'], request.json['qty'], 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), }
def test_repository_can_retrieve_a_batch_wit_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 = 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 == { OrderLine('order1', 'GENERIC-SOFA', 12), }
def __enter__(self): self.session = self.session_factory() # type: Session self.batches = repository.SqlAlchemyRepository(self.session) return super().__enter__()
def __enter__(self): self.db = self.db_factory() # type: db #(2) self.batches = repository.SqlAlchemyRepository(self.db) # (2) return super().__enter__()
def __enter__(self): self.session = self.session_factory() self.products = repository.SqlAlchemyRepository(self.session) return super().__enter__()