Changeset View
Changeset View
Standalone View
Standalone View
swh/core/tests/test_utils.py
# Copyright (C) 2015-2018 The Software Heritage developers | # Copyright (C) 2015-2018 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import unittest | import unittest | ||||
from swh.core import utils | from swh.core import utils | ||||
class UtilsLib(unittest.TestCase): | class UtilsLib(unittest.TestCase): | ||||
def test_grouper(self): | def test_grouper(self): | ||||
# given | # given | ||||
actual_data = utils.grouper((i for i in range(0, 9)), 2) | actual_data = utils.grouper((i for i in range(0, 9)), 2) | ||||
out = [] | out = [] | ||||
for d in actual_data: | for d in actual_data: | ||||
out.append(list(d)) # force generator resolution for checks | out.append(list(d)) # force generator resolution for checks | ||||
self.assertEqual(out, [[0, 1], [2, 3], [4, 5], [6, 7], [8]]) | self.assertEqual(out, [[0, 1], [2, 3], [4, 5], [6, 7], [8]]) | ||||
# given | # given | ||||
actual_data = utils.grouper((i for i in range(9, 0, -1)), 4) | actual_data = utils.grouper((i for i in range(9, 0, -1)), 4) | ||||
out = [] | out = [] | ||||
for d in actual_data: | for d in actual_data: | ||||
out.append(list(d)) # force generator resolution for checks | out.append(list(d)) # force generator resolution for checks | ||||
self.assertEqual(out, [[9, 8, 7, 6], [5, 4, 3, 2], [1]]) | self.assertEqual(out, [[9, 8, 7, 6], [5, 4, 3, 2], [1]]) | ||||
def test_grouper_with_stop_value(self): | def test_grouper_with_stop_value(self): | ||||
# given | # given | ||||
actual_data = utils.grouper(((i, i+1) for i in range(0, 9)), 2) | actual_data = utils.grouper(((i, i + 1) for i in range(0, 9)), 2) | ||||
out = [] | out = [] | ||||
for d in actual_data: | for d in actual_data: | ||||
out.append(list(d)) # force generator resolution for checks | out.append(list(d)) # force generator resolution for checks | ||||
self.assertEqual(out, [ | self.assertEqual( | ||||
out, | |||||
[ | |||||
[(0, 1), (1, 2)], | [(0, 1), (1, 2)], | ||||
[(2, 3), (3, 4)], | [(2, 3), (3, 4)], | ||||
[(4, 5), (5, 6)], | [(4, 5), (5, 6)], | ||||
[(6, 7), (7, 8)], | [(6, 7), (7, 8)], | ||||
[(8, 9)]]) | [(8, 9)], | ||||
], | |||||
) | |||||
# given | # given | ||||
actual_data = utils.grouper((i for i in range(9, 0, -1)), 4) | actual_data = utils.grouper((i for i in range(9, 0, -1)), 4) | ||||
out = [] | out = [] | ||||
for d in actual_data: | for d in actual_data: | ||||
out.append(list(d)) # force generator resolution for checks | out.append(list(d)) # force generator resolution for checks | ||||
self.assertEqual(out, [[9, 8, 7, 6], [5, 4, 3, 2], [1]]) | self.assertEqual(out, [[9, 8, 7, 6], [5, 4, 3, 2], [1]]) | ||||
def test_backslashescape_errors(self): | def test_backslashescape_errors(self): | ||||
raw_data_err = b'abcd\x80' | raw_data_err = b"abcd\x80" | ||||
with self.assertRaises(UnicodeDecodeError): | with self.assertRaises(UnicodeDecodeError): | ||||
raw_data_err.decode('utf-8', 'strict') | raw_data_err.decode("utf-8", "strict") | ||||
self.assertEqual( | self.assertEqual(raw_data_err.decode("utf-8", "backslashescape"), "abcd\\x80") | ||||
raw_data_err.decode('utf-8', 'backslashescape'), | |||||
'abcd\\x80', | |||||
) | |||||
raw_data_ok = b'abcd\xc3\xa9' | raw_data_ok = b"abcd\xc3\xa9" | ||||
self.assertEqual( | self.assertEqual( | ||||
raw_data_ok.decode('utf-8', 'backslashescape'), | raw_data_ok.decode("utf-8", "backslashescape"), | ||||
raw_data_ok.decode('utf-8', 'strict'), | raw_data_ok.decode("utf-8", "strict"), | ||||
) | ) | ||||
unicode_data = 'abcdef\u00a3' | unicode_data = "abcdef\u00a3" | ||||
self.assertEqual( | self.assertEqual( | ||||
unicode_data.encode('ascii', 'backslashescape'), | unicode_data.encode("ascii", "backslashescape"), b"abcdef\\xa3" | ||||
b'abcdef\\xa3', | |||||
) | ) | ||||
def test_encode_with_unescape(self): | def test_encode_with_unescape(self): | ||||
valid_data = '\\x01020304\\x00' | valid_data = "\\x01020304\\x00" | ||||
valid_data_encoded = b'\x01020304\x00' | valid_data_encoded = b"\x01020304\x00" | ||||
self.assertEqual( | self.assertEqual(valid_data_encoded, utils.encode_with_unescape(valid_data)) | ||||
valid_data_encoded, | |||||
utils.encode_with_unescape(valid_data) | |||||
) | |||||
def test_encode_with_unescape_invalid_escape(self): | def test_encode_with_unescape_invalid_escape(self): | ||||
invalid_data = 'test\\abcd' | invalid_data = "test\\abcd" | ||||
with self.assertRaises(ValueError) as exc: | with self.assertRaises(ValueError) as exc: | ||||
utils.encode_with_unescape(invalid_data) | utils.encode_with_unescape(invalid_data) | ||||
self.assertIn('invalid escape', exc.exception.args[0]) | self.assertIn("invalid escape", exc.exception.args[0]) | ||||
self.assertIn('position 4', exc.exception.args[0]) | self.assertIn("position 4", exc.exception.args[0]) | ||||
def test_decode_with_escape(self): | def test_decode_with_escape(self): | ||||
backslashes = b'foo\\bar\\\\baz' | backslashes = b"foo\\bar\\\\baz" | ||||
backslashes_escaped = 'foo\\\\bar\\\\\\\\baz' | backslashes_escaped = "foo\\\\bar\\\\\\\\baz" | ||||
self.assertEqual( | self.assertEqual(backslashes_escaped, utils.decode_with_escape(backslashes)) | ||||
backslashes_escaped, | |||||
utils.decode_with_escape(backslashes), | |||||
) | |||||
valid_utf8 = b'foo\xc3\xa2' | valid_utf8 = b"foo\xc3\xa2" | ||||
valid_utf8_escaped = 'foo\u00e2' | valid_utf8_escaped = "foo\u00e2" | ||||
self.assertEqual( | self.assertEqual(valid_utf8_escaped, utils.decode_with_escape(valid_utf8)) | ||||
valid_utf8_escaped, | |||||
utils.decode_with_escape(valid_utf8), | |||||
) | |||||
invalid_utf8 = b'foo\xa2' | invalid_utf8 = b"foo\xa2" | ||||
invalid_utf8_escaped = 'foo\\xa2' | invalid_utf8_escaped = "foo\\xa2" | ||||
self.assertEqual( | self.assertEqual(invalid_utf8_escaped, utils.decode_with_escape(invalid_utf8)) | ||||
invalid_utf8_escaped, | |||||
utils.decode_with_escape(invalid_utf8), | |||||
) | |||||
valid_utf8_nul = b'foo\xc3\xa2\x00' | valid_utf8_nul = b"foo\xc3\xa2\x00" | ||||
valid_utf8_nul_escaped = 'foo\u00e2\\x00' | valid_utf8_nul_escaped = "foo\u00e2\\x00" | ||||
self.assertEqual( | self.assertEqual( | ||||
valid_utf8_nul_escaped, | valid_utf8_nul_escaped, utils.decode_with_escape(valid_utf8_nul) | ||||
utils.decode_with_escape(valid_utf8_nul), | |||||
) | ) | ||||
def test_commonname(self): | def test_commonname(self): | ||||
# when | # when | ||||
actual_commonname = utils.commonname('/some/where/to/', | actual_commonname = utils.commonname("/some/where/to/", "/some/where/to/go/to") | ||||
'/some/where/to/go/to') | |||||
# then | # then | ||||
self.assertEqual('go/to', actual_commonname) | self.assertEqual("go/to", actual_commonname) | ||||
# when | # when | ||||
actual_commonname2 = utils.commonname(b'/some/where/to/', | actual_commonname2 = utils.commonname( | ||||
b'/some/where/to/go/to') | b"/some/where/to/", b"/some/where/to/go/to" | ||||
) | |||||
# then | # then | ||||
self.assertEqual(b'go/to', actual_commonname2) | self.assertEqual(b"go/to", actual_commonname2) |