2023-11-30 15:55:51 +00:00
|
|
|
from moto.dynamodb.models.dynamo_type import DynamoType, Item, serializer
|
2023-06-08 17:10:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestFindNestedKeys:
|
2023-07-04 09:35:31 +00:00
|
|
|
def setup_method(self):
|
2023-06-08 17:10:14 +00:00
|
|
|
self.dct = {
|
|
|
|
"simplestring": "val",
|
|
|
|
"nesteddict": {
|
|
|
|
"level21": {"ll31": "val", "ll32": "val"},
|
|
|
|
"level22": {"ll31": "val", "ll32": "val"},
|
|
|
|
"nestedlist": [
|
|
|
|
{"ll21": {"ll31": "val", "ll32": "val"}},
|
|
|
|
{"ll22": {"ll31": "val", "ll32": "val"}},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
"rootlist": [
|
|
|
|
{"ll21": {"ll31": "val", "ll32": "val"}},
|
|
|
|
{"ll22": {"ll31": "val", "ll32": "val"}},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
x = serializer.serialize(self.dct)["M"]
|
|
|
|
self.item = Item(
|
|
|
|
hash_key=DynamoType({"pk": {"S": "v"}}), range_key=None, attrs=x
|
|
|
|
)
|
|
|
|
|
|
|
|
def _project(self, expression, result):
|
|
|
|
x = self.item.project(expression)
|
|
|
|
y = Item(
|
|
|
|
hash_key=DynamoType({"pk": {"S": "v"}}),
|
|
|
|
range_key=None,
|
|
|
|
attrs=serializer.serialize(result)["M"],
|
|
|
|
)
|
|
|
|
assert x == y
|
|
|
|
|
|
|
|
def test_find_nothing(self):
|
2023-08-21 21:52:58 +00:00
|
|
|
self._project([[""]], result={})
|
2023-06-08 17:10:14 +00:00
|
|
|
|
|
|
|
def test_find_unknown_key(self):
|
2023-08-21 21:52:58 +00:00
|
|
|
self._project([["unknown"]], result={})
|
2023-06-08 17:10:14 +00:00
|
|
|
|
|
|
|
def test_project_single_key_string(self):
|
2023-08-21 21:52:58 +00:00
|
|
|
self._project([["simplestring"]], result={"simplestring": "val"})
|
2023-06-08 17:10:14 +00:00
|
|
|
|
|
|
|
def test_project_single_key_dict(self):
|
|
|
|
self._project(
|
2023-08-21 21:52:58 +00:00
|
|
|
[["nesteddict"]],
|
2023-06-08 17:10:14 +00:00
|
|
|
result={
|
|
|
|
"nesteddict": {
|
|
|
|
"level21": {"ll31": "val", "ll32": "val"},
|
|
|
|
"level22": {"ll31": "val", "ll32": "val"},
|
|
|
|
"nestedlist": [
|
|
|
|
{"ll21": {"ll31": "val", "ll32": "val"}},
|
|
|
|
{"ll22": {"ll31": "val", "ll32": "val"}},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_project_nested_key(self):
|
|
|
|
self._project(
|
2023-08-21 21:52:58 +00:00
|
|
|
[["nesteddict", "level21"]],
|
2023-06-08 17:10:14 +00:00
|
|
|
result={"nesteddict": {"level21": {"ll31": "val", "ll32": "val"}}},
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_project_multi_level_nested_key(self):
|
|
|
|
self._project(
|
2023-08-21 21:52:58 +00:00
|
|
|
[["nesteddict", "level21", "ll32"]],
|
2023-06-08 17:10:14 +00:00
|
|
|
result={"nesteddict": {"level21": {"ll32": "val"}}},
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_project_nested_key__partial_fix(self):
|
2023-08-21 21:52:58 +00:00
|
|
|
self._project([["nesteddict", "levelunknown"]], result={})
|
2023-06-08 17:10:14 +00:00
|
|
|
|
|
|
|
def test_project_nested_key__partial_fix2(self):
|
2023-08-21 21:52:58 +00:00
|
|
|
self._project([["nesteddict", "unknown", "unknown2"]], result={})
|
2023-06-08 17:10:14 +00:00
|
|
|
|
|
|
|
def test_list_index(self):
|
|
|
|
self._project(
|
2023-08-21 21:52:58 +00:00
|
|
|
[["rootlist[0]"]],
|
2023-06-08 17:10:14 +00:00
|
|
|
result={"rootlist": [{"ll21": {"ll31": "val", "ll32": "val"}}]},
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_nested_list_index(self):
|
|
|
|
self._project(
|
2023-08-21 21:52:58 +00:00
|
|
|
[["nesteddict", "nestedlist[1]"]],
|
2023-06-08 17:10:14 +00:00
|
|
|
result={
|
|
|
|
"nesteddict": {"nestedlist": [{"ll22": {"ll31": "val", "ll32": "val"}}]}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_nested_obj_in_list(self):
|
|
|
|
self._project(
|
2023-08-21 21:52:58 +00:00
|
|
|
[["nesteddict", "nestedlist[1]", "ll22", "ll31"]],
|
2023-06-08 17:10:14 +00:00
|
|
|
result={"nesteddict": {"nestedlist": [{"ll22": {"ll31": "val"}}]}},
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_list_unknown_indexes(self):
|
2023-08-21 21:52:58 +00:00
|
|
|
self._project([["nesteddict", "nestedlist[25]"]], result={})
|
2023-06-08 17:10:14 +00:00
|
|
|
|
|
|
|
def test_multiple_projections(self):
|
|
|
|
self._project(
|
2023-08-21 21:52:58 +00:00
|
|
|
[["nesteddict", "nestedlist[1]", "ll22"], ["rootlist[0]"]],
|
2023-06-08 17:10:14 +00:00
|
|
|
result={
|
|
|
|
"nesteddict": {
|
|
|
|
"nestedlist": [{"ll22": {"ll31": "val", "ll32": "val"}}]
|
|
|
|
},
|
|
|
|
"rootlist": [{"ll21": {"ll31": "val", "ll32": "val"}}],
|
|
|
|
},
|
|
|
|
)
|