我如何 运行 对使用 Google GAE 上的云存储的模型进行本地单元测试 (python)
How can I run local unit tests on models that use Google Cloud Storage on GAE (python)
我正在尝试为调用存储在 google 云存储中的文件的模型编写单元测试,但我还没有找到任何关于如何模拟 GCS 服务进行单元测试的示例。
那里似乎应该有一个 stub service I can use, and I see some references to gcs within the testbed docs 的描述,但我还没有确定使用它的示例。
这是我拥有的模型的 condensed/example 版本:
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)
我将构建测试用例:
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()
进入模块测试如:
from application.tests.testcase import TestCase
from application.models import Example
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
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)
我正在尝试为调用存储在 google 云存储中的文件的模型编写单元测试,但我还没有找到任何关于如何模拟 GCS 服务进行单元测试的示例。
那里似乎应该有一个 stub service I can use, and I see some references to gcs within the testbed docs 的描述,但我还没有确定使用它的示例。
这是我拥有的模型的 condensed/example 版本:
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)
我将构建测试用例:
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()
进入模块测试如:
from application.tests.testcase import TestCase
from application.models import Example
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
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)