1

我一直在一個錯誤,我沒有正確地檢查一個實體被放到數據存儲。它對事務和上下文緩存和內存緩存特別困惑。測試ndb實體被放到數據存儲區

考慮這個實體,其中每種方法的目的是爲了增加n和實體保存到數據存儲:

class MyEntity(ndb.Model): 
    n = ndb.IntegerProperty() 

    def inc(self): 
     self.n += 1 
     # self.put() # oops, forgot to put the entity 

    @staticmethod 
    @ndb.transactional 
    def inc_trans(key): 
     x = key.get() 
     x.n += 1 
     # x.put() # oops, forgot to put the entity 

這些單元測試:

def testInc(self): 
    x = MyEntity(n=0) 
    x.put() 
    x.inc() 
    x = x.key.get() 
    self.assertEqual(x.n, 1) 

def testIncTrans(self): 
    x = MyEntity(n=0) 
    x.put() 
    MyEntity.inc_trans(x.key) 
    x = x.key.get() 
    self.assertEqual(x.n, 1) 

我要補充這些測試以確保實體實際上已被保存到數據存儲中? (即,我們沒有檢查存儲在環境關聯的緩存或內存緩存?值)是否足夠來電來incinc_trans後添加

ndb.get_context().clear_cache() 

的單元測試?

回答

1

做了進一步的研究之後,我發現,我們可以強制get不使用任何緩存是這樣的:

x = x.key.get(use_cache=False, use_memcache=False) 

使用無緩存得到測試好像要走的路。

相關問題