def create(): location = _populate_from_request(Location()) dal = LocationDal() dal.create(location) return make_response(jsonify(location), 201)
def update(location_id): dal = LocationDal() location = _populate_from_request(Location()) location.id = location_id dal.update(location) return jsonify(location)
def test_create_visit_date_required(): location = Location(product_id=1, latitude=-85, longitude=94, elevation=600) with raises(LocationFieldRequiredException): try: dal = LocationDal() dal.create(location) except LocationFieldRequiredException as e: assert e.field_name == 'visit_date' assert str(e) == 'The visit_date field is required.' raise
def test_create_elevation_required(): location = Location(product_id=1, latitude=-85, longitude=94, visit_date=datetime.datetime.utcnow()) with raises(LocationFieldRequiredException): try: dal = LocationDal() dal.create(location) except LocationFieldRequiredException as e: assert e.field_name == 'elevation' assert str(e) == 'The elevation field is required.' raise
def test_find_by_id(): with raises(LocationNotFoundException): location_id = 10000000 try: dal = LocationDal() dal.find_by_id(location_id) except LocationNotFoundException as e: assert e.location_id == location_id assert str(e) == 'No location with id {} was found.'.format(location_id) raise
def test_update_longitude_required(): location = Location(location_id=1, product_id=1, latitude=-85, elevation=600, visit_date=datetime.datetime.utcnow()) with raises(LocationFieldRequiredException): try: dal = LocationDal() dal.update(location) except LocationFieldRequiredException as e: assert e.field_name == 'longitude' assert str(e) == 'The longitude field is required.' raise
def read(): dal = LocationDal() 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) locations = dal.find(page_number=page_number, page_size=page_size) return jsonify(locations)
def test_create(): dal = LocationDal() expected = Location(product_id=1, latitude=-85, longitude=94, elevation=600, visit_date=datetime.datetime.utcnow()) 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.product_id == actual.product_id assert expected.latitude == actual.latitude assert expected.longitude == actual.longitude assert expected.elevation == actual.elevation
def test_update(): dal = LocationDal() location_id = 1 expected = Location(location_id=location_id, product_id=1, latitude=-85, longitude=94, elevation=600, visit_date=datetime.datetime.utcnow()) dal.update(expected) actual = dal.find_by_id(location_id) assert expected.id == actual.id assert expected.product_id == actual.product_id assert expected.latitude == actual.latitude assert expected.longitude == actual.longitude assert expected.elevation == actual.elevation
def read_by_id(location_id): dal = LocationDal() return jsonify(dal.find_by_id(location_id))
def delete(location_id): dal = LocationDal() dal.delete(dal.find_by_id(location_id)) return jsonify({'result': True})