getting file size for all dict files to be downloaded. coming to be 400mb or so.
[worddb.git] / libs / openid / test / oidutil.py
blobfbeadf250cf4e50abaa9f4e40e48134fe36a51f1
1 import unittest
2 import codecs
3 import string
4 import random
5 from openid import oidutil
7 def test_base64():
8 allowed_s = string.ascii_letters + string.digits + '+/='
9 allowed_d = {}
10 for c in allowed_s:
11 allowed_d[c] = None
12 isAllowed = allowed_d.has_key
14 def checkEncoded(s):
15 for c in s:
16 assert isAllowed(c), s
18 cases = [
19 '',
20 'x',
21 '\x00',
22 '\x01',
23 '\x00' * 100,
24 ''.join(map(chr, range(256))),
27 for s in cases:
28 b64 = oidutil.toBase64(s)
29 checkEncoded(b64)
30 s_prime = oidutil.fromBase64(b64)
31 assert s_prime == s, (s, b64, s_prime)
33 # Randomized test
34 for _ in xrange(50):
35 n = random.randrange(2048)
36 s = ''.join(map(chr, map(lambda _: random.randrange(256), range(n))))
37 b64 = oidutil.toBase64(s)
38 checkEncoded(b64)
39 s_prime = oidutil.fromBase64(b64)
40 assert s_prime == s, (s, b64, s_prime)
42 class AppendArgsTest(unittest.TestCase):
43 def __init__(self, desc, args, expected):
44 unittest.TestCase.__init__(self)
45 self.desc = desc
46 self.args = args
47 self.expected = expected
49 def runTest(self):
50 result = oidutil.appendArgs(*self.args)
51 self.assertEqual(self.expected, result, self.args)
53 def shortDescription(self):
54 return self.desc
58 class TestSymbol(unittest.TestCase):
59 def testCopyHash(self):
60 import copy
61 s = oidutil.Symbol("Foo")
62 d = {s: 1}
63 d_prime = copy.deepcopy(d)
64 self.failUnless(s in d_prime, "%r isn't in %r" % (s, d_prime))
66 t = oidutil.Symbol("Bar")
67 self.failIfEqual(hash(s), hash(t))
70 def buildAppendTests():
71 simple = 'http://www.example.com/'
72 cases = [
73 ('empty list',
74 (simple, []),
75 simple),
77 ('empty dict',
78 (simple, {}),
79 simple),
81 ('one list',
82 (simple, [('a', 'b')]),
83 simple + '?a=b'),
85 ('one dict',
86 (simple, {'a':'b'}),
87 simple + '?a=b'),
89 ('two list (same)',
90 (simple, [('a', 'b'), ('a', 'c')]),
91 simple + '?a=b&a=c'),
93 ('two list',
94 (simple, [('a', 'b'), ('b', 'c')]),
95 simple + '?a=b&b=c'),
97 ('two list (order)',
98 (simple, [('b', 'c'), ('a', 'b')]),
99 simple + '?b=c&a=b'),
101 ('two dict (order)',
102 (simple, {'b':'c', 'a':'b'}),
103 simple + '?a=b&b=c'),
105 ('escape',
106 (simple, [('=', '=')]),
107 simple + '?%3D=%3D'),
109 ('escape (URL)',
110 (simple, [('this_url', simple)]),
111 simple + '?this_url=http%3A%2F%2Fwww.example.com%2F'),
113 ('use dots',
114 (simple, [('openid.stuff', 'bother')]),
115 simple + '?openid.stuff=bother'),
117 ('args exist (empty)',
118 (simple + '?stuff=bother', []),
119 simple + '?stuff=bother'),
121 ('args exist',
122 (simple + '?stuff=bother', [('ack', 'ack')]),
123 simple + '?stuff=bother&ack=ack'),
125 ('args exist',
126 (simple + '?stuff=bother', [('ack', 'ack')]),
127 simple + '?stuff=bother&ack=ack'),
129 ('args exist (dict)',
130 (simple + '?stuff=bother', {'ack': 'ack'}),
131 simple + '?stuff=bother&ack=ack'),
133 ('args exist (dict 2)',
134 (simple + '?stuff=bother', {'ack': 'ack', 'zebra':'lion'}),
135 simple + '?stuff=bother&ack=ack&zebra=lion'),
137 ('three args (dict)',
138 (simple, {'stuff': 'bother', 'ack': 'ack', 'zebra':'lion'}),
139 simple + '?ack=ack&stuff=bother&zebra=lion'),
141 ('three args (list)',
142 (simple, [('stuff', 'bother'), ('ack', 'ack'), ('zebra', 'lion')]),
143 simple + '?stuff=bother&ack=ack&zebra=lion'),
146 tests = []
148 for name, args, expected in cases:
149 test = AppendArgsTest(name, args, expected)
150 tests.append(test)
152 return unittest.TestSuite(tests)
154 def pyUnitTests():
155 some = buildAppendTests()
156 some.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestSymbol))
157 return some
159 def test_appendArgs():
160 suite = buildAppendTests()
161 suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestSymbol))
162 runner = unittest.TextTestRunner()
163 result = runner.run(suite)
164 assert result.wasSuccessful()
166 # XXX: there are more functions that could benefit from being better
167 # specified and tested in oidutil.py These include, but are not
168 # limited to appendArgs
170 def test(skipPyUnit=True):
171 test_base64()
172 if not skipPyUnit:
173 test_appendArgs()
175 if __name__ == '__main__':
176 test(skipPyUnit=False)