如何使用GAE dev服务器testbed for python模拟文件上传到blobstore [英] How to simulate file upload to blobstore using GAE dev server testbed for python

查看:114
本文介绍了如何使用GAE dev服务器testbed for python模拟文件上传到blobstore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢编写一些单元测试,除其他外,还需要读取一个blobstore文件



如何编写一个单元测试 setUp 测试平台 blobstore,以便它可以通过以下方式阅读:



pre $ blob_info = blob_info = blob_key
reader = BlobReader(blob_info)
reader.readline()

编辑

不寻找测试文件API 的方法,我想在测试用例blobstore存储中放置一些任意数据,以避免测试用例setUp阶段,所以我可以针对这些数据运行测试。

解决方案

您可以将以下内容添加到您的setUp方法中,并可能存储 blob_key 作为 self.blob_key 供以后使用。 init_files_stub非常重要,因为它使用内存blobstore初始化文件服务。

  self.testbed.init_blobstore_stub()
self.testbed.init_files_stub()
from google.appengine.api导入文件
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)

请注意,testbed引用来自google.appengine.ext的 import testbed self.testbed 是测试平台实例。



使用init_files_stub,这与文件


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.

解决方案

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:

这篇关于如何使用GAE dev服务器testbed for python模拟文件上传到blobstore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆