如何在GAE上使用Google云端存储的模型上运行本地单元测试(python) [英] How can I run local unit tests on models that use Google Cloud Storage on GAE (python)

查看:119
本文介绍了如何在GAE上使用Google云端存储的模型上运行本地单元测试(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为调用存储在谷歌云存储上的文件的模型编写单元测试,但是我没有找到关于如何模拟单元测试的GCS服务的示例。



似乎应该有一个存根服务我可以使用,并在

  import peewee 
from google.appengine.api import app_identity
import cloudstorage

类示例(模型):
uuid = peewee.CharField(default = uuid4)
some_property = peewee.CharField()

@属性
def raw_file_one(self):
bucket = app_identity.get_default_gcs_bucket_name()
filename ='/{0}/repo_one/{1}'.format(bucket,self.uuid)
with cloudstorage.open(filename,'r')as f:
return f.read()

def store_raw_file(self,raw_file_one):
bucket = app_identity.get_default_gcs_bucket_name()

filename =' /{0}/stat_doms/{1}'.format(bucket,self.uuid)
with cloudstorage.open(filename,'w')as f:
f.write(raw_file_one)

我将构建测试用例:

  import unittest 

from google.appengine.ext import testbed

class TestCase(unittest.TestCase):
def run( (),$ args,** kwargs)
self.stub_google_services()
result = super(TestCase,self).run(* args,** kwargs)
self.unstub_google_services()
返回结果

def stub_google_services(self) :
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_all_stubs()
$ b $ def unstub_google_services(self):
self.testbed.deactivate()

进入如下模块测试:

$从application.tests.testcase导入TestCase
从application.models导入示例

class ExampleTest(TestCase) :
def test_store_raw_file(self):
...
[assert something]

我假设我会做一些像 blobstore = self.testbed.get_stub('blobstore')来创建一个服务,我可以对其执行测试(例如 blobstore.CreateBlob(blob_key,image)) - 但我在测试平台的参考文档中看不到GCS的服务。



关于如何使用GCS实现单元测试的想法?

解决方案

我想你正在寻找:

  from google.appengine.ext.cloudstorage import cloudstorage_stub $ b $ from google.appengine.api.blobstore import blobstore_stub 

和:

  blob_stub = blobstore_stub.BlobstoreServiceStub(blob_storage)
storage_stub = cloudstorage_stub.CloudStorageStub(blob_storage)

testbed._register_stub('blobstore',self.blob_stub)
testbed._register_stub( cloudstorage,self.storage_stub)


I'm trying to write unit tests for a model that calls on files stored on google cloud storage, but I haven't found any examples on how to simulate the GCS service for unit testing.

It seems that there should be a stub service I can use, and I see some references to gcs within the testbed docs described there, but haven't nailed down an example using it I can work off of.

Here's a condensed/example version of the model I have:

import peewee
from google.appengine.api import app_identity
import cloudstorage

class Example(Model):
    uuid = peewee.CharField(default=uuid4)
    some_property = peewee.CharField()

    @property
    def raw_file_one(self):
        bucket = app_identity.get_default_gcs_bucket_name()
        filename = '/{0}/repo_one/{1}'.format(bucket, self.uuid)
        with cloudstorage.open(filename, 'r') as f:
            return f.read()

    def store_raw_file(self, raw_file_one):
        bucket = app_identity.get_default_gcs_bucket_name()

        filename = '/{0}/stat_doms/{1}'.format(bucket, self.uuid)
        with cloudstorage.open(filename, 'w') as f:
            f.write(raw_file_one)

I'll build test cases with:

import unittest

from google.appengine.ext import testbed    

class TestCase(unittest.TestCase):
    def run(self, *args, **kwargs):
        self.stub_google_services()
        result = super(TestCase, self).run(*args, **kwargs)
        self.unstub_google_services()
        return result

    def stub_google_services(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_all_stubs()

    def unstub_google_services(self):
        self.testbed.deactivate()

Into module tests like:

from application.tests.testcase import TestCase
from application.models import Example

class ExampleTest(TestCase):
    def test_store_raw_file(self):
    ...
    [assert something]

I presume I'd do something like blobstore = self.testbed.get_stub('blobstore') to create a service I can perform tests on (e.g. blobstore.CreateBlob(blob_key, image)) -- but I don't see a service for GCS in the testbed ref docs.

Thoughts on how to implement unit tests with GCS?

解决方案

I think you're looking for:

from google.appengine.ext.cloudstorage import cloudstorage_stub
from google.appengine.api.blobstore import blobstore_stub

and:

blob_stub = blobstore_stub.BlobstoreServiceStub(blob_storage)
storage_stub = cloudstorage_stub.CloudStorageStub(blob_storage)

testbed._register_stub('blobstore', self.blob_stub)
testbed._register_stub("cloudstorage", self.storage_stub)

这篇关于如何在GAE上使用Google云端存储的模型上运行本地单元测试(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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