2013-07-01 12:22:27 +00:00
|
|
|
from unittest import TestCase
|
|
|
|
|
2021-05-05 21:28:08 +00:00
|
|
|
from django.test import ignore_warnings
|
|
|
|
from django.utils.deprecation import RemovedInDjango50Warning
|
2011-05-21 14:41:14 +00:00
|
|
|
|
2021-05-05 21:28:08 +00:00
|
|
|
with ignore_warnings(category=RemovedInDjango50Warning):
|
|
|
|
from django.utils.baseconv import (
|
|
|
|
BaseConverter,
|
|
|
|
base2,
|
|
|
|
base16,
|
|
|
|
base36,
|
|
|
|
base56,
|
|
|
|
base62,
|
|
|
|
base64,
|
|
|
|
)
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2021-05-05 21:28:08 +00:00
|
|
|
|
|
|
|
# RemovedInDjango50Warning
|
2011-05-21 14:41:14 +00:00
|
|
|
class TestBaseConv(TestCase):
|
|
|
|
def test_baseconv(self):
|
2018-09-28 13:57:12 +00:00
|
|
|
nums = [-(10**10), 10**10, *range(-100, 100)]
|
2011-05-21 14:41:14 +00:00
|
|
|
for converter in [base2, base16, base36, base56, base62, base64]:
|
|
|
|
for i in nums:
|
|
|
|
self.assertEqual(i, converter.decode(converter.encode(i)))
|
|
|
|
|
|
|
|
def test_base11(self):
|
|
|
|
base11 = BaseConverter("0123456789-", sign="$")
|
|
|
|
self.assertEqual(base11.encode(1234), "-22")
|
|
|
|
self.assertEqual(base11.decode("-22"), 1234)
|
|
|
|
self.assertEqual(base11.encode(-1234), "$-22")
|
|
|
|
self.assertEqual(base11.decode("$-22"), -1234)
|
|
|
|
|
|
|
|
def test_base20(self):
|
|
|
|
base20 = BaseConverter("0123456789abcdefghij")
|
|
|
|
self.assertEqual(base20.encode(1234), "31e")
|
|
|
|
self.assertEqual(base20.decode("31e"), 1234)
|
|
|
|
self.assertEqual(base20.encode(-1234), "-31e")
|
|
|
|
self.assertEqual(base20.decode("-31e"), -1234)
|
|
|
|
|
|
|
|
def test_base64(self):
|
|
|
|
self.assertEqual(base64.encode(1234), "JI")
|
|
|
|
self.assertEqual(base64.decode("JI"), 1234)
|
|
|
|
self.assertEqual(base64.encode(-1234), "$JI")
|
|
|
|
self.assertEqual(base64.decode("$JI"), -1234)
|
|
|
|
|
|
|
|
def test_base7(self):
|
|
|
|
base7 = BaseConverter("cjdhel3", sign="g")
|
|
|
|
self.assertEqual(base7.encode(1234), "hejd")
|
|
|
|
self.assertEqual(base7.decode("hejd"), 1234)
|
|
|
|
self.assertEqual(base7.encode(-1234), "ghejd")
|
|
|
|
self.assertEqual(base7.decode("ghejd"), -1234)
|
|
|
|
|
|
|
|
def test_exception(self):
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
BaseConverter("abc", sign="a")
|
2013-05-21 09:42:15 +00:00
|
|
|
self.assertIsInstance(BaseConverter("abc", sign="d"), BaseConverter)
|
2016-11-29 05:54:17 +00:00
|
|
|
|
|
|
|
def test_repr(self):
|
|
|
|
base7 = BaseConverter("cjdhel3", sign="g")
|
|
|
|
self.assertEqual(repr(base7), "<BaseConverter: base7 (cjdhel3)>")
|