File tree Expand file tree Collapse file tree 2 files changed +20
-3
lines changed Expand file tree Collapse file tree 2 files changed +20
-3
lines changed Original file line number Diff line number Diff line change @@ -43,8 +43,8 @@ def from_decimal(self, i):
43
43
def to_decimal (self , s ):
44
44
if not isinstance (s , basestring ):
45
45
raise DecodingError ('%s is not a basestring()' % s )
46
- for char in s :
47
- if char not in self .digits :
46
+ for index , char in enumerate ( s ) :
47
+ if char not in self .digits and not char == '-' and not index == 0 :
48
48
raise DecodingError ('Invalid character for encoding: %s' % char )
49
49
return int (self .convert (s , self .digits , self .decimal_digits ))
50
50
Original file line number Diff line number Diff line change @@ -204,9 +204,17 @@ def test_create_with_custom_id(self):
204
204
link = Link .objects .create (id = id , url = 'http://www.python.org' )
205
205
self .assertEqual (link .to_base62 (), base62 .from_decimal (id ))
206
206
207
+ def test_unicode (self ):
208
+ """
209
+ unicode test
210
+ """
211
+ url = 'http://www.python.org'
212
+ link = Link .objects .create (url = url )
213
+ self .assertTrue (url in unicode (link ))
214
+
207
215
208
216
class BaseconvTestCase (TestCase ):
209
- def test_symmetry_int (self ):
217
+ def test_symmetry_positive_int (self ):
210
218
"""
211
219
symmetry for encoding/decoding values
212
220
"""
@@ -215,6 +223,15 @@ def test_symmetry_int(self):
215
223
encoded_int = base62 .from_decimal (random_int )
216
224
self .assertEqual (random_int , base62 .to_decimal (encoded_int ))
217
225
226
+ def test_symmetry_negative_int (self ):
227
+ """
228
+ symmetry for negative numbers
229
+ """
230
+ for x in xrange (1000 ):
231
+ random_int = random .randint (- 1 * sys .maxint - 1 , 0 )
232
+ encoded_int = base62 .from_decimal (random_int )
233
+ self .assertEqual (random_int , base62 .to_decimal (encoded_int ))
234
+
218
235
def test_encoding_non_int_fails (self ):
219
236
"""
220
237
calling from_decimal() on letters raises an EncodingError
You can’t perform that action at this time.
0 commit comments