示例#1
0
    def test_regression_10847(self):
        """
        Regression for #10847: the list of extra columns can always be
        accurately evaluated. Using an inner query ensures that as_sql() is
        producing correct output without requiring full evaluation and
        execution of the inner query.
        """
        obj = TestObject(first='first', second='second', third='third')
        obj.save()

        self.assertEqual(
            list(TestObject.objects.extra(select={
                'extra': 1
            }).values('pk')), [{
                'pk': obj.pk
            }])

        self.assertQuerysetEqual(
            TestObject.objects.filter(pk__in=TestObject.objects.extra(
                select={
                    'extra': 1
                }).values('pk')),
            ['<TestObject: TestObject: first,second,third>'])

        self.assertEqual(
            list(TestObject.objects.values('pk').extra(select={'extra': 1})),
            [{
                'pk': obj.pk
            }])

        self.assertQuerysetEqual(
            TestObject.objects.filter(
                pk__in=TestObject.objects.values('pk').extra(
                    select={'extra': 1})),
            ['<TestObject: TestObject: first,second,third>'])

        self.assertQuerysetEqual(
            TestObject.objects.filter(pk=obj.pk)
            | TestObject.objects.extra(where=["id > %s"], params=[obj.pk]),
            ['<TestObject: TestObject: first,second,third>'])
示例#2
0
文件: tests.py 项目: 0xmilk/appscale
    def test_regression_10847(self):
        """
        Regression for #10847: the list of extra columns can always be
        accurately evaluated. Using an inner query ensures that as_sql() is
        producing correct output without requiring full evaluation and
        execution of the inner query.
        """
        obj = TestObject(first='first', second='second', third='third')
        obj.save()

        self.assertEqual(
            list(TestObject.objects.extra(select={'extra': 1}).values('pk')),
            [{'pk': obj.pk}]
        )

        self.assertQuerysetEqual(
            TestObject.objects.filter(
                    pk__in=TestObject.objects.extra(select={'extra': 1}).values('pk')
            ),
            ['<TestObject: TestObject: first,second,third>']
        )

        self.assertEqual(
            list(TestObject.objects.values('pk').extra(select={'extra': 1})),
            [{'pk': obj.pk}]
        )

        self.assertQuerysetEqual(
            TestObject.objects.filter(
                 pk__in=TestObject.objects.values('pk').extra(select={'extra': 1})
            ),
            ['<TestObject: TestObject: first,second,third>']
        )

        self.assertQuerysetEqual(
            TestObject.objects.filter(pk=obj.pk) |
               TestObject.objects.extra(where=["id > %s"], params=[obj.pk]),
            ['<TestObject: TestObject: first,second,third>']
        )
示例#3
0
    def test_values_with_extra(self):
        """
        Regression test for #10256... If there is a values() clause, Extra
        columns are only returned if they are explicitly mentioned.
        """
        obj = TestObject(first='first', second='second', third='third')
        obj.save()

        self.assertEqual(
            list(
                TestObject.objects.extra(
                    select=SortedDict((('foo', 'first'), ('bar', 'second'),
                                       ('whiz', 'third')))).values()),
            [{
                'bar': u'second',
                'third': u'third',
                'second': u'second',
                'whiz': u'third',
                'foo': u'first',
                'id': obj.pk,
                'first': u'first'
            }])

        # Extra clauses after an empty values clause are still included
        self.assertEqual(
            list(TestObject.objects.values().extra(
                select=SortedDict((('foo', 'first'), ('bar', 'second'),
                                   ('whiz', 'third'))))), [{
                                       'bar': u'second',
                                       'third': u'third',
                                       'second': u'second',
                                       'whiz': u'third',
                                       'foo': u'first',
                                       'id': obj.pk,
                                       'first': u'first'
                                   }])

        # Extra columns are ignored if not mentioned in the values() clause
        self.assertEqual(
            list(
                TestObject.objects.extra(
                    select=SortedDict((('foo', 'first'), ('bar', 'second'),
                                       ('whiz',
                                        'third')))).values('first', 'second')),
            [{
                'second': u'second',
                'first': u'first'
            }])

        # Extra columns after a non-empty values() clause are ignored
        self.assertEqual(
            list(
                TestObject.objects.values('first', 'second').extra(
                    select=SortedDict((('foo', 'first'), ('bar', 'second'),
                                       ('whiz', 'third'))))),
            [{
                'second': u'second',
                'first': u'first'
            }])

        # Extra columns can be partially returned
        self.assertEqual(
            list(
                TestObject.objects.extra(
                    select=SortedDict((('foo', 'first'), ('bar', 'second'),
                                       ('whiz', 'third')))).values(
                                           'first', 'second', 'foo')),
            [{
                'second': u'second',
                'foo': u'first',
                'first': u'first'
            }])

        # Also works if only extra columns are included
        self.assertEqual(
            list(
                TestObject.objects.extra(
                    select=SortedDict((('foo', 'first'), ('bar', 'second'),
                                       ('whiz',
                                        'third')))).values('foo', 'whiz')),
            [{
                'foo': u'first',
                'whiz': u'third'
            }])

        # Values list works the same way
        # All columns are returned for an empty values_list()
        self.assertEqual(
            list(
                TestObject.objects.extra(
                    select=SortedDict((('foo', 'first'), ('bar', 'second'),
                                       ('whiz', 'third')))).values_list()),
            [(u'first', u'second', u'third', obj.pk, u'first', u'second',
              u'third')])

        # Extra columns after an empty values_list() are still included
        self.assertEqual(
            list(TestObject.objects.values_list().extra(
                select=SortedDict((('foo', 'first'), ('bar', 'second'),
                                   ('whiz', 'third'))))),
            [(u'first', u'second', u'third', obj.pk, u'first', u'second',
              u'third')])

        # Extra columns ignored completely if not mentioned in values_list()
        self.assertEqual(
            list(
                TestObject.objects.extra(
                    select=SortedDict((('foo', 'first'), ('bar', 'second'),
                                       ('whiz', 'third')))).values_list(
                                           'first', 'second')),
            [(u'first', u'second')])

        # Extra columns after a non-empty values_list() clause are ignored completely
        self.assertEqual(
            list(
                TestObject.objects.values_list('first', 'second').extra(
                    select=SortedDict((('foo', 'first'), ('bar', 'second'),
                                       ('whiz', 'third'))))),
            [(u'first', u'second')])

        self.assertEqual(
            list(
                TestObject.objects.extra(
                    select=SortedDict((('foo', 'first'), ('bar', 'second'),
                                       ('whiz',
                                        'third')))).values_list('second',
                                                                flat=True)),
            [u'second'])

        # Only the extra columns specified in the values_list() are returned
        self.assertEqual(
            list(
                TestObject.objects.extra(
                    select=SortedDict((('foo', 'first'), ('bar', 'second'),
                                       ('whiz', 'third')))).values_list(
                                           'first', 'second', 'whiz')),
            [(u'first', u'second', u'third')])

        # ...also works if only extra columns are included
        self.assertEqual(
            list(
                TestObject.objects.extra(
                    select=SortedDict((('foo', 'first'), ('bar', 'second'),
                                       ('whiz',
                                        'third')))).values_list('foo',
                                                                'whiz')),
            [(u'first', u'third')])

        self.assertEqual(
            list(
                TestObject.objects.extra(
                    select=SortedDict((('foo', 'first'), ('bar', 'second'),
                                       ('whiz',
                                        'third')))).values_list('whiz',
                                                                flat=True)),
            [u'third'])

        # ... and values are returned in the order they are specified
        self.assertEqual(
            list(
                TestObject.objects.extra(
                    select=SortedDict((('foo', 'first'), ('bar', 'second'),
                                       ('whiz',
                                        'third')))).values_list('whiz',
                                                                'foo')),
            [(u'third', u'first')])

        self.assertEqual(
            list(
                TestObject.objects.extra(
                    select=SortedDict((('foo', 'first'), ('bar', 'second'),
                                       ('whiz',
                                        'third')))).values_list('first',
                                                                'id')),
            [(u'first', obj.pk)])

        self.assertEqual(
            list(
                TestObject.objects.extra(
                    select=SortedDict((('foo', 'first'), ('bar', 'second'),
                                       ('whiz', 'third')))).values_list(
                                           'whiz', 'first', 'bar', 'id')),
            [(u'third', u'first', u'second', obj.pk)])
示例#4
0
文件: tests.py 项目: 0xmilk/appscale
    def test_values_with_extra(self):
        """
        Regression test for #10256... If there is a values() clause, Extra
        columns are only returned if they are explicitly mentioned.
        """
        obj = TestObject(first='first', second='second', third='third')
        obj.save()

        self.assertEqual(
            list(TestObject.objects.extra(select=SortedDict((('foo','first'), ('bar','second'), ('whiz','third')))).values()),
            [{'bar': u'second', 'third': u'third', 'second': u'second', 'whiz': u'third', 'foo': u'first', 'id': obj.pk, 'first': u'first'}]
        )

        # Extra clauses after an empty values clause are still included
        self.assertEqual(
            list(TestObject.objects.values().extra(select=SortedDict((('foo','first'), ('bar','second'), ('whiz','third'))))),
            [{'bar': u'second', 'third': u'third', 'second': u'second', 'whiz': u'third', 'foo': u'first', 'id': obj.pk, 'first': u'first'}]
        )

        # Extra columns are ignored if not mentioned in the values() clause
        self.assertEqual(
            list(TestObject.objects.extra(select=SortedDict((('foo','first'), ('bar','second'), ('whiz','third')))).values('first', 'second')),
            [{'second': u'second', 'first': u'first'}]
        )

        # Extra columns after a non-empty values() clause are ignored
        self.assertEqual(
            list(TestObject.objects.values('first', 'second').extra(select=SortedDict((('foo','first'), ('bar','second'), ('whiz','third'))))),
            [{'second': u'second', 'first': u'first'}]
        )

        # Extra columns can be partially returned
        self.assertEqual(
            list(TestObject.objects.extra(select=SortedDict((('foo','first'), ('bar','second'), ('whiz','third')))).values('first', 'second', 'foo')),
            [{'second': u'second', 'foo': u'first', 'first': u'first'}]
        )

        # Also works if only extra columns are included
        self.assertEqual(
            list(TestObject.objects.extra(select=SortedDict((('foo','first'), ('bar','second'), ('whiz','third')))).values('foo', 'whiz')),
            [{'foo': u'first', 'whiz': u'third'}]
        )

        # Values list works the same way
        # All columns are returned for an empty values_list()
        self.assertEqual(
            list(TestObject.objects.extra(select=SortedDict((('foo','first'), ('bar','second'), ('whiz','third')))).values_list()),
            [(u'first', u'second', u'third', obj.pk, u'first', u'second', u'third')]
        )

        # Extra columns after an empty values_list() are still included
        self.assertEqual(
            list(TestObject.objects.values_list().extra(select=SortedDict((('foo','first'), ('bar','second'), ('whiz','third'))))),
            [(u'first', u'second', u'third', obj.pk, u'first', u'second', u'third')]
        )

        # Extra columns ignored completely if not mentioned in values_list()
        self.assertEqual(
            list(TestObject.objects.extra(select=SortedDict((('foo','first'), ('bar','second'), ('whiz','third')))).values_list('first', 'second')),
            [(u'first', u'second')]
        )

        # Extra columns after a non-empty values_list() clause are ignored completely
        self.assertEqual(
            list(TestObject.objects.values_list('first', 'second').extra(select=SortedDict((('foo','first'), ('bar','second'), ('whiz','third'))))),
            [(u'first', u'second')]
        )

        self.assertEqual(
            list(TestObject.objects.extra(select=SortedDict((('foo','first'), ('bar','second'), ('whiz','third')))).values_list('second', flat=True)),
            [u'second']
        )

        # Only the extra columns specified in the values_list() are returned
        self.assertEqual(
            list(TestObject.objects.extra(select=SortedDict((('foo','first'), ('bar','second'), ('whiz','third')))).values_list('first', 'second', 'whiz')),
            [(u'first', u'second', u'third')]
        )

        # ...also works if only extra columns are included
        self.assertEqual(
            list(TestObject.objects.extra(select=SortedDict((('foo','first'), ('bar','second'), ('whiz','third')))).values_list('foo','whiz')),
            [(u'first', u'third')]
        )

        self.assertEqual(
            list(TestObject.objects.extra(select=SortedDict((('foo','first'), ('bar','second'), ('whiz','third')))).values_list('whiz', flat=True)),
            [u'third']
        )

        # ... and values are returned in the order they are specified
        self.assertEqual(
            list(TestObject.objects.extra(select=SortedDict((('foo','first'), ('bar','second'), ('whiz','third')))).values_list('whiz','foo')),
            [(u'third', u'first')]
        )

        self.assertEqual(
            list(TestObject.objects.extra(select=SortedDict((('foo','first'), ('bar','second'), ('whiz','third')))).values_list('first','id')),
            [(u'first', obj.pk)]
        )

        self.assertEqual(
            list(TestObject.objects.extra(select=SortedDict((('foo','first'), ('bar','second'), ('whiz','third')))).values_list('whiz', 'first', 'bar', 'id')),
            [(u'third', u'first', u'second', obj.pk)]
        )