File: test_encodeutils.py

package info (click to toggle)
rally 5.0.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,364 kB
  • sloc: python: 42,541; javascript: 487; sh: 198; makefile: 192; xml: 43
file content (89 lines) | stat: -rw-r--r-- 3,379 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from unittest import mock

from rally.utils import encodeutils
from tests.unit import test


def b(s):
    return s.encode("latin-1")


class EncodeUtilsTestCase(test.TestCase):

    def test_safe_decode(self):
        safe_decode = encodeutils.safe_decode
        self.assertRaises(TypeError, safe_decode, True)
        self.assertEqual("ni\xf1o",
                         safe_decode(b("ni\xc3\xb1o"), incoming="utf-8"))
        self.assertEqual("strange",
                         safe_decode(b("\x80strange"), errors="ignore"))

        self.assertEqual("\xc0",
                         safe_decode(b("\xc0"), incoming="iso-8859-1"))

        # Forcing incoming to ascii so it falls back to utf-8
        self.assertEqual("ni\xf1o",
                         safe_decode(b("ni\xc3\xb1o"), incoming="ascii"))

        self.assertEqual("foo", safe_decode(b"foo"))

    def test_safe_encode_none_instead_of_text(self):
        self.assertRaises(TypeError, encodeutils.safe_encode, None)

    def test_safe_encode_bool_instead_of_text(self):
        self.assertRaises(TypeError, encodeutils.safe_encode, True)

    def test_safe_encode_int_instead_of_text(self):
        self.assertRaises(TypeError, encodeutils.safe_encode, 1)

    def test_safe_encode_list_instead_of_text(self):
        self.assertRaises(TypeError, encodeutils.safe_encode, [])

    def test_safe_encode_dict_instead_of_text(self):
        self.assertRaises(TypeError, encodeutils.safe_encode, {})

    def test_safe_encode_tuple_instead_of_text(self):
        self.assertRaises(TypeError, encodeutils.safe_encode, ("foo", "bar",))

    def test_safe_encode_force_incoming_utf8_to_ascii(self):
        # Forcing incoming to ascii so it falls back to utf-8
        self.assertEqual(
            b("ni\xc3\xb1o"),
            encodeutils.safe_encode(b("ni\xc3\xb1o"), incoming="ascii"),
        )

    def test_safe_encode_same_encoding_different_cases(self):
        with mock.patch.object(encodeutils, "safe_decode", mock.Mock()):
            utf8 = encodeutils.safe_encode(
                "foo\xf1bar", encoding="utf-8")
            self.assertEqual(
                encodeutils.safe_encode(utf8, "UTF-8", "utf-8"),
                encodeutils.safe_encode(utf8, "utf-8", "UTF-8"),
            )
            self.assertEqual(
                encodeutils.safe_encode(utf8, "UTF-8", "utf-8"),
                encodeutils.safe_encode(utf8, "utf-8", "utf-8"),
            )
            encodeutils.safe_decode.assert_has_calls([])

    def test_safe_encode_different_encodings(self):
        text = "foo\xc3\xb1bar"
        result = encodeutils.safe_encode(
            text=text, incoming="utf-8", encoding="iso-8859-1")
        self.assertNotEqual(text, result)

        self.assertNotEqual(b("foo\xf1bar"), result)