Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Date trait type to reject datetime instances #1429

Merged
merged 7 commits into from
Feb 4, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Test cleanup
  • Loading branch information
mdickinson committed Feb 2, 2021
commit 4b61b1b985fa4bd34b8287371b9c78c60bd7a96f
69 changes: 37 additions & 32 deletions traits/tests/test_date.py
Original file line number Diff line number Diff line change
@@ -28,70 +28,75 @@


class HasDateTraits(HasStrictTraits):
#: Cake expiry date
expiry = Date(allow_datetime=False)

#: Datetime allowed!
solstice = Date(allow_datetime=True)
#: Simple case - no default, no parameters, no metadata
simple_date = Date()

#: Date with default
epoch = Date(UNIX_EPOCH)

#: Date with default spelled out explicitly using the keyword.
#: 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)


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)
self.assertEqual(obj.expiry, None)

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

def test_assign_not_a_date(self):
obj = HasDateTraits()
with self.assertRaises(TraitError):
obj.expiry = "1975-2-13"
obj.simple_date = test_date
self.assertEqual(obj.simple_date, test_date)

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

def test_assign_datetime(self):
# By default, datetime instances are permitted.
test_datetime = datetime.datetime(1975, 2, 13)
obj = HasDateTraits()
obj.simple_date = test_datetime
self.assertEqual(obj.simple_date, test_datetime)

def test_assign_none(self):
# This is a test for the current behaviour. There may be an argument
# for optionally disallowing None. Note that specifying
# allow_none=False in the trait definition does not work as expected.
obj = HasDateTraits(expiry=UNIX_EPOCH)
obj.expiry = None
self.assertIsNone(obj.expiry)
# (Ref: enthought/traits#495)
obj = HasDateTraits(simple_date=UNIX_EPOCH)
obj.simple_date = None
self.assertIsNone(obj.simple_date)

def test_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", message)

def test_assign_a_datetime_legacy(self):
# Legacy case: by default, datetime instances are permitted.
def test_allow_datetime_true(self):
test_datetime = datetime.datetime(1975, 2, 13)
obj = HasDateTraits()
obj.solstice = test_datetime
self.assertEqual(obj.solstice, test_datetime)
obj.datetime_allowed = test_datetime
self.assertEqual(obj.datetime_allowed, test_datetime)

@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)

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