File: test_datetime_editor.py

package info (click to toggle)
python-traitsui 8.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,232 kB
  • sloc: python: 58,982; makefile: 113
file content (290 lines) | stat: -rw-r--r-- 11,211 bytes parent folder | download
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

import contextlib
import datetime
import unittest

from traits.api import HasTraits, Datetime
from traitsui.api import DatetimeEditor, Item, View
from traitsui.tests._tools import (
    BaseTestMixin,
    create_ui,
    GuiTestAssistant,
    process_cascade_events,
    requires_toolkit,
    reraise_exceptions,
    ToolkitName,
    no_gui_test_assistant,
)


class InstanceWithDatetime(HasTraits):
    """Demo class to show Datetime editors."""

    date_time = Datetime(allow_none=True)


def to_datetime(q_datetime):
    try:
        return q_datetime.toPyDateTime()
    except AttributeError:
        return q_datetime.toPython()


def get_date_time_simple_view(editor_factory):
    view = View(
        Item(
            name="date_time",
            style="simple",
            editor=editor_factory,
        )
    )
    return view


@requires_toolkit([ToolkitName.qt])
@unittest.skipIf(no_gui_test_assistant, "No GuiTestAssistant")
class TestDatetimeEditorQt(BaseTestMixin, GuiTestAssistant, unittest.TestCase):
    """Tests for DatetimeEditor using Qt."""

    def setUp(self):
        BaseTestMixin.setUp(self)
        GuiTestAssistant.setUp(self)

    def tearDown(self):
        GuiTestAssistant.tearDown(self)
        BaseTestMixin.tearDown(self)

    def test_datetime_editor_simple(self):
        view = get_date_time_simple_view(DatetimeEditor())
        date_time = datetime.datetime(2000, 1, 2, 1, 2, 3)
        instance = InstanceWithDatetime(date_time=date_time)
        with reraise_exceptions(), self.launch_editor(instance, view):
            pass

    def test_datetime_editor_simple_with_minimum_datetime(self):
        # Test the editor uses the minimum datetime as expected
        minimum_datetime = datetime.datetime(2000, 1, 1)
        editor_factory = DatetimeEditor(
            minimum_datetime=minimum_datetime,
        )
        view = get_date_time_simple_view(editor_factory)
        instance = InstanceWithDatetime()
        with reraise_exceptions(), self.launch_editor(
            instance, view
        ) as editor:
            q_minimum_datetime = editor.control.minimumDateTime()
            actual_minimum_datetime = to_datetime(q_minimum_datetime)

        self.assertEqual(actual_minimum_datetime, minimum_datetime)

    def test_datetime_editor_simple_with_minimum_datetime_out_of_bound(self):
        # Test when out-of-bound value is given, it is reset to the bounded
        # value.
        minimum_datetime = datetime.datetime(2000, 1, 1)
        editor_factory = DatetimeEditor(
            minimum_datetime=minimum_datetime,
        )
        view = get_date_time_simple_view(editor_factory)
        instance = InstanceWithDatetime()
        with reraise_exceptions(), self.launch_editor(
            instance, view
        ) as editor:
            instance.date_time = datetime.datetime(1980, 1, 1)

            # does not seem needed to flush the event loop, but just in case.
            process_cascade_events()

            displayed_value = to_datetime(editor.control.dateTime())

        self.assertEqual(displayed_value, minimum_datetime)
        self.assertEqual(instance.date_time, minimum_datetime)

    def test_datetime_editor_mutate_minimum_datetime_after_init(self):
        # Test if the minimum_datetime on the editor is mutated after init,
        # the values on the edited object and the view are synchronized.
        editor_factory = DatetimeEditor(
            minimum_datetime=datetime.datetime(2000, 1, 1),
        )
        view = get_date_time_simple_view(editor_factory)
        instance = InstanceWithDatetime()
        with reraise_exceptions(), self.launch_editor(
            instance, view
        ) as editor:

            # This value is in-range
            instance.date_time = datetime.datetime(2001, 1, 1)

            # Now change the editor's minimum datetime such that the original
            # value is out-of-range
            new_minimum_datetime = datetime.datetime(2010, 1, 1)
            editor.minimum_datetime = new_minimum_datetime

            # does not seem needed to flush the event loop, but just in case.
            process_cascade_events()

            displayed_value = to_datetime(editor.control.dateTime())

        self.assertEqual(instance.date_time, displayed_value)
        self.assertEqual(displayed_value, new_minimum_datetime)

    def test_datetime_editor_mutate_minimum_datetime_bad_order(self):
        # Test if the minimum_datetime on the editor is mutated after init,
        # the values on the edited object and the view are synchronized.
        editor_factory = DatetimeEditor(
            minimum_datetime=datetime.datetime(2000, 1, 1),
            maximum_datetime=datetime.datetime(2010, 1, 1),
        )
        view = get_date_time_simple_view(editor_factory)
        instance = InstanceWithDatetime()
        with reraise_exceptions(), self.launch_editor(
            instance, view
        ) as editor:

            # This value is in-range
            instance.date_time = datetime.datetime(2001, 1, 1)

            # Now change the editor's minimum datetime such that the original
            # value is out-of-range and the minimum is greater than maximum
            new_minimum_datetime = datetime.datetime(2020, 1, 1)
            editor.minimum_datetime = new_minimum_datetime

            # does not seem needed to flush the event loop, but just in case.
            process_cascade_events()

            displayed_value = to_datetime(editor.control.dateTime())

        self.assertEqual(instance.date_time, displayed_value)
        self.assertEqual(displayed_value, new_minimum_datetime)
        self.assertEqual(editor.maximum_datetime, new_minimum_datetime)

    def test_datetime_editor_simple_with_maximum_datetime(self):
        # Test the editor uses the maximum datetime as expected
        maximum_datetime = datetime.datetime(2000, 1, 1)
        editor_factory = DatetimeEditor(
            maximum_datetime=maximum_datetime,
        )
        view = get_date_time_simple_view(editor_factory)
        instance = InstanceWithDatetime()
        with reraise_exceptions(), self.launch_editor(
            instance, view
        ) as editor:
            q_maximum_datetime = editor.control.maximumDateTime()

            # does not seem needed to flush the event loop, but just in case.
            process_cascade_events()

            actual_maximum_datetime = to_datetime(q_maximum_datetime)

        self.assertEqual(actual_maximum_datetime, maximum_datetime)

    def test_datetime_editor_simple_with_maximum_datetime_out_of_bound(self):
        maximum_datetime = datetime.datetime(2000, 1, 1)
        editor_factory = DatetimeEditor(
            maximum_datetime=maximum_datetime,
        )
        view = get_date_time_simple_view(editor_factory)
        instance = InstanceWithDatetime()
        with reraise_exceptions(), self.launch_editor(
            instance, view
        ) as editor:
            # out-of-bound
            instance.date_time = datetime.datetime(2020, 1, 1)

            # does not seem needed to flush the event loop, but just in case.
            process_cascade_events()

            displayed_value = to_datetime(editor.control.dateTime())

        self.assertEqual(displayed_value, maximum_datetime)
        self.assertEqual(instance.date_time, maximum_datetime)

    def test_datetime_editor_mutate_maximum_datetime_after_init(self):
        # Test if the maximum_datetime on the editor is mutated after init,
        # the values on the edited object and the view are synchronized.
        editor_factory = DatetimeEditor(
            maximum_datetime=datetime.datetime(2000, 1, 1),
        )
        view = get_date_time_simple_view(editor_factory)
        instance = InstanceWithDatetime()
        with reraise_exceptions(), self.launch_editor(
            instance, view
        ) as editor:

            # This value is in-range
            instance.date_time = datetime.datetime(1999, 1, 1)

            # Now change the editor's maximum datetime such that the original
            # value is out-of-range
            new_maximum_datetime = datetime.datetime(1900, 1, 1)
            editor.maximum_datetime = new_maximum_datetime

            # does not seem needed to flush the event loop, but just in case.
            process_cascade_events()

            displayed_value = to_datetime(editor.control.dateTime())

        self.assertEqual(instance.date_time, displayed_value)
        self.assertEqual(displayed_value, new_maximum_datetime)

    def test_datetime_editor_python_datetime_out_of_bound(self):
        # The minimum datetime supported by Qt is larger than the minimum
        # datetime supported by Python.
        editor_factory = DatetimeEditor()
        view = get_date_time_simple_view(editor_factory)
        init_datetime = datetime.datetime(1900, 1, 1)
        instance = InstanceWithDatetime(date_time=init_datetime)
        with reraise_exceptions(), self.launch_editor(
            instance, view
        ) as editor:
            # This value is too early and is not supported by Qt
            # But the editor should not crash
            new_value = datetime.datetime(1, 1, 1)
            instance.date_time = new_value

            # does not seem needed to flush the event loop, but just in case.
            process_cascade_events()

            displayed_value = to_datetime(editor.control.dateTime())

        # The value has changed to a filled value
        self.assertNotEqual(instance.date_time, new_value)
        self.assertEqual(displayed_value, instance.date_time)

    def test_datetime_editor_qt_datetime_out_of_bound(self):
        # The maximum datetime supported by Qt is larger than the maximum
        # datetime supported by Python
        editor_factory = DatetimeEditor()
        view = get_date_time_simple_view(editor_factory)
        init_datetime = datetime.datetime(1900, 1, 1)
        instance = InstanceWithDatetime(date_time=init_datetime)
        with reraise_exceptions(), self.launch_editor(
            instance, view
        ) as editor:
            # the user set the datetime on the Qt widget to a value
            # too large for Python
            from pyface.qt.QtCore import QDateTime, QDate, QTime

            q_datetime = QDateTime(
                QDate(datetime.MAXYEAR + 1, 1, 1), QTime(0, 0)
            )
            editor.control.setDateTime(q_datetime)

            # Get the displayed value back.
            displayed_value = to_datetime(editor.control.dateTime())

        self.assertEqual(instance.date_time, displayed_value)

    @contextlib.contextmanager
    def launch_editor(self, object, view):
        with create_ui(object, dict(view=view)) as ui:
            (editor,) = ui._editors
            yield editor