Ejemplo n.º 1
0
def test_invalid_query(connection_exception_mock):
    with patch("data_django.exec_sql.connect", return_value=connection_exception_mock) as conn_mock, patch(
        "data_django.exec_sql.config", return_value={}
    ) as config_mock:
        assert (conn_mock.called or config_mock.called) is False
        exec_dql_query("SELECT abc FROM xyz", True)  # errors caught
        exec_dml_query("SELECT abc FROM xyz", True)
Ejemplo n.º 2
0
def test_valid_dql_query(connection_mock):
    with patch("data_django.exec_sql.connect", return_value=connection_mock) as conn_mock, patch(
        "data_django.exec_sql.config", return_value={}
    ) as config_mock:
        assert (conn_mock.called or config_mock.called) is False
        result = exec_dql_query("SELECT abc FROM xyz", True)
        assert conn_mock.called and config_mock.called
        assert result == [["Berlin"], ["Tokyo"]]
Ejemplo n.º 3
0
def handle_get_supported_cities() -> Tuple[str, int]:
    """Returns a list containing the currently supported cities.

    Returns
    -------
    content: str
        Response content.
    http_status: int
        HTTP status code.
    """
    cities_query = 'select distinct(city_name) from data_mart_layer.current_trained_models order by city_name asc'
    cities = exec_dql_query(cities_query, return_result=True)
    cities = [] if not cities else list(map(lambda _city: _city[0], cities))

    return dumps({'cities': cities}), 200
def is_city_existing(city: str) -> bool:
    """Returns whether the passed city exists.

    Parameters
    ----------
    city: str
        Name of the city.

    Returns
    -------
    is_existing: bool
        Whether the passed city exists.
    """
    cities_query = "select distinct(city_name) from data_mart_layer.current_trained_models"
    cities = set(
        map(lambda _city: _city[0].upper(),
            exec_dql_query(cities_query, return_result=True)))
    return city.upper() in cities
def get_latest_model_version(city: str) -> int:
    """Returns the version number of the latest model belonging to the passed city.

    Parameters
    ----------
    city: str
        Name of the city.

    Returns
    -------
    latest_version: int
        Latest model version.
    """
    trained_model_query = (
        f"SELECT version FROM data_mart_layer.current_trained_models "
        f"WHERE city_name = '{city.upper()}'")
    found_version = exec_dql_query(trained_model_query, return_result=True)
    if found_version:
        return int(found_version[0][0])

    return -1
def get_downloaded_model(city: str) -> Optional[bytes]:
    """Returns the downloaded and trained model for the specified city if it is available in the data warehouse.

    Parameters
    ----------
    city: str
        Name of the city.

    Returns
    -------
    found_model: bytes or None
        Retrieved .pt model file.
    """
    trained_model_query = (
        f"SELECT trained_model FROM data_mart_layer.current_trained_models "
        f"WHERE city_name = '{city.upper()}'")
    found_model = exec_dql_query(trained_model_query, return_result=True)
    if found_model:
        return found_model[0][0].tobytes()

    return None