def testGetDerivativeRetrievesDerivative(dummy_derivative): # Add dummy derivative to database session db.session.add(dummy_derivative) db.session.flush() # Assert that getDerivative returns the derivative assert derivative_management.getDerivative(dummy_derivative.id) == dummy_derivative
def deleteDerivative(derivative_id): # Verify request if not request.data or not request.is_json: return abort(400, 'empty request body') # Retreive json body from request body = request.get_json() user_id = body.get('user_id') # Verify user exists if user_management.getUser(user_id) is None: return abort(404, f'user id {user_id} does not exist') # Retreive derivative from database derivative = derivative_management.getDerivative(derivative_id) # Verify derivative exists if derivative is None: return abort(404, f'derivative id {derivative_id} does not exist') # Delete the derivative try: derivative_management.deleteDerivative(derivative, user_id) except AbsoluteDerivativeException: return abort(400, 'derivative is absolute, deletion denied') # Commit the deletion db.session.commit() # Return a 204, no content return '', 204
def getDerivative(derivative_id): # Get derivative from database derivative = derivative_management.getDerivative(derivative_id) # Verify derivative exists if derivative is None: return abort(404, f'derivative with id {derivative_id} not found') # Make response return jsonify(derivative=derivative)
def updateDerivative(derivative_id): # Verify request if not request.data or not request.is_json: return abort(400, 'empty request body') # Retreive json body from request body = request.get_json() user_id = body.get('user_id') tree_id = body.get('tree_id') # Verify user exists if user_management.getUser(user_id) is None: return abort(404, f'user id {user_id} does not exist') # Obtain updates updates = body.get('updates') # Retreive the specified derivative derivative = derivative_management.getDerivative(derivative_id) # Verify derivative exists if derivative is None: return abort(404, f'derivative id {derivative_id} does not exist') # Update the derivative try: update_log = derivative_management.updateDerivative(derivative, user_id, tree_id, updates) except AbsoluteDerivativeException: return abort(400, 'derivative is absolute, update denied') # If no updates were made to the derivative, abort if not update_log: return abort(400, 'no valid updates') # Commit the derivative updates to the database db.session.commit() # Make response return jsonify(update_log=update_log)
def testGetDerivativeReturnsNoneIfNotFound(free_derivtive_id): # Assert that None is returned for the free id assert derivative_management.getDerivative(free_derivtive_id) is None