clear-skies-aws 1.9.17__py3-none-any.whl → 1.10.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {clear_skies_aws-1.9.17.dist-info → clear_skies_aws-1.10.0.dist-info}/METADATA +6 -3
- {clear_skies_aws-1.9.17.dist-info → clear_skies_aws-1.10.0.dist-info}/RECORD +17 -13
- {clear_skies_aws-1.9.17.dist-info → clear_skies_aws-1.10.0.dist-info}/WHEEL +1 -1
- clearskies_aws/actions/assume_role_test.py +5 -2
- clearskies_aws/backends/__init__.py +10 -0
- clearskies_aws/backends/dynamo_db_backend.py +12 -10
- clearskies_aws/backends/dynamo_db_backend_test.py +71 -65
- clearskies_aws/backends/dynamo_db_condition_parser.py +365 -0
- clearskies_aws/backends/dynamo_db_condition_parser_test.py +266 -0
- clearskies_aws/backends/dynamo_db_parti_ql_backend.py +1000 -0
- clearskies_aws/backends/dynamo_db_parti_ql_backend_test.py +540 -0
- clearskies_aws/contexts/lambda_sqs_standard_partial_batch_test.py +11 -16
- clearskies_aws/di/standard_dependencies.py +47 -5
- clearskies_aws/input_outputs/lambda_api_gateway_test.py +27 -19
- clearskies_aws/secrets/parameter_store_test.py +5 -2
- clearskies_aws/secrets/secrets_manager_test.py +5 -2
- {clear_skies_aws-1.9.17.dist-info → clear_skies_aws-1.10.0.dist-info}/LICENSE +0 -0
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import json
|
|
3
|
+
import logging
|
|
4
|
+
from decimal import Decimal, DecimalException
|
|
5
|
+
from typing import Any, Dict, List, Tuple
|
|
6
|
+
|
|
7
|
+
from clearskies import ConditionParser
|
|
8
|
+
|
|
9
|
+
# Ensure AttributeValueTypeDef is imported from the correct boto3 types package
|
|
10
|
+
# This is crucial for the "ideal fix".
|
|
11
|
+
from types_boto3_dynamodb.type_defs import AttributeValueTypeDef
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class DynamoDBConditionParser(ConditionParser):
|
|
17
|
+
"""
|
|
18
|
+
Parses string conditions into a structured format suitable for DynamoDB PartiQL queries.
|
|
19
|
+
|
|
20
|
+
This class handles various SQL-like operators and translates them into
|
|
21
|
+
PartiQL compatible expressions and parameters. It also includes a utility
|
|
22
|
+
to convert Python values into the DynamoDB AttributeValue format.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
operator_lengths: Dict[str, int] = {
|
|
26
|
+
"<>": 2,
|
|
27
|
+
"<=": 2,
|
|
28
|
+
">=": 2,
|
|
29
|
+
"!=": 2,
|
|
30
|
+
">": 1,
|
|
31
|
+
"<": 1,
|
|
32
|
+
"=": 1,
|
|
33
|
+
"in": 4,
|
|
34
|
+
"is not null": 12,
|
|
35
|
+
"is null": 8,
|
|
36
|
+
"is not": 8,
|
|
37
|
+
"is": 4,
|
|
38
|
+
"like": 6,
|
|
39
|
+
"is not missing": 15,
|
|
40
|
+
"is missing": 11,
|
|
41
|
+
"contains": 9,
|
|
42
|
+
"begins_with": 12,
|
|
43
|
+
}
|
|
44
|
+
operators: List[str] = [
|
|
45
|
+
# Longer operators first to help with matching
|
|
46
|
+
"is not null",
|
|
47
|
+
"is not missing",
|
|
48
|
+
"is null",
|
|
49
|
+
"is missing",
|
|
50
|
+
"begins_with",
|
|
51
|
+
"contains",
|
|
52
|
+
"<>",
|
|
53
|
+
"!=",
|
|
54
|
+
"<=",
|
|
55
|
+
">=",
|
|
56
|
+
"is not",
|
|
57
|
+
"is",
|
|
58
|
+
"like",
|
|
59
|
+
">",
|
|
60
|
+
"<",
|
|
61
|
+
"=",
|
|
62
|
+
"in",
|
|
63
|
+
]
|
|
64
|
+
operators_for_matching: Dict[str, str] = {
|
|
65
|
+
"like": " like ",
|
|
66
|
+
"in": " in ",
|
|
67
|
+
"is not missing": " is not missing",
|
|
68
|
+
"is missing": " is missing",
|
|
69
|
+
"is not null": " is not null",
|
|
70
|
+
"is null": " is null",
|
|
71
|
+
"is": " is ",
|
|
72
|
+
"is not": " is not ",
|
|
73
|
+
"begins_with": " begins_with",
|
|
74
|
+
"contains": " contains",
|
|
75
|
+
}
|
|
76
|
+
operators_with_simple_placeholders: Dict[str, bool] = {
|
|
77
|
+
"<>": True,
|
|
78
|
+
"<=": True,
|
|
79
|
+
">=": True,
|
|
80
|
+
"!=": True,
|
|
81
|
+
"=": True,
|
|
82
|
+
"<": True,
|
|
83
|
+
">": True,
|
|
84
|
+
"is": True,
|
|
85
|
+
"is not": True,
|
|
86
|
+
}
|
|
87
|
+
operators_without_placeholders: set[str] = {
|
|
88
|
+
"is not missing",
|
|
89
|
+
"is missing",
|
|
90
|
+
}
|
|
91
|
+
operator_needs_remap: Dict[str, str] = {
|
|
92
|
+
"is not null": "is not missing",
|
|
93
|
+
"is null": "is missing",
|
|
94
|
+
}
|
|
95
|
+
operators_with_special_placeholders: set[str] = {"begins_with", "contains"}
|
|
96
|
+
|
|
97
|
+
def parse_condition(self, condition: str) -> Dict[str, Any]:
|
|
98
|
+
"""
|
|
99
|
+
Parses a string condition into a structured dictionary.
|
|
100
|
+
|
|
101
|
+
The "values" key in the returned dictionary will contain List[AttributeValueTypeDef].
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
condition: The condition string to parse.
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
A dictionary with keys: "table", "column", "operator", "values" (DynamoDB formatted),
|
|
108
|
+
and "parsed" (the SQL fragment).
|
|
109
|
+
"""
|
|
110
|
+
lowercase_condition: str = condition.lower()
|
|
111
|
+
matching_operator: str = ""
|
|
112
|
+
matching_index: int = -1
|
|
113
|
+
current_best_match_len: int = 0
|
|
114
|
+
|
|
115
|
+
for operator in self.operators:
|
|
116
|
+
try:
|
|
117
|
+
operator_for_match: str = self.operators_for_matching.get(
|
|
118
|
+
operator, operator
|
|
119
|
+
)
|
|
120
|
+
index: int = lowercase_condition.index(operator_for_match)
|
|
121
|
+
|
|
122
|
+
if matching_index == -1 or index < matching_index:
|
|
123
|
+
matching_index = index
|
|
124
|
+
matching_operator = operator
|
|
125
|
+
current_best_match_len = len(operator_for_match)
|
|
126
|
+
elif index == matching_index:
|
|
127
|
+
if len(operator_for_match) > current_best_match_len:
|
|
128
|
+
matching_operator = operator
|
|
129
|
+
current_best_match_len = len(operator_for_match)
|
|
130
|
+
except ValueError:
|
|
131
|
+
continue
|
|
132
|
+
|
|
133
|
+
if not matching_operator:
|
|
134
|
+
raise ValueError(f"No supported operators found in condition {condition}")
|
|
135
|
+
|
|
136
|
+
column: str = condition[:matching_index].strip()
|
|
137
|
+
value: str = condition[
|
|
138
|
+
matching_index + self.operator_lengths[matching_operator] :
|
|
139
|
+
].strip()
|
|
140
|
+
|
|
141
|
+
if len(value) >= 2:
|
|
142
|
+
first_char = value[0]
|
|
143
|
+
last_char = value[-1]
|
|
144
|
+
if (first_char == "'" and last_char == "'") or (
|
|
145
|
+
first_char == '"' and last_char == '"'
|
|
146
|
+
):
|
|
147
|
+
value = value[1:-1]
|
|
148
|
+
|
|
149
|
+
raw_values: List[str] = []
|
|
150
|
+
|
|
151
|
+
if matching_operator == "in":
|
|
152
|
+
raw_values = self._parse_condition_list(value) if value else []
|
|
153
|
+
elif matching_operator not in self.operators_without_placeholders and not (
|
|
154
|
+
matching_operator in self.operator_needs_remap
|
|
155
|
+
and self.operator_needs_remap[matching_operator]
|
|
156
|
+
in self.operators_without_placeholders
|
|
157
|
+
):
|
|
158
|
+
raw_values = [value]
|
|
159
|
+
|
|
160
|
+
if matching_operator.lower() == "like":
|
|
161
|
+
if value.startswith("%") and value.endswith("%") and len(value) > 1:
|
|
162
|
+
matching_operator = "contains"
|
|
163
|
+
raw_values = [value[1:-1]]
|
|
164
|
+
elif value.endswith("%") and not value.startswith("%"):
|
|
165
|
+
matching_operator = "begins_with"
|
|
166
|
+
raw_values = [value[:-1]]
|
|
167
|
+
elif value.startswith("%") and not value.endswith("%"):
|
|
168
|
+
raise ValueError(
|
|
169
|
+
"DynamoDB PartiQL does not directly support 'ends_with'"
|
|
170
|
+
)
|
|
171
|
+
else:
|
|
172
|
+
matching_operator = "="
|
|
173
|
+
raw_values = [value]
|
|
174
|
+
|
|
175
|
+
matching_operator = self.operator_needs_remap.get(
|
|
176
|
+
matching_operator.lower(), matching_operator
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
table_name: str = ""
|
|
180
|
+
final_column_name: str = column
|
|
181
|
+
if "." in column:
|
|
182
|
+
table_prefix, column_name_part = column.split(".", 1)
|
|
183
|
+
table_name = table_prefix.strip().replace('"', "").replace("`", "")
|
|
184
|
+
final_column_name = (
|
|
185
|
+
column_name_part.strip().replace('"', "").replace("`", "")
|
|
186
|
+
)
|
|
187
|
+
else:
|
|
188
|
+
final_column_name = column.replace('"', "").replace("`", "")
|
|
189
|
+
|
|
190
|
+
# This list will now correctly be List[AttributeValueTypeDef]
|
|
191
|
+
parameters: List[AttributeValueTypeDef] = []
|
|
192
|
+
if matching_operator.lower() not in self.operators_without_placeholders:
|
|
193
|
+
for val_item in raw_values:
|
|
194
|
+
parameters.append(self.to_dynamodb_attribute_value(val_item))
|
|
195
|
+
|
|
196
|
+
column_for_parsed: str = (
|
|
197
|
+
f"{table_name}.{final_column_name}" if table_name else final_column_name
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
"table": table_name,
|
|
202
|
+
"column": final_column_name,
|
|
203
|
+
"operator": matching_operator.upper(),
|
|
204
|
+
"values": parameters, # This is now correctly typed for MyPy
|
|
205
|
+
"parsed": self._with_placeholders(
|
|
206
|
+
column_for_parsed,
|
|
207
|
+
matching_operator,
|
|
208
|
+
parameters,
|
|
209
|
+
),
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
def _with_placeholders(
|
|
213
|
+
self,
|
|
214
|
+
column: str,
|
|
215
|
+
operator: str,
|
|
216
|
+
values: List[
|
|
217
|
+
AttributeValueTypeDef
|
|
218
|
+
], # Parameter 'values' is List[AttributeValueTypeDef]
|
|
219
|
+
escape: bool = True,
|
|
220
|
+
escape_character: str = '"',
|
|
221
|
+
) -> str:
|
|
222
|
+
"""
|
|
223
|
+
Formats a SQL fragment with placeholders for a given column, operator, and parameters.
|
|
224
|
+
"""
|
|
225
|
+
quoted_column = column
|
|
226
|
+
if escape:
|
|
227
|
+
parts: List[str] = column.split(".", 1)
|
|
228
|
+
cleaned_parts: List[str] = [part.strip('"`') for part in parts]
|
|
229
|
+
if len(cleaned_parts) == 2:
|
|
230
|
+
quoted_column = (
|
|
231
|
+
f"{escape_character}{cleaned_parts[0]}{escape_character}"
|
|
232
|
+
"."
|
|
233
|
+
f"{escape_character}{cleaned_parts[1]}{escape_character}"
|
|
234
|
+
)
|
|
235
|
+
else:
|
|
236
|
+
quoted_column = (
|
|
237
|
+
f"{escape_character}{cleaned_parts[0]}{escape_character}"
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
upper_case_operator: str = operator.upper()
|
|
241
|
+
lower_case_operator: str = operator.lower()
|
|
242
|
+
|
|
243
|
+
if lower_case_operator in self.operators_with_simple_placeholders:
|
|
244
|
+
return f"{quoted_column} {upper_case_operator} ?"
|
|
245
|
+
if lower_case_operator in self.operators_without_placeholders:
|
|
246
|
+
return f"{quoted_column} {upper_case_operator}"
|
|
247
|
+
if lower_case_operator in self.operators_with_special_placeholders:
|
|
248
|
+
return f"{lower_case_operator}({quoted_column}, ?)"
|
|
249
|
+
|
|
250
|
+
if lower_case_operator == "in":
|
|
251
|
+
placeholders_str: str = ", ".join(["?" for _ in values])
|
|
252
|
+
return f"{quoted_column} IN ({placeholders_str})"
|
|
253
|
+
|
|
254
|
+
raise ValueError(f"Unsupported operator for placeholder generation: {operator}")
|
|
255
|
+
|
|
256
|
+
def to_dynamodb_attribute_value(
|
|
257
|
+
self, value: Any
|
|
258
|
+
) -> AttributeValueTypeDef: # Return type changed
|
|
259
|
+
"""
|
|
260
|
+
Converts a Python variable into a DynamoDB-formatted attribute value dictionary.
|
|
261
|
+
"""
|
|
262
|
+
if isinstance(value, str):
|
|
263
|
+
if value.lower() == "true":
|
|
264
|
+
return {"BOOL": True}
|
|
265
|
+
if value.lower() == "false":
|
|
266
|
+
return {"BOOL": False}
|
|
267
|
+
if value.lower() == "null":
|
|
268
|
+
return {"NULL": True}
|
|
269
|
+
try:
|
|
270
|
+
if value.isdigit() or (value.startswith("-") and value[1:].isdigit()):
|
|
271
|
+
return {"N": str(int(value))}
|
|
272
|
+
return {"N": str(Decimal(value))}
|
|
273
|
+
except (ValueError, TypeError, json.JSONDecodeError, DecimalException):
|
|
274
|
+
return {"S": value}
|
|
275
|
+
elif isinstance(value, bool):
|
|
276
|
+
return {"BOOL": value}
|
|
277
|
+
elif isinstance(value, (int, float, Decimal)):
|
|
278
|
+
return {"N": str(value)}
|
|
279
|
+
elif value is None:
|
|
280
|
+
return {"NULL": True}
|
|
281
|
+
elif isinstance(value, bytes):
|
|
282
|
+
return {"B": base64.b64encode(value).decode("utf-8")}
|
|
283
|
+
elif isinstance(value, list):
|
|
284
|
+
# Each item will be AttributeValueTypeDef, so the list is List[AttributeValueTypeDef]
|
|
285
|
+
return {"L": [self.to_dynamodb_attribute_value(item) for item in value]}
|
|
286
|
+
elif isinstance(value, dict):
|
|
287
|
+
# Each value in the map will be AttributeValueTypeDef
|
|
288
|
+
return {
|
|
289
|
+
"M": {
|
|
290
|
+
str(k): self.to_dynamodb_attribute_value(v)
|
|
291
|
+
for k, v in value.items()
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
elif isinstance(value, set):
|
|
295
|
+
if not value:
|
|
296
|
+
raise ValueError(
|
|
297
|
+
"Cannot determine DynamoDB Set type from an empty Python set."
|
|
298
|
+
)
|
|
299
|
+
if all(isinstance(item, str) for item in value):
|
|
300
|
+
return {"SS": sorted(list(value))}
|
|
301
|
+
elif all(isinstance(item, (int, float, Decimal)) for item in value):
|
|
302
|
+
return {"NS": sorted([str(item) for item in value])}
|
|
303
|
+
elif all(isinstance(item, bytes) for item in value):
|
|
304
|
+
return {
|
|
305
|
+
"BS": sorted(
|
|
306
|
+
[base64.b64encode(item).decode("utf-8") for item in value]
|
|
307
|
+
)
|
|
308
|
+
}
|
|
309
|
+
raise ValueError(
|
|
310
|
+
"Set contains mixed types or unsupported types for DynamoDB Sets."
|
|
311
|
+
)
|
|
312
|
+
else:
|
|
313
|
+
raise TypeError(
|
|
314
|
+
f"Unsupported Python type for DynamoDB conversion: {type(value)}"
|
|
315
|
+
)
|
|
316
|
+
|
|
317
|
+
def _parse_condition_list(self, list_string: str) -> List[str]:
|
|
318
|
+
"""
|
|
319
|
+
Parses a string representation of a list into a list of strings.
|
|
320
|
+
"""
|
|
321
|
+
if not list_string.strip():
|
|
322
|
+
return []
|
|
323
|
+
|
|
324
|
+
if list_string.startswith("(") and list_string.endswith(")"):
|
|
325
|
+
list_string = list_string[1:-1]
|
|
326
|
+
if not list_string.strip():
|
|
327
|
+
return []
|
|
328
|
+
|
|
329
|
+
items: List[str] = []
|
|
330
|
+
current_item: str = ""
|
|
331
|
+
in_quotes: bool = False
|
|
332
|
+
quote_char: str = ""
|
|
333
|
+
for char in list_string:
|
|
334
|
+
if char in ("'", '"'):
|
|
335
|
+
if in_quotes and char == quote_char:
|
|
336
|
+
in_quotes = False
|
|
337
|
+
elif not in_quotes:
|
|
338
|
+
in_quotes = True
|
|
339
|
+
quote_char = char
|
|
340
|
+
else:
|
|
341
|
+
current_item += char
|
|
342
|
+
elif char == "," and not in_quotes:
|
|
343
|
+
stripped_item = current_item.strip()
|
|
344
|
+
if stripped_item:
|
|
345
|
+
items.append(stripped_item)
|
|
346
|
+
current_item = ""
|
|
347
|
+
else:
|
|
348
|
+
current_item += char
|
|
349
|
+
|
|
350
|
+
stripped_current_item = current_item.strip()
|
|
351
|
+
if stripped_current_item:
|
|
352
|
+
items.append(stripped_current_item)
|
|
353
|
+
|
|
354
|
+
final_items = []
|
|
355
|
+
for item in items:
|
|
356
|
+
processed_item = item
|
|
357
|
+
if len(processed_item) >= 2:
|
|
358
|
+
if processed_item.startswith("'") and processed_item.endswith("'"):
|
|
359
|
+
processed_item = processed_item[1:-1]
|
|
360
|
+
elif processed_item.startswith('"') and processed_item.endswith('"'):
|
|
361
|
+
processed_item = processed_item[1:-1]
|
|
362
|
+
|
|
363
|
+
if processed_item:
|
|
364
|
+
final_items.append(processed_item)
|
|
365
|
+
return final_items
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
from decimal import Decimal
|
|
3
|
+
|
|
4
|
+
from clearskies_aws.backends.dynamo_db_condition_parser import DynamoDBConditionParser
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class TestDynamoDBConditionParser(unittest.TestCase):
|
|
8
|
+
def setUp(self):
|
|
9
|
+
"""Set up the parser for each test."""
|
|
10
|
+
self.parser = DynamoDBConditionParser()
|
|
11
|
+
|
|
12
|
+
def test_to_dynamodb_attribute_value_string(self):
|
|
13
|
+
"""Test conversion of a simple string."""
|
|
14
|
+
self.assertEqual(
|
|
15
|
+
self.parser.to_dynamodb_attribute_value("hello"), {"S": "hello"}
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
def test_to_dynamodb_attribute_value_int(self):
|
|
19
|
+
"""Test conversion of an integer."""
|
|
20
|
+
self.assertEqual(self.parser.to_dynamodb_attribute_value(123), {"N": "123"})
|
|
21
|
+
|
|
22
|
+
def test_to_dynamodb_attribute_value_float(self):
|
|
23
|
+
"""Test conversion of a float."""
|
|
24
|
+
self.assertEqual(
|
|
25
|
+
self.parser.to_dynamodb_attribute_value(123.45), {"N": "123.45"}
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
def test_to_dynamodb_attribute_value_decimal(self):
|
|
29
|
+
"""Test conversion of a Decimal object."""
|
|
30
|
+
self.assertEqual(
|
|
31
|
+
self.parser.to_dynamodb_attribute_value(Decimal("99.01")), {"N": "99.01"}
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
def test_to_dynamodb_attribute_value_bool_true(self):
|
|
35
|
+
"""Test conversion of a boolean True."""
|
|
36
|
+
self.assertEqual(self.parser.to_dynamodb_attribute_value(True), {"BOOL": True})
|
|
37
|
+
|
|
38
|
+
def test_to_dynamodb_attribute_value_bool_false(self):
|
|
39
|
+
"""Test conversion of a boolean False."""
|
|
40
|
+
self.assertEqual(
|
|
41
|
+
self.parser.to_dynamodb_attribute_value(False), {"BOOL": False}
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
def test_to_dynamodb_attribute_value_none(self):
|
|
45
|
+
"""Test conversion of None."""
|
|
46
|
+
self.assertEqual(self.parser.to_dynamodb_attribute_value(None), {"NULL": True})
|
|
47
|
+
|
|
48
|
+
def test_to_dynamodb_attribute_value_string_true_false_null(self):
|
|
49
|
+
"""Test conversion of string representations of boolean, null, and numbers."""
|
|
50
|
+
self.assertEqual(
|
|
51
|
+
self.parser.to_dynamodb_attribute_value("true"), {"BOOL": True}
|
|
52
|
+
)
|
|
53
|
+
self.assertEqual(
|
|
54
|
+
self.parser.to_dynamodb_attribute_value("FALSE"), {"BOOL": False}
|
|
55
|
+
)
|
|
56
|
+
self.assertEqual(
|
|
57
|
+
self.parser.to_dynamodb_attribute_value("NuLl"), {"NULL": True}
|
|
58
|
+
)
|
|
59
|
+
self.assertEqual(self.parser.to_dynamodb_attribute_value("123"), {"N": "123"})
|
|
60
|
+
self.assertEqual(self.parser.to_dynamodb_attribute_value("-45"), {"N": "-45"})
|
|
61
|
+
self.assertEqual(
|
|
62
|
+
self.parser.to_dynamodb_attribute_value("123.45"), {"N": "123.45"}
|
|
63
|
+
)
|
|
64
|
+
self.assertEqual(self.parser.to_dynamodb_attribute_value("text"), {"S": "text"})
|
|
65
|
+
|
|
66
|
+
def test_to_dynamodb_attribute_value_list(self):
|
|
67
|
+
"""Test conversion of a list with mixed data types."""
|
|
68
|
+
val = ["a", 1, True, None, Decimal("2.3")]
|
|
69
|
+
expected = {
|
|
70
|
+
"L": [
|
|
71
|
+
{"S": "a"},
|
|
72
|
+
{"N": "1"},
|
|
73
|
+
{"BOOL": True},
|
|
74
|
+
{"NULL": True},
|
|
75
|
+
{"N": "2.3"},
|
|
76
|
+
]
|
|
77
|
+
}
|
|
78
|
+
self.assertEqual(self.parser.to_dynamodb_attribute_value(val), expected)
|
|
79
|
+
|
|
80
|
+
def test_to_dynamodb_attribute_value_map(self):
|
|
81
|
+
"""Test conversion of a dictionary (map)."""
|
|
82
|
+
val = {"key_s": "val", "key_n": 100}
|
|
83
|
+
expected = {"M": {"key_s": {"S": "val"}, "key_n": {"N": "100"}}}
|
|
84
|
+
self.assertEqual(self.parser.to_dynamodb_attribute_value(val), expected)
|
|
85
|
+
|
|
86
|
+
def test_to_dynamodb_attribute_value_set_string(self):
|
|
87
|
+
"""Test conversion of a set of strings."""
|
|
88
|
+
val = {"a", "b", "a"}
|
|
89
|
+
expected = {"SS": sorted(["a", "b"])}
|
|
90
|
+
self.assertEqual(self.parser.to_dynamodb_attribute_value(val), expected)
|
|
91
|
+
|
|
92
|
+
def test_to_dynamodb_attribute_value_set_number(self):
|
|
93
|
+
"""Test conversion of a set of numbers (int and Decimal)."""
|
|
94
|
+
val = {1, 2, Decimal("3.0")}
|
|
95
|
+
expected = {"NS": sorted(["1", "2", "3.0"])}
|
|
96
|
+
self.assertEqual(self.parser.to_dynamodb_attribute_value(val), expected)
|
|
97
|
+
|
|
98
|
+
def test_to_dynamodb_attribute_value_unsupported(self):
|
|
99
|
+
"""Test conversion of an unsupported data type raises TypeError."""
|
|
100
|
+
|
|
101
|
+
class MyObject:
|
|
102
|
+
pass
|
|
103
|
+
|
|
104
|
+
with self.assertRaises(TypeError):
|
|
105
|
+
self.parser.to_dynamodb_attribute_value(MyObject())
|
|
106
|
+
|
|
107
|
+
def test_parse_condition_list_simple(self):
|
|
108
|
+
"""Test the internal _parse_condition_list helper method."""
|
|
109
|
+
self.assertEqual(
|
|
110
|
+
self.parser._parse_condition_list("'a', 'b', 'c'"), ["a", "b", "c"]
|
|
111
|
+
)
|
|
112
|
+
self.assertEqual(self.parser._parse_condition_list("1, 2, 3"), ["1", "2", "3"])
|
|
113
|
+
self.assertEqual(self.parser._parse_condition_list("('a', \"b\")"), ["a", "b"])
|
|
114
|
+
self.assertEqual(self.parser._parse_condition_list(""), [])
|
|
115
|
+
self.assertEqual(self.parser._parse_condition_list(" "), [])
|
|
116
|
+
self.assertEqual(self.parser._parse_condition_list(" 'item1' "), ["item1"])
|
|
117
|
+
|
|
118
|
+
def test_parse_condition_equals_string(self):
|
|
119
|
+
"""Test parsing an equality condition with a string value."""
|
|
120
|
+
result = self.parser.parse_condition("name = 'John Doe'")
|
|
121
|
+
self.assertEqual(result["column"], "name")
|
|
122
|
+
self.assertEqual(result["operator"], "=")
|
|
123
|
+
self.assertEqual(result["values"], [{"S": "John Doe"}])
|
|
124
|
+
self.assertEqual(result["parsed"], '"name" = ?')
|
|
125
|
+
self.assertEqual(result["table"], "")
|
|
126
|
+
|
|
127
|
+
def test_parse_condition_equals_number(self):
|
|
128
|
+
"""Test parsing an equality condition with a numeric value."""
|
|
129
|
+
result = self.parser.parse_condition("age = 30")
|
|
130
|
+
self.assertEqual(result["column"], "age")
|
|
131
|
+
self.assertEqual(result["operator"], "=")
|
|
132
|
+
self.assertEqual(result["values"], [{"N": "30"}])
|
|
133
|
+
self.assertEqual(result["parsed"], '"age" = ?')
|
|
134
|
+
|
|
135
|
+
def test_parse_condition_greater_than(self):
|
|
136
|
+
"""Test parsing a greater than condition."""
|
|
137
|
+
result = self.parser.parse_condition("price > 10.5")
|
|
138
|
+
self.assertEqual(result["column"], "price")
|
|
139
|
+
self.assertEqual(result["operator"], ">")
|
|
140
|
+
self.assertEqual(result["values"], [{"N": "10.5"}])
|
|
141
|
+
self.assertEqual(result["parsed"], '"price" > ?')
|
|
142
|
+
|
|
143
|
+
def test_parse_condition_table_column(self):
|
|
144
|
+
"""Test parsing a condition with a table-prefixed column."""
|
|
145
|
+
result = self.parser.parse_condition("user.id = 'user123'")
|
|
146
|
+
self.assertEqual(result["table"], "user")
|
|
147
|
+
self.assertEqual(result["column"], "id")
|
|
148
|
+
self.assertEqual(result["operator"], "=")
|
|
149
|
+
self.assertEqual(result["values"], [{"S": "user123"}])
|
|
150
|
+
self.assertEqual(result["parsed"], '"user"."id" = ?')
|
|
151
|
+
|
|
152
|
+
def test_parse_condition_is_null(self):
|
|
153
|
+
"""Test parsing an 'IS NULL' condition (remapped to IS MISSING)."""
|
|
154
|
+
result = self.parser.parse_condition("email is null")
|
|
155
|
+
self.assertEqual(result["column"], "email")
|
|
156
|
+
self.assertEqual(result["operator"], "IS MISSING")
|
|
157
|
+
self.assertEqual(result["values"], [])
|
|
158
|
+
self.assertEqual(result["parsed"], '"email" IS MISSING')
|
|
159
|
+
|
|
160
|
+
def test_parse_condition_is_not_null(self):
|
|
161
|
+
"""Test parsing an 'IS NOT NULL' condition (remapped to IS NOT MISSING)."""
|
|
162
|
+
result = self.parser.parse_condition("address is not null")
|
|
163
|
+
self.assertEqual(result["column"], "address")
|
|
164
|
+
self.assertEqual(result["operator"], "IS NOT MISSING")
|
|
165
|
+
self.assertEqual(result["values"], [])
|
|
166
|
+
self.assertEqual(result["parsed"], '"address" IS NOT MISSING')
|
|
167
|
+
|
|
168
|
+
def test_parse_condition_like_begins_with(self):
|
|
169
|
+
"""Test parsing a 'LIKE value%' condition (becomes BEGINS_WITH)."""
|
|
170
|
+
result = self.parser.parse_condition("name LIKE 'Jo%'")
|
|
171
|
+
self.assertEqual(result["column"], "name")
|
|
172
|
+
self.assertEqual(result["operator"], "BEGINS_WITH")
|
|
173
|
+
self.assertEqual(result["values"], [{"S": "Jo"}])
|
|
174
|
+
self.assertEqual(result["parsed"], 'begins_with("name", ?)')
|
|
175
|
+
|
|
176
|
+
def test_parse_condition_like_contains(self):
|
|
177
|
+
"""Test parsing a 'LIKE %value%' condition (becomes CONTAINS)."""
|
|
178
|
+
result = self.parser.parse_condition("description LIKE '%word%'")
|
|
179
|
+
self.assertEqual(result["column"], "description")
|
|
180
|
+
self.assertEqual(result["operator"], "CONTAINS")
|
|
181
|
+
self.assertEqual(result["values"], [{"S": "word"}])
|
|
182
|
+
self.assertEqual(result["parsed"], 'contains("description", ?)')
|
|
183
|
+
|
|
184
|
+
def test_parse_condition_like_exact(self):
|
|
185
|
+
"""Test parsing a 'LIKE value' condition (no wildcards, becomes =)."""
|
|
186
|
+
result = self.parser.parse_condition("tag LIKE 'exactmatch'")
|
|
187
|
+
self.assertEqual(result["column"], "tag")
|
|
188
|
+
self.assertEqual(result["operator"], "=")
|
|
189
|
+
self.assertEqual(result["values"], [{"S": "exactmatch"}])
|
|
190
|
+
self.assertEqual(result["parsed"], '"tag" = ?')
|
|
191
|
+
|
|
192
|
+
def test_parse_condition_like_ends_with_error(self):
|
|
193
|
+
"""Test that 'LIKE %value' (ends with) raises an error."""
|
|
194
|
+
with self.assertRaisesRegex(
|
|
195
|
+
ValueError, "DynamoDB PartiQL does not directly support 'ends_with'"
|
|
196
|
+
):
|
|
197
|
+
self.parser.parse_condition("filename LIKE '%doc'")
|
|
198
|
+
|
|
199
|
+
def test_parse_condition_in_list_strings(self):
|
|
200
|
+
"""Test parsing an 'IN' condition with a list of strings."""
|
|
201
|
+
result = self.parser.parse_condition("status IN ('active', 'pending')")
|
|
202
|
+
self.assertEqual(result["column"], "status")
|
|
203
|
+
self.assertEqual(result["operator"], "IN")
|
|
204
|
+
self.assertEqual(result["values"], [{"S": "active"}, {"S": "pending"}])
|
|
205
|
+
self.assertEqual(result["parsed"], '"status" IN (?, ?)')
|
|
206
|
+
|
|
207
|
+
def test_parse_condition_in_list_numbers(self):
|
|
208
|
+
"""Test parsing an 'IN' condition with a list of numbers."""
|
|
209
|
+
result = self.parser.parse_condition("id IN (1, 2, 3)")
|
|
210
|
+
self.assertEqual(result["column"], "id")
|
|
211
|
+
self.assertEqual(result["operator"], "IN")
|
|
212
|
+
self.assertEqual(result["values"], [{"N": "1"}, {"N": "2"}, {"N": "3"}])
|
|
213
|
+
self.assertEqual(result["parsed"], '"id" IN (?, ?, ?)')
|
|
214
|
+
|
|
215
|
+
def test_parse_condition_in_list_single_value(self):
|
|
216
|
+
"""Test parsing an 'IN' condition with a single value in the list."""
|
|
217
|
+
result = self.parser.parse_condition("id IN (1)")
|
|
218
|
+
self.assertEqual(result["column"], "id")
|
|
219
|
+
self.assertEqual(result["operator"], "IN")
|
|
220
|
+
self.assertEqual(result["values"], [{"N": "1"}])
|
|
221
|
+
self.assertEqual(result["parsed"], '"id" IN (?)')
|
|
222
|
+
|
|
223
|
+
def test_parse_condition_contains_function(self):
|
|
224
|
+
"""Test parsing a 'CONTAINS' function call."""
|
|
225
|
+
result = self.parser.parse_condition("tags CONTAINS 'important'")
|
|
226
|
+
self.assertEqual(result["column"], "tags")
|
|
227
|
+
self.assertEqual(result["operator"], "CONTAINS")
|
|
228
|
+
self.assertEqual(result["values"], [{"S": "important"}])
|
|
229
|
+
self.assertEqual(result["parsed"], 'contains("tags", ?)')
|
|
230
|
+
|
|
231
|
+
def test_parse_condition_begins_with_function(self):
|
|
232
|
+
"""Test parsing a 'BEGINS_WITH' function call."""
|
|
233
|
+
result = self.parser.parse_condition("sku BEGINS_WITH 'ABC-'")
|
|
234
|
+
self.assertEqual(result["column"], "sku")
|
|
235
|
+
self.assertEqual(result["operator"], "BEGINS_WITH")
|
|
236
|
+
self.assertEqual(result["values"], [{"S": "ABC-"}])
|
|
237
|
+
self.assertEqual(result["parsed"], 'begins_with("sku", ?)')
|
|
238
|
+
|
|
239
|
+
def test_parse_condition_quoted_column(self):
|
|
240
|
+
"""Test parsing a condition with a double-quoted column name."""
|
|
241
|
+
result = self.parser.parse_condition('"my-column" = "test value"')
|
|
242
|
+
self.assertEqual(result["column"], "my-column")
|
|
243
|
+
self.assertEqual(result["operator"], "=")
|
|
244
|
+
self.assertEqual(result["values"], [{"S": "test value"}])
|
|
245
|
+
self.assertEqual(result["parsed"], '"my-column" = ?')
|
|
246
|
+
|
|
247
|
+
def test_parse_condition_no_operator(self):
|
|
248
|
+
"""Test that parsing a condition without a valid operator raises an error."""
|
|
249
|
+
with self.assertRaisesRegex(ValueError, "No supported operators found"):
|
|
250
|
+
self.parser.parse_condition("column value")
|
|
251
|
+
|
|
252
|
+
def test_parse_condition_is_operator(self):
|
|
253
|
+
"""Test parsing an 'IS' condition."""
|
|
254
|
+
result = self.parser.parse_condition("status IS 'active'")
|
|
255
|
+
self.assertEqual(result["column"], "status")
|
|
256
|
+
self.assertEqual(result["operator"], "IS")
|
|
257
|
+
self.assertEqual(result["values"], [{"S": "active"}])
|
|
258
|
+
self.assertEqual(result["parsed"], '"status" IS ?')
|
|
259
|
+
|
|
260
|
+
def test_parse_condition_is_not_operator(self):
|
|
261
|
+
"""Test parsing an 'IS NOT' condition."""
|
|
262
|
+
result = self.parser.parse_condition("type IS NOT 'internal'")
|
|
263
|
+
self.assertEqual(result["column"], "type")
|
|
264
|
+
self.assertEqual(result["operator"], "IS NOT")
|
|
265
|
+
self.assertEqual(result["values"], [{"S": "internal"}])
|
|
266
|
+
self.assertEqual(result["parsed"], '"type" IS NOT ?')
|