def create(): product = _populate_from_request(Product()) dal = ProductDal() dal.create(product) return make_response(jsonify(product), 201)
def test_create_description_required(): with raises(ProductFieldRequiredException): try: dal = ProductDal() dal.create(Product()) except ProductFieldRequiredException as e: assert e.field_name == 'description' assert str(e) == 'The description field is required.' raise
def update(product_id): dal = ProductDal() product = _populate_from_request(Product()) product.id = product_id dal.update(product) return jsonify(product)
def test_find_by_id(): with raises(ProductNotFoundException): product_id = 10000000 try: dal = ProductDal() dal.find_by_id(product_id) except ProductNotFoundException as e: assert e.product_id == product_id assert str(e) == 'No product with id {} was found.'.format(product_id) raise
def test_update(): dal = ProductDal() product_id = 1 expected = Product(product_id=product_id, description='Hubble Satellite') dal.update(expected) actual = dal.find_by_id(product_id) assert expected.description == actual.description
def test_update_description_required(): with raises(ProductFieldRequiredException): try: dal = ProductDal() product = dal.find_by_id(1) product.description = '' dal.update(product) except ProductFieldRequiredException as e: assert e.field_name == 'description' assert str(e) == 'The description field is required.' raise
def test_create(): dal = ProductDal() expected = Product(description='Hubble Satellite') assert expected.id is None dal.create(expected) assert expected.id is not None actual = dal.find_by_id(expected.id) assert expected.id == actual.id assert expected.description == actual.description
def read(): dal = ProductDal() page_number = request.args.get('page_number') page_size = request.args.get('page_size') if is_int(page_number): page_number = int(page_number) if is_int(page_size): page_size = int(page_size) products = dal.find(page_number=page_number, page_size=page_size) return jsonify(products)
def validate(self, entity): required_field_message_template = 'The {} field is required.' required_field_name = None if not entity.product_id: required_field_name = 'product_id' elif not entity.latitude: required_field_name = 'latitude' elif not entity.longitude: required_field_name = 'longitude' elif not entity.elevation: required_field_name = 'elevation' elif not entity.visit_date: required_field_name = 'visit_date' if required_field_name: raise LocationFieldRequiredException( message=required_field_message_template.format( required_field_name), field_name=required_field_name) # The below condition will raise a LocationNotFoundException if the location id is not None and the corresponding location does not exist. if entity.id: self.find_by_id(entity.id) # If the product is not found, the below line will raise a ProductNotFoundException ProductDal().find_by_id(entity.product_id)
def delete(product_id): dal = ProductDal() dal.delete(dal.find_by_id(product_id)) return jsonify({'result': True})
def read_by_id(product_id): dal = ProductDal() return jsonify(dal.find_by_id(product_id))