# misc tests import pytest import re import sys import time import unittest import roundup.anypy.cmp_ from roundup.anypy.strings import StringIO # define StringIO from roundup.cgi import cgitb from roundup.cgi.accept_language import parse from roundup.support import PrioList, Progress, TruthDict class AcceptLanguageTest(unittest.TestCase): def testParse(self): self.assertEqual(parse("da, en-gb;q=0.8, en;q=0.7"), ['da', 'en_gb', 'en']) self.assertEqual(parse("da, en-gb;q=0.7, en;q=0.8"), ['da', 'en', 'en_gb']) self.assertEqual(parse("en;q=0.2, fr;q=1"), ['fr', 'en']) self.assertEqual(parse("zn; q = 0.2 ,pt-br;q =1"), ['pt_br', 'zn']) self.assertEqual(parse("pt-br;q =1, zn; q = 0.2"), ['pt_br', 'zn']) self.assertEqual(parse("pt-br,zn;q= 0.1, en-US;q=0.5"), ['pt_br', 'en_US', 'zn']) # verify that items with q=1.0 are in same output order as input self.assertEqual(parse("pt-br,en-US; q=0.5, zn;q= 1.0" ), ['pt_br', 'zn', 'en_US']) self.assertEqual(parse("zn;q=1.0;q= 1.0,pt-br,en-US; q=0.5" ), ['zn', 'pt_br', 'en_US']) self.assertEqual(parse("es-AR"), ['es_AR']) self.assertEqual(parse("es-es-cat"), ['es_es_cat']) self.assertEqual(parse(""), []) self.assertEqual(parse(None),[]) self.assertEqual(parse(" "), []) self.assertEqual(parse("en,"), ['en']) class CmpTest(unittest.TestCase): def testCmp(self): roundup.anypy.cmp_._test() class PrioListTest(unittest.TestCase): def testPL(self): start_data = [(3, 33), (1, -2), (2, 10)] pl = PrioList(key=lambda x: x[1]) for i in start_data: pl.append(i) l = [x for x in pl] self.assertEqual(l, [(1, -2), (2, 10), (3, 33)]) pl = PrioList() for i in start_data: pl.append(i) l = [x for x in pl] self.assertEqual(l, [(1, -2), (2, 10), (3, 33)]) class ProgressTest(unittest.TestCase): @pytest.fixture(autouse=True) def inject_fixtures(self, capsys): self._capsys = capsys def testProgress(self): for x in Progress("5 Items@2 sec:", [1,2,3,4,5]): time.sleep(2) captured = self._capsys.readouterr() split_capture = captured.out.split('\r') # lines padded to 75 characters test should be long enough to # get an ETA printed at 100%, 80% and 60% hopefully this # doesn't become a flakey test on different hardware. self.assertIn("5 Items@2 sec: 0%".ljust(75), split_capture) self.assertIn("5 Items@2 sec: 60% (ETA 00:00:02)".ljust(75), split_capture) self.assertIn("5 Items@2 sec: 100% (ETA 00:00:00)".ljust(75), split_capture) print(captured.err) class TruthDictTest(unittest.TestCase): def testTD(self): td = TruthDict([]) # empty TruthDict always returns True. self.assertTrue(td['a']) self.assertTrue(td['z']) self.assertTrue(td['']) self.assertTrue(td[None]) td = TruthDict(['a', 'b', 'c']) self.assertTrue(td['a']) self.assertFalse(td['z']) class VersionCheck(unittest.TestCase): def test_Version_Check(self): # test for valid versions from roundup.version_check import VERSION_NEEDED self.assertEqual((2, 7), VERSION_NEEDED) del(sys.modules['roundup.version_check']) # fake an invalid version real_ver = sys.version_info sys.version_info = (2, 1) # exit is called on failure, but that breaks testing so # just return and discard the exit code. real_exit = sys.exit sys.exit = lambda code: code # error case uses print(), capture and check capturedOutput = StringIO() sys.stdout = capturedOutput from roundup.version_check import VERSION_NEEDED sys.stdout = sys.__stdout__ self.assertIn("Roundup requires Python 2.7", capturedOutput.getvalue()) # reset to valid values for future tests sys.exit = real_exit sys.version_info = real_ver class CgiTbCheck(unittest.TestCase): def test_NiceDict(self): d = cgitb.niceDict(" ", { "two": "three", "four": "five" }) expected = ( "four'five'\n" "two'three'" ) self.assertEqual(expected, d) def test_breaker(self): b = cgitb.breaker() expected = ('' ' > ') self.assertEqual(expected, b) def test_pt_html(self): """ templating error """ try: f = 5 d = a + 4 except Exception: p = cgitb.pt_html(context=2) expected2 = """

Templating Error

<type 'exceptions.NameError'>: global name 'a' is not defined

Debugging information follows

Full traceback:
Traceback (most recent call last):
  File "XX/test/test_misc.py", line XX, in test_pt_html
    d = a + 4
NameError: global name 'a' is not defined

 

""" expected3 = """

Templating Error

<class 'NameError'>: name 'a' is not defined

Debugging information follows

Full traceback:
Traceback (most recent call last):
  File "XX/test/test_misc.py", line XX, in test_pt_html
    d = a + 4
NameError: name 'a' is not defined

 

""" expected3_11 = """

Templating Error

<class 'NameError'>: name 'a' is not defined

Debugging information follows

Full traceback:
Traceback (most recent call last):
  File "XX/test/test_misc.py", line XX, in test_pt_html
    d = a + 4
        ^
NameError: name 'a' is not defined

 

""" # allow file directory prefix and line number to change p = re.sub(r'\\',r'/', p) # support windows \ => / # [A-Z]?:? optional drive spec on windows p = re.sub(r'(File ")[A-Z]?:?/.*/(test/test_misc.py",)', r'\1XX/\2', p) p = re.sub(r'(", line )\d*,', r'\1XX,', p) print(p) if sys.version_info > (3, 11, 0): self.assertEqual(expected3_11, p) elif sys.version_info > (3, 0, 0): self.assertEqual(expected3, p) else: self.assertEqual(expected2, p) def notest_html(self): """ templating error """ # enabiling this will cause the test to fail as the variable # is included in the live output but not in expected. # self.maxDiff = None try: f = 5 d = a + 4 except Exception: h = cgitb.html(context=2) expected2 = """
 
 
NameError: global name 'a' is not defined
Python XX

A problem occurred while running a Python script. Here is the sequence of function calls leading up to the error, with the most recent (innermost) call first. The exception attributes are:
      __class__ = <type 'exceptions.NameError'>
      __delattr__ = <method-wrapper '__delattr__' of exceptions.NameError object>
      __dict__ = {}
      __doc__ = 'Name not found globally.'
      __format__ = <built-in method __format__ of exceptions.NameError object>
      __getattribute__ = <method-wrapper '__getattribute__' of exceptions.NameError object>
      __getitem__ = <method-wrapper '__getitem__' of exceptions.NameError object>
      __getslice__ = <method-wrapper '__getslice__' of exceptions.NameError object>
      __hash__ = <method-wrapper '__hash__' of exceptions.NameError object>
      __init__ = <method-wrapper '__init__' of exceptions.NameError object>
      __new__ = <built-in method __new__ of type object>
      __reduce__ = <built-in method __reduce__ of exceptions.NameError object>
      __reduce_ex__ = <built-in method __reduce_ex__ of exceptions.NameError object>
      __repr__ = <method-wrapper '__repr__' of exceptions.NameError object>
      __setattr__ = <method-wrapper '__setattr__' of exceptions.NameError object>
      __setstate__ = <built-in method __setstate__ of exceptions.NameError object>
      __sizeof__ = <built-in method __sizeof__ of exceptions.NameError object>
      __str__ = <method-wrapper '__str__' of exceptions.NameError object>
      __subclasshook__ = <built-in method __subclasshook__ of type object>
      __unicode__ = <built-in method __unicode__ of exceptions.NameError object>
      args = ("global name 'a' is not defined",)
      message = "global name 'a' is not defined"

XX/test/test_misc.py in test_html(self=<test.test_misc.CgiTbCheck testMethod=test_html>)
  XX             f = 5
  XX             d = a + 4
      d = undefined, global a = undefined

 

""" expected3 = """\n\n\n
 
\n 
NameError: name \'a\' is not defined
Python XX
\n

A problem occurred while running a Python script. Here is the sequence of function calls leading up to the error, with the most recent (innermost) call first. The exception attributes are:
      __cause__ = None
      __class__ = <class \'NameError\'>
      __context__ = None
      __delattr__ = <method-wrapper \'__delattr__\' of NameError object>
      __dict__ = {}
      __dir__ = <built-in method __dir__ of NameError object>
      __doc__ = \'Name not found globally.\'
      __eq__ = <method-wrapper \'__eq__\' of NameError object>
      __format__ = <built-in method __format__ of NameError object>
      __ge__ = <method-wrapper \'__ge__\' of NameError object>
      __getattribute__ = <method-wrapper \'__getattribute__\' of NameError object>
      __gt__ = <method-wrapper \'__gt__\' of NameError object>
      __hash__ = <method-wrapper \'__hash__\' of NameError object>
      __init__ = <method-wrapper \'__init__\' of NameError object>
      __init_subclass__ = <built-in method __init_subclass__ of type object>
      __le__ = <method-wrapper \'__le__\' of NameError object>
      __lt__ = <method-wrapper \'__lt__\' of NameError object>
      __ne__ = <method-wrapper \'__ne__\' of NameError object>
      __new__ = <built-in method __new__ of type object>
      __reduce__ = <built-in method __reduce__ of NameError object>
      __reduce_ex__ = <built-in method __reduce_ex__ of NameError object>
      __repr__ = <method-wrapper \'__repr__\' of NameError object>
      __setattr__ = <method-wrapper \'__setattr__\' of NameError object>
      __setstate__ = <built-in method __setstate__ of NameError object>
      __sizeof__ = <built-in method __sizeof__ of NameError object>
      __str__ = <method-wrapper \'__str__\' of NameError object>
      __subclasshook__ = <built-in method __subclasshook__ of type object>
      __suppress_context__ = False
      __traceback__ = <traceback object>
      args = ("name \'a\' is not defined",)
      with_traceback = <built-in method with_traceback of NameError object>

\n\n
XX/test/test_misc.py in test_html(self=<test.test_misc.CgiTbCheck testMethod=test_html>)
\n  XX             f = 5
\n
\n\n\n\n
  XX             d = a + 4
\n
\n      d = undefined, global a = undefined

 

""" # strip file path prefix from href and text # /home/user/develop/roundup/test/test_misc.py in test_html h = re.sub(r'(file:)/.*/(test/test_misc.py")', r'\1XX/\2', h) h = re.sub(r'(/test_misc.py">)/.*/(test/test_misc.py)', r'\1XX/\2', h) # replace code line numbers with XX h = re.sub(r'( )\d*()', r'\1XX\2', h) # normalize out python version/path h = re.sub(r'(Python )[\d.]*
[^<]*(<)', r'\1XX\2', h) print(h) if sys.version_info > (3, 0, 0): self.assertEqual(expected3, h) else: self.assertEqual(expected2, h)