Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ on:
permissions:
contents: write

env:
PYTHONUTF8: 1

jobs:
build:
name: Build distributions (Py${{ matrix.python-version }} • ${{ matrix.os }})
unit-tests:
name: Unit testing (Py${{ matrix.python-version }} • ${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
Expand All @@ -30,5 +33,8 @@ jobs:
- name: Check files format
uses: astral-sh/ruff-action@v3

- name: Check cognitive complexity
run: uv run --frozen --all-extras complexipy mockylla -d low

- name: Run Tests
run: uv run --frozen --all-extras pytest tests -vv
28 changes: 16 additions & 12 deletions mockylla/parser/insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ def _parse_values(values_str):
return values


def assign_row_data_value(val, cql_type, defined_types):
if cql_type in defined_types:
if isinstance(val, str):
return _parse_udt_literal(val)
else:
return cast_value(val, cql_type)
elif cql_type:
if isinstance(val, str):
return cast_value(val.strip("'\""), cql_type)
else:
return cast_value(val, cql_type)
else:
return val


def handle_insert_into(insert_match, session, state, parameters=None):
(
table_name_full,
Expand Down Expand Up @@ -86,18 +101,7 @@ def handle_insert_into(insert_match, session, state, parameters=None):
row_data = {}
for col, val in zip(columns, values):
cql_type = table_schema.get(col)
if cql_type in defined_types:
if isinstance(val, str):
row_data[col] = _parse_udt_literal(val)
else:
row_data[col] = cast_value(val, cql_type)
elif cql_type:
if isinstance(val, str):
row_data[col] = cast_value(val.strip("'\""), cql_type)
else:
row_data[col] = cast_value(val, cql_type)
else:
row_data[col] = val
row_data[col] = assign_row_data_value(val, cql_type, defined_types)

if if_not_exists:
pk_to_insert = {
Expand Down
46 changes: 22 additions & 24 deletions mockylla/parser/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@
from mockylla.row import Row


def replace_placeholders(segment, params, start_idx):
if not segment or "%s" not in segment:
return segment, start_idx

parts = segment.split("%s")
if len(parts) - 1 + start_idx > len(params):
raise ValueError(
"Number of parameters does not match number of placeholders in UPDATE query"
)

new_segment = parts[0]
idx = start_idx
for i in range(len(parts) - 1):
param = params[idx]
param_str = f"'{param}'" if isinstance(param, str) else str(param)
new_segment += param_str + parts[i + 1]
idx += 1
return new_segment, idx


def handle_update(update_match, session, state, parameters=None):
"""Handle UPDATE query by parsing and executing the update operation."""
(
Expand All @@ -17,32 +37,10 @@ def handle_update(update_match, session, state, parameters=None):
) = update_match.groups()

if parameters and "%s" in (set_clause_str + where_clause_str):

def _replace_placeholders(segment, params, start_idx):
if not segment or "%s" not in segment:
return segment, start_idx

parts = segment.split("%s")
if len(parts) - 1 + start_idx > len(params):
raise ValueError(
"Number of parameters does not match number of placeholders in UPDATE query"
)

new_segment = parts[0]
idx = start_idx
for i in range(len(parts) - 1):
param = params[idx]
param_str = (
f"'{param}'" if isinstance(param, str) else str(param)
)
new_segment += param_str + parts[i + 1]
idx += 1
return new_segment, idx

set_clause_str, next_idx = _replace_placeholders(
set_clause_str, next_idx = replace_placeholders(
set_clause_str, parameters, 0
)
where_clause_str, _ = _replace_placeholders(
where_clause_str, _ = replace_placeholders(
where_clause_str, parameters, next_idx
)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ classifiers = [
dependencies = ["scylla-driver"]

[project.optional-dependencies]
dev = ["pytest>=7.0.1", "ruff>=0.0.17"]
dev = ["complexipy>=3.1.0", "pytest>=7.0.1", "ruff>=0.0.17"]

[tool.ruff]
line-length = 80
Loading