Package openid :: Package test :: Module kvform
[frames] | no frames]

Source Code for Module openid.test.kvform

  1  from openid import kvform 
  2  from openid import oidutil 
  3  import unittest 
  4   
5 -class KVBaseTest(unittest.TestCase):
6 - def shortDescription(self):
7 return '%s test for %r' % (self.__class__.__name__, self.kvform)
8
9 - def log(self, message, unused_priority=None):
10 self.warnings.append(message)
11
12 - def checkWarnings(self, num_warnings):
13 self.failUnlessEqual(num_warnings, len(self.warnings), repr(self.warnings))
14
15 - def setUp(self):
16 self.warnings = [] 17 self.old_log = oidutil.log 18 self.log_func = oidutil.log = self.log 19 self.failUnless(self.log_func is oidutil.log, 20 (oidutil.log, self.log_func))
21
22 - def tearDown(self):
23 oidutil.log = self.old_log
24
25 -class KVDictTest(KVBaseTest):
26 - def __init__(self, kv, dct, warnings):
27 unittest.TestCase.__init__(self) 28 self.kvform = kv 29 self.dict = dct 30 self.expected_warnings = warnings
31
32 - def runTest(self):
33 # Convert KVForm to dict 34 d = kvform.kvToDict(self.kvform) 35 36 # make sure it parses to expected dict 37 self.failUnlessEqual(self.dict, d) 38 39 # Check to make sure we got the expected number of warnings 40 self.checkWarnings(self.expected_warnings) 41 42 # Convert back to KVForm and round-trip back to dict to make 43 # sure that *** dict -> kv -> dict is identity. *** 44 kv = kvform.dictToKV(d) 45 d2 = kvform.kvToDict(kv) 46 self.failUnlessEqual(d, d2)
47
48 -class KVSeqTest(KVBaseTest):
49 - def __init__(self, seq, kv, expected_warnings):
50 unittest.TestCase.__init__(self) 51 self.kvform = kv 52 self.seq = seq 53 self.expected_warnings = expected_warnings
54
55 - def cleanSeq(self, seq):
56 """Create a new sequence by stripping whitespace from start 57 and end of each value of each pair""" 58 clean = [] 59 for k, v in self.seq: 60 if type(k) is str: 61 k = k.decode('utf8') 62 if type(v) is str: 63 v = v.decode('utf8') 64 clean.append((k.strip(), v.strip())) 65 return clean
66
67 - def runTest(self):
68 # seq serializes to expected kvform 69 actual = kvform.seqToKV(self.seq) 70 self.failUnlessEqual(self.kvform, actual) 71 self.failUnless(type(actual) is str) 72 73 # Parse back to sequence. Expected to be unchanged, except 74 # stripping whitespace from start and end of values 75 # (i. e. ordering, case, and internal whitespace is preserved) 76 seq = kvform.kvToSeq(actual) 77 clean_seq = self.cleanSeq(seq) 78 79 self.failUnlessEqual(seq, clean_seq) 80 self.checkWarnings(self.expected_warnings)
81 82 kvdict_cases = [ 83 # (kvform, parsed dictionary, expected warnings) 84 ('', {}, 0), 85 ('college:harvey mudd\n', {'college':'harvey mudd'}, 0), 86 ('city:claremont\nstate:CA\n', 87 {'city':'claremont', 'state':'CA'}, 0), 88 ('is_valid:true\ninvalidate_handle:{HMAC-SHA1:2398410938412093}\n', 89 {'is_valid':'true', 90 'invalidate_handle':'{HMAC-SHA1:2398410938412093}'}, 0), 91 92 # Warnings from lines with no colon: 93 ('x\n', {}, 1), 94 ('x\nx\n', {}, 2), 95 ('East is least\n', {}, 1), 96 97 # But not from blank lines (because LJ generates them) 98 ('x\n\n', {}, 1), 99 100 # Warning from empty key 101 (':\n', {'':''}, 1), 102 (':missing key\n', {'':'missing key'}, 1), 103 104 # Warnings from leading or trailing whitespace in key or value 105 (' street:foothill blvd\n', {'street':'foothill blvd'}, 1), 106 ('major: computer science\n', {'major':'computer science'}, 1), 107 (' dorm : east \n', {'dorm':'east'}, 2), 108 109 # Warnings from missing trailing newline 110 ('e^(i*pi)+1:0', {'e^(i*pi)+1':'0'}, 1), 111 ('east:west\nnorth:south', {'east':'west', 'north':'south'}, 1), 112 ] 113 114 kvseq_cases = [ 115 ([], '', 0), 116 117 # Make sure that we handle non-ascii characters (also wider than 8 bits) 118 ([(u'\u03bbx', u'x')], '\xce\xbbx:x\n', 0), 119 120 # If it's a UTF-8 str, make sure that it's equivalent to the same 121 # string, decoded. 122 ([('\xce\xbbx', 'x')], '\xce\xbbx:x\n', 0), 123 124 ([('openid', 'useful'), ('a', 'b')], 'openid:useful\na:b\n', 0), 125 126 # Warnings about leading whitespace 127 ([(' openid', 'useful'), ('a', 'b')], ' openid:useful\na:b\n', 2), 128 129 # Warnings about leading and trailing whitespace 130 ([(' openid ', ' useful '), 131 (' a ', ' b ')], ' openid : useful \n a : b \n', 8), 132 133 # warnings about leading and trailing whitespace, but not about 134 # internal whitespace. 135 ([(' open id ', ' use ful '), 136 (' a ', ' b ')], ' open id : use ful \n a : b \n', 8), 137 138 ([(u'foo', 'bar')], 'foo:bar\n', 0), 139 ] 140 141 kvexc_cases = [ 142 [('openid', 'use\nful')], 143 [('open\nid', 'useful')], 144 [('open\nid', 'use\nful')], 145 [('open:id', 'useful')], 146 [('foo', 'bar'), ('ba\n d', 'seed')], 147 [('foo', 'bar'), ('bad:', 'seed')], 148 ] 149
150 -class KVExcTest(unittest.TestCase):
151 - def __init__(self, seq):
152 unittest.TestCase.__init__(self) 153 self.seq = seq
154
155 - def shortDescription(self):
156 return 'KVExcTest for %r' % (self.seq,)
157
158 - def runTest(self):
159 self.failUnlessRaises(ValueError, kvform.seqToKV, self.seq)
160
161 -class GeneralTest(KVBaseTest):
162 kvform = '<None>' 163
164 - def test_convert(self):
165 result = kvform.seqToKV([(1,1)]) 166 self.failUnlessEqual(result, '1:1\n') 167 self.checkWarnings(2)
168
169 -def pyUnitTests():
170 tests = [KVDictTest(*case) for case in kvdict_cases] 171 tests.extend([KVSeqTest(*case) for case in kvseq_cases]) 172 tests.extend([KVExcTest(case) for case in kvexc_cases]) 173 tests.append(unittest.defaultTestLoader.loadTestsFromTestCase(GeneralTest)) 174 return unittest.TestSuite(tests)
175