many pep8 improvements

This commit is contained in:
mdipierro
2012-10-19 12:33:53 -05:00
parent b716df1a05
commit 4b2ba185ae
117 changed files with 7160 additions and 5670 deletions
+3 -2
View File
@@ -18,6 +18,7 @@ from cache import CacheInRam, CacheOnDisk
oldcwd = None
def setUpModule():
global oldcwd
if oldcwd is None:
@@ -25,12 +26,14 @@ def setUpModule():
if not os.path.isdir('gluon'):
os.chdir(os.path.realpath('../../'))
def tearDownModule():
global oldcwd
if oldcwd:
os.chdir(oldcwd)
oldcwd = None
class TestCache(unittest.TestCase):
def testCacheInRam(self):
@@ -70,5 +73,3 @@ if __name__ == '__main__':
setUpModule() # pre-python-2.7
unittest.main()
tearDownModule()
+3 -3
View File
@@ -22,13 +22,14 @@ class TestContribs(unittest.TestCase):
def test_fpdf(self):
""" Basic PDF test and sanity checks """
self.assertEqual(fpdf.FPDF_VERSION, pyfpdf.FPDF_VERSION, 'version mistmatch')
self.assertEqual(
fpdf.FPDF_VERSION, pyfpdf.FPDF_VERSION, 'version mistmatch')
self.assertEqual(fpdf.FPDF, pyfpdf.FPDF, 'class mistmatch')
pdf = fpdf.FPDF()
pdf.add_page()
pdf.compress = False
pdf.set_font('Arial', '',14)
pdf.set_font('Arial', '', 14)
pdf.ln(10)
pdf.write(5, 'hello world')
pdf_out = pdf.output('', 'S')
@@ -39,4 +40,3 @@ class TestContribs(unittest.TestCase):
if __name__ == '__main__':
unittest.main()
+5 -6
View File
@@ -130,7 +130,7 @@ class TestBareHelpers(unittest.TestCase):
def testOPTION(self):
self.assertEqual(OPTION('<>', _a='1', _b='2').xml(),
'<option a="1" b="2" value="&lt;&gt;">&lt;&gt;' + \
'<option a="1" b="2" value="&lt;&gt;">&lt;&gt;' +
'</option>')
def testP(self):
@@ -149,7 +149,7 @@ class TestBareHelpers(unittest.TestCase):
def testSELECT(self):
self.assertEqual(SELECT('<>', _a='1', _b='2').xml(),
'<select a="1" b="2">'+ \
'<select a="1" b="2">' +
'<option value="&lt;&gt;">&lt;&gt;</option></select>')
def testSPAN(self):
@@ -162,7 +162,7 @@ class TestBareHelpers(unittest.TestCase):
def testTABLE(self):
self.assertEqual(TABLE('<>', _a='1', _b='2').xml(),
'<table a="1" b="2"><tr><td>&lt;&gt;</td></tr>' + \
'<table a="1" b="2"><tr><td>&lt;&gt;</td></tr>' +
'</table>')
def testTBODY(self):
@@ -175,8 +175,8 @@ class TestBareHelpers(unittest.TestCase):
def testTEXTAREA(self):
self.assertEqual(TEXTAREA('<>', _a='1', _b='2').xml(),
'<textarea a="1" b="2" cols="40" rows="10">&lt;&gt;' + \
'</textarea>')
'<textarea a="1" b="2" cols="40" rows="10">&lt;&gt;' +
'</textarea>')
def testTFOOT(self):
self.assertEqual(TFOOT('<>', _a='1', _b='2').xml(),
@@ -209,4 +209,3 @@ class TestBareHelpers(unittest.TestCase):
if __name__ == '__main__':
unittest.main()
+93 -63
View File
@@ -31,7 +31,8 @@ class TestIsUrl(unittest.TestCase):
self.assertEqual(x('unreal.blargg'), ('unreal.blargg',
'enter a valid URL'))
self.assertEqual(x('google..ca'), ('google..ca', 'enter a valid URL'))
self.assertEqual(x('google.ca..'), ('google.ca..', 'enter a valid URL'))
self.assertEqual(
x('google.ca..'), ('google.ca..', 'enter a valid URL'))
# explicit use of 'http' mode
@@ -116,9 +117,9 @@ class TestIsUrl(unittest.TestCase):
# 'generic' mode
x = IS_URL(mode='generic')
self.assertEqual(x('http://google.ca'), ('http://google.ca',None))
self.assertEqual(x('http://google.ca'), ('http://google.ca', None))
self.assertEqual(x('google.ca'), ('google.ca', None))
self.assertEqual(x('google.ca:80'), ('http://google.ca:80',None))
self.assertEqual(x('google.ca:80'), ('http://google.ca:80', None))
self.assertEqual(x('blargg://unreal'), ('blargg://unreal',
'enter a valid URL'))
@@ -152,7 +153,7 @@ class TestIsUrl(unittest.TestCase):
# 'generic' mode with overriden allowed_schemes and prepend_scheme
x = IS_URL(mode='generic', allowed_schemes=[None, 'ftp', 'ftps'
], prepend_scheme='ftp')
], prepend_scheme='ftp')
self.assertEqual(x('http://google.ca'), ('http://google.ca',
'enter a valid URL'))
self.assertEqual(x('google.ca'), ('google.ca', None))
@@ -191,10 +192,11 @@ class TestIsUrl(unittest.TestCase):
# error at calling time
except Exception, e:
if str(e)\
!= "allowed_scheme value 'ftp' is not in [None, 'http', 'https']":
!= "allowed_scheme value 'ftp' is not in [None, 'http', 'https']":
self.fail('Wrong exception: ' + str(e))
else:
self.fail("Accepted invalid allowed_schemes: [None, 'ftp', 'ftps']")
self.fail(
"Accepted invalid allowed_schemes: [None, 'ftp', 'ftps']")
# prepend_scheme's value must be in allowed_schemes (default for 'http'
# mode is [None, 'http', 'https'])
@@ -205,7 +207,7 @@ class TestIsUrl(unittest.TestCase):
# error at calling time
except Exception, e:
if str(e)\
!= "prepend_scheme='ftp' is not in allowed_schemes=[None, 'http', 'https']":
!= "prepend_scheme='ftp' is not in allowed_schemes=[None, 'http', 'https']":
self.fail('Wrong exception: ' + str(e))
else:
self.fail("Accepted invalid prepend_scheme: 'ftp'")
@@ -217,7 +219,7 @@ class TestIsUrl(unittest.TestCase):
x = IS_URL(allowed_schemes=[None, 'https'])
except Exception, e:
if str(e)\
!= "prepend_scheme='http' is not in allowed_schemes=[None, 'https']":
!= "prepend_scheme='http' is not in allowed_schemes=[None, 'https']":
self.fail('Wrong exception: ' + str(e))
else:
self.fail("Accepted invalid prepend_scheme: 'http'")
@@ -229,7 +231,7 @@ class TestIsUrl(unittest.TestCase):
prepend_scheme='https')
except Exception, e:
if str(e)\
!= "prepend_scheme='https' is not in allowed_schemes=[None, 'http']":
!= "prepend_scheme='https' is not in allowed_schemes=[None, 'http']":
self.fail('Wrong exception: ' + str(e))
else:
self.fail("Accepted invalid prepend_scheme: 'https'")
@@ -241,7 +243,7 @@ class TestIsUrl(unittest.TestCase):
'ftps'])
except Exception, e:
if str(e)\
!= "prepend_scheme='http' is not in allowed_schemes=[None, 'ftp', 'ftps']":
!= "prepend_scheme='http' is not in allowed_schemes=[None, 'ftp', 'ftps']":
self.fail('Wrong exception: ' + str(e))
else:
self.fail("Accepted invalid prepend_scheme: 'http'")
@@ -251,7 +253,8 @@ class TestIsUrl(unittest.TestCase):
try:
x = IS_URL(mode='generic', prepend_scheme='blargg')
x('http://www.google.ca') # we can only reasonably know about the error at calling time
x('http://www.google.ca')
# we can only reasonably know about the error at calling time
except Exception, e:
if not str(e).startswith(
"prepend_scheme='blargg' is not in allowed_schemes="):
@@ -266,7 +269,7 @@ class TestIsUrl(unittest.TestCase):
prepend_scheme='blargg')
except Exception, e:
if str(e)\
!= "prepend_scheme='blargg' is not in allowed_schemes=[None, 'http']":
!= "prepend_scheme='blargg' is not in allowed_schemes=[None, 'http']":
self.fail('Wrong exception: ' + str(e))
else:
self.fail("Accepted invalid prepend_scheme: 'blargg'")
@@ -282,7 +285,7 @@ class TestIsUrl(unittest.TestCase):
# prepend_scheme has the invalid value 'http', we don't care!
x = IS_URL(mode='generic', allowed_schemes=['https'],
prepend_scheme='https')
prepend_scheme='https')
self.assertEqual(x('google.ca'), ('google.ca', 'enter a valid URL'))
@@ -335,12 +338,12 @@ class TestIsGenericUrl(unittest.TestCase):
'ht,tp://www.benn.ca',
'ht:tp://www.benn.ca',
'htp://invalid_scheme.com',
]
]
failures = []
for url in urlsToCheckA + urlsToCheckB:
if self.x(url)[1] == None:
if self.x(url)[1] is None:
failures.append('Incorrectly accepted: ' + str(url))
if len(failures) > 0:
@@ -377,14 +380,13 @@ class TestIsGenericUrl(unittest.TestCase):
'http://localhost:8080/',
'http://localhost:8080/hello',
'http://localhost:8080/hello/',
'file:///C:/Documents%20and%20Settings/Jonathan/Desktop/view.py'
,
]
'file:///C:/Documents%20and%20Settings/Jonathan/Desktop/view.py',
]
failures = []
for url in urlsToCheck:
if self.x(url)[1] != None:
if self.x(url)[1] is not None:
failures.append('Incorrectly rejected: ' + str(url))
if len(failures) > 0:
@@ -405,7 +407,7 @@ class TestIsGenericUrl(unittest.TestCase):
# because a scheme is required
y = IS_GENERIC_URL(allowed_schemes=['http', 'blargg'],
prepend_scheme='http')
prepend_scheme='http')
self.assertEqual(y('google.ca'), ('google.ca', 'enter a valid URL'))
@@ -459,12 +461,12 @@ class TestIsHttpUrl(unittest.TestCase):
'path/segment/without/starting/slash',
'http://www.math.uio.no;param=3',
'://ABC.com:/%7esmith/home.html',
]
]
failures = []
for url in urlsToCheck:
if self.x(url)[1] == None:
if self.x(url)[1] is None:
failures.append('Incorrectly accepted: ' + str(url))
if len(failures) > 0:
@@ -523,8 +525,7 @@ class TestIsHttpUrl(unittest.TestCase):
'HTTPS://localhost.',
'http://localhost#fragment',
'http://localhost/hello;param=value',
'http://localhost/hello;param=value/hi;param2=value2;param3=value3'
,
'http://localhost/hello;param=value/hi;param2=value2;param3=value3',
'http://localhost/hello?query=True',
'http://www.benn.ca/hello;param=value/hi;param2=value2;param3=value3/index.html?query=3',
'http://localhost/hello/?query=1500&five=6',
@@ -534,12 +535,12 @@ class TestIsHttpUrl(unittest.TestCase):
'http://localhost:8080/hello%20world/',
'http://www.a.3.be-nn.5.ca',
'http://www.amazon.COM',
]
]
failures = []
for url in urlsToCheck:
if self.x(url)[1] != None:
if self.x(url)[1] is not None:
failures.append('Incorrectly rejected: ' + str(url))
if len(failures) > 0:
@@ -557,17 +558,20 @@ class TestIsHttpUrl(unittest.TestCase):
self.assertEqual(self.x('https://google.ca'),
('https://google.ca', None))
y = IS_HTTP_URL(prepend_scheme='https', allowed_schemes=[None, 'https'])
self.assertEqual(y('google.ca'), ('https://google.ca', None)) # prepends https if asked
y = IS_HTTP_URL(
prepend_scheme='https', allowed_schemes=[None, 'https'])
self.assertEqual(y('google.ca'), (
'https://google.ca', None)) # prepends https if asked
z = IS_HTTP_URL(prepend_scheme=None)
self.assertEqual(z('google.ca:8080'), ('google.ca:8080', None)) # prepending disabled
self.assertEqual(z('google.ca:8080'), ('google.ca:8080',
None)) # prepending disabled
try:
IS_HTTP_URL(prepend_scheme='mailto')
except Exception, e:
if str(e)\
!= "prepend_scheme='mailto' is not in allowed_schemes=[None, 'http', 'https']":
!= "prepend_scheme='mailto' is not in allowed_schemes=[None, 'http', 'https']":
self.fail('Wrong exception: ' + str(e))
else:
self.fail("Got invalid prepend_scheme: 'mailto'")
@@ -579,67 +583,93 @@ class TestIsHttpUrl(unittest.TestCase):
self.assertEqual(a('google.ca:80'), ('google.ca:80',
'enter a valid URL'))
class TestUnicode(unittest.TestCase):
x = IS_URL()
y = IS_URL(allowed_schemes=['https'], prepend_scheme='https') #excludes the option for abbreviated URLs with no scheme
z = IS_URL(prepend_scheme=None) # disables prepending the scheme in the return value
y = IS_URL(allowed_schemes=['https'], prepend_scheme='https')
#excludes the option for abbreviated URLs with no scheme
z = IS_URL(prepend_scheme=None)
# disables prepending the scheme in the return value
def testUnicodeToAsciiUrl(self):
self.assertEquals(unicode_to_ascii_authority(u'www.Alliancefran\xe7aise.nu'), 'www.xn--alliancefranaise-npb.nu')
self.assertEquals(unicode_to_ascii_authority(u'www.benn.ca'), 'www.benn.ca')
self.assertRaises(UnicodeError, unicode_to_ascii_authority, u'\u4e2d'*1000) #label is too long
self.assertEquals(
unicode_to_ascii_authority(u'www.benn.ca'), 'www.benn.ca')
self.assertRaises(UnicodeError, unicode_to_ascii_authority,
u'\u4e2d' * 1000) # label is too long
def testValidUrls(self):
self.assertEquals(self.x(u'www.Alliancefrancaise.nu'), ('http://www.Alliancefrancaise.nu', None))
self.assertEquals(self.x(u'www.Alliancefran\xe7aise.nu'), ('http://www.xn--alliancefranaise-npb.nu', None))
self.assertEquals(self.x(u'www.Alliancefran\xe7aise.nu:8080'), ('http://www.xn--alliancefranaise-npb.nu:8080', None))
self.assertEquals(self.x(u'http://www.Alliancefran\xe7aise.nu'), ('http://www.xn--alliancefranaise-npb.nu', None))
self.assertEquals(self.x(u'www.Alliancefrancaise.nu'), (
'http://www.Alliancefrancaise.nu', None))
self.assertEquals(self.x(u'www.Alliancefran\xe7aise.nu'), (
'http://www.xn--alliancefranaise-npb.nu', None))
self.assertEquals(self.x(u'www.Alliancefran\xe7aise.nu:8080'), (
'http://www.xn--alliancefranaise-npb.nu:8080', None))
self.assertEquals(self.x(u'http://www.Alliancefran\xe7aise.nu'),
('http://www.xn--alliancefranaise-npb.nu', None))
self.assertEquals(self.x(u'http://www.Alliancefran\xe7aise.nu/parnaise/blue'), ('http://www.xn--alliancefranaise-npb.nu/parnaise/blue', None))
self.assertEquals(self.x(u'http://www.Alliancefran\xe7aise.nu/parnaise/blue#fragment'), ('http://www.xn--alliancefranaise-npb.nu/parnaise/blue#fragment', None))
self.assertEquals(self.x(u'http://www.Alliancefran\xe7aise.nu/parnaise/blue?query=value#fragment'), ('http://www.xn--alliancefranaise-npb.nu/parnaise/blue?query=value#fragment', None))
self.assertEquals(self.x(u'http://www.Alliancefran\xe7aise.nu:8080/parnaise/blue?query=value#fragment'), ('http://www.xn--alliancefranaise-npb.nu:8080/parnaise/blue?query=value#fragment', None))
self.assertEquals(self.x(u'www.Alliancefran\xe7aise.nu/parnaise/blue?query=value#fragment'), ('http://www.xn--alliancefranaise-npb.nu/parnaise/blue?query=value#fragment', None))
self.assertEquals(self.x(u'http://\u4e2d\u4fd4.com'), ('http://xn--fiq13b.com', None))
self.assertEquals(self.x(u'http://\u4e2d\u4fd4.com/\u4e86'), ('http://xn--fiq13b.com/%4e%86', None))
self.assertEquals(self.x(
u'http://\u4e2d\u4fd4.com'), ('http://xn--fiq13b.com', None))
self.assertEquals(self.x(u'http://\u4e2d\u4fd4.com/\u4e86'),
('http://xn--fiq13b.com/%4e%86', None))
self.assertEquals(self.x(u'http://\u4e2d\u4fd4.com/\u4e86?query=\u4e86'), ('http://xn--fiq13b.com/%4e%86?query=%4e%86', None))
self.assertEquals(self.x(u'http://\u4e2d\u4fd4.com/\u4e86?query=\u4e86#fragment'), ('http://xn--fiq13b.com/%4e%86?query=%4e%86#fragment', None))
self.assertEquals(self.x(u'http://\u4e2d\u4fd4.com?query=\u4e86#fragment'), ('http://xn--fiq13b.com?query=%4e%86#fragment', None))
self.assertEquals(self.x(u'http://B\xfccher.ch'), ('http://xn--bcher-kva.ch', None))
self.assertEquals(self.x(u'http://\xe4\xf6\xfc\xdf.com'), ('http://xn--ss-uia6e4a.com', None))
self.assertEquals(self.x(u'http://visegr\xe1d.com'), ('http://xn--visegrd-mwa.com', None))
self.assertEquals(self.x(u'http://h\xe1zipatika.com'), ('http://xn--hzipatika-01a.com', None))
self.assertEquals(self.x(u'http://www.\xe7ukurova.com'), ('http://www.xn--ukurova-txa.com', None))
self.assertEquals(
self.x(u'http://B\xfccher.ch'), ('http://xn--bcher-kva.ch', None))
self.assertEquals(self.x(u'http://\xe4\xf6\xfc\xdf.com'), (
'http://xn--ss-uia6e4a.com', None))
self.assertEquals(self.x(
u'http://visegr\xe1d.com'), ('http://xn--visegrd-mwa.com', None))
self.assertEquals(self.x(u'http://h\xe1zipatika.com'), (
'http://xn--hzipatika-01a.com', None))
self.assertEquals(self.x(u'http://www.\xe7ukurova.com'), (
'http://www.xn--ukurova-txa.com', None))
self.assertEquals(self.x(u'http://nixier\xf6hre.nixieclock-tube.com'), ('http://xn--nixierhre-57a.nixieclock-tube.com', None))
self.assertEquals(self.x(u'google.ca.'), ('http://google.ca.', None))
self.assertEquals(self.y(u'https://google.ca'), ('https://google.ca', None))
self.assertEquals(self.y(u'https://\u4e2d\u4fd4.com'), ('https://xn--fiq13b.com', None))
self.assertEquals(
self.y(u'https://google.ca'), ('https://google.ca', None))
self.assertEquals(self.y(
u'https://\u4e2d\u4fd4.com'), ('https://xn--fiq13b.com', None))
self.assertEquals(self.z(u'google.ca'), ('google.ca', None))
def testInvalidUrls(self):
self.assertEquals(self.x(u'://ABC.com'), (u'://ABC.com', 'enter a valid URL'))
self.assertEquals(self.x(u'http://\u4e2d\u4fd4.dne'), (u'http://\u4e2d\u4fd4.dne', 'enter a valid URL'))
self.assertEquals(self.x(u'https://google.dne'), (u'https://google.dne', 'enter a valid URL'))
self.assertEquals(self.x(u'https://google..ca'), (u'https://google..ca', 'enter a valid URL'))
self.assertEquals(self.x(u'google..ca'), (u'google..ca', 'enter a valid URL'))
self.assertEquals(self.x(u'http://' + u'\u4e2d'*1000 + u'.com'), (u'http://' + u'\u4e2d'*1000 + u'.com', 'enter a valid URL'))
self.assertEquals(
self.x(u'://ABC.com'), (u'://ABC.com', 'enter a valid URL'))
self.assertEquals(self.x(u'http://\u4e2d\u4fd4.dne'), (
u'http://\u4e2d\u4fd4.dne', 'enter a valid URL'))
self.assertEquals(self.x(u'https://google.dne'), (
u'https://google.dne', 'enter a valid URL'))
self.assertEquals(self.x(u'https://google..ca'), (
u'https://google..ca', 'enter a valid URL'))
self.assertEquals(
self.x(u'google..ca'), (u'google..ca', 'enter a valid URL'))
self.assertEquals(self.x(u'http://' + u'\u4e2d' * 1000 + u'.com'), (
u'http://' + u'\u4e2d' * 1000 + u'.com', 'enter a valid URL'))
self.assertEquals(self.x(u'http://google.com#fragment_\u4e86'), (u'http://google.com#fragment_\u4e86', 'enter a valid URL'))
self.assertEquals(self.x(u'http\u4e86://google.com'), (u'http\u4e86://google.com', 'enter a valid URL'))
self.assertEquals(self.x(u'http\u4e86://google.com#fragment_\u4e86'), (u'http\u4e86://google.com#fragment_\u4e86', 'enter a valid URL'))
self.assertEquals(self.x(u'http://google.com#fragment_\u4e86'), (
u'http://google.com#fragment_\u4e86', 'enter a valid URL'))
self.assertEquals(self.x(u'http\u4e86://google.com'), (
u'http\u4e86://google.com', 'enter a valid URL'))
self.assertEquals(self.x(u'http\u4e86://google.com#fragment_\u4e86'), (
u'http\u4e86://google.com#fragment_\u4e86', 'enter a valid URL'))
self.assertEquals(self.y(u'http://\u4e2d\u4fd4.com/\u4e86'), (u'http://\u4e2d\u4fd4.com/\u4e86', 'enter a valid URL'))
self.assertEquals(self.y(u'http://\u4e2d\u4fd4.com/\u4e86'), (
u'http://\u4e2d\u4fd4.com/\u4e86', 'enter a valid URL'))
#self.assertEquals(self.y(u'google.ca'), (u'google.ca', 'enter a valid URL'))
self.assertEquals(self.z(u'invalid.domain..com'), (u'invalid.domain..com', 'enter a valid URL'))
self.assertEquals(self.z(u'invalid.\u4e2d\u4fd4.blargg'), (u'invalid.\u4e2d\u4fd4.blargg', 'enter a valid URL'))
self.assertEquals(self.z(u'invalid.domain..com'), (
u'invalid.domain..com', 'enter a valid URL'))
self.assertEquals(self.z(u'invalid.\u4e2d\u4fd4.blargg'), (
u'invalid.\u4e2d\u4fd4.blargg', 'enter a valid URL'))
# ##############################################################################
if __name__ == '__main__':
unittest.main()
+3 -5
View File
@@ -49,12 +49,11 @@ try:
def test_reads_and_writes(self):
readwriters = 10
pool = multiprocessing.Pool(processes = readwriters)
pool = multiprocessing.Pool(processes=readwriters)
results = pool.map(read_write, [[self.filename, 10]] * readwriters)
for result in results:
self.assertTrue(result)
class TestTranslations(unittest.TestCase):
def setUp(self):
@@ -62,11 +61,11 @@ try:
if os.path.isdir('gluon'):
self.request.folder = 'applications/welcome'
else:
self.request.folder = os.path.realpath('../../applications/welcome')
self.request.folder = os.path.realpath(
'../../applications/welcome')
self.request.env = Storage()
self.request.env.http_accept_language = 'en'
def tearDown(self):
pass
@@ -99,4 +98,3 @@ except ImportError:
if __name__ == '__main__':
unittest.main()
+1 -1
View File
@@ -13,10 +13,10 @@ else:
import unittest
from contrib.markmin.markmin2html import run_doctests
class TestMarkmin(unittest.TestCase):
def testMarkmin(self):
run_doctests()
if __name__ == '__main__':
unittest.main()
+868 -513
View File
File diff suppressed because it is too large Load Diff
+172 -91
View File
@@ -10,9 +10,9 @@ import tempfile
import logging
if os.path.isdir('gluon'):
sys.path.append(os.path.realpath('gluon')) # running from web2py base
sys.path.append(os.path.realpath('gluon')) # running from web2py base
else:
sys.path.append(os.path.realpath('../')) # running from gluon/tests/
sys.path.append(os.path.realpath('../')) # running from gluon/tests/
os.environ['web2py_path'] = os.path.realpath('../../') # for settings
from rewrite import load, filter_url, filter_err, get_effective_router, regex_filter_out, regex_select
@@ -26,6 +26,7 @@ logger = None
oldcwd = None
root = None
def setUpModule():
def make_apptree():
"build a temporary applications tree"
@@ -39,13 +40,16 @@ def setUpModule():
os.mkdir(abspath('applications', app, subdir))
# applications/admin/controllers/*.py
for ctr in ('appadmin', 'default', 'gae', 'mercurial', 'shell', 'wizard'):
open(abspath('applications', 'admin', 'controllers', '%s.py' % ctr), 'w').close()
open(abspath('applications', 'admin',
'controllers', '%s.py' % ctr), 'w').close()
# applications/examples/controllers/*.py
for ctr in ('ajax_examples', 'appadmin', 'default', 'global', 'spreadsheet'):
open(abspath('applications', 'examples', 'controllers', '%s.py' % ctr), 'w').close()
open(abspath('applications', 'examples',
'controllers', '%s.py' % ctr), 'w').close()
# applications/welcome/controllers/*.py
for ctr in ('appadmin', 'default'):
open(abspath('applications', 'welcome', 'controllers', '%s.py' % ctr), 'w').close()
open(abspath('applications', 'welcome',
'controllers', '%s.py' % ctr), 'w').close()
# create an app-specific routes.py for examples app
routes = open(abspath('applications', 'examples', 'routes.py'), 'w')
routes.write("default_function='exdef'\n")
@@ -55,7 +59,8 @@ def setUpModule():
if oldcwd is None: # do this only once
oldcwd = os.getcwd()
if not os.path.isdir('gluon'):
os.chdir(os.path.realpath('../../')) # run from web2py base directory
os.chdir(os.path.realpath(
'../../')) # run from web2py base directory
import main # for initialization after chdir
global logger
logger = logging.getLogger('web2py.rewrite')
@@ -64,6 +69,7 @@ def setUpModule():
root = global_settings.applications_parent
make_apptree()
def tearDownModule():
global oldcwd
if oldcwd is not None:
@@ -78,16 +84,26 @@ class TestRoutes(unittest.TestCase):
""" Tests a null routes table """
load(data='')
# incoming
self.assertEqual(filter_url('http://domain.com'), '/init/default/index')
self.assertEqual(filter_url('http://domain.com/'), '/init/default/index')
self.assertEqual(filter_url('http://domain.com/abc'), '/abc/default/index')
self.assertEqual(filter_url('http://domain.com/abc/'), '/abc/default/index')
self.assertEqual(filter_url('http://domain.com/abc/def'), "/abc/def/index")
self.assertEqual(filter_url('http://domain.com/abc/def/'), "/abc/def/index")
self.assertEqual(filter_url('http://domain.com/abc/def/ghi'), "/abc/def/ghi")
self.assertEqual(filter_url('http://domain.com/abc/def/ghi/'), "/abc/def/ghi")
self.assertEqual(filter_url('http://domain.com/abc/def/ghi/jkl'), "/abc/def/ghi ['jkl']")
self.assertEqual(filter_url('http://domain.com/abc/def/ghi/j%20kl'), "/abc/def/ghi ['j_kl']")
self.assertEqual(
filter_url('http://domain.com'), '/init/default/index')
self.assertEqual(
filter_url('http://domain.com/'), '/init/default/index')
self.assertEqual(
filter_url('http://domain.com/abc'), '/abc/default/index')
self.assertEqual(
filter_url('http://domain.com/abc/'), '/abc/default/index')
self.assertEqual(
filter_url('http://domain.com/abc/def'), "/abc/def/index")
self.assertEqual(
filter_url('http://domain.com/abc/def/'), "/abc/def/index")
self.assertEqual(
filter_url('http://domain.com/abc/def/ghi'), "/abc/def/ghi")
self.assertEqual(
filter_url('http://domain.com/abc/def/ghi/'), "/abc/def/ghi")
self.assertEqual(filter_url(
'http://domain.com/abc/def/ghi/jkl'), "/abc/def/ghi ['jkl']")
self.assertEqual(filter_url(
'http://domain.com/abc/def/ghi/j%20kl'), "/abc/def/ghi ['j_kl']")
self.assertEqual(filter_url('http://domain.com/welcome/static/path/to/static'), "%s/applications/welcome/static/path/to/static" % root)
self.assertRaises(HTTP, filter_url, 'http://domain.com/welcome/static/bad/path/to/st~tic')
try:
@@ -96,9 +112,11 @@ class TestRoutes(unittest.TestCase):
except AttributeError:
pass
# outgoing
self.assertEqual(filter_url('http://domain.com/init/default/index', out=True), '/init/default/index')
self.assertEqual(filter_url('http://domain.com/init/default/index',
out=True), '/init/default/index')
self.assertEqual(filter_url('http://domain.com/init/default/index/arg1', out=True), '/init/default/index/arg1')
self.assertEqual(filter_url('http://domain.com/init/default/abc', out=True), '/init/default/abc')
self.assertEqual(filter_url('http://domain.com/init/default/abc',
out=True), '/init/default/abc')
def test_routes_query(self):
""" Test query appending """
@@ -125,8 +143,10 @@ routes_app = [
]
'''
load(data=data)
self.assertEqual(filter_url('http://domain.com/welcome'), '/welcome/default/index')
self.assertEqual(filter_url('http://domain.com/examples'), '/examples/default/exdef')
self.assertEqual(
filter_url('http://domain.com/welcome'), '/welcome/default/index')
self.assertEqual(filter_url(
'http://domain.com/examples'), '/examples/default/exdef')
def test_routes_defapp(self):
""" Test the default-application function """
@@ -135,12 +155,17 @@ default_application = 'defapp'
'''
load(data=data)
# incoming
self.assertEqual(filter_url('http://domain.com'), '/defapp/default/index')
self.assertEqual(filter_url('http://domain.com/'), '/defapp/default/index')
self.assertEqual(filter_url('http://domain.com/welcome'), '/welcome/default/index')
self.assertEqual(filter_url('http://domain.com/app'), '/app/default/index')
self.assertEqual(
filter_url('http://domain.com'), '/defapp/default/index')
self.assertEqual(
filter_url('http://domain.com/'), '/defapp/default/index')
self.assertEqual(
filter_url('http://domain.com/welcome'), '/welcome/default/index')
self.assertEqual(
filter_url('http://domain.com/app'), '/app/default/index')
self.assertEqual(filter_url('http://domain.com/welcome/default/index/abc'), "/welcome/default/index ['abc']")
self.assertEqual(filter_url('http://domain.com/welcome/static/abc'), '%s/applications/welcome/static/abc' % root)
self.assertEqual(filter_url('http://domain.com/welcome/static/abc'),
'%s/applications/welcome/static/abc' % root)
self.assertEqual(filter_url('http://domain.com/defapp/static/path/to/static'), "%s/applications/defapp/static/path/to/static" % root)
def test_routes_raise(self):
@@ -149,15 +174,21 @@ default_application = 'defapp'
'''
# test non-exception variants
load(data='')
self.assertEqual(filter_url('http://domain.com/init'), "/init/default/index")
self.assertEqual(filter_url('http://domain.com/init/default'), "/init/default/index")
self.assertEqual(filter_url('http://domain.com/init/default/fcn.ext'), "/init/default/fcn.ext")
self.assertEqual(filter_url('http://domain.com/init/default/fcn/arg'), "/init/default/fcn ['arg']")
self.assertEqual(
filter_url('http://domain.com/init'), "/init/default/index")
self.assertEqual(filter_url(
'http://domain.com/init/default'), "/init/default/index")
self.assertEqual(filter_url('http://domain.com/init/default/fcn.ext'),
"/init/default/fcn.ext")
self.assertEqual(filter_url('http://domain.com/init/default/fcn/arg'),
"/init/default/fcn ['arg']")
# now raise-HTTP variants
self.assertRaises(HTTP, filter_url, 'http://domain.com/bad!ctl')
self.assertRaises(HTTP, filter_url, 'http://domain.com/ctl/bad!fcn')
self.assertRaises(HTTP, filter_url, 'http://domain.com/ctl/fcn.bad!ext')
self.assertRaises(HTTP, filter_url, 'http://domain.com/ctl/fcn/bad!arg')
self.assertRaises(
HTTP, filter_url, 'http://domain.com/ctl/fcn.bad!ext')
self.assertRaises(
HTTP, filter_url, 'http://domain.com/ctl/fcn/bad!arg')
try:
# 2.7+ only
self.assertRaisesRegexp(HTTP, '400 BAD REQUEST \[invalid path\]', filter_url, 'http://domain.com/init/bad!ctl')
@@ -167,7 +198,8 @@ default_application = 'defapp'
except AttributeError:
pass
self.assertEqual(filter_url('http://domain.com/welcome/default/fcn_1'), "/welcome/default/fcn_1")
self.assertEqual(filter_url('http://domain.com/welcome/default/fcn_1'),
"/welcome/default/fcn_1")
#self.assertRaises(HTTP, filter_url, 'http://domain.com/welcome/default/fcn-1')
#try:
# # 2.7+ only
@@ -194,17 +226,26 @@ default_application = 'defapp'
('/favicon.ico', '/welcome/static/favicon.ico'),
('/admin$anything', '/admin$anything'),
('.*:https?://(.*\\.)?domain1.com:$method /', '/app1/default'),
('.*:https?://(.*\\.)?domain1.com:$method /static/$anything', '/app1/static/$anything'),
('.*:https?://(.*\\.)?domain1.com:$method /appadmin/$anything', '/app1/appadmin/$anything'),
('.*:https?://(.*\\.)?domain1.com:$method /$anything', '/app1/default/$anything'),
('.*:https?://(.*\\.)?domain1.com:$method /static/$anything',
'/app1/static/$anything'),
('.*:https?://(.*\\.)?domain1.com:$method /appadmin/$anything',
'/app1/appadmin/$anything'),
('.*:https?://(.*\\.)?domain1.com:$method /$anything',
'/app1/default/$anything'),
('.*:https?://(.*\\.)?domain2.com:$method /', '/app2/default'),
('.*:https?://(.*\\.)?domain2.com:$method /static/$anything', '/app2/static/$anything'),
('.*:https?://(.*\\.)?domain2.com:$method /appadmin/$anything', '/app2/appadmin/$anything'),
('.*:https?://(.*\\.)?domain2.com:$method /$anything', '/app2/default/$anything'),
('.*:https?://(.*\\.)?domain2.com:$method /static/$anything',
'/app2/static/$anything'),
('.*:https?://(.*\\.)?domain2.com:$method /appadmin/$anything',
'/app2/appadmin/$anything'),
('.*:https?://(.*\\.)?domain2.com:$method /$anything',
'/app2/default/$anything'),
('.*:https?://(.*\\.)?domain3.com:$method /', '/app3/defcon3'),
('.*:https?://(.*\\.)?domain3.com:$method /static/$anything', '/app3/static/$anything'),
('.*:https?://(.*\\.)?domain3.com:$method /appadmin/$anything', '/app3/appadmin/$anything'),
('.*:https?://(.*\\.)?domain3.com:$method /$anything', '/app3/defcon3/$anything'),
('.*:https?://(.*\\.)?domain3.com:$method /static/$anything',
'/app3/static/$anything'),
('.*:https?://(.*\\.)?domain3.com:$method /appadmin/$anything',
'/app3/appadmin/$anything'),
('.*:https?://(.*\\.)?domain3.com:$method /$anything',
'/app3/defcon3/$anything'),
('/', '/welcome/default'),
('/welcome/default/$anything', '/welcome/default/$anything'),
('/welcome/$anything', '/welcome/default/$anything'),
@@ -228,47 +269,69 @@ routes_out = [
]
'''
load(data=data)
self.assertEqual(filter_url('http://domain.com/welcome/default/f/arg1'),
self.assertEqual(
filter_url('http://domain.com/welcome/default/f/arg1'),
"/welcome/default/f ['arg1']")
self.assertEqual(filter_url('http://domain.com/welcome/default/f/arg1/'),
self.assertEqual(
filter_url('http://domain.com/welcome/default/f/arg1/'),
"/welcome/default/f ['arg1']")
self.assertEqual(filter_url('http://domain.com/welcome/default/f/arg1//'),
self.assertEqual(
filter_url('http://domain.com/welcome/default/f/arg1//'),
"/welcome/default/f ['arg1', '']")
self.assertEqual(filter_url('http://domain.com/welcome/default/f//arg1'),
self.assertEqual(
filter_url('http://domain.com/welcome/default/f//arg1'),
"/welcome/default/f ['', 'arg1']")
self.assertEqual(filter_url('http://domain.com/welcome/default/f/arg1/arg2'),
self.assertEqual(
filter_url('http://domain.com/welcome/default/f/arg1/arg2'),
"/welcome/default/f ['arg1', 'arg2']")
self.assertEqual(filter_url('http://domain.com/welcome/default/f/arg1//arg2'),
self.assertEqual(
filter_url('http://domain.com/welcome/default/f/arg1//arg2'),
"/welcome/default/f ['arg1', '', 'arg2']")
self.assertEqual(filter_url('http://domain.com/welcome/default/f/arg1//arg3/'),
self.assertEqual(
filter_url('http://domain.com/welcome/default/f/arg1//arg3/'),
"/welcome/default/f ['arg1', '', 'arg3']")
self.assertEqual(filter_url('http://domain.com/welcome/default/f/arg1//arg3//'),
self.assertEqual(
filter_url('http://domain.com/welcome/default/f/arg1//arg3//'),
"/welcome/default/f ['arg1', '', 'arg3', '']")
self.assertEqual(filter_url('http://domain.com/welcome/default/f', out=True), "/f")
self.assertEqual(
filter_url('http://domain.com/welcome/default/f', out=True), "/f")
self.assertEqual(regex_filter_out('/welcome/default/f'), "/f")
self.assertEqual(str(URL(a='welcome', c='default', f='f', args=None)), "/f")
self.assertEqual(str(URL(a='welcome', c='default', f='f', args=['arg1'])), "/f/arg1")
self.assertEqual(str(URL(a='welcome', c='default', f='f', args=['arg1', ''])), "/f/arg1//")
self.assertEqual(str(URL(a='welcome', c='default', f='f', args=['arg1', '', 'arg3'])), "/f/arg1//arg3")
self.assertEqual(str(URL(a='welcome', c='default', f='f', args=['ar g'])), "/f/ar%20g")
self.assertEqual(str(URL(a='welcome', c='default', f='f', args=['årg'])), "/f/%C3%A5rg")
self.assertEqual(str(URL(a='welcome', c='default', f='fünc')), "/f\xc3\xbcnc")
self.assertEqual(
str(URL(a='welcome', c='default', f='f', args=None)), "/f")
self.assertEqual(str(
URL(a='welcome', c='default', f='f', args=['arg1'])), "/f/arg1")
self.assertEqual(str(URL(
a='welcome', c='default', f='f', args=['arg1', ''])), "/f/arg1//")
self.assertEqual(str(URL(a='welcome', c='default', f='f',
args=['arg1', '', 'arg3'])), "/f/arg1//arg3")
self.assertEqual(str(
URL(a='welcome', c='default', f='f', args=['ar g'])), "/f/ar%20g")
self.assertEqual(str(URL(
a='welcome', c='default', f='f', args=['årg'])), "/f/%C3%A5rg")
self.assertEqual(
str(URL(a='welcome', c='default', f='fünc')), "/f\xc3\xbcnc")
def test_routes_anchor(self):
'''
Test URL with anchor
'''
self.assertEqual(str(URL(a='a', c='c', f='f', anchor='anchor')), "/a/c/f#anchor")
self.assertEqual(
str(URL(a='a', c='c', f='f', anchor='anchor')), "/a/c/f#anchor")
load(data='')
self.assertEqual(str(URL(a='a', c='c', f='f', anchor='anchor')), "/a/c/f#anchor")
self.assertEqual(
str(URL(a='a', c='c', f='f', anchor='anchor')), "/a/c/f#anchor")
args = ['a1', 'a2']
self.assertEqual(str(URL(a='a', c='c', f='f', args=args, anchor='anchor')),
self.assertEqual(
str(URL(a='a', c='c', f='f', args=args, anchor='anchor')),
"/a/c/f/a1/a2#anchor")
vars = dict(v1=1, v2=2)
self.assertEqual(str(URL(a='a', c='c', f='f', vars=vars, anchor='anchor')),
self.assertEqual(
str(URL(a='a', c='c', f='f', vars=vars, anchor='anchor')),
"/a/c/f?v1=1&v2=2#anchor")
self.assertEqual(str(URL(a='a', c='c', f='f', args=args, vars=vars, anchor='anchor')),
self.assertEqual(
str(URL(
a='a', c='c', f='f', args=args, vars=vars, anchor='anchor')),
"/a/c/f/a1/a2?v1=1&v2=2#anchor")
data = r'''routes_out = [
@@ -276,8 +339,9 @@ routes_out = [
]'''
load(data=data)
self.assertEqual(str(URL(a='init', c='default', f='index')),
"/")
self.assertEqual(str(URL(a='init', c='default', f='index', anchor='anchor')),
"/")
self.assertEqual(
str(URL(a='init', c='default', f='index', anchor='anchor')),
"/init/default/index#anchor")
data = r'''routes_out = [
@@ -285,8 +349,9 @@ routes_out = [
]'''
load(data=data)
self.assertEqual(str(URL(a='init', c='default', f='index')),
"/")
self.assertEqual(str(URL(a='init', c='default', f='index', anchor='anchor')),
"/")
self.assertEqual(
str(URL(a='init', c='default', f='index', anchor='anchor')),
"/#anchor")
data = r'''routes_out = [
@@ -294,13 +359,17 @@ routes_out = [
]'''
load(data=data)
self.assertEqual(str(URL(a='init', c='default', f='index')),
"/")
self.assertEqual(str(URL(a='init', c='default', f='index', anchor='anchor')),
"/")
self.assertEqual(
str(URL(a='init', c='default', f='index', anchor='anchor')),
"/#anchor")
query = dict(var='abc')
self.assertEqual(str(URL(a='init', c='default', f='index', vars=query)),
self.assertEqual(
str(URL(a='init', c='default', f='index', vars=query)),
"/?var=abc")
self.assertEqual(str(URL(a='init', c='default', f='index', vars=query, anchor='anchor')),
self.assertEqual(
str(URL(a='init', c='default', f='index',
vars=query, anchor='anchor')),
"/?var=abc#anchor")
def test_routes_absolute(self):
@@ -311,37 +380,46 @@ routes_out = [
r = Storage()
r.env = Storage()
r.env.http_host = 'domain.com'
r.env.wsgi_url_scheme = 'httpx' # distinguish incoming scheme
r.env.wsgi_url_scheme = 'httpx' # distinguish incoming scheme
self.assertEqual(str(URL(r=r, a='a', c='c', f='f')), "/a/c/f")
self.assertEqual(str(URL(r=r, a='a', c='c', f='f', host=True)),
"httpx://domain.com/a/c/f")
"httpx://domain.com/a/c/f")
self.assertEqual(str(URL(r=r, a='a', c='c', f='f', host='host.com')),
"httpx://host.com/a/c/f")
"httpx://host.com/a/c/f")
self.assertEqual(str(URL(r=r, a='a', c='c', f='f', scheme=True)),
"httpx://domain.com/a/c/f")
"httpx://domain.com/a/c/f")
self.assertEqual(str(URL(r=r, a='a', c='c', f='f', scheme=False)),
"/a/c/f")
"/a/c/f")
self.assertEqual(str(URL(r=r, a='a', c='c', f='f', scheme='https')),
"https://domain.com/a/c/f")
"https://domain.com/a/c/f")
self.assertEqual(str(URL(r=r, a='a', c='c', f='f', scheme='wss')),
"wss://domain.com/a/c/f")
self.assertEqual(str(URL(r=r, a='a', c='c', f='f', scheme=True, host=True)),
"wss://domain.com/a/c/f")
self.assertEqual(
str(URL(r=r, a='a', c='c', f='f', scheme=True, host=True)),
"httpx://domain.com/a/c/f")
self.assertEqual(str(URL(r=r, a='a', c='c', f='f', scheme='https', host=True)),
self.assertEqual(
str(URL(r=r, a='a', c='c', f='f', scheme='https', host=True)),
"https://domain.com/a/c/f")
self.assertEqual(str(URL(r=r, a='a', c='c', f='f', scheme=False, host=True)),
self.assertEqual(
str(URL(r=r, a='a', c='c', f='f', scheme=False, host=True)),
"httpx://domain.com/a/c/f")
self.assertEqual(str(URL(r=r, a='a', c='c', f='f', scheme=True, host='host.com')),
self.assertEqual(
str(URL(r=r, a='a', c='c', f='f', scheme=True, host='host.com')),
"httpx://host.com/a/c/f")
self.assertEqual(str(URL(r=r, a='a', c='c', f='f', scheme=False, host='host.com')),
self.assertEqual(
str(URL(r=r, a='a', c='c', f='f', scheme=False, host='host.com')),
"httpx://host.com/a/c/f")
self.assertEqual(str(URL(r=r, a='a', c='c', f='f', port=1234)),
"httpx://domain.com:1234/a/c/f")
self.assertEqual(
str(URL(r=r, a='a', c='c', f='f', scheme=True, port=1234)),
"httpx://domain.com:1234/a/c/f")
self.assertEqual(str(URL(r=r, a='a', c='c', f='f', scheme=True, port=1234)),
"httpx://domain.com:1234/a/c/f")
self.assertEqual(str(URL(r=r, a='a', c='c', f='f', host='host.com', port=1234)),
self.assertEqual(
str(URL(r=r, a='a', c='c', f='f', host='host.com', port=1234)),
"httpx://host.com:1234/a/c/f")
self.assertEqual(str(URL(r=r, a='a', c='c', f='f', scheme='wss', host='host.com', port=1234)),
self.assertEqual(
str(URL(r=r, a='a', c='c', f='f', scheme='wss',
host='host.com', port=1234)),
"wss://host.com:1234/a/c/f")
def test_request_uri(self):
@@ -354,13 +432,17 @@ routes_out = [
]
'''
load(data=data)
self.assertEqual(filter_url('http://domain.com/abc', env=True).request_uri,
self.assertEqual(
filter_url('http://domain.com/abc', env=True).request_uri,
'/init/default/abc')
self.assertEqual(filter_url('http://domain.com/abc?def', env=True).request_uri,
self.assertEqual(
filter_url('http://domain.com/abc?def', env=True).request_uri,
'/init/default/abc?def')
self.assertEqual(filter_url('http://domain.com/index/abc', env=True).request_uri,
self.assertEqual(
filter_url('http://domain.com/index/abc', env=True).request_uri,
"/init/default/index/abc")
self.assertEqual(filter_url('http://domain.com/index/a%20bc', env=True).request_uri,
self.assertEqual(
filter_url('http://domain.com/index/a%20bc', env=True).request_uri,
"/init/default/index/a bc")
@@ -368,4 +450,3 @@ if __name__ == '__main__':
setUpModule() # pre-2.7
unittest.main()
tearDownModule()
-3
View File
@@ -39,7 +39,6 @@ class TestStorage(unittest.TestCase):
s.d = list()
self.assertTrue(s.d is s['d'])
def test_store_none(self):
""" Test Storage store-None handling
s.key = None deletes an item
@@ -59,7 +58,6 @@ class TestStorage(unittest.TestCase):
self.assertTrue('a' in s)
self.assertTrue(s.a is None)
def test_item(self):
""" Tests Storage item handling """
@@ -76,4 +74,3 @@ class TestStorage(unittest.TestCase):
if __name__ == '__main__':
unittest.main()
+20 -12
View File
@@ -14,15 +14,17 @@ else:
import unittest
from template import render
class TestVirtualFields(unittest.TestCase):
def testRun(self):
self.assertEqual(render(content='{{for i in range(n):}}{{=i}}{{pass}}',
context=dict(n=3)), '012')
context=dict(n=3)), '012')
self.assertEqual(render(content='{{if n>2:}}ok{{pass}}',
context=dict(n=3)), 'ok')
self.assertEqual(render(content='{{try:}}{{n/0}}{{except:}}fail{{pass}}',
context=dict(n=3)), 'fail')
context=dict(n=3)), 'ok')
self.assertEqual(
render(content='{{try:}}{{n/0}}{{except:}}fail{{pass}}',
context=dict(n=3)), 'fail')
self.assertEqual(render(content='{{="<&>"}}'), '&lt;&amp;&gt;')
self.assertEqual(render(content='"abc"'), '"abc"')
self.assertEqual(render(content='"a\'bc"'), '"a\'bc"')
@@ -38,13 +40,18 @@ class TestVirtualFields(unittest.TestCase):
self.assertEqual(render(content='{{ ="abc" }}'), 'abc')
self.assertEqual(render(content='{{pass\n="abc" }}'), 'abc')
# = recognized only at the beginning of a physical line
self.assertEqual(render(content='{{xyz = "xyz"\n="abc"\n="def"\n=xyz }}'), 'abcdefxyz')
self.assertEqual(render(
content='{{xyz = "xyz"\n="abc"\n="def"\n=xyz }}'), 'abcdefxyz')
# = in python blocks
self.assertEqual(render(content='{{if True:\n="abc"\npass }}'), 'abc')
self.assertEqual(render(content='{{if True:\n="abc"\npass\n="def" }}'), 'abcdef')
self.assertEqual(render(content='{{if False:\n="abc"\npass\n="def" }}'), 'def')
self.assertEqual(render(content='{{if True:\n="abc"\nelse:\n="def"\npass }}'), 'abc')
self.assertEqual(render(content='{{if False:\n="abc"\nelse:\n="def"\npass }}'), 'def')
self.assertEqual(
render(content='{{if True:\n="abc"\npass\n="def" }}'), 'abcdef')
self.assertEqual(
render(content='{{if False:\n="abc"\npass\n="def" }}'), 'def')
self.assertEqual(render(
content='{{if True:\n="abc"\nelse:\n="def"\npass }}'), 'abc')
self.assertEqual(render(
content='{{if False:\n="abc"\nelse:\n="def"\npass }}'), 'def')
# codeblock-leading = handles internal newlines, escaped or not
self.assertEqual(render(content='{{=list((1,2,3))}}'), '[1, 2, 3]')
self.assertEqual(render(content='{{=list((1,2,\\\n3))}}'), '[1, 2, 3]')
@@ -52,10 +59,11 @@ class TestVirtualFields(unittest.TestCase):
# ...but that means no more = operators in the codeblock
self.assertRaises(SyntaxError, render, content='{{="abc"\n="def" }}')
# = embedded in codeblock won't handle newlines in its argument
self.assertEqual(render(content='{{pass\n=list((1,2,\\\n3))}}'), '[1, 2, 3]')
self.assertRaises(SyntaxError, render, content='{{pass\n=list((1,2,\n3))}}')
self.assertEqual(
render(content='{{pass\n=list((1,2,\\\n3))}}'), '[1, 2, 3]')
self.assertRaises(
SyntaxError, render, content='{{pass\n=list((1,2,\n3))}}')
if __name__ == '__main__':
unittest.main()
-1
View File
@@ -25,4 +25,3 @@ class TestUtils(unittest.TestCase):
if __name__ == '__main__':
unittest.main()
+13 -12
View File
@@ -13,6 +13,7 @@ else:
import unittest
from contrib.webclient import WebClient
class TestWeb(unittest.TestCase):
def testWebClient(self):
client = WebClient('http://127.0.0.1:8000/welcome/default/')
@@ -20,13 +21,13 @@ class TestWeb(unittest.TestCase):
client.get('index')
# register
data = dict(first_name = 'Homer',
last_name = 'Simpson',
email = 'homer@web2py.com',
password = 'test',
password_two = 'test',
_formname = 'register')
client.post('user/register',data = data)
data = dict(first_name='Homer',
last_name='Simpson',
email='homer@web2py.com',
password='test',
password_two='test',
_formname='register')
client.post('user/register', data=data)
# logout
client.get('user/logout')
@@ -34,21 +35,22 @@ class TestWeb(unittest.TestCase):
# login again
data = dict(email='homer@web2py.com',
password='test',
_formname = 'login')
client.post('user/login',data = data)
_formname='login')
client.post('user/login', data=data)
# check registration and login were successful
client.get('index')
self.assertTrue('Welcome Homer' in client.text)
client = WebClient('http://127.0.0.1:8000/admin/default/')
client.post('index',data=dict(password='hello'))
client.post('index', data=dict(password='hello'))
client.get('site')
client.get('design/welcome')
class TestStaticCacheControl(unittest.TestCase):
def testWebClient(self):
s=WebClient('http://127.0.0.1:8000/welcome/')
s = WebClient('http://127.0.0.1:8000/welcome/')
s.get('static/js/web2py.js')
assert('expires' not in s.headers)
assert(not s.headers['cache-control'].startswith('max-age'))
@@ -60,4 +62,3 @@ class TestStaticCacheControl(unittest.TestCase):
if __name__ == '__main__':
unittest.main()