Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python GAE-如何在测试床中使用blobstore存根?_Python_Google App Engine - Fatal编程技术网

Python GAE-如何在测试床中使用blobstore存根?

Python GAE-如何在测试床中使用blobstore存根?,python,google-app-engine,Python,Google App Engine,我的代码是这样的: self.testbed.init_blobstore_stub() upload_url = blobstore.create_upload_url('/image') upload_url = re.sub('^http://testbed\.example\.com', '', upload_url) response = self.testapp.post(upload_url, params={ 'shopid': id, 'description

我的代码是这样的:

self.testbed.init_blobstore_stub()
upload_url = blobstore.create_upload_url('/image')
upload_url = re.sub('^http://testbed\.example\.com', '', upload_url)

response = self.testapp.post(upload_url, params={
    'shopid': id,
    'description': 'JLo',
    }, upload_files=[('file', imgPath)])
self.assertEqual(response.status_int, 200)

为什么显示404错误?由于某些原因,上载路径似乎根本不存在。

您不能这样做。我认为问题在于webtest(我认为self.testapp就是从这里来的)不能很好地与测试床blobstore功能配合使用。你可以在这里找到一些信息

我的解决方案是重写
unittest.TestCase
,并添加以下方法:

def create_blob(self, contents, mime_type):
    "Since uploading blobs doesn't work in testing, create them this way."
    fn = files.blobstore.create(mime_type = mime_type,
                                _blobinfo_uploaded_filename = "foo.blt")
    with files.open(fn, 'a') as f:
        f.write(contents)
    files.finalize(fn)
    return files.blobstore.get_blob_key(fn)

def get_blob(self, key):
    return self.blobstore_stub.storage.OpenBlob(key).read()
您还需要解决方案

对于我通常对blobstore处理程序执行get或post的测试,我将调用上面两个方法之一。它有点粗糙,但很有效


我正在考虑的另一个解决方案是使用Selenium的HtmlUnit驱动程序。这需要dev服务器运行,但应该允许对blobstore和javascript进行全面测试(作为附带的好处)。

您不能这样做。我认为问题在于webtest(我认为self.testapp就是从这里来的)不能很好地与测试床blobstore功能配合使用。你可以在这里找到一些信息

我的解决方案是重写
unittest.TestCase
,并添加以下方法:

def create_blob(self, contents, mime_type):
    "Since uploading blobs doesn't work in testing, create them this way."
    fn = files.blobstore.create(mime_type = mime_type,
                                _blobinfo_uploaded_filename = "foo.blt")
    with files.open(fn, 'a') as f:
        f.write(contents)
    files.finalize(fn)
    return files.blobstore.get_blob_key(fn)

def get_blob(self, key):
    return self.blobstore_stub.storage.OpenBlob(key).read()
您还需要解决方案

对于我通常对blobstore处理程序执行get或post的测试,我将调用上面两个方法之一。它有点粗糙,但很有效


我正在考虑的另一个解决方案是使用Selenium的HtmlUnit驱动程序。这需要dev服务器运行,但应该允许对blobstore和javascript进行全面测试(作为一个附带好处)。

我认为Kekito是对的,您不能直接发布到upload\u url

但是如果您想测试BlobstoreUploadHandler,您可以通过以下方式伪造它通常从blobstore接收的POST请求。假设您的处理程序位于/handler:

    import email
    ...

    def test_upload(self):
        blob_key = 'abcd'
        # The blobstore upload handler receives a multipart form request
        # containing uploaded files. But instead of containing the actual
        # content, the files contain an 'email' message that has some meta
        # information about the file. They also contain a blob-key that is
        # the key to get the blob from the blobstore

        # see blobstore._get_upload_content
        m = email.message.Message()
        m.add_header('Content-Type', 'image/png')
        m.add_header('Content-Length', '100')
        m.add_header('X-AppEngine-Upload-Creation', '2014-03-02 23:04:05.123456')
        # This needs to be valie base64 encoded
        m.add_header('content-md5', 'd74682ee47c3fffd5dcd749f840fcdd4')
        payload = m.as_string()
        # The blob-key in the Content-type is important
        params = [('file', webtest.forms.Upload('test.png', payload,
                                                'image/png; blob-key='+blob_key))]

        self.testapp.post('/handler', params, content_type='blob-key')

我通过钻研blobstore代码找到了答案。重要的一点是blobstore发送给UploadHandler的POST请求不包含文件内容。相反,它包含一个“电子邮件消息”(嗯,像电子邮件一样编码的信息)以及关于文件的元数据(内容类型、内容长度、上载时间和md5)。它还包含一个blob密钥,可用于从blob存储中检索文件。

我认为Kekito是对的,您不能直接发布到上载url

但是如果您想测试BlobstoreUploadHandler,您可以通过以下方式伪造它通常从blobstore接收的POST请求。假设您的处理程序位于/handler:

    import email
    ...

    def test_upload(self):
        blob_key = 'abcd'
        # The blobstore upload handler receives a multipart form request
        # containing uploaded files. But instead of containing the actual
        # content, the files contain an 'email' message that has some meta
        # information about the file. They also contain a blob-key that is
        # the key to get the blob from the blobstore

        # see blobstore._get_upload_content
        m = email.message.Message()
        m.add_header('Content-Type', 'image/png')
        m.add_header('Content-Length', '100')
        m.add_header('X-AppEngine-Upload-Creation', '2014-03-02 23:04:05.123456')
        # This needs to be valie base64 encoded
        m.add_header('content-md5', 'd74682ee47c3fffd5dcd749f840fcdd4')
        payload = m.as_string()
        # The blob-key in the Content-type is important
        params = [('file', webtest.forms.Upload('test.png', payload,
                                                'image/png; blob-key='+blob_key))]

        self.testapp.post('/handler', params, content_type='blob-key')

我通过钻研blobstore代码找到了答案。重要的一点是blobstore发送给UploadHandler的POST请求不包含文件内容。相反,它包含一个“电子邮件消息”(嗯,像电子邮件一样编码的信息)以及关于文件的元数据(内容类型、内容长度、上载时间和md5)。它还包含一个blob密钥,可用于从blob存储中检索文件。

很抱歉不够清晰。我想做的是在“/image”测试post处理程序,如果我不能使用“upload\u url”,这是不可能的。我必须承认这个部件不能进行单元测试。@Khoi,请看我的第一句话。:)无法测试blobstore处理程序的操作。剩下的部分是为了给你提供解决这个问题的其他选择。很抱歉没有说清楚。我想做的是在“/image”测试post处理程序,如果我不能使用“upload\u url”,这是不可能的。我必须承认这个部件不能进行单元测试。@Khoi,请看我的第一句话。:)无法测试blobstore处理程序的操作。剩下的部分是为了给你提供解决这个问题的其他选择。