Ticket #29851: 29851.diff

File 29851.diff, 2.6 KB (added by Mariusz Felisiak, 6 years ago)

Test.

  • tests/expressions_window/tests.py

    commit 8a45fcbe6674e454a62e3cf5f65a8a1c649d082c
    Author: Mariusz Felisiak <felisiak.mariusz@gmail.com>
    Date:   Fri Dec 14 22:25:43 2018 +0100
    
        Fixed #29851 -- Fixed
    
    diff --git a/tests/expressions_window/tests.py b/tests/expressions_window/tests.py
    index ba757dfb94..9759e5cbfa 100644
    a b from unittest import skipIf, skipUnless  
    44from django.core.exceptions import FieldError
    55from django.db import NotSupportedError, connection
    66from django.db.models import (
    7     F, RowRange, Value, ValueRange, Window, WindowFrame,
     7    F, OuterRef, RowRange, Subquery, Value, ValueRange, Window, WindowFrame,
    88)
    99from django.db.models.aggregates import Avg, Max, Min, Sum
    1010from django.db.models.functions import (
    class WindowFunctionTests(TestCase):  
    584584            ('Brown', 'Sales', 53000, datetime.date(2009, 9, 1), 148000)
    585585        ], transform=lambda row: (row.name, row.department, row.salary, row.hire_date, row.sum))
    586586
     587    def test_subquery_row_range_rank(self):
     588        qs = Employee.objects.annotate(
     589            highest_avg_salary_date=Subquery(
     590                Employee.objects.filter(
     591                    department=OuterRef('department'),
     592                ).annotate(
     593                    avg_salary=Window(
     594                        expression=Avg('salary'),
     595                        order_by=[F('hire_date').asc()],
     596                        frame=RowRange(start=-1, end=1),
     597                    ),
     598                ).order_by('-avg_salary', '-hire_date').values('hire_date')[:1],
     599            ),
     600        ).order_by('department', 'name')
     601        self.assertQuerysetEqual(qs, [
     602            ('Adams', 'Accounting', datetime.date(2005, 11, 1)),
     603            ('Jenson', 'Accounting', datetime.date(2005, 11, 1)),
     604            ('Jones', 'Accounting', datetime.date(2005, 11, 1)),
     605            ('Williams', 'Accounting', datetime.date(2005, 11, 1)),
     606            ('Moore', 'IT', datetime.date(2013, 8, 1)),
     607            ('Wilkinson', 'IT', datetime.date(2013, 8, 1)),
     608            ('Johnson', 'Management', datetime.date(2005, 7, 1)),
     609            ('Miller', 'Management', datetime.date(2005, 7, 1)),
     610            ('Johnson', 'Marketing', datetime.date(2012, 3, 1)),
     611            ('Smith', 'Marketing', datetime.date(2012, 3, 1)),
     612            ('Brown', 'Sales', datetime.date(2009, 9, 1)),
     613            ('Smith', 'Sales', datetime.date(2009, 9, 1)),
     614        ], transform=lambda row: (row.name, row.department, row.highest_avg_salary_date))
     615
    587616    def test_row_range_rank(self):
    588617        """
    589618        A query with ROWS BETWEEN UNBOUNDED PRECEDING AND 3 FOLLOWING.
Back to Top