File: test_date.py

package info (click to toggle)
python-traits 6.4.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,648 kB
  • sloc: python: 34,801; ansic: 4,266; makefile: 102
file content (148 lines) | stat: -rw-r--r-- 5,318 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
# (C) Copyright 2005-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!

"""
Tests for the Date trait type.
"""

import datetime
import unittest

from traits.testing.optional_dependencies import requires_traitsui, traitsui

from traits.api import Date, HasStrictTraits, TraitError


#: Unix epoch date.
UNIX_EPOCH = datetime.date(1970, 1, 1)

#: Windows NT epoch
NT_EPOCH = datetime.date(1600, 1, 1)


class HasDateTraits(HasStrictTraits):
    #: Simple case - no default, no parameters, no metadata
    simple_date = Date()

    #: Date with default
    epoch = Date(UNIX_EPOCH)

    #: Date with default provided via keyword.
    alternative_epoch = Date(default_value=NT_EPOCH)

    #: Datetime instances prohibited
    datetime_prohibited = Date(allow_datetime=False)

    #: Datetime instances allowed
    datetime_allowed = Date(allow_datetime=True)

    #: None prohibited
    none_prohibited = Date(allow_none=False)

    #: None allowed
    none_allowed = Date(allow_none=True)

    #: Strictly a non-None non-datetime date
    strict = Date(allow_datetime=False, allow_none=False)


class TestDate(unittest.TestCase):
    def test_default(self):
        obj = HasDateTraits()
        self.assertEqual(obj.simple_date, None)
        self.assertEqual(obj.epoch, UNIX_EPOCH)
        self.assertEqual(obj.alternative_epoch, NT_EPOCH)

    def test_assign_date(self):
        test_date = datetime.date(1975, 2, 13)
        obj = HasDateTraits()
        obj.simple_date = test_date
        self.assertEqual(obj.simple_date, test_date)

    def test_assign_non_date(self):
        obj = HasDateTraits()
        with self.assertRaises(TraitError) as exception_context:
            obj.simple_date = "1975-2-13"
        message = str(exception_context.exception)
        self.assertIn("must be a date or None, but", message)

    def test_assign_none_with_allow_none_not_given(self):
        obj = HasDateTraits(simple_date=UNIX_EPOCH)
        self.assertIsNotNone(obj.simple_date)
        with self.assertWarns(DeprecationWarning) as warnings_cm:
            obj.simple_date = None
        self.assertIsNone(obj.simple_date)

        _, _, this_module = __name__.rpartition(".")
        self.assertIn(this_module, warnings_cm.filename)
        self.assertIn(
            "None will no longer be accepted",
            str(warnings_cm.warning),
        )

    def test_assign_none_with_allow_none_false(self):
        obj = HasDateTraits(none_prohibited=UNIX_EPOCH)
        with self.assertRaises(TraitError) as exception_context:
            obj.none_prohibited = None
        message = str(exception_context.exception)
        self.assertIn("must be a date, but", message)

    def test_assign_none_with_allow_none_true(self):
        obj = HasDateTraits(none_allowed=UNIX_EPOCH)
        self.assertIsNotNone(obj.none_allowed)
        obj.none_allowed = None
        self.assertIsNone(obj.none_allowed)

    def test_assign_datetime_with_allow_datetime_false(self):
        test_datetime = datetime.datetime(1975, 2, 13)
        obj = HasDateTraits()
        with self.assertRaises(TraitError) as exception_context:
            obj.datetime_prohibited = test_datetime
        message = str(exception_context.exception)
        self.assertIn("must be a non-datetime date or None, but", message)

    def test_assign_datetime_with_allow_datetime_true(self):
        test_datetime = datetime.datetime(1975, 2, 13)
        obj = HasDateTraits()
        obj.datetime_allowed = test_datetime
        self.assertEqual(obj.datetime_allowed, test_datetime)

    def test_assign_datetime_with_allow_datetime_not_given(self):
        # For traits where "allow_datetime" is not specified, a
        # DeprecationWarning should be issued on assignment of datetime.
        test_datetime = datetime.datetime(1975, 2, 13)
        obj = HasDateTraits()
        with self.assertWarns(DeprecationWarning) as warnings_cm:
            # Note: the warning depends on the type, not the value; this case
            # should warn even though the time component of the datetime is
            # zero.
            obj.simple_date = test_datetime
        self.assertEqual(obj.simple_date, test_datetime)

        _, _, this_module = __name__.rpartition(".")
        self.assertIn(this_module, warnings_cm.filename)
        self.assertIn(
            "datetime instances will no longer be accepted",
            str(warnings_cm.warning),
        )

    def test_allow_none_false_allow_datetime_false(self):
        obj = HasDateTraits(strict=UNIX_EPOCH)
        with self.assertRaises(TraitError) as exception_context:
            obj.strict = None
        message = str(exception_context.exception)
        self.assertIn("must be a non-datetime date, but", message)

    @requires_traitsui
    def test_get_editor(self):
        obj = HasDateTraits()
        trait = obj.base_trait("epoch")
        editor_factory = trait.get_editor()
        self.assertIsInstance(editor_factory, traitsui.api.DateEditor)