Skip to content

Commit

Permalink
Merge branch 'main' into kb-begins
Browse files Browse the repository at this point in the history
  • Loading branch information
altvod authored Nov 17, 2023
2 parents 75fc289 + cb59450 commit f2e4c27
Show file tree
Hide file tree
Showing 12 changed files with 474 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -995,3 +995,22 @@ def test_bi_4652_measure_filter_with_total_in_select(self, control_api, data_api
data_rows = get_data_rows(result_resp)
dim_values = [row[0] for row in data_rows]
assert len(dim_values) == len(set(dim_values)), "Dimension values are not unique"

@pytest.mark.xfail(reason="https://github.com/datalens-tech/datalens-backend/issues/98") # FIXME
def test_fixed_with_unknown_field(self, control_api, data_api, saved_dataset):
ds = add_formulas_to_dataset(
api_v1=control_api,
dataset=saved_dataset,
formulas={
"sales sum fx unknown": "SUM([sales] FIXED [unknown])",
},
)

result_resp = data_api.get_result(
dataset=ds,
fields=[
ds.find_field(title="sales sum fx unknown"),
],
fail_ok=True,
)
assert result_resp.status_code == HTTPStatus.BAD_REQUEST, result_resp.json
100 changes: 100 additions & 0 deletions lib/dl_api_lib/dl_api_lib_tests/db/data_api/result/test_parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from __future__ import annotations

from http import HTTPStatus

import pytest

from dl_api_client.dsmaker.primitives import (
IntegerParameterValue,
RangeParameterValueConstraint,
)
from dl_api_client.dsmaker.shortcuts.dataset import (
add_formulas_to_dataset,
add_parameters_to_dataset,
)
from dl_api_client.dsmaker.shortcuts.result_data import get_data_rows
from dl_api_lib_tests.db.base import DefaultApiTestBase
from dl_constants.enums import UserDataType


class TestParameters(DefaultApiTestBase):
@pytest.mark.parametrize(
("multiplier", "expected_status_code"),
(
(None, HTTPStatus.OK),
(2, HTTPStatus.OK),
(5, HTTPStatus.OK),
(-1, HTTPStatus.BAD_REQUEST),
),
)
def test_parameter_in_formula(self, control_api, data_api, saved_dataset, multiplier, expected_status_code):
default_multiplier = 1
ds = add_parameters_to_dataset(
api_v1=control_api,
dataset_id=saved_dataset.id,
parameters={
"Multiplier": (
IntegerParameterValue(default_multiplier),
RangeParameterValueConstraint(min=IntegerParameterValue(default_multiplier)),
),
},
)

integer_field = next(field for field in saved_dataset.result_schema if field.data_type == UserDataType.integer)
ds = add_formulas_to_dataset(
api_v1=control_api,
dataset=ds,
formulas={
"Multiplied Field": f"[{integer_field.title}] * [Multiplier]",
},
)

result_resp = data_api.get_result(
dataset=ds,
fields=[
integer_field,
ds.find_field(title="Multiplier"),
ds.find_field(title="Multiplied Field"),
],
parameters=[
ds.find_field(title="Multiplier").parameter_value(multiplier),
],
fail_ok=True,
)
assert result_resp.status_code == expected_status_code, result_resp.json

if expected_status_code == HTTPStatus.OK:
data_rows = get_data_rows(result_resp)
assert data_rows
for row in data_rows:
assert int(row[1]) == (multiplier or default_multiplier)
assert int(row[0]) * int(row[1]) == int(row[2])

def test_parameter_no_constraint(self, control_api, data_api, dataset_id):
ds = add_parameters_to_dataset(
api_v1=control_api,
dataset_id=dataset_id,
parameters={
"Param": (IntegerParameterValue(0), None),
},
)
ds = add_formulas_to_dataset(
api_v1=control_api,
dataset=ds,
formulas={
"Value": "[Param]",
},
)

result_resp = data_api.get_result(
dataset=ds,
fields=[
ds.find_field(title="Value"),
],
parameters=[
ds.find_field(title="Param").parameter_value(1),
],
limit=1,
)
assert result_resp.status_code == HTTPStatus.OK, result_resp.json
assert int(get_data_rows(result_resp)[0][0]) == 1
33 changes: 33 additions & 0 deletions lib/dl_api_lib/dl_api_lib_tests/db/data_api/test_ping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from __future__ import annotations

import pytest
import shortuuid

from dl_api_lib_tests.db.base import DefaultApiTestBase
from dl_configs.enums import RequiredService


class TestPing(DefaultApiTestBase):
@pytest.mark.asyncio
async def test_ping(self, data_api_lowlevel_aiohttp_client):
client = data_api_lowlevel_aiohttp_client
req_id = shortuuid.uuid()

resp = await client.get("/ping", headers={"x-request-id": req_id})
assert resp.status == 200
js = await resp.json()
assert js["request_id"].startswith(req_id + "--")

@pytest.mark.asyncio
async def test_ping_ready(self, data_api_lowlevel_aiohttp_client):
client = data_api_lowlevel_aiohttp_client
req_id = shortuuid.uuid()

resp = await client.get("/ping_ready", headers={"x-request-id": req_id})
assert resp.status == 200
js = await resp.json()
assert js["request_id"].startswith(req_id + "--")
details = js["details"]
assert details[RequiredService.POSTGRES.name] is True
assert details[RequiredService.RQE_INT_SYNC.name] == 200
assert details[RequiredService.RQE_EXT_SYNC.name] == 200
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,17 @@ async def _execute_by_steps(self, db_adapter_query: DBAdapterQuery) -> AsyncIter

chunk_size = db_adapter_query.get_effective_chunk_size(self._default_chunk_size)
query = db_adapter_query.query
debug_compiled_query = db_adapter_query.debug_compiled_query
escape_percent = not db_adapter_query.is_dashsql_query # DON'T escape only for dashsql
compiled_query, compiled_query_parameters = compile_mysql_query(
query, dialect=self._dialect, escape_percent=escape_percent
)
debug_query = query if isinstance(query, str) else compile_query_for_debug(query, self._dialect)
debug_query = None
if self._target_dto.pass_db_query_to_user:
if debug_compiled_query is not None:
debug_query = debug_compiled_query
else:
debug_query = query if isinstance(query, str) else compile_query_for_debug(query, self._dialect)

with self.handle_execution_error(debug_query):
async with self._get_connection(db_adapter_query.db_name) as conn:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ def _(exc: Exception) -> bool:
return _


def is_source_connect_async_error() -> ExcMatchCondition:
def _(exc: Exception) -> bool:
if isinstance(exc, pymysql.OperationalError):
if len(exc.args) >= 2 and exc.args[0] == 2003:
return True
return False

return _


class AsyncMysqlChainedDbErrorTransformer(error_transformer.ChainedDbErrorTransformer):
@staticmethod
def _get_error_kw(
Expand All @@ -53,6 +63,10 @@ def _get_error_kw(

async_mysql_db_error_transformer: DbErrorTransformer = AsyncMysqlChainedDbErrorTransformer(
(
Rule(
when=is_source_connect_async_error(),
then_raise=exc.SourceConnectError,
),
Rule(
when=is_table_does_not_exist_async_error(),
then_raise=MysqlSourceDoesNotExistError,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from dl_core_testing.testcases.adapter import BaseAsyncAdapterTestClass
from dl_testing.regulated_test import RegulatedTestParams

from dl_connector_mysql.core.async_adapters_mysql import AsyncMySQLAdapter
from dl_connector_mysql.core.target_dto import MySQLConnTargetDTO
from dl_connector_mysql_tests.db.core.base import BaseMySQLTestClass


class TestAsyncMySQLAdapter(
BaseMySQLTestClass,
BaseAsyncAdapterTestClass[MySQLConnTargetDTO],
):
test_params = RegulatedTestParams(
mark_tests_skipped={
BaseAsyncAdapterTestClass.test_default_pass_db_query_to_user: "Not relevant",
},
)

ASYNC_ADAPTER_CLS = AsyncMySQLAdapter
8 changes: 4 additions & 4 deletions terrarium/dl_gitmanager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ pip install -Ue <path-to-package>

Show the main help message

```
```bash
dl-git --help
dl-git --h
```

The `--help` (`-h`) option can also be used for any command:
```
```bash
dl-git <command> --help
```

Expand All @@ -43,7 +43,7 @@ If the path is inside a submodule, then the submodule is considered to be the ro

List files that have changed between two given revisions including the changes in all submodules

```
```bash
dl-git range-diff-paths --base <base> --head <head>
dl-git range-diff-paths --base <base> --head <head> --absolute
dl-git range-diff-paths --base <base> --head <head> --only-added-commits
Expand All @@ -53,7 +53,7 @@ Here `base` and `head` can be a commit ID, branch name, `HEAD~3` or any similar
that is usually accepted by git.
These arguments are optional. By default `head` is `HEAD` and `base` is `HEAD~1`.
Thgis means you can use the following command to see the diff of the last commit in the current branch:
```
```bash
dl-git range-diff-paths
```

Expand Down
Loading

0 comments on commit f2e4c27

Please sign in to comment.