I’m trying to test an application that runs on App Engine. I’m using the Testbed framework, and so far it works like a charm, except for the following unexpected behaviour:
A test like this would work just fine (simplified version without a framework):
from google.appengine.ext import db, testbed
testbed = testbed.Testbed()
testbed.activate()
testbed.init_datastore_v3_stub()
class Foo(db.Model):
pass
# now for the tests:
key = Foo().put()
assert key == db.Key.from_path('Foo', key.id())
assert Foo.all(keys_only=True).get() == db.Key.from_path('Foo', key.id())
assert db.get(db.Key.from_path('Foo', key.id())) # fails!
testbed.deactivate()
However, the following would fail (again, simplified version):
from google.appengine.ext import db, testbed
testbed = testbed.Testbed()
testbed.activate()
testbed.init_datastore_v3_stub()
from myapp.models import Foo
# now for the tests:
key = Foo().put()
assert key == db.Key.from_path('Foo', key.id()) # fails!
assert Foo.all(keys_only=True).get() == db.Key.from_path('Foo', key.id()) # fails!
assert db.get(db.Key.from_path('Foo', key.id())) # fails!
# however, the following will succeed:
assert key == db.Key.from_path('Model', key.id())
assert Foo.all(keys_only=True).get() == db.Key.from_path('Model', key.id())
assert db.get(key)
testbed.deactivate()
Where does the model name disappear during the tests? And how come it only happens with imported modules?
EDIT:
Thanks proppy, typos fixed.
Nick Johnson, let me try to explain it better.
When I query a result from the testbed datastore stub, I get an entity, just like I’d expect. But when I call the .key() method on that entity, I get things like datastore_types.Key.from_path(u'Model', 1L, _app=u'testbed-test'), while Model is clearly not the correct kind of my entity.
When I try to get the entity for that key (datastore_types.Key.from_path(u'Model', 1L, _app=u'testbed-test')), it works just fine.
The problem is, when I only know the id of a datastore entity, and I try to construct the key manually, using db.Key.from_path(...).
For example, the kind should be User, so I can construct the key using db.Key.from_path('User', 1). But I cannot get an entity from the datastore using that key. I can, however, using db.Key.from_path('Model', 1), but, as I said, Model is not the correct datastore kind.
In other words:
from myapp.models import User
User(email='dont@write.us').kind() # returns 'Model', not 'User'!
Note that this unexpected behaviour does not occur in production or in the dev server, only when using testbed, and only when the db.Model subclass is defined in my application code (i.e. not in the test cases themselves).
I am not using Django, I’m using Pyramid with traversal, but the unit tests I’m running here do not call any framework-specific code.
Note that I haven’t tried this with the same thing happens with ndb yetndb as well..
Edit 2:
Apparently I haven’t noticed that all of my classes were subclasses of a google.appengine.ext.db.polymodel.PolyModel subclass, so the keys were in fact correctly set as Model, because the kind is Model on all PolyModel subclasses.
To answer my own question, this problem was caused by a file called
migrations.pythat I had in my project, which redefined my models using classes that had different properties. When I was running the tests,nosetried to import all available files to post coverage results, which caused the module definition clash, and thus all the confusion.