def test_no_trainingsdata_time_points_for_timeframe(self):
        wrong_config = copy.deepcopy(config)

        when2(rm.read_query, ANY, ANY).thenReturn(self.df_wrong)

        with self.assertRaises(ex.ConfigException):
            training_engine.train_classifier(wrong_config)
    def test_train_test_split_SKlearnException(self):
        wrong_config = copy.deepcopy(config)

        when2(rm.read_query, ANY, ANY).thenReturn(self.df_normal)
        when2(mp.load_classifier, ANY, ANY, True).thenReturn(self.model)

        with self.assertRaises(ex.SklearnException):
            training_engine.train_classifier(wrong_config)
    def test_start_time_is_empty_throws_InvalidConfigValue(self):
        wrong_config = copy.deepcopy(config)
        wrong_config['timeframe'] = [
            "2020-02-28 00:00:00.000 UTC", "2020-02-31 00:00:00.000 UTC"
        ]

        with self.assertRaises(ex.InvalidConfigValueException):
            training_engine.train_classifier(wrong_config)
    def test_raise_persistor_exception(self):
        wrong_config = copy.deepcopy(config)

        when2(rm.read_query, ANY, ANY).thenReturn(self.df_normal)
        when2(mp.load_classifier, ANY, ANY,
              True).thenRaise(ex.PersistorException)

        with self.assertRaises(ex.PersistorException):
            training_engine.train_classifier(wrong_config)
def train():
    """
    Name in documentation: 'train'

    This method is used to train models.

    :return: a Flask response contains one of the following status codes
    :return: 400 - Bad Request - e.g. if the request body is not a JSON
    :return: 500 - Internal Server Error - in any error cases not described here
    :return: 900 - Config Error - if the message body contains an invalid config
    :return: 901 - DBException - if there are problems with the database connection
    :return: 902 - PersistorException - if there are problems with persisting the new models
    :return: 903 - FileException - if there are problems im model persistor with the files
    """

    logger.info(
        "Received request on 'train' endpoint. Starting training procedure...")
    response = 200
    if request.is_json:
        try:
            config = request.get_json()
            trainingsdata.enrich_data(config)
            logger.info("Enrich data was successful.")
            trainingsdata.mark_data(config)
            logger.info("Mark data was successful.")
            training.train_classifier(config)
            logger.info("Training was successful. Returning " +
                        str(http_status_codes.get("HTTPOK")))
        except ex.ConfigException as e:
            exception_name = e.__class__.__name__
            stack_trace = ''.join(
                traceback.format_exception(etype=type(e),
                                           value=e,
                                           tb=e.__traceback__))
            logger.error(exception_name + " was caught. StackTrace: " +
                         stack_trace)
            response = http_status_codes.get("ConfigException")
            logger.error("Returning " + str(response))
        except ex.DBException as e:
            exception_name = e.__class__.__name__
            stack_trace = ''.join(
                traceback.format_exception(etype=type(e),
                                           value=e,
                                           tb=e.__traceback__))
            logger.error(exception_name + " was caught. StackTrace: " +
                         stack_trace)
            response = http_status_codes.get("DBException")
            logger.error("Returning " + str(response))
        except ex.PersistorException as e:
            exception_name = e.__class__.__name__
            stack_trace = ''.join(
                traceback.format_exception(etype=type(e),
                                           value=e,
                                           tb=e.__traceback__))
            logger.error(exception_name + " was caught. StackTrace: " +
                         stack_trace)
            response = http_status_codes.get("PersistorException")
            logger.error("Returning " + str(response))
        except ex.FileException as e:
            exception_name = e.__class__.__name__
            stack_trace = ''.join(
                traceback.format_exception(etype=type(e),
                                           value=e,
                                           tb=e.__traceback__))
            logger.error(exception_name + " was caught. StackTrace: " +
                         stack_trace)
            response = http_status_codes.get("FileException")
            logger.error("Returning " + str(response))
        except Exception as e:
            exception_name = e.__class__.__name__
            stack_trace = ''.join(
                traceback.format_exception(etype=type(e),
                                           value=e,
                                           tb=e.__traceback__))
            logger.error(exception_name +
                         " was caught (unknown). StackTrace: " + stack_trace)
            logger.error("Returning " +
                         str(http_status_codes.get("HTTPInternalServerError")))
            response = http_status_codes.get("HTTPInternalServerError")
    else:
        logger.info("Request is not JSON. Returning " +
                    str(http_status_codes.get("HTTPBadRequest")))
        response = http_status_codes.get("HTTPBadRequest")

    status_code = Response(status=response)
    return status_code