def post(self) -> Response: """Creates a new Database --- post: description: >- Create a new Database. requestBody: description: Database schema required: true content: application/json: schema: $ref: '#/components/schemas/{{self.__class__.__name__}}.post' responses: 201: description: Database added content: application/json: schema: type: object properties: id: type: number result: $ref: '#/components/schemas/{{self.__class__.__name__}}.post' 302: description: Redirects to the current digest 400: $ref: '#/components/responses/400' 401: $ref: '#/components/responses/401' 404: $ref: '#/components/responses/404' 500: $ref: '#/components/responses/500' """ if not request.is_json: return self.response_400(message="Request is not JSON") try: item = self.add_model_schema.load(request.json) # This validates custom Schema with custom validations except ValidationError as error: return self.response_400(message=error.messages) try: new_model = CreateDatabaseCommand(g.user, item).run() # Return censored version for sqlalchemy URI item["sqlalchemy_uri"] = new_model.sqlalchemy_uri return self.response(201, id=new_model.id, result=item) except DatabaseInvalidError as ex: return self.response_422(message=ex.normalized_messages()) except DatabaseConnectionFailedError as ex: return self.response_422(message=str(ex)) except DatabaseCreateFailedError as ex: logger.error( "Error creating model %s: %s", self.__class__.__name__, str(ex), exc_info=True, ) return self.response_422(message=str(ex))
def test_multiple_error_logging(self, mock_g, mock_logger): mock_g.user = security_manager.find_user("admin") command = CreateDatabaseCommand({}) with pytest.raises(DatabaseInvalidError) as excinfo: command.run() assert str(excinfo.value) == ("Database parameters are invalid.") # logger should list a unique set of errors with no duplicates mock_logger.assert_called_with(action="db_connection_failed." "DatabaseInvalidError." "DatabaseRequiredFieldValidationError")
def test_create_duplicate_error(self, mock_g, mock_logger): example_db = get_example_database() mock_g.user = security_manager.find_user("admin") command = CreateDatabaseCommand( {"database_name": example_db.database_name}, ) with pytest.raises(DatabaseInvalidError) as excinfo: command.run() assert str(excinfo.value) == ("Database parameters are invalid.") # logger should list classnames of all errors mock_logger.assert_called_with(action="db_connection_failed." "DatabaseInvalidError." "DatabaseExistsValidationError." "DatabaseRequiredFieldValidationError")