Beispiel #1
0
def create():
    product = _populate_from_request(Product())

    dal = ProductDal()
    dal.create(product)

    return make_response(jsonify(product), 201)
Beispiel #2
0
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
Beispiel #3
0
def update(product_id):
    dal = ProductDal()

    product = _populate_from_request(Product())
    product.id = product_id

    dal.update(product)

    return jsonify(product)
Beispiel #4
0
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
Beispiel #5
0
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
Beispiel #6
0
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
Beispiel #7
0
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
Beispiel #8
0
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)
Beispiel #9
0
    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)
Beispiel #10
0
def delete(product_id):
    dal = ProductDal()

    dal.delete(dal.find_by_id(product_id))

    return jsonify({'result': True})
Beispiel #11
0
def read_by_id(product_id):
    dal = ProductDal()
    return jsonify(dal.find_by_id(product_id))