Django

Code

Ticket #1145: defaultfilters_tests_patch.diff

File defaultfilters_tests_patch.diff, 10.5 kB (added by L.Plant.98@cantab.net, 3 years ago)

patch adding tests and fixing two bugs

  • django/utils/timesince.py

    old new  
    2424    else: 
    2525        tz = None 
    2626    now = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5], tzinfo=tz) 
    27     delta = now - d 
     27     
     28    # ignore microsecond part of 'd' since we removed it from 'now' 
     29    delta = now - (d - datetime.timedelta(0, 0, d.microsecond)) 
    2830    since = delta.days * 24 * 60 * 60 + delta.seconds 
    2931    for i, (seconds, name) in enumerate(chunks): 
    3032        count = since / seconds 
    3133        if count != 0: 
    3234            break 
    3335    if count < 0: 
    34         return '%d milliseconds' % math.floor(delta.microseconds / 1000) 
     36        return '%d milliseconds' % math.floor((now - d).microseconds / 1000) 
    3537    s = '%d %s' % (count, name(count)) 
    3638    if i + 1 < len(chunks): 
    3739        # Now get the second item 
  • django/core/template/defaultfilters.py

    old new  
    117117 
    118118def urlizetrunc(value, limit): 
    119119    """ 
    120     Converts URLs into clickable links, truncating URLs to the given character limit 
     120    Converts URLs into clickable links, truncating URLs to the given character limit, 
     121    and adding 'rel=nofollow' attribute to discourage spamming. 
    121122 
    122123    Argument: Length to truncate URLs to. 
    123124    """ 
     
    254255    for an introduction. 
    255256    """ 
    256257    try: 
    257         return value[slice(*[x and int(x) or None for x in arg.split(':')])] 
     258        bits = [] 
     259        for x in arg.split(':'): 
     260            if len(x) == 0: 
     261                bits.append(None) 
     262            else: 
     263                bits.append(int(x)) 
     264        return value[slice(*bits)] 
     265 
    258266    except (ValueError, TypeError): 
    259267        return value # Fail silently. 
    260268 
  • tests/othertests/defaultfilters.py

    old new  
    1111'0.0' 
    1212>>> floatformat(0.0) 
    1313'0' 
     14 
     15>>> addslashes('"double quotes" and \\'single quotes\\'') 
     16'\\\\"double quotes\\\\" and \\\\\\'single quotes\\\\\\'' 
     17 
     18>>> capfirst('hello world') 
     19'Hello world' 
     20 
     21>>> fix_ampersands('Jack & Jill & Jeroboam') 
     22'Jack &amp; Jill &amp; Jeroboam' 
     23 
     24>>> linenumbers('line 1\\nline 2') 
     25'1. line 1\\n2. line 2' 
     26 
     27>>> linenumbers('\\n'.join(['x'] * 10)) 
     28'01. x\\n02. x\\n03. x\\n04. x\\n05. x\\n06. x\\n07. x\\n08. x\\n09. x\\n10. x' 
     29 
     30>>> lower('TEST') 
     31'test' 
     32 
     33>>> lower(u'\\xcb') # uppercase E umlaut 
     34u'\\xeb' 
     35 
     36>>> make_list('abc') 
     37['a', 'b', 'c'] 
     38 
     39>>> make_list(1234) 
     40['1', '2', '3', '4'] 
     41 
     42>>> slugify(' Jack & Jill like numbers 1,2,3 and 4 and silly characters ?%.$!/') 
     43'jack-jill-like-numbers-123-and-4-and-silly-characters' 
     44 
     45>>> stringformat(1, '03d') 
     46'001' 
     47 
     48>>> stringformat(1, 'z') 
     49'' 
     50 
     51>>> title('a nice title, isn\\'t it?') 
     52"A Nice Title, Isn't It?" 
     53 
     54 
     55>>> truncatewords('A sentence with a few words in it', 1) 
     56'A ...' 
     57 
     58>>> truncatewords('A sentence with a few words in it', 5) 
     59'A sentence with a few ...' 
     60 
     61>>> truncatewords('A sentence with a few words in it', 100) 
     62'A sentence with a few words in it' 
     63 
     64>>> truncatewords('A sentence with a few words in it', 'not a number') 
     65'A sentence with a few words in it' 
     66 
     67 
     68>>> upper('Mixed case input') 
     69'MIXED CASE INPUT' 
     70 
     71>>> upper(u'\\xeb') # lowercase e umlaut 
     72u'\\xcb' 
     73 
     74 
     75>>> urlencode('jack & jill') 
     76'jack%20%26%20jill' 
     77 
     78 
     79>>> urlizetrunc('http://short.com/', 20) 
     80'<a href="http://short.com/" rel="nofollow">http://short.com/</a>' 
     81 
     82>>> urlizetrunc('http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20) 
     83'<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google.co...</a>' 
     84 
     85>>> wordcount('') 
     860 
     87 
     88>>> wordcount('oneword') 
     891 
     90 
     91>>> wordcount('lots of words') 
     923 
     93 
     94>>> wordwrap('this is a long paragraph of text that really needs to be wrapped I\\'m afraid', 14) 
     95"this is a long\\nparagraph of\\ntext that\\nreally needs\\nto be wrapped\\nI'm afraid" 
     96 
     97>>> ljust('test', 10) 
     98'test      ' 
     99 
     100>>> ljust('test', 3) 
     101'test' 
     102 
     103>>> rjust('test', 10) 
     104'      test' 
     105 
     106>>> rjust('test', 3) 
     107'test' 
     108 
     109>>> center('test', 6) 
     110' test ' 
     111 
     112>>> cut('a string to be mangled', 'a') 
     113' string to be mngled' 
     114 
     115>>> cut('a string to be mangled', 'ng') 
     116'a stri to be maled' 
     117 
     118>>> cut('a string to be mangled', 'strings') 
     119'a string to be mangled' 
     120 
     121>>> escape('<some html & special characters > here') 
     122'&lt;some html &amp; special characters &gt; here' 
     123 
     124>>> linebreaks('line 1') 
     125'<p>line 1</p>' 
     126 
     127>>> linebreaks('line 1\\nline 2') 
     128'<p>line 1<br />line 2</p>' 
     129 
     130>>> removetags('some <b>html</b> with <script>alert("You smell")</script> disallowed <img /> tags', 'script img') 
     131'some <b>html</b> with alert("You smell") disallowed  tags' 
     132 
     133>>> striptags('some <b>html</b> with <script>alert("You smell")</script> disallowed <img /> tags') 
     134'some html with alert("You smell") disallowed  tags' 
     135 
     136>>> dictsort([{'age': 23, 'name': 'Barbara-Ann'},\ 
     137              {'age': 63, 'name': 'Ra Ra Rasputin'},\ 
     138              {'name': 'Jonny B Goode', 'age': 18}], 'age') 
     139[{'age': 18, 'name': 'Jonny B Goode'},\ 
     140 {'age': 23, 'name': 'Barbara-Ann'},\ 
     141 {'age': 63, 'name': 'Ra Ra Rasputin'}] 
     142 
     143>>> dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'},\ 
     144              {'age': 63, 'name': 'Ra Ra Rasputin'},\ 
     145              {'name': 'Jonny B Goode', 'age': 18}], 'age') 
     146[{'age': 63, 'name': 'Ra Ra Rasputin'},\ 
     147 {'age': 23, 'name': 'Barbara-Ann'},\ 
     148 {'age': 18, 'name': 'Jonny B Goode'}] 
     149 
     150>>> first([0,1,2]) 
     1510 
     152 
     153>>> first('') 
     154'' 
     155 
     156>>> first('test') 
     157't' 
     158 
     159>>> join([0,1,2], 'glue') 
     160'0glue1glue2' 
     161 
     162>>> length('1234') 
     1634 
     164 
     165>>> length([1,2,3,4]) 
     1664 
     167 
     168>>> length_is([], 0) 
     169True 
     170 
     171>>> length_is([], 1) 
     172False 
     173 
     174>>> length_is('a', 1) 
     175True 
     176 
     177>>> length_is('a', 10) 
     178False 
     179 
     180>>> slice_('abcdefg', '0') 
     181'' 
     182 
     183>>> slice_('abcdefg', '1') 
     184'a' 
     185 
     186>>> slice_('abcdefg', '-1') 
     187'abcdef' 
     188 
     189>>> slice_('abcdefg', '1:2') 
     190'b' 
     191 
     192>>> slice_('abcdefg', '1:3') 
     193'bc' 
     194 
     195>>> slice_('abcdefg', '0::2') 
     196'aceg' 
     197 
     198>>> unordered_list(['item 1', []]) 
     199'\\t<li>item 1</li>' 
     200 
     201>>> unordered_list(['item 1', [['item 1.1', []]]]) 
     202'\\t<li>item 1\\n\\t<ul>\\n\\t\\t<li>item 1.1</li>\\n\\t</ul>\\n\\t</li>' 
     203 
     204>>> unordered_list(['item 1', [['item 1.1', []], ['item 1.2', []]]]) 
     205'\\t<li>item 1\\n\\t<ul>\\n\\t\\t<li>item 1.1</li>\\n\\t\\t<li>item 1.2</li>\\n\\t</ul>\\n\\t</li>' 
     206 
     207>>> add('1', '2') 
     2083 
     209 
     210>>> get_digit(123, 1) 
     2113 
     212 
     213>>> get_digit(123, 2) 
     2142 
     215 
     216>>> get_digit(123, 3) 
     2171 
     218 
     219>>> get_digit(123, 4) 
     2200 
     221 
     222>>> get_digit(123, 0) 
     223123 
     224 
     225>>> get_digit('xyz', 0) 
     226'xyz' 
     227 
     228# real testing of date() is in dateformat.py 
     229>>> date(datetime.datetime(2005, 12, 29), "d F Y") 
     230'29 December 2005' 
     231 
     232# real testing of time() is done in dateformat.py 
     233>>> time(datetime.time(13), "h") 
     234'01' 
     235 
     236# real testing is done in timesince.py, where we can provide our own 'now' 
     237>>> timesince(datetime.datetime.now() - datetime.timedelta(1)) 
     238'1 day' 
     239 
     240>>> default("val", "default") 
     241'val' 
     242 
     243>>> default(None, "default") 
     244'default' 
     245 
     246>>> default('', "default") 
     247'default' 
     248 
     249>>> default_if_none("val", "default") 
     250'val' 
     251 
     252>>> default_if_none(None, "default") 
     253'default' 
     254 
     255>>> default_if_none('', "default") 
     256'' 
     257 
     258>>> divisibleby(4, 2) 
     259True 
     260 
     261>>> divisibleby(4, 3) 
     262False 
     263 
     264>>> yesno(True) 
     265'yes' 
     266 
     267>>> yesno(False) 
     268'no' 
     269 
     270>>> yesno(None) 
     271'maybe' 
     272 
     273>>> yesno(True, 'certainly,get out of town,perhaps') 
     274'certainly' 
     275 
     276>>> yesno(False, 'certainly,get out of town,perhaps') 
     277'get out of town' 
     278 
     279>>> yesno(None, 'certainly,get out of town,perhaps') 
     280'perhaps' 
     281 
     282>>> yesno(None, 'certainly,get out of town') 
     283'get out of town' 
     284 
     285>>> filesizeformat(1023) 
     286'1023 bytes' 
     287 
     288>>> filesizeformat(1024) 
     289'1.0 KB' 
     290 
     291>>> filesizeformat(10*1024) 
     292'10.0 KB' 
     293 
     294>>> filesizeformat(1024*1024-1) 
     295'1024.0 KB' 
     296 
     297>>> filesizeformat(1024*1024) 
     298'1.0 MB' 
     299 
     300>>> filesizeformat(1024*1024*50) 
     301'50.0 MB' 
     302 
     303>>> filesizeformat(1024*1024*1024-1) 
     304'1024.0 MB' 
     305 
     306>>> filesizeformat(1024*1024*1024) 
     307'1.0 GB' 
     308 
     309>>> pluralize(1) 
     310'' 
     311 
     312>>> pluralize(0) 
     313's' 
     314 
     315>>> pluralize(2) 
     316's' 
     317 
     318>>> phone2numeric('0800 flowers') 
     319'0800 3569377' 
     320 
     321 
     322 
    14323""" 
    15324 
    16325from django.core.template.defaultfilters import * 
     326import datetime 
    17327 
    18328if __name__ == '__main__': 
    19329    import doctest 
  • tests/othertests/timesince.py

    old new  
     1""" 
     2>>> timesince(now - timedelta(365*2), now=now) 
     3'2 years' 
     4 
     5>>> timesince(now - timedelta(365*2+30*2), now=now) 
     6'2 years, 2 months' 
     7 
     8>>> timesince(now - timedelta(366), now=now) 
     9'1 year' 
     10 
     11>>> timesince(now - timedelta(365), now=now) 
     12'1 year' 
     13 
     14>>> timesince(now - timedelta(365+30), now=now) 
     15'1 year, 1 month' 
     16 
     17>>> timesince(now - timedelta(2), now=now) 
     18'2 days' 
     19 
     20>>> timesince(now - timedelta(1), now=now) 
     21'1 day' 
     22 
     23>>> timesince(now - timedelta(1.5), now=now) 
     24'1 day, 12 hours' 
     25 
     26>>> timesince(now - timedelta(0.5), now=now) 
     27'12 hours' 
     28 
     29>>> timesince(now - timedelta(0, 3600), now=now) 
     30'1 hour' 
     31 
     32>>> timesince(now - timedelta(0, 3601), now=now) 
     33'1 hour' 
     34 
     35>>> timesince(now - timedelta(0, 60*62), now=now) 
     36'1 hour, 2 minutes' 
     37 
     38>>> timesince(now - timedelta(0, 60*121), now=now) 
     39'2 hours, 1 minute' 
     40 
     41>>> timesince(now - timedelta(0, 600), now=now) 
     42'10 minutes' 
     43 
     44""" 
     45 
     46from django.utils.timesince import timesince 
     47from django.utils import translation 
     48from datetime import datetime, timedelta 
     49 
     50translation.activate('en-us') 
     51 
     52now = datetime.now() 
     53 
     54if __name__ == '__main__': 
     55         
     56    import doctest 
     57    doctest.testmod() 
  • tests/runtests.py

    old new  
    206206    parser.add_option('-v', help='How verbose should the output be? Choices are 0, 1 and 2, where 2 is most verbose. Default is 0.', 
    207207        type='choice', choices=['0', '1', '2']) 
    208208    parser.add_option('--settings', 
    209         help='Python path to settings module, e.g. "myproject.settings.main". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.') 
     209        help='Python path to settings module, e.g. "myproject.settings". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.') 
    210210    options, args = parser.parse_args() 
    211211    verbosity_level = 0 
    212212    if options.v: