<s> from __future__ import unicode_literals <EOL> from datetime import datetime <EOL> from operator import attrgetter <EOL> from django . db . models import F <EOL> from django . test import TestCase <EOL> from . models import Article , Author , Reference <EOL> class OrderingTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . a1 = Article . objects . create ( <EOL> headline = "<STR_LIT>" , pub_date = datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ) <EOL> ) <EOL> self . a2 = Article . objects . create ( <EOL> headline = "<STR_LIT>" , pub_date = datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ) <EOL> ) <EOL> self . a3 = Article . objects . create ( <EOL> headline = "<STR_LIT>" , pub_date = datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ) <EOL> ) <EOL> self . a4 = Article . objects . create ( <EOL> headline = "<STR_LIT>" , pub_date = datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ) <EOL> ) <EOL> def test_default_ordering ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . all ( ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> self . assertEqual ( Article . objects . all ( ) [ <NUM_LIT:0> ] , self . a4 ) <EOL> def test_default_ordering_override ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . order_by ( "<STR_LIT>" ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . order_by ( "<STR_LIT>" , "<STR_LIT>" ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> def test_order_by_override ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . order_by ( "<STR_LIT:id>" ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . order_by ( "<STR_LIT:id>" ) . order_by ( "<STR_LIT>" ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> def test_stop_slicing ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . order_by ( "<STR_LIT>" ) [ : <NUM_LIT:2> ] , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> def test_stop_start_slicing ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . order_by ( "<STR_LIT>" ) [ <NUM_LIT:1> : <NUM_LIT:3> ] , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> def test_random_ordering ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( <EOL> len ( list ( Article . objects . order_by ( "<STR_LIT:?>" ) ) ) , <NUM_LIT:4> <EOL> ) <EOL> def test_reversed_ordering ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . all ( ) . reverse ( ) [ : <NUM_LIT:2> ] , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> def test_reverse_ordering_pure ( self ) : <EOL> qs1 = Article . objects . order_by ( F ( '<STR_LIT>' ) . asc ( ) ) <EOL> qs2 = qs1 . reverse ( ) <EOL> self . assertQuerysetEqual ( <EOL> qs1 , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> qs2 , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> def test_extra_ordering ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . extra ( select = { "<STR_LIT:foo>" : "<STR_LIT>" } , order_by = [ "<STR_LIT:foo>" , "<STR_LIT>" ] ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> def test_extra_ordering_quoting ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . extra ( select = { "<STR_LIT>" : "<STR_LIT>" } , order_by = [ "<STR_LIT>" , "<STR_LIT>" ] ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> def test_extra_ordering_with_table_name ( self ) : <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . extra ( order_by = [ '<STR_LIT>' ] ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . extra ( order_by = [ '<STR_LIT>' ] ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> def test_order_by_pk ( self ) : <EOL> """<STR_LIT>""" <EOL> Author . objects . create ( pk = <NUM_LIT:1> ) <EOL> Author . objects . create ( pk = <NUM_LIT:2> ) <EOL> Author . objects . create ( pk = <NUM_LIT:3> ) <EOL> Author . objects . create ( pk = <NUM_LIT:4> ) <EOL> self . assertQuerysetEqual ( <EOL> Author . objects . all ( ) , [ <EOL> <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> def test_order_by_fk_attname ( self ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:5> ) : <EOL> author = Author . objects . create ( pk = i ) <EOL> article = getattr ( self , "<STR_LIT>" % ( <NUM_LIT:5> - i ) ) <EOL> article . author = author <EOL> article . save ( update_fields = { '<STR_LIT>' } ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . order_by ( '<STR_LIT>' ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> def test_order_by_f_expression ( self ) : <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . order_by ( F ( '<STR_LIT>' ) ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . order_by ( F ( '<STR_LIT>' ) . asc ( ) ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . order_by ( F ( '<STR_LIT>' ) . desc ( ) ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> def test_order_by_f_expression_duplicates ( self ) : <EOL> """<STR_LIT>""" <EOL> qs = Article . objects . order_by ( F ( '<STR_LIT>' ) . asc ( ) , F ( '<STR_LIT>' ) . desc ( ) ) <EOL> sql = str ( qs . query ) . upper ( ) <EOL> fragment = sql [ sql . find ( '<STR_LIT>' ) : ] <EOL> self . assertEqual ( fragment . count ( '<STR_LIT>' ) , <NUM_LIT:1> ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> qs = Article . objects . order_by ( F ( '<STR_LIT>' ) . desc ( ) , F ( '<STR_LIT>' ) . asc ( ) ) <EOL> sql = str ( qs . query ) . upper ( ) <EOL> fragment = sql [ sql . find ( '<STR_LIT>' ) : ] <EOL> self . assertEqual ( fragment . count ( '<STR_LIT>' ) , <NUM_LIT:1> ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT>" ) <EOL> ) <EOL> def test_related_ordering_duplicate_table_reference ( self ) : <EOL> """<STR_LIT>""" <EOL> first_author = Author . objects . create ( ) <EOL> second_author = Author . objects . create ( ) <EOL> self . a1 . author = first_author <EOL> self . a1 . second_author = second_author <EOL> self . a1 . save ( ) <EOL> self . a2 . author = second_author <EOL> self . a2 . second_author = first_author <EOL> self . a2 . save ( ) <EOL> r1 = Reference . objects . create ( article_id = self . a1 . pk ) <EOL> r2 = Reference . objects . create ( article_id = self . a2 . pk ) <EOL> self . assertQuerysetEqual ( Reference . objects . all ( ) , [ r2 , r1 ] , lambda x : x ) </s>
