gpt4 book ai didi

unit-testing - 使用 Google App Engine `files` api 测试功能

转载 作者:太空宇宙 更新时间:2023-11-03 15:18:43 25 4
gpt4 key购买 nike

我有一个使用 Google Blobstore API 的函数,这里是一个退化的案例:

#!/usr/bin/python
from google.appengine.ext import testbed

def foo():
from google.appengine.api import files
blob_filename = files.blobstore.create(mime_type='text/plain')
with files.open(blob_filename, 'a') as googfile:
googfile.write("Test data")

files.finalize(blob_filename)

tb = testbed.Testbed()
tb.activate()
tb.init_blobstore_stub()

foo() # in reality, I'm a function called from a 'faux client'
# in a unittest testcase.

产生的错误是:

Traceback (most recent call last):  File "e.py", line 18, in     foo() # in reality, I'm a function called from a 'faux client'  File "e.py", line 8, in foo    blob_filename = files.blobstore.create(mime_type='text/plain')  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/blobstore.py", line 68, in create    return files._create(_BLOBSTORE_FILESYSTEM, params=params)  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file.py", line 491, in _create    _make_call('Create', request, response)  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file.py", line 230, in _make_call    rpc = _create_rpc(deadline=deadline)  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file.py", line 213, in _create_rpc    return apiproxy_stub_map.UserRPC('file', deadline)  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 393, in __init__    self.__rpc = CreateRPC(service, stubmap)  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 67, in CreateRPC    assert stub, 'No api proxy found for service "%s"' % serviceAssertionError: No api proxy found for service "file"

我不想必须修改 foo 才能对其进行测试。有没有办法让 foo 在 Google App Engine 的单元测试中按预期工作(即创建给定的文件)?

我希望能够用 Google's API Proxy 做到这一点,但我对它的理解还不够深入,无法自行解决。

非常感谢您的想法和建议。

感谢阅读。

最佳答案

似乎 testbed.init_blobstore_stub() 已经过时了,因为 dev_appserver 以不同的方式初始化 blobstore stub 。这是我对 init_blobstore_stub 的实现,它允许您在测试中写入和读取 blobstore。

from google.appengine.ext import testbed
from google.appengine.api.blobstore import blobstore_stub, file_blob_storage
from google.appengine.api.files import file_service_stub

class TestbedWithFiles(testbed.Testbed):

def init_blobstore_stub(self):
blob_storage = file_blob_storage.FileBlobStorage('/tmp/testbed.blobstore',
testbed.DEFAULT_APP_ID)
blob_stub = blobstore_stub.BlobstoreServiceStub(blob_storage)
file_stub = file_service_stub.FileServiceStub(blob_storage)
self._register_stub('blobstore', blob_stub)
self._register_stub('file', file_stub)

# Your code...
def foo():
from google.appengine.api import files
blob_filename = files.blobstore.create(mime_type='text/plain')
with files.open(blob_filename, 'a') as googfile:
googfile.write("Test data")

files.finalize(blob_filename)

tb = TestbedWithFiles()
tb.activate()
tb.init_blobstore_stub()

foo()

关于unit-testing - 使用 Google App Engine `files` api 测试功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8130063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com