@@ -646,3 +646,40 @@ def test_migrations_include_order(self):
646646 found = True
647647 self .assertEqual (type (field ), models .IntegerField )
648648 assert found , '_order not in fields ' + repr (model_state .fields )
649+
650+
651+ class TestLatest (TestCase ):
652+ """"Test behavior of `latest()` without any field parameters"""
653+
654+ def setUp (self ):
655+ poll = Poll .objects .create (question = "Does `latest()` work?" , pub_date = yesterday )
656+ poll .pub_date = today
657+ poll .save ()
658+
659+ def write_history (self , new_attributes ):
660+ poll_history = HistoricalPoll .objects .all ()
661+ for historical_poll , new_values in zip (poll_history , new_attributes ):
662+ for fieldname , value in new_values .items ():
663+ setattr (historical_poll , fieldname , value )
664+ historical_poll .save ()
665+
666+ def test_ordered (self ):
667+ self .write_history ([
668+ {'pk' : 1 , 'history_date' : yesterday },
669+ {'pk' : 2 , 'history_date' : today },
670+ ])
671+ assert HistoricalPoll .objects .latest ().pk == 2
672+
673+ def test_jumbled (self ):
674+ self .write_history ([
675+ {'pk' : 1 , 'history_date' : today },
676+ {'pk' : 2 , 'history_date' : yesterday },
677+ ])
678+ assert HistoricalPoll .objects .latest ().pk == 1
679+
680+ def test_sameinstant (self ):
681+ self .write_history ([
682+ {'pk' : 1 , 'history_date' : yesterday },
683+ {'pk' : 2 , 'history_date' : yesterday },
684+ ])
685+ assert HistoricalPoll .objects .latest ().pk == 1
0 commit comments