BadRequestError:app" testbed-test"无法访问应用" _"的数据

时间:2017-06-14 05:19:24

标签: app-engine-ndb python-unittest

这是我的python类

from app.commons.abstract.dal import ndbService
from google.appengine.ext import ndb
from app.conference.models.conference import ConferenceNDB, ConferenceMessageNDB


conference_key = ndb.Key('Conference', 'CFC Australia')


    class ConferenceService(ndbService):
        def convert_to_object(self, value):
            conf = ConferenceNDB(parent=conference_key)
            conf.name = value['name']
            conf.year = value['year']
            conf.description = value['description']
            return conf

        def write(self, value):
            conference = self.convert_to_object(value)
            return conference.put()

        def read(self, value):
            key = ndb.Key(urlsafe=value)
            conference = key.get()
            return conference

        def remove(self, value):
            pass

这是单元测试

  def test_conference_write(self):
        conf = {'name': 'xxxx',
                'year': 2017,
                'description': 'xxxx'}

        svc_conf = ConferenceService()
        key = svc_conf.write(conf)
        print(key)
        self.assertIsNotNone(key)

我的单元测试失败,出现以下错误。

Error
Traceback (most recent call last):
  File "C:\Python27\lib\unittest\case.py", line 329, in run
    testMethod()
  File "C:\Code\App-Engine\cfc-melbourne-website\tests\test_conference.py", line 40, in test_conference_write
    key = svc_conf.write(conf)
  File "C:\Code\App-Engine\cfc-melbourne-website\app\admin\services\conferenceservice.py", line 19, in write
    return conference.put()
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\model.py", line 3458, in _put
    return self._put_async(**ctx_options).get_result()
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\tasklets.py", line 383, in get_result
    self.check_success()
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\tasklets.py", line 427, in _help_tasklet_along
    value = gen.throw(exc.__class__, exc, tb)
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\context.py", line 824, in put
    key = yield self._put_batcher.add(entity, options)
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\tasklets.py", line 427, in _help_tasklet_along
    value = gen.throw(exc.__class__, exc, tb)
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\context.py", line 358, in _put_tasklet
    keys = yield self._conn.async_put(options, datastore_entities)
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\tasklets.py", line 513, in _on_rpc_completion
    result = rpc.get_result()
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 613, in get_result
    return self.__get_result_hook(self)
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\datastore\datastore_rpc.py", line 1879, in __put_hook
    self.check_rpc_success(rpc)
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\datastore\datastore_rpc.py", line 1371, in check_rpc_success
    raise _ToDatastoreError(err)
BadRequestError: app "testbed-test" cannot access app "_"'s data

-----------更新------- 2017年8月21日

使用我的测试库中的以下代码修复它

def setUp(self):
    self.testbed = testbed.Testbed()
    self.testbed.setup_env(
        app_id='_',
        overwrite=True
    )
    self.testbed.activate()
    self.testbed.init_datastore_v3_stub()
    self.testbed.init_memcache_stub()
    ndb.get_context().clear_cache()

1 个答案:

答案 0 :(得分:1)

键包括app_id。你可以通过key.app()来看到它。

默认的app_id是' _'。您的conference_key是在导入源文件时使用默认的app_id(' _')进行的。

之后,您将app_id设置为' testbed-test'你应用中的某个地方。然后所有密钥都包含app_id(' testbed-test'),并且您的密钥不匹配。

因此,在设置app_id后创建密钥。