简体   繁体   English

Python 断言 NDB 项目相等

[英]Python Assert NDB Items equal

I'm writing a unit test for a project that checks ndb entities from the database.我正在为从数据库中检查 ndb 实体的项目编写单元测试。

The following test下面的测试

def test_active_chains(self):
    chains = self.user.active_chains()
    self.maxDiff = None
    self.assertItemsEqual(self.convert(self.chains), chains)

Produces the following FAIL产生以下 FAIL

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)

One can see that the elements being compared in the assertion are the exact same, so why does the assertion not recognise the elements as equal?可以看到断言中被比较的元素完全相同,那么为什么断言不承认元素相等呢?

I think it has something to do with the items being fetched from the database and thus stored in different memory locations.我认为这与从数据库中获取并因此存储在不同内存位置的项目有关。 Does assertItemsEqual require the same memory location of the models it is comparing? assertItemsEqual 是否需要与它比较的模型相同的内存位置?

Note:笔记:

I have not included any of the methods of my code (ie convert() or active_chains()) as I don't think the issue lies in here.我没有包含我的代码的任何方法(即 convert() 或 active_chains()),因为我认为问题不在这里。 I can include if required.如果需要,我可以包括在内。

Alternate Solution :替代解决方案

I found a work around by creating the following function我通过创建以下函数找到了解决方法

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

and replacing my unit test with并将我的单元测试替换为

def test_active_chains(self):
    chains = self.user.active_chains()
    self.maxDiff = None
    self.checkAssertItems(self.convert(self.chains), chains)

I am curious to know why I can not compare ndb.Models directly.我很想知道为什么我不能直接比较 ndb.Models。

I found a work around by creating the following function我通过创建以下函数找到了解决方法

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

and replacing my unit test with并将我的单元测试替换为

def test_active_chains(self):
    chains = self.user.active_chains()
    self.maxDiff = None
    self.checkAssertItems(self.convert(self.chains), chains)

I am curious to know why I can not compare ndb.Models directly.我很想知道为什么我不能直接比较 ndb.Models。

You can use self.assertEqual(a, b) to compare two ndb.Model instances, it just works.您可以使用self.assertEqual(a, b)来比较两个ndb.Model实例,它只是有效。

You can also use self.assertEqual(a, b) to compare two lists.您还可以使用self.assertEqual(a, b)来比较两个列表。 Comparison will be element-wise.比较将是按元素进行的。 So if you have two lists containing ndb.Model in the same order, they will compare equal.因此,如果您有两个以相同顺序包含ndb.Model列表,它们将比较相等。

self.assertItemsEqual(a, b) sorts the two lists a , b and then compares them; self.assertItemsEqual(a, b)对两个列表a , b进行排序,然后比较它们; Unless you define a sort order, the sort order will fall back to order by memory location.除非您定义排序顺序,否则排序顺序将回退到按内存位置排序。 Which is probably not what you want.这可能不是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM