示例#1
0
 def test_basic(self):
     Author.objects.create(name='John Smith', alias='smithj')
     Author.objects.create(name='Rhonda')
     authors = Author.objects.annotate(lower_name=Lower('name'))
     self.assertQuerysetEqual(authors.order_by('name'),
                              ['john smith', 'rhonda'],
                              lambda a: a.lower_name)
     Author.objects.update(name=Lower('name'))
     self.assertQuerysetEqual(authors.order_by('name'), [
         ('john smith', 'john smith'),
         ('rhonda', 'rhonda'),
     ], lambda a: (a.lower_name, a.name))
示例#2
0
 def test_bulk_insert_expressions(self):
     Restaurant.objects.bulk_create([
         Restaurant(name="Sam's Shake Shack"),
         Restaurant(name=Lower(Value("Betty's Beetroot Bar")))
     ])
     bbb = Restaurant.objects.filter(name="betty's beetroot bar")
     self.assertEqual(bbb.count(), 1)
示例#3
0
 def test_basic(self):
     authors = Author.objects.annotate(name_part=Right('name', 5))
     self.assertQuerysetEqual(authors.order_by('name'), ['Smith', 'honda'],
                              lambda a: a.name_part)
     # If alias is null, set it to the first 2 lower characters of the name.
     Author.objects.filter(alias__isnull=True).update(
         alias=Lower(Right('name', 2)))
     self.assertQuerysetEqual(authors.order_by('name'), ['smithj', 'da'],
                              lambda a: a.alias)
示例#4
0
 def test_mixed_values(self):
     a1 = Author.objects.create(name='John Smith', alias='smithj')
     a2 = Author.objects.create(name='Rhonda')
     ar1 = Article.objects.create(
         title='How to Django',
         text=lorem_ipsum,
         written=timezone.now(),
     )
     ar1.authors.add(a1)
     ar1.authors.add(a2)
     # mixed Text and Char
     article = Article.objects.annotate(headline=Coalesce(
         'summary', 'text', output_field=TextField()), )
     self.assertQuerysetEqual(article.order_by('title'), [lorem_ipsum],
                              lambda a: a.headline)
     # mixed Text and Char wrapped
     article = Article.objects.annotate(headline=Coalesce(
         Lower('summary'), Lower('text'), output_field=TextField()), )
     self.assertQuerysetEqual(article.order_by('title'),
                              [lorem_ipsum.lower()], lambda a: a.headline)
示例#5
0
 def test_basic(self):
     Author.objects.create(name='John Smith', alias='smithj')
     Author.objects.create(name='Rhonda')
     authors = Author.objects.annotate(name_part=Substr('name', 5, 3))
     self.assertQuerysetEqual(
         authors.order_by('name'), [' Sm', 'da'],
         lambda a: a.name_part
     )
     authors = Author.objects.annotate(name_part=Substr('name', 2))
     self.assertQuerysetEqual(
         authors.order_by('name'), ['ohn Smith', 'honda'],
         lambda a: a.name_part
     )
     # If alias is null, set to first 5 lower characters of the name.
     Author.objects.filter(alias__isnull=True).update(
         alias=Lower(Substr('name', 1, 5)),
     )
     self.assertQuerysetEqual(
         authors.order_by('name'), ['smithj', 'rhond'],
         lambda a: a.alias
     )
示例#6
0
    def test_custom_functions_can_ref_other_functions(self):
        Company(name='Apple',
                motto=None,
                ticker_name='APPL',
                description='Beautiful Devices').save()
        Company(name='Django Software Foundation',
                motto=None,
                ticker_name=None,
                description=None).save()
        Company(name='Google',
                motto='Do No Evil',
                ticker_name='GOOG',
                description='Internet Company').save()
        Company(name='Yahoo',
                motto=None,
                ticker_name=None,
                description='Internet Company').save()

        class Lower(Func):
            function = 'LOWER'

        qs = Company.objects.annotate(tagline=Func(
            F('motto'),
            F('ticker_name'),
            F('description'),
            Value('No Tag'),
            function='COALESCE',
        )).annotate(tagline_lower=Lower(F(
            'tagline'), output_field=CharField())).order_by('name')

        # LOWER function supported by:
        # oracle, postgres, mysql, sqlite, sqlserver

        self.assertQuerysetEqual(
            qs, [('Apple', 'APPL'.lower()),
                 ('Django Software Foundation', 'No Tag'.lower()),
                 ('Google', 'Do No Evil'.lower()),
                 ('Yahoo', 'Internet Company'.lower())], lambda c:
            (c.name, c.tagline_lower))
示例#7
0
    def test_distinct_on_with_annotation(self):
        store = Store.objects.create(
            name='test store',
            original_opening=datetime.datetime.now(),
            friday_night_closing=datetime.time(21, 00, 00),
        )
        names = [
            'Theodore Roosevelt',
            'Eleanor Roosevelt',
            'Franklin Roosevelt',
            'Ned Stark',
            'Catelyn Stark',
        ]
        for name in names:
            Employee.objects.create(
                store=store,
                first_name=name.split()[0],
                last_name=name.split()[1],
                age=30,
                salary=2000,
            )

        people = Employee.objects.annotate(
            name_lower=Lower('last_name'), ).distinct('name_lower')

        self.assertEqual({p.last_name for p in people}, {'Stark', 'Roosevelt'})
        self.assertEqual(len(people), 2)

        people2 = Employee.objects.annotate(
            test_alias=F('store__name'), ).distinct('test_alias')
        self.assertEqual(len(people2), 1)

        lengths = Employee.objects.annotate(
            name_len=Length('first_name'), ).distinct('name_len').values_list(
                'name_len', flat=True)
        self.assertSequenceEqual(lengths, [3, 7, 8])
示例#8
0
 def test_num_args(self):
     with self.assertRaisesMessage(
             TypeError, "'Lower' takes exactly 1 argument (2 given)"):
         Author.objects.update(name=Lower('name', 'name'))