def test_entity_unsupported_result_type(model): model._entity_from_protobuf.return_value = "bar" result = _datastore_query._Result( "foo", mock.Mock(entity="foo", cursor=b"123", spec=("entity", "cursor")), ) with pytest.raises(NotImplementedError): result.entity()
def test_entity_full_entity(model): model._entity_from_protobuf.return_value = "bar" result = _datastore_query._Result( _datastore_query.RESULT_TYPE_FULL, mock.Mock(entity="foo", cursor=b"123", spec=("entity", "cursor")), ) assert result.entity() == "bar" model._entity_from_protobuf.assert_called_once_with("foo")
def test_entity_key_only(): key_pb = entity_pb2.Key( partition_id=entity_pb2.PartitionId(project_id="testing"), path=[entity_pb2.Key.PathElement(kind="ThisKind", id=42)], ) result = _datastore_query._Result( _datastore_query.RESULT_TYPE_KEY_ONLY, mock.Mock( entity=mock.Mock(key=key_pb, spec=("key",)), cursor=b"123", spec=("entity", "cursor"), ), ) assert result.entity() == key_module.Key("ThisKind", 42)
def result(foo, bar=0, baz=""): return _datastore_query._Result( result_type=None, result_pb=query_pb2.EntityResult(entity=entity_pb2.Entity( properties={ "foo": entity_pb2.Value(string_value=foo), "bar": entity_pb2.Value(integer_value=bar), "baz": entity_pb2.Value(string_value=baz), })), order_by=[ query_module.PropertyOrder("foo"), query_module.PropertyOrder("bar", reverse=True), ], )
def test_entity_full_entity(model): key_pb = entity_pb2.Key( partition_id=entity_pb2.PartitionId(project_id="testing"), path=[entity_pb2.Key.PathElement(kind="ThisKind", id=42)], ) entity = mock.Mock(key=key_pb) model._entity_from_protobuf.return_value = entity result = _datastore_query._Result( _datastore_query.RESULT_TYPE_FULL, mock.Mock(entity=entity, cursor=b"123", spec=("entity", "cursor")), ) assert result.entity() is entity model._entity_from_protobuf.assert_called_once_with(entity)
def test_entity_full_entity_no_cache(model): context = context_module.get_context() with context.new(cache_policy=False).use(): key_pb = entity_pb2.Key( partition_id=entity_pb2.PartitionId(project_id="testing"), path=[entity_pb2.Key.PathElement(kind="ThisKind", id=42)], ) entity = mock.Mock(key=key_pb) model._entity_from_protobuf.return_value = entity result = _datastore_query._Result( _datastore_query.RESULT_TYPE_FULL, mock.Mock(entity=entity, cursor=b"123", spec=("entity", "cursor")), ) assert result.entity() is entity
def test_entity_projection(model): entity = mock.Mock(spec=("_set_projection",)) entity_pb = mock.Mock( properties={"a": 0, "b": 1}, spec=("properties",) ) model._entity_from_protobuf.return_value = entity result = _datastore_query._Result( _datastore_query.RESULT_TYPE_PROJECTION, mock.Mock( entity=entity_pb, cursor=b"123", spec=("entity", "cursor") ), ) assert result.entity() is entity model._entity_from_protobuf.assert_called_once_with(entity_pb) projection = entity._set_projection.call_args[0][0] assert sorted(projection) == ["a", "b"] entity._set_projection.assert_called_once_with(projection)
def test_entity_full_entity_cached(model): key = key_module.Key("ThisKind", 42) key_pb = entity_pb2.Key( partition_id=entity_pb2.PartitionId(project_id="testing"), path=[entity_pb2.Key.PathElement(kind="ThisKind", id=42)], ) entity = mock.Mock(key=key_pb) cached_entity = mock.Mock(key=key_pb, _key=key) context = context_module.get_context() context.cache[key] = cached_entity model._entity_from_protobuf.return_value = entity result = _datastore_query._Result( _datastore_query.RESULT_TYPE_FULL, mock.Mock(entity=entity, cursor=b"123", spec=("entity", "cursor")), ) assert result.entity() is not entity assert result.entity() is cached_entity
def test__compare_no_order_by(): result = _datastore_query._Result( None, mock.Mock(cursor=b"123", spec=("cursor",)) ) with pytest.raises(NotImplementedError): result._compare("other")