27
27
class TestFileIds(tests.TestCase):
28
28
"""Test functions which generate file ids"""
30
def assertGenFileId(self, regex, filename):
31
"""gen_file_id should create a file id matching the regex.
33
The file id should be ascii, and should be an 8-bit string
35
file_id = generate_ids.gen_file_id(filename)
36
self.assertContainsRe(file_id, '^'+regex+'$')
37
# It should be a utf8 file_id, not a unicode one
38
self.assertIsInstance(file_id, str)
39
# gen_file_id should always return ascii file ids.
40
file_id.decode('ascii')
30
42
def test_gen_file_id(self):
31
43
gen_file_id = generate_ids.gen_file_id
43
55
# we remove unicode characters, and still don't end up with a
45
57
self.assertStartsWith(gen_file_id(u'\xe5\xb5.txt'), 'txt-')
47
59
# Our current method of generating unique ids adds 33 characters
48
60
# plus an serial number (log10(N) characters)
49
61
# to the end of the filename. We now restrict the filename portion to
60
72
self.assertStartsWith(fid, 'abcdefghijklmnopqrst-')
61
73
self.failUnless(len(fid) < 60)
75
def test_file_ids_are_ascii(self):
76
tail = r'-\d{14}-[a-z0-9]{16}-\d+'
77
self.assertGenFileId('foo' + tail, 'foo')
78
self.assertGenFileId('foo' + tail, u'foo')
79
self.assertGenFileId('bar' + tail, u'bar')
80
self.assertGenFileId('br' + tail, u'b\xe5r')
63
82
def test__next_id_suffix_sets_suffix(self):
64
83
generate_ids._gen_file_id_suffix = None
65
84
generate_ids._next_id_suffix()
102
121
"""gen_revision_id should create a revision id matching the regex"""
103
122
revision_id = generate_ids.gen_revision_id(username, timestamp)
104
123
self.assertMatchesRe(regex, revision_id)
124
# It should be a utf8 revision_id, not a unicode one
125
self.assertIsInstance(revision_id, str)
126
# gen_revision_id should always return ascii revision ids.
127
revision_id.decode('ascii')
106
129
def test_timestamp(self):
107
130
"""passing a timestamp should cause it to be used"""
123
146
def test_gen_revision_id_user(self):
124
147
"""If there is no email, fall back to the whole username"""
125
148
tail = r'-\d{14}-[a-z0-9]{16}'
126
self.assertGenRevisionId('joe_bar' + tail,'Joe Bar')
149
self.assertGenRevisionId('joe_bar' + tail, 'Joe Bar')
127
150
self.assertGenRevisionId('joebar' + tail, 'joebar')
128
151
self.assertGenRevisionId('joe_br' + tail, u'Joe B\xe5r')
129
152
self.assertGenRevisionId(r'joe_br_user\+joe_bar_foo-bar.com' + tail,
130
153
u'Joe B\xe5r <user+Joe_Bar_Foo-Bar.com>')
155
def test_revision_ids_are_ascii(self):
156
"""gen_revision_id should always return an ascii revision id."""
157
tail = r'-\d{14}-[a-z0-9]{16}'
158
self.assertGenRevisionId('joe_bar' + tail, 'Joe Bar')
159
self.assertGenRevisionId('joe_bar' + tail, u'Joe Bar')
160
self.assertGenRevisionId('joe@foo' + tail, u'Joe Bar <joe@foo>')
161
# We cheat a little with this one, because email-addresses shouldn't
162
# contain non-ascii characters, but generate_ids should strip them
164
self.assertGenRevisionId('joe@f' + tail, u'Joe Bar <joe@f\xb6>')