Ticket #6050: 6050.diff

File 6050.diff, 3.2 KB (added by Chris Beaven, 17 years ago)

With a new test module for SortedDict - hardly complete but shows basic usage and expected behaviour of this bug

  • django/utils/datastructures.py

     
    6060        if isinstance(data, dict):
    6161            self.keyOrder = data.keys()
    6262        else:
    63             self.keyOrder = [key for key, value in data]
     63            self.keyOrder = []
     64            for key, value in data:
     65                if key not in self.keyOrder:
     66                    self.keyOrder.append(key)
    6467
    6568    def __deepcopy__(self, memo):
    6669        from copy import deepcopy
  • tests/regressiontests/utils/datastructures.py

     
     1"""
     2>>> from django.utils.datastructures import SortedDict
     3
     4>>> d = SortedDict()
     5>>> d[7] = 'seven'
     6>>> d[1] = 'one'
     7>>> d[9] = 'nine'
     8>>> d.keys()
     9[7, 1, 9]
     10>>> d.values()
     11['seven', 'one', 'nine']
     12>>> d.items()
     13[(7, 'seven'), (1, 'one'), (9, 'nine')]
     14
     15# Overwriting an item keeps it's place.
     16>>> d[1] = 'ONE'
     17>>> d.values()
     18['seven', 'ONE', 'nine']
     19
     20# New items go to the end.
     21>>> d[0] = 'nil'
     22>>> d.keys()
     23[7, 1, 9, 0]
     24
     25# Deleting an item, then inserting the same key again will place it at the end.
     26>>> del d[7]
     27>>> d.keys()
     28[1, 9, 0]
     29>>> d[7] = 'lucky number 7'
     30>>> d.keys()
     31[1, 9, 0, 7]
     32
     33# Changing the keys won't do anything, it's only a copy of the keys dict.
     34>>> k = d.keys()
     35>>> k.remove(9)
     36>>> d.keys()
     37[1, 9, 0, 7]
     38
     39# Initialising a SortedDict with two keys will just take the first one. A real
     40# dict will actually take the second value so we will too, but we'll keep the
     41# ordering from the first key found.
     42>>> tuples = ((2, 'two'), (1, 'one'), (2, 'second-two'))
     43>>> d = SortedDict(tuples)
     44>>> d.keys()
     45[2, 1]
     46>>> real_dict = dict(tuples)
     47>>> real_dict.values()
     48['one', 'second-two']
     49>>> d.values()
     50['second-two', 'one']
     51"""
     52 No newline at end of file
  • tests/regressiontests/utils/timesince.py

     
    1 timesince_tests = """
     1"""
    22>>> from datetime import datetime, timedelta
    33>>> from django.utils.timesince import timesince
    44
  • tests/regressiontests/utils/tests.py

     
    66
    77from django.utils import html, checksums
    88
    9 from timesince import timesince_tests
     9import timesince
     10import datastructures
    1011
     12# Extra tests
     13__test__ = {
     14    'timesince': timesince,
     15    'datastructures': datastructures,
     16}
     17
    1118class TestUtilsHtml(TestCase):
    1219
    1320    def check_output(self, function, value, output=None):
     
    142149        for value, output in items:
    143150            self.check_output(f, value, output)
    144151
    145 __test__ = {
    146     'timesince_tests': timesince_tests,
    147 }
    148 
    149152if __name__ == "__main__":
    150153    import doctest
    151154    doctest.testmod()
Back to Top