我正在为一个从数据库检查ndb实体的项目编写单元测试。
以下测试
def test_active_chains(self):
chains = self.user.active_chains()
self.maxDiff = None
self.assertItemsEqual(self.convert(self.chains), chains)
产生以下失败
AssertionError: Element counts were not equal:
First has 1, Second has 0: Chain(key=Key('Chain', 4), active=False, address_1=u'20 St.Saviourgate', address_2=None, chain_id=None, country=None, county=u'North Yorkshire', created=datetime.datetime(2018, 2, 7, 13, 31, 56, 197427), deleted=False, eagle_eye_campaign_id=None, eagle_eye_compaign_channel=None, eagle_eye_drinki_channel=None, eagle_eye_offer_channel=None, geo_location=None, name=u'TestChain1', number_of_venues=0, phone=None, post_code=u'YO1 8NN', test_group=False, town=u'York', users=[Key('User_v2', 2)], uses_codes=False)
First has 1, Second has 0: Chain(key=Key('Chain', 3), active=False, address_1=u'20 St.Saviourgate', address_2=None, chain_id=None, country=None, county=u'North Yorkshire', created=datetime.datetime(2018, 2, 7, 13, 31, 56, 197553), deleted=False, eagle_eye_campaign_id=None, eagle_eye_compaign_channel=None, eagle_eye_drinki_channel=None, eagle_eye_offer_channel=None, geo_location=None, name=u'TestChain2', number_of_venues=0, phone=None, post_code=u'YO1 8NN', test_group=False, town=u'York', users=[Key('User_v2', 2)], uses_codes=False)
First has 0, Second has 1: Chain(key=Key('Chain', 3), active=False, address_1=u'20 St.Saviourgate', address_2=None, chain_id=None, country=None, county=u'North Yorkshire', created=datetime.datetime(2018, 2, 7, 13, 31, 56, 197553), deleted=False, eagle_eye_campaign_id=None, eagle_eye_compaign_channel=None, eagle_eye_drinki_channel=None, eagle_eye_offer_channel=None, geo_location=None, name=u'TestChain2', number_of_venues=0, phone=None, post_code=u'YO1 8NN', test_group=False, town=u'York', users=[Key('User_v2', 2)], uses_codes=False)
First has 0, Second has 1: Chain(key=Key('Chain', 4), active=False, address_1=u'20 St.Saviourgate', address_2=None, chain_id=None, country=None, county=u'North Yorkshire', created=datetime.datetime(2018, 2, 7, 13, 31, 56, 197427), deleted=False, eagle_eye_campaign_id=None, eagle_eye_compaign_channel=None, eagle_eye_drinki_channel=None, eagle_eye_offer_channel=None, geo_location=None, name=u'TestChain1', number_of_venues=0, phone=None, post_code=u'YO1 8NN', test_group=False, town=u'York', users=[Key('User_v2', 2)], uses_codes=False)
可以看到断言中要比较的元素完全相同,那么为什么断言不能将元素识别为相等?
我认为这与从数据库中获取并存储在不同内存位置的项目有关。 assertItemsEqual是否需要与要比较的模型相同的内存位置?
注意:
我没有在代码中包含任何方法(即convert()或active_chains()),因为我认为问题不在这里。如果需要,我可以包括在内。
替代解决方案:
我通过创建以下函数找到了解决方法
def checkAssertItems(self, item1, item2):
res = self.assertEqual(len(item1), len(item2))
if res:
for i in range(0, len(item1)):
self.assertEqual(item1[i], item2[i])
并用替换我的单元测试
def test_active_chains(self):
chains = self.user.active_chains()
self.maxDiff = None
self.checkAssertItems(self.convert(self.chains), chains)
我很好奇我为什么不能直接比较ndb.Models。
最佳答案
我通过创建以下函数找到了解决方法
def checkAssertItems(self, item1, item2):
res = self.assertEqual(len(item1), len(item2))
if res:
for i in range(0, len(item1)):
self.assertEqual(item1[i], item2[i])
并用替换我的单元测试
def test_active_chains(self):
chains = self.user.active_chains()
self.maxDiff = None
self.checkAssertItems(self.convert(self.chains), chains)
我很好奇我为什么不能直接比较ndb.Models。
关于python - Python声明NDB项相等,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48665640/