File: translationentry.py

package info (click to toggle)
libmodulemd 2.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 5,152 kB
  • sloc: ansic: 37,845; python: 3,236; xml: 1,739; sh: 377; makefile: 42
file content (241 lines) | stat: -rw-r--r-- 8,818 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/python3
# -*- coding: utf-8 -*-

# This file is part of libmodulemd
# Copyright (C) 2018 Red Hat, Inc.
#
# Fedora-License-Identifier: MIT
# SPDX-2.0-License-Identifier: MIT
# SPDX-3.0-License-Identifier: MIT
#
# This program is free software.
# For more information on the license, see COPYING.
# For more information on free software, see
# <https://www.gnu.org/philosophy/free-sw.en.html>.

import sys

try:
    import unittest
    import gi

    gi.require_version("Modulemd", "2.0")
    from gi.repository import Modulemd
except ImportError:
    # Return error 77 to skip this test on platforms without the necessary
    # python modules
    sys.exit(77)

from base import TestBase


class TestTranslationEntry(TestBase):
    def test_constructors(self):
        # Test that the new() function works
        te = Modulemd.TranslationEntry.new("en_US")
        assert te
        assert te.props.locale == "en_US"
        assert te.get_locale() == "en_US"
        assert te.props.summary is None
        assert te.get_summary() is None
        assert te.props.description is None
        assert te.get_description() is None
        assert te.get_profiles() == []
        assert te.get_profile_description("test") is None

        # Test that keyword arg locale is accepted
        te = Modulemd.TranslationEntry(locale="en_US")
        assert te
        assert te.props.locale == "en_US"
        assert te.get_locale() == "en_US"
        assert te.props.summary is None
        assert te.get_summary() is None
        assert te.props.description is None
        assert te.get_description() is None
        assert te.get_profiles() == []
        assert te.get_profile_description("test") is None

        # Test that init works with locale and summary
        te = Modulemd.TranslationEntry(locale="en_US", summary="foobar")
        assert te
        assert te.props.locale == "en_US"
        assert te.get_locale() == "en_US"
        assert te.props.summary == "foobar"
        assert te.get_summary() == "foobar"
        assert te.props.description is None
        assert te.get_description() is None
        assert te.get_profiles() == []
        assert te.get_profile_description("test") is None

        # Test that init works with locale and description
        te = Modulemd.TranslationEntry(locale="en_US", description="barfoo")
        assert te
        assert te.props.locale == "en_US"
        assert te.get_locale() == "en_US"
        assert te.props.summary is None
        assert te.get_summary() is None
        assert te.props.description == "barfoo"
        assert te.get_description() == "barfoo"
        assert te.get_profiles() == []
        assert te.get_profile_description("test") is None

        # Test that init works with locale, summary and description
        te = Modulemd.TranslationEntry(
            locale="en_US", summary="foobar", description="barfoo"
        )
        assert te
        assert te.props.locale == "en_US"
        assert te.get_locale() == "en_US"
        assert te.props.summary == "foobar"
        assert te.get_summary() == "foobar"
        assert te.props.description == "barfoo"
        assert te.get_description() == "barfoo"
        assert te.get_profiles() == []
        assert te.get_profile_description("test") is None

        # Test that init works with locale, unicode summary and unicode
        # description
        te = Modulemd.TranslationEntry(
            locale="ro_TA",  # robots_Tables
            summary="(┛ಠ_ಠ)┛彡┻━┻",
            description="(┛ಠ_ಠ)┛彡",
        )
        assert te
        assert te.props.locale == "ro_TA"
        assert te.get_locale() == "ro_TA"
        assert te.props.summary == "(┛ಠ_ಠ)┛彡┻━┻"
        assert te.get_summary() == "(┛ಠ_ಠ)┛彡┻━┻"
        assert te.props.description == "(┛ಠ_ಠ)┛彡"
        assert te.get_description() == "(┛ಠ_ಠ)┛彡"
        assert te.get_profiles() == []
        assert te.get_profile_description("test") is None

        # Test that we fail if we call new() with a None locale
        try:
            te = Modulemd.TranslationEntry.new(None)
            assert False
        except TypeError as e:
            assert "does not allow None as a value" in e.__str__()

        # Test that we fail if object is instantiated without a locale
        with self.expect_signal():
            Modulemd.TranslationEntry()

        # Test that we fail if object is instantiated with a None locale
        with self.expect_signal():
            Modulemd.TranslationEntry(locale=None)

    def test_copy(self):
        te_orig = Modulemd.TranslationEntry(locale="en_US")
        te = te_orig.copy()
        assert te
        assert te.props.locale == "en_US"
        assert te.get_locale() == "en_US"
        assert te.props.summary is None
        assert te.get_summary() is None
        assert te.props.description is None
        assert te.get_description() is None
        assert te.get_profiles() == []
        assert te.get_profile_description("test") is None

        te_orig.set_summary("foobar")
        te_orig.set_description("barfoo")
        te_orig.set_profile_description("test1", "brown fox")
        te_orig.set_profile_description("test2", "jumped")

        te = te_orig.copy()
        assert te
        assert te.props.locale == "en_US"
        assert te.get_locale() == "en_US"
        assert te.props.summary == "foobar"
        assert te.get_summary() == "foobar"
        assert te.props.description == "barfoo"
        assert te.get_description() == "barfoo"
        assert te.get_profiles() == ["test1", "test2"]
        assert te.get_profile_description("test") is None
        assert te.get_profile_description("test1") == "brown fox"
        assert te.get_profile_description("test2") == "jumped"

    def test_get_locale(self):
        te = Modulemd.TranslationEntry(locale="en_US")

        assert te.get_locale() == "en_US"
        assert te.props.locale == "en_US"

        with self.expect_signal():
            te.props.locale = "en_GB"

    def test_get_set_summary(self):
        te = Modulemd.TranslationEntry(locale="en_US")

        assert te.props.locale == "en_US"
        assert te.get_locale() == "en_US"
        assert te.props.summary is None
        assert te.get_summary() is None

        te.set_summary("foobar")
        assert te.props.summary == "foobar"
        assert te.get_summary() == "foobar"

        te.props.summary = "barfoo"
        assert te.props.summary == "barfoo"
        assert te.get_summary() == "barfoo"

        te.props.summary = None
        assert te.props.summary is None
        assert te.get_summary() is None

    def test_get_set_description(self):
        te = Modulemd.TranslationEntry(locale="en_US")

        assert te.props.locale == "en_US"
        assert te.get_locale() == "en_US"
        assert te.props.description is None
        assert te.get_description() is None

        te.set_description("foobar")
        assert te.props.description == "foobar"
        assert te.get_description() == "foobar"

        te.props.description = "barfoo"
        assert te.props.description == "barfoo"
        assert te.get_description() == "barfoo"

        te.props.description = None
        assert te.props.description is None
        assert te.get_description() is None

    def test_profile_descriptions(self):
        te = Modulemd.TranslationEntry(locale="en_US")

        assert te.props.locale == "en_US"
        assert te.get_locale() == "en_US"
        assert te.get_profiles() == []
        assert te.get_profile_description("test1") is None
        assert te.get_profile_description("test2") is None
        assert te.get_profile_description("test3") is None

        # Add a profile
        te.set_profile_description("test1", "foobar")
        assert te.get_profiles() == ["test1"]
        assert te.get_profile_description("test1") == "foobar"
        assert te.get_profile_description("test2") is None
        assert te.get_profile_description("test3") is None

        # Add a second profile
        te.set_profile_description("test2", "barfoo")
        assert te.get_profiles() == ["test1", "test2"]
        assert te.get_profile_description("test1") == "foobar"
        assert te.get_profile_description("test2") == "barfoo"
        assert te.get_profile_description("test3") is None

        # Add a third one that is supposed to go before the others
        te.set_profile_description("test3", "foobarfoo")
        assert te.get_profiles() == ["test1", "test2", "test3"]
        assert te.get_profile_description("test1") == "foobar"
        assert te.get_profile_description("test2") == "barfoo"
        assert te.get_profile_description("test3") == "foobarfoo"


if __name__ == "__main__":
    unittest.main()