2012-01-17 148 views
1

我試圖測試在App Engine上運行的應用程序。我使用the Testbed framework,到目前爲止,它就像一個魅力,除了以下異常行爲:Testbed中的數據存儲區密鑰不正確

像這樣的測試將工作就好(沒有框架的簡化版本):

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() 

然而,下面將失敗(再次,簡化版本):

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() 

哪裏型號名稱在測試過程中消失?以及它如何纔會發生在導入模塊?

編輯:

由於proppy,固定錯別字。

Nick Johnson,讓我試着更好地解釋它。

當我查詢testbed數據存根存根的結果時,我得到一個實體,就像我所期望的那樣。但是當我在該實體上調用.key()方法時,我得到的東西是datastore_types.Key.from_path(u'Model', 1L, _app=u'testbed-test'),而Model顯然不是我實體的正確類型。

當我嘗試獲取該密鑰的實體(datastore_types.Key.from_path(u'Model', 1L, _app=u'testbed-test'))時,它工作得很好。

問題是,當我只知道數據存儲實體的id,我嘗試使用db.Key.from_path(...)手動構建密鑰。

例如,種類應該是User,所以我可以使用db.Key.from_path('User', 1)構造密鑰。但是我無法使用該密鑰從數據存儲中獲取實體。但是,我可以使用db.Key.from_path('Model', 1),但正如我所說的,Model不是正確的數據存儲類型。

換句話說:

from myapp.models import User 
User(email='[email protected]').kind() # returns 'Model', not 'User'! 

注意,這個意外的行爲不使用testbed只有當db.Model子類在我的應用程序代碼被定義發生在生產或開發服務器,並且只(即不在測試案例本身)。

我沒有使用Django,我使用遍歷的Pyramid,但是我在這裏運行的單元測試不會調用任何特定於框架的代碼。

注意 我還沒有 ndb同樣的事情發生了ndb也試過這種..

編輯2:

顯然我沒有注意到,我的全部類是google.appengine.ext.db.polymodel.PolyModel子類的子類,所以實際上將鍵正確設置爲Model,因爲所有PolyModel子類中的種類均爲Model

+1

我相信,在你的代碼爲S/Triu /真/ g的錯字 – proppy 2012-01-17 01:07:40

+0

那麼,什麼是'key'在這種情況下?沒有任何附加信息很難診斷。你在用Django嗎? – 2012-01-17 03:11:59

+0

@NickJohnson,查看更新。 – 2012-01-17 12:39:20

回答

0

要回答我的問題,這個問題是由一個名爲migrations.py的文件,我在我的項目,該項目重新使用具有不同屬性的類我的模型引起的。當我運行測試時,nose嘗試導入所有可用文件以發佈覆蓋率結果,這導致模塊定義衝突,從而導致所有混淆。