summaryrefslogtreecommitdiff
path: root/tests/unit/test_utils.py
blob: 9f909830d85fc626c7bc1a2e9c616aca2b54c37d (plain)
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
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019 Gauvain Pocentek <gauvain@pocentek.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import json

from gitlab import utils


class TestEncodedId:
    def test_init_str(self):
        obj = utils.EncodedId("Hello")
        assert "Hello" == obj
        assert "Hello" == str(obj)
        assert "Hello" == f"{obj}"

        obj = utils.EncodedId("this/is a/path")
        assert "this%2Fis%20a%2Fpath" == str(obj)
        assert "this%2Fis%20a%2Fpath" == f"{obj}"

    def test_init_int(self):
        obj = utils.EncodedId(23)
        assert 23 == obj
        assert "23" == str(obj)
        assert "23" == f"{obj}"

    def test_init_encodeid_str(self):
        value = "Goodbye"
        obj_init = utils.EncodedId(value)
        obj = utils.EncodedId(obj_init)
        assert value == str(obj)
        assert value == f"{obj}"

        value = "we got/a/path"
        expected = "we%20got%2Fa%2Fpath"
        obj_init = utils.EncodedId(value)
        assert expected == str(obj_init)
        assert expected == f"{obj_init}"
        # Show that no matter how many times we recursively call it we still only
        # URL-encode it once.
        obj = utils.EncodedId(
            utils.EncodedId(utils.EncodedId(utils.EncodedId(utils.EncodedId(obj_init))))
        )
        assert expected == str(obj)
        assert expected == f"{obj}"

        # Show assignments still only encode once
        obj2 = obj
        assert expected == str(obj2)
        assert expected == f"{obj2}"

    def test_init_encodeid_int(self):
        value = 23
        expected = f"{value}"
        obj_init = utils.EncodedId(value)
        obj = utils.EncodedId(obj_init)
        assert expected == str(obj)
        assert expected == f"{obj}"

    def test_json_serializable(self):
        obj = utils.EncodedId("someone")
        assert '"someone"' == json.dumps(obj)

        obj = utils.EncodedId("we got/a/path")
        assert '"we%20got%2Fa%2Fpath"' == json.dumps(obj)