How to simulate file upload to blobstore using GAE dev server testbed for python

血红的双手。 提交于 2020-01-13 16:47:26

问题


Id like to write some unit tests that among other thing will need to read a blobstore file

How to write a unit test setUp that puts some file in testbed blobstore so it will availabe for read this way:

blob_info = BlobInfo(blob_key)
reader = BlobReader(blob_info)
reader.readline()

EDIT:

I do not look for a way to test files API, I want to put some arbitrary data in the testbed blobstore storage dusring the test case setUp phase, so I can run tests against this data.


回答1:


You can add the following to your setUp method, and perhaps store the blob_key as self.blob_key for later use. The init_files_stub is important as it initializes the file service with the memory blobstore.

self.testbed.init_blobstore_stub()
self.testbed.init_files_stub()
from google.appengine.api import files
file_name = files.blobstore.create(mime_type='application/octet-stream')
with files.open(file_name, 'a') as f:
    f.write('blobdata')
files.finalize(file_name)
blob_key = files.blobstore.get_blob_key(file_name)

Note that testbed refers to from google.appengine.ext import testbed and self.testbed is the testbed instance.

With init_files_stub, this is exactly as described in docs:



来源:https://stackoverflow.com/questions/10870968/how-to-simulate-file-upload-to-blobstore-using-gae-dev-server-testbed-for-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!