【问题标题】:Django's assertQuerysetEqual() method failing despite the two query sets printing out the same in the shell?Django 的 assertQuerysetEqual() 方法失败,尽管两个查询集在 shell 中打印出相同的结果?
【发布时间】:2018-07-10 00:54:46
【问题描述】:

我有一个 Family 和一个 Session 具有一对多关系,因此实例 family 有一个 session_set。此外,Session 有一个 session_number 字段,它是一个整数。

我有两个 Familyfamily1family2 实例,因此如果我在 ipdb 调试器中打印出它们的 session_set.order_by('session_number'),我会发现它们看起来完全一样:

ipdb> family1.session_set.order_by('session_number')
<QuerySet [<Session: Welcome>, <Session: First-Time Parents: The Basics of Birth>, <Session: Initial Postpartum Lactation>, <Session: Sleep Techniques for New Babies>, <Session: Breastfeeding Preparation>, <Session: Newborn Care Basics>, <Session: Easing the Transition Back to Work>, <Session: Preparing for Parenting>, <Session: Decoding Baby Cues>, <Session: Postpartum Doula Support>, <Session: First-Time Parents: Birth Prep Q&A>, <Session: Postpartum Lactation Follow-Up>, <Session: Sleep Training for 4 Months & Beyond>, <Session: Mental Wellness in Pregnancy>, <Session: Infant CPR>, <Session: Prenatal Pelvic Physical Therapy>, <Session: Prenatal Massage>]>
ipdb> family2.session_set.order_by('session_number')
<QuerySet [<Session: Welcome>, <Session: First-Time Parents: The Basics of Birth>, <Session: Initial Postpartum Lactation>, <Session: Sleep Techniques for New Babies>, <Session: Breastfeeding Preparation>, <Session: Newborn Care Basics>, <Session: Easing the Transition Back to Work>, <Session: Preparing for Parenting>, <Session: Decoding Baby Cues>, <Session: Postpartum Doula Support>, <Session: First-Time Parents: Birth Prep Q&A>, <Session: Postpartum Lactation Follow-Up>, <Session: Sleep Training for 4 Months & Beyond>, <Session: Mental Wellness in Pregnancy>, <Session: Infant CPR>, <Session: Prenatal Pelvic Physical Therapy>, <Session: Prenatal Massage>]>

但是,如果我将这些查询集输入到assertQuerysetEqual,我得到一个测试失败:

ipdb> self.assertQuerysetEqual(family1.session_set.order_by('session_number'), family2.session_set.order_by('session_number'))
*** AssertionError: Lists differ: ['<Session: Welcome>', '<Session: First-Ti[642 chars]ge>'] != [<Session: Welcome>, <Session: First-Time [608 chars]age>]

First differing element 0:
'<Session: Welcome>'
<Session: Welcome>

Diff is 2186 characters long. Set self.maxDiff to None to see it.

这里是maxDiff设置为None的更详细比较:

ipdb> self.maxDiff = None
ipdb> self.assertQuerysetEqual(family1.session_set.order_by('session_number'), family2.session_set.order_by('session_number'))
*** AssertionError: Lists differ: ['<Session: Welcome>', '<Session: First-Ti[642 chars]ge>'] != [<Session: Welcome>, <Session: First-Time [608 chars]age>]

First differing element 0:
'<Session: Welcome>'
<Session: Welcome>

- ['<Session: Welcome>',
?  -                  -

+ [<Session: Welcome>,
-  '<Session: First-Time Parents: The Basics of Birth>',
?  -                                                  -

+  <Session: First-Time Parents: The Basics of Birth>,
-  '<Session: Initial Postpartum Lactation>',
?  -                                       -

+  <Session: Initial Postpartum Lactation>,
-  '<Session: Sleep Techniques for New Babies>',
?  -                                          -

+  <Session: Sleep Techniques for New Babies>,
-  '<Session: Breastfeeding Preparation>',
?  -                                    -

+  <Session: Breastfeeding Preparation>,
-  '<Session: Newborn Care Basics>',
?  -                              -

+  <Session: Newborn Care Basics>,
-  '<Session: Easing the Transition Back to Work>',
?  -                                             -

+  <Session: Easing the Transition Back to Work>,
-  '<Session: Preparing for Parenting>',
?  -                                  -

+  <Session: Preparing for Parenting>,
-  '<Session: Decoding Baby Cues>',
?  -                             -

+  <Session: Decoding Baby Cues>,
-  '<Session: Postpartum Doula Support>',
?  -                                   -

+  <Session: Postpartum Doula Support>,
-  '<Session: First-Time Parents: Birth Prep Q&A>',
?  -                                             -

+  <Session: First-Time Parents: Birth Prep Q&A>,
-  '<Session: Postpartum Lactation Follow-Up>',
?  -                                         -

+  <Session: Postpartum Lactation Follow-Up>,
-  '<Session: Sleep Training for 4 Months & Beyond>',
?  -                                               -

+  <Session: Sleep Training for 4 Months & Beyond>,
-  '<Session: Mental Wellness in Pregnancy>',
?  -                                       -

+  <Session: Mental Wellness in Pregnancy>,
-  '<Session: Infant CPR>',
?  -                     -

+  <Session: Infant CPR>,
-  '<Session: Prenatal Pelvic Physical Therapy>',
?  -                                           -

+  <Session: Prenatal Pelvic Physical Therapy>,
-  '<Session: Prenatal Massage>']
?  -                           -

+  <Session: Prenatal Massage>]

似乎比较“不同步”。不过session_number也是一样的:

ipdb> [(session.session_number, str(session.session_type)) for session in family1.session_set.order_by('session_number')]
[(0, 'Welcome'), (1, 'First-Time Parents: The Basics of Birth'), (2, 'Initial Postpartum Lactation'), (3, 'Sleep Techniques for New Babies'), (4, 'Breastfeeding Preparation'), (5, 'Newborn Care Basics'), (6, 'Easing the Transition Back to Work'), (7, 'Preparing for Parenting'), (8, 'Decoding Baby Cues'), (9, 'Postpartum Doula Support'), (10, 'First-Time Parents: Birth Prep Q&A'), (11, 'Postpartum Lactation Follow-Up'), (12, 'Sleep Training for 4 Months & Beyond'), (13, 'Mental Wellness in Pregnancy'), (14, 'Infant CPR'), (15, 'Prenatal Pelvic Physical Therapy'), (16, 'Prenatal Massage')]
ipdb> [(session.session_number, str(session.session_type)) for session in family2.session_set.order_by('session_number')]
[(0, 'Welcome'), (1, 'First-Time Parents: The Basics of Birth'), (2, 'Initial Postpartum Lactation'), (3, 'Sleep Techniques for New Babies'), (4, 'Breastfeeding Preparation'), (5, 'Newborn Care Basics'), (6, 'Easing the Transition Back to Work'), (7, 'Preparing for Parenting'), (8, 'Decoding Baby Cues'), (9, 'Postpartum Doula Support'), (10, 'First-Time Parents: Birth Prep Q&A'), (11, 'Postpartum Lactation Follow-Up'), (12, 'Sleep Training for 4 Months & Beyond'), (13, 'Mental Wellness in Pregnancy'), (14, 'Infant CPR'), (15, 'Prenatal Pelvic Physical Therapy'), (16, 'Prenatal Massage')]

为什么assertQuerysetEqual 在这里没有按预期工作?

【问题讨论】:

    标签: python django django-testing


    【解决方案1】:

    这对我有用:

    self.assertQuerysetEqual(qs1, map(repr, qs2), ordered=False)
    

    仅将第二个查询集转换为列表是不够的,因为引号仍然存在。对每个对象调用 repr 是使值匹配的原因。

    【讨论】:

      【解决方案2】:

      这是因为assertQuerysetEqual 不会将一个查询集与另一个查询集进行比较,而是将一个查询集与一个值列表进行比较。

      来自assertQuerysetEqual's documentation

      TransactionTestCase.assertQuerysetEqual(qs, values, transform=repr, ordered=True, msg=None)

      断言查询集 qs 返回一个 特定值列表values

      qsvalues 的内容的比较是使用 函数变换;默认情况下,这意味着每个的 repr() 比较值。

      所以你应该把第二个参数转换成一个列表:

      self.assertQuerysetEqual(family1.session_set.order_by('session_number'), list(family2.session_set.order_by('session_number')))
      

      【讨论】:

        猜你喜欢
        • 2016-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多