1

I've wrote this simple code to test my model:

class NDBTestCase(unittest.TestCase):
    def setUp(self):
        logging.getLogger().setLevel(logging.INFO)
        # First, create an instance of the Testbed class.
        self.testbed = testbed.Testbed()
        # Then activate the testbed, which prepares the service stubs for use.
        self.testbed.activate()
        # Next, declare which service stubs you want to use.
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        self.owner = m_User(username="owner")
        self.owner.put()


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


    def testClub(self):
        # this id is a manually assigned
        club = m_Club(id="1", name="test", email="test@test.com", description="desc", url="example.com",
                      owners=[self.owner.key], training_type=["balance", "stability"], tags=["test", "trento"])
        club.put()

in the models the owners is like this owners = ndb.KeyProperty(kind="User", repeated=True)

if i run this code with unittest it works perfectly. I tried to run it with unitest and nosegae and it fails for a problem with the Key

Traceback (most recent call last):
  File "/Users/stefano/Documents/SW/gymcentral/tester_ndb.py", line 25, in setUp
    self.owner.put()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 3379, in _put
    return self._put_async(**ctx_options).get_result()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/tasklets.py", line 325, in get_result
    self.check_success()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/tasklets.py", line 368, in _help_tasklet_along
    value = gen.throw(exc.__class__, exc, tb)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/context.py", line 810, in put
    key = yield self._put_batcher.add(entity, options)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/tasklets.py", line 371, in _help_tasklet_along
    value = gen.send(val)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/context.py", line 350, in _put_tasklet
    ent._key = key
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 1363, in __set__
    self._set_value(entity, value)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 1513, in _set_value
    value = _validate_key(value, entity=entity)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 1481, in _validate_key
    raise datastore_errors.BadValueError('Expected Key, got %r' % value)
BadValueError: Expected Key, got Key('User', 1)

any idea why? i run the test from console with this command nosetests tester_ndb.py --with-gae

Dmytro Sadovnychyi
  • 6,171
  • 5
  • 33
  • 60
EsseTi
  • 4,079
  • 5
  • 36
  • 63

2 Answers2

1

I've gotten weird errors like this as well. I think there is a bug somewhere in Google's code. I got around it by tweaking my code to do the same thing in a slightly different way.

Try changing

self.owner.put()

to

ndb.put(self.owner)
new name
  • 15,861
  • 19
  • 68
  • 114
  • mmm interesting. i would avoid "hack" to make it working and i would like to understand why the normal code is not working. Plus, google has in the pipline to "test and deploy" test cases with nose, so i'm wondering how it can work if it fails to pass a simple test like this.. – EsseTi Oct 23 '14 at 08:22
  • what do you import to use `ndb.put`? i imported `from google.appengine.ext import ndb` but `AttributeError: 'module' object has no attribute 'put'` – EsseTi Oct 23 '14 at 08:24
  • I agree it is not an ideal solution (if it even works for you). I'm still using `db` and there is a `db.put()` there. I assumed it was that same for `ndb` but you'll have to check the docs to see if there is an alternative like that. – new name Oct 23 '14 at 12:40
  • 1
    the other answer solves the problem. check it out, it should work for you as well. – EsseTi Oct 23 '14 at 13:57
1

Could you try running nose with the flag --without-sandbox?

There seems to be an older issue on the former tracker about the same thing.

https://code.google.com/p/nose-gae/issues/detail?id=60

I recently took over maintaining NoseGAE so I will look into the root cause as well but I am assuming it is something internal to the App Engine SDK.

EDIT: --without-sandbox was removed in NoseGAE 0.4.0 when I migrated it to dev_appserver2

Josh J
  • 6,813
  • 3
  • 25
  • 47
  • 1
    just in case someone needs it `nosetests --logging-level=ERROR --with-gae --without-sandbox --no-path-adjustment --gae-application=.` – EsseTi Oct 23 '14 at 13:58