|
26 | 26 | from google.cloud.datastore_v1.proto import query_pb2 |
27 | 27 |
|
28 | 28 | from google.cloud.ndb import _datastore_query |
| 29 | +from google.cloud.ndb import context as context_module |
29 | 30 | from google.cloud.ndb import exceptions |
30 | 31 | from google.cloud.ndb import key as key_module |
31 | 32 | from google.cloud.ndb import model |
@@ -1052,16 +1053,64 @@ def test_entity_unsupported_result_type(model): |
1052 | 1053 | result.entity() |
1053 | 1054 |
|
1054 | 1055 | @staticmethod |
| 1056 | + @pytest.mark.usefixtures("in_context") |
1055 | 1057 | @mock.patch("google.cloud.ndb._datastore_query.model") |
1056 | 1058 | def test_entity_full_entity(model): |
1057 | | - model._entity_from_protobuf.return_value = "bar" |
| 1059 | + key_pb = entity_pb2.Key( |
| 1060 | + partition_id=entity_pb2.PartitionId(project_id="testing"), |
| 1061 | + path=[entity_pb2.Key.PathElement(kind="ThisKind", id=42)], |
| 1062 | + ) |
| 1063 | + entity = mock.Mock(key=key_pb) |
| 1064 | + model._entity_from_protobuf.return_value = entity |
1058 | 1065 | result = _datastore_query._Result( |
1059 | 1066 | _datastore_query.RESULT_TYPE_FULL, |
1060 | | - mock.Mock(entity="foo", cursor=b"123", spec=("entity", "cursor")), |
| 1067 | + mock.Mock(entity=entity, cursor=b"123", spec=("entity", "cursor")), |
1061 | 1068 | ) |
1062 | 1069 |
|
1063 | | - assert result.entity() == "bar" |
1064 | | - model._entity_from_protobuf.assert_called_once_with("foo") |
| 1070 | + assert result.entity() is entity |
| 1071 | + model._entity_from_protobuf.assert_called_once_with(entity) |
| 1072 | + |
| 1073 | + @staticmethod |
| 1074 | + @pytest.mark.usefixtures("in_context") |
| 1075 | + @mock.patch("google.cloud.ndb._datastore_query.model") |
| 1076 | + def test_entity_full_entity_cached(model): |
| 1077 | + key = key_module.Key("ThisKind", 42) |
| 1078 | + key_pb = entity_pb2.Key( |
| 1079 | + partition_id=entity_pb2.PartitionId(project_id="testing"), |
| 1080 | + path=[entity_pb2.Key.PathElement(kind="ThisKind", id=42)], |
| 1081 | + ) |
| 1082 | + entity = mock.Mock(key=key_pb) |
| 1083 | + cached_entity = mock.Mock(key=key_pb, _key=key) |
| 1084 | + context = context_module.get_context() |
| 1085 | + context.cache.data[key] = cached_entity |
| 1086 | + model._entity_from_protobuf.return_value = entity |
| 1087 | + result = _datastore_query._Result( |
| 1088 | + _datastore_query.RESULT_TYPE_FULL, |
| 1089 | + mock.Mock(entity=entity, cursor=b"123", spec=("entity", "cursor")), |
| 1090 | + ) |
| 1091 | + |
| 1092 | + assert result.entity() is not entity |
| 1093 | + assert result.entity() is cached_entity |
| 1094 | + |
| 1095 | + @staticmethod |
| 1096 | + @pytest.mark.usefixtures("in_context") |
| 1097 | + @mock.patch("google.cloud.ndb._datastore_query.model") |
| 1098 | + def test_entity_full_entity_no_cache(model): |
| 1099 | + context = context_module.get_context() |
| 1100 | + with context.new(cache_policy=False).use(): |
| 1101 | + key_pb = entity_pb2.Key( |
| 1102 | + partition_id=entity_pb2.PartitionId(project_id="testing"), |
| 1103 | + path=[entity_pb2.Key.PathElement(kind="ThisKind", id=42)], |
| 1104 | + ) |
| 1105 | + entity = mock.Mock(key=key_pb) |
| 1106 | + model._entity_from_protobuf.return_value = entity |
| 1107 | + result = _datastore_query._Result( |
| 1108 | + _datastore_query.RESULT_TYPE_FULL, |
| 1109 | + mock.Mock( |
| 1110 | + entity=entity, cursor=b"123", spec=("entity", "cursor") |
| 1111 | + ), |
| 1112 | + ) |
| 1113 | + assert result.entity() is entity |
1065 | 1114 |
|
1066 | 1115 | @staticmethod |
1067 | 1116 | @pytest.mark.usefixtures("in_context") |
|
0 commit comments