Skip to content

Commit

Permalink
fixed issue when metadata key contains json (#169)
Browse files Browse the repository at this point in the history
* fixed issue when metadata key contains json

* fmt
  • Loading branch information
epinzur authored Oct 1, 2024
1 parent 4455889 commit 282a713
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ __pypackages__/
.idea

.DS_Store

.vscode
3 changes: 2 additions & 1 deletion src/cassio/table/mixins/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ def _extract_where_clause_blocks(
these_wc_vals_list: List[Any] = []
# WHERE creation:
for k, v in sorted(split_metadata.get("metadata_s", {}).items()):
these_wc_blocks.append(f"metadata_s['{k}'] = %s")
escaped_k = k.replace("{", "{{").replace("}", "}}")
these_wc_blocks.append(f"metadata_s['{escaped_k}'] = %s")
these_wc_vals_list.append(v)
# no new kwargs keys are created, all goes to WHERE
this_args_dict: Dict[str, Any] = {}
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/test_tableclasses_cql_generation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
CQL for mixin-based table classes tests
"""
import json

from cassio.table.cql import MockDBSession
from cassio.table.query import Predicate, PredicateOperator
from cassio.table.tables import (
Expand Down Expand Up @@ -206,6 +208,7 @@ def test_multi_clustering_column_metadata_vector_cassandra_table(

md1 = {"num1": 123, "num2": 456, "str1": "STR1", "tru1": True}
md2 = {"tru1": True, "tru2": True}
md_json_key = {"link_" + json.dumps({"kind": "kw"}): "link"}
vt_multi_cc.put(
partition_id="PARTITIONID",
row_id=(1, 2),
Expand Down Expand Up @@ -272,6 +275,21 @@ def test_multi_clustering_column_metadata_vector_cassandra_table(
]
)

vt_multi_cc.put(partition_id="PARTITIONID", row_id=(1, 2), metadata=md_json_key)
mock_db_session.assert_last_equal(
[
(
"INSERT INTO k.tn (metadata_s, row_id_0, row_id_1, partition_id) VALUES (?, ?, ?, ?);", # noqa: E501
(
{'link_{"kind": "kw"}': "link"},
1,
2,
"PARTITIONID",
),
),
]
)

vt_multi_cc.get_partition(partition_id="PARTITIONID", n=10)
mock_db_session.assert_last_equal(
[
Expand Down Expand Up @@ -399,6 +417,16 @@ def test_multi_clustering_column_metadata_vector_cassandra_table(
]
)

vt_multi_cc.get_partition(partition_id="MD_JSON_KEY", metadata=md_json_key)
mock_db_session.assert_last_equal(
[
(
'SELECT * FROM k.tn WHERE metadata_s[\'link_{"kind": "kw"}\'] = ? AND partition_id = ? ;', # noqa: E501
("link", "MD_JSON_KEY"),
),
]
)

vt_multi_cc.clear()
mock_db_session.assert_last_equal(
[
Expand Down

0 comments on commit 282a713

Please sign in to comment.