| 1 |
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
import threading |
| 12 |
import time |
| 13 |
import warnings |
| 14 |
|
| 15 |
from traits.api import ( |
| 16 |
Bool, |
| 17 |
Event, |
| 18 |
Float, |
| 19 |
HasTraits, |
| 20 |
Int, |
| 21 |
List, |
| 22 |
on_trait_change, |
| 23 |
) |
| 24 |
from traits.testing.api import UnittestTools |
| 25 |
# unittest_tools provides a reference to unittest for historical reasons, and |
| 26 |
# downstream packages may still be doing "from traits.testing.unittest_tools |
| 27 |
# import unittest". We keep this import as-is (instead of doing a simple |
| 28 |
# "import unittest") to make sure we don't break those packages. |
| 29 |
from traits.testing.unittest_tools import unittest |
| 30 |
from traits.util.api import deprecated |
| 31 |
|
| 32 |
|
| 33 |
@deprecated("This function is outdated. Use 'shiny' instead!") |
| 34 |
def old_and_dull(): |
| 35 |
""" A deprecated function, for use in assertDeprecated tests. |
| 36 |
|
| 37 |
""" |
| 38 |
pass |
| 39 |
|
| 40 |
|
| 41 |
class ExampleObject(HasTraits): |
| 42 |
|
| 43 |
number = Float(2.0) |
| 44 |
list_of_numbers = List(Float) |
| 45 |
flag = Bool |
| 46 |
|
| 47 |
@on_trait_change("number") |
| 48 |
def _add_number_to_list(self, value): |
| 49 |
self.list_of_numbers.append(value) |
| 50 |
|
| 51 |
def add_to_number(self, value): |
| 52 |
self.number += value |
| 53 |
|
| 54 |
|
| 55 |
class UnittestToolsTestCase(unittest.TestCase, UnittestTools): |
| 56 |
def setUp(self): |
| 57 |
self.test_object = ExampleObject() |
| 58 |
|
| 59 |
def test_when_using_with(self): |
| 60 |
""" Check normal use cases as a context manager. |
| 61 |
""" |
| 62 |
test_object = self.test_object |
| 63 |
|
| 64 |
# Change event should NOT BE detected |
| 65 |
with self.assertTraitDoesNotChange(test_object, "number") as result: |
| 66 |
test_object.flag = True |
| 67 |
test_object.number = 2.0 |
| 68 |
|
| 69 |
msg = "The assertion result is not None: {0}".format(result.event) |
| 70 |
self.assertIsNone(result.event, msg=msg) |
| 71 |
|
| 72 |
# Change event should BE detected |
| 73 |
with self.assertTraitChanges(test_object, "number") as result: |
| 74 |
test_object.flag = False |
| 75 |
test_object.number = 5.0 |
| 76 |
|
| 77 |
expected = (test_object, "number", 2.0, 5.0) |
| 78 |
self.assertSequenceEqual(expected, result.event) |
| 79 |
|
| 80 |
# Change event should BE detected exactly 2 times |
| 81 |
with self.assertTraitChanges(test_object, "number", count=2) as result: |
| 82 |
test_object.flag = False |
| 83 |
test_object.number = 4.0 |
| 84 |
test_object.number = 3.0 |
| 85 |
|
| 86 |
expected = [ |
| 87 |
(test_object, "number", 5.0, 4.0), |
| 88 |
(test_object, "number", 4.0, 3.0), |
| 89 |
] |
| 90 |
self.assertSequenceEqual(expected, result.events) |
| 91 |
self.assertSequenceEqual(expected[-1], result.event) |
| 92 |
|
| 93 |
# Change event should BE detected |
| 94 |
with self.assertTraitChanges(test_object, "number") as result: |
| 95 |
test_object.flag = True |
| 96 |
test_object.add_to_number(10.0) |
| 97 |
|
| 98 |
expected = (test_object, "number", 3.0, 13.0) |
| 99 |
self.assertSequenceEqual(expected, result.event) |
| 100 |
|
| 101 |
# Change event should BE detected exactly 3 times |
| 102 |
with self.assertTraitChanges(test_object, "number", count=3) as result: |
| 103 |
test_object.flag = True |
| 104 |
test_object.add_to_number(10.0) |
| 105 |
test_object.add_to_number(10.0) |
| 106 |
test_object.add_to_number(10.0) |
| 107 |
|
| 108 |
expected = [ |
| 109 |
(test_object, "number", 13.0, 23.0), |
| 110 |
(test_object, "number", 23.0, 33.0), |
| 111 |
(test_object, "number", 33.0, 43.0), |
| 112 |
] |
| 113 |
self.assertSequenceEqual(expected, result.events) |
| 114 |
self.assertSequenceEqual(expected[-1], result.event) |
| 115 |
|
| 116 |
def test_assert_multi_changes(self): |
| 117 |
test_object = self.test_object |
| 118 |
|
| 119 |
# Change event should NOT BE detected |
| 120 |
with self.assertMultiTraitChanges( |
| 121 |
[test_object], [], ["flag", "number", "list_of_numbers[]"] |
| 122 |
) as results: |
| 123 |
test_object.number = 2.0 |
| 124 |
|
| 125 |
events = list(filter(bool, (result.event for result in results))) |
| 126 |
msg = "The assertion result is not None: {0}".format(", ".join(events)) |
| 127 |
self.assertFalse(events, msg=msg) |
| 128 |
|
| 129 |
# Change event should BE detected |
| 130 |
with self.assertMultiTraitChanges( |
| 131 |
[test_object], ["number", "list_of_numbers[]"], ["flag"] |
| 132 |
) as results: |
| 133 |
test_object.number = 5.0 |
| 134 |
|
| 135 |
events = list(filter(bool, (result.event for result in results))) |
| 136 |
msg = "The assertion result is None" |
| 137 |
self.assertTrue(events, msg=msg) |
| 138 |
|
| 139 |
def test_when_using_functions(self): |
| 140 |
test_object = self.test_object |
| 141 |
|
| 142 |
# Change event should BE detected |
| 143 |
self.assertTraitChanges( |
| 144 |
test_object, "number", 1, test_object.add_to_number, 13.0 |
| 145 |
) |
| 146 |
|
| 147 |
# Change event should NOT BE detected |
| 148 |
self.assertTraitDoesNotChange( |
| 149 |
test_object, "flag", test_object.add_to_number, 13.0 |
| 150 |
) |
| 151 |
|
| 152 |
def test_indirect_events(self): |
| 153 |
""" Check catching indirect change events. |
| 154 |
""" |
| 155 |
test_object = self.test_object |
| 156 |
|
| 157 |
# Change event should BE detected |
| 158 |
with self.assertTraitChanges( |
| 159 |
test_object, "list_of_numbers[]" |
| 160 |
) as result: |
| 161 |
test_object.flag = True |
| 162 |
test_object.number = -3.0 |
| 163 |
|
| 164 |
expected = (test_object, "list_of_numbers_items", [], [-3.0]) |
| 165 |
self.assertSequenceEqual(expected, result.event) |
| 166 |
|
| 167 |
def test_exception_inside_context(self): |
| 168 |
""" Check that exception inside the context statement block are |
| 169 |
propagated. |
| 170 |
|
| 171 |
""" |
| 172 |
test_object = self.test_object |
| 173 |
|
| 174 |
with self.assertRaises(AttributeError): |
| 175 |
with self.assertTraitChanges(test_object, "number"): |
| 176 |
test_object.i_do_exist |
| 177 |
|
| 178 |
with self.assertRaises(AttributeError): |
| 179 |
with self.assertTraitDoesNotChange(test_object, "number"): |
| 180 |
test_object.i_do_exist |
| 181 |
|
| 182 |
def test_non_change_on_failure(self): |
| 183 |
""" Check behaviour when assertion should be raised for non trait |
| 184 |
change. |
| 185 |
|
| 186 |
""" |
| 187 |
test_object = self.test_object |
| 188 |
traits = "flag, number" |
| 189 |
with self.assertRaises(AssertionError): |
| 190 |
with self.assertTraitDoesNotChange(test_object, traits) as result: |
| 191 |
test_object.flag = True |
| 192 |
test_object.number = -3.0 |
| 193 |
expected = [ |
| 194 |
(test_object, "flag", False, True), |
| 195 |
(test_object, "number", 2.0, -3.0), |
| 196 |
] |
| 197 |
self.assertEqual(result.events, expected) |
| 198 |
|
| 199 |
def test_change_on_failure(self): |
| 200 |
""" Check behaviour when assertion should be raised for trait change. |
| 201 |
""" |
| 202 |
test_object = self.test_object |
| 203 |
with self.assertRaises(AssertionError): |
| 204 |
with self.assertTraitChanges(test_object, "number") as result: |
| 205 |
test_object.flag = True |
| 206 |
self.assertEqual(result.events, []) |
| 207 |
|
| 208 |
# Change event will not be fired 3 times |
| 209 |
with self.assertRaises(AssertionError): |
| 210 |
with self.assertTraitChanges( |
| 211 |
test_object, "number", count=3 |
| 212 |
) as result: |
| 213 |
test_object.flag = True |
| 214 |
test_object.add_to_number(10.0) |
| 215 |
test_object.add_to_number(10.0) |
| 216 |
|
| 217 |
expected = [ |
| 218 |
(test_object, "number", 2.0, 12.0), |
| 219 |
(test_object, "number", 12.0, 22.0), |
| 220 |
] |
| 221 |
self.assertSequenceEqual(expected, result.events) |
| 222 |
|
| 223 |
def test_asserts_in_context_block(self): |
| 224 |
""" Make sure that the traits context manager does not stop |
| 225 |
regular assertions inside the managed code block from happening. |
| 226 |
""" |
| 227 |
test_object = ExampleObject(number=16.0) |
| 228 |
|
| 229 |
with self.assertTraitDoesNotChange(test_object, "number"): |
| 230 |
self.assertEqual(test_object.number, 16.0) |
| 231 |
|
| 232 |
with self.assertRaisesRegex(AssertionError, r"16\.0 != 12\.0"): |
| 233 |
with self.assertTraitDoesNotChange(test_object, "number"): |
| 234 |
self.assertEqual(test_object.number, 12.0) |
| 235 |
|
| 236 |
def test_special_case_for_count(self): |
| 237 |
""" Count equal to 0 should be valid but it is discouraged. |
| 238 |
""" |
| 239 |
test_object = ExampleObject(number=16.0) |
| 240 |
|
| 241 |
with self.assertTraitChanges(test_object, "number", count=0): |
| 242 |
test_object.flag = True |
| 243 |
|
| 244 |
def test_assert_trait_changes_async(self): |
| 245 |
# Exercise assertTraitChangesAsync. |
| 246 |
thread_count = 10 |
| 247 |
events_per_thread = 1000 |
| 248 |
|
| 249 |
class A(HasTraits): |
| 250 |
event = Event |
| 251 |
|
| 252 |
a = A() |
| 253 |
|
| 254 |
def thread_target(obj, count): |
| 255 |
"Fire obj.event 'count' times." |
| 256 |
for _ in range(count): |
| 257 |
obj.event = True |
| 258 |
|
| 259 |
threads = [ |
| 260 |
threading.Thread(target=thread_target, args=(a, events_per_thread)) |
| 261 |
for _ in range(thread_count) |
| 262 |
] |
| 263 |
|
| 264 |
expected_count = thread_count * events_per_thread |
| 265 |
with self.assertTraitChangesAsync( |
| 266 |
a, "event", expected_count, timeout=60.0 |
| 267 |
): |
| 268 |
for t in threads: |
| 269 |
t.start() |
| 270 |
|
| 271 |
for t in threads: |
| 272 |
t.join() |
| 273 |
|
| 274 |
def test_assert_trait_changes_async_events(self): |
| 275 |
# Check access to the events after the with |
| 276 |
# block completes. |
| 277 |
thread_count = 10 |
| 278 |
events_per_thread = 100 |
| 279 |
|
| 280 |
class A(HasTraits): |
| 281 |
event = Event(Int) |
| 282 |
|
| 283 |
a = A() |
| 284 |
|
| 285 |
def thread_target(obj, count): |
| 286 |
"Fire obj.event 'count' times." |
| 287 |
for n in range(count): |
| 288 |
time.sleep(0.001) |
| 289 |
obj.event = n |
| 290 |
|
| 291 |
threads = [ |
| 292 |
threading.Thread(target=thread_target, args=(a, events_per_thread)) |
| 293 |
for _ in range(thread_count) |
| 294 |
] |
| 295 |
|
| 296 |
expected_count = thread_count * events_per_thread |
| 297 |
with self.assertTraitChangesAsync( |
| 298 |
a, "event", expected_count, timeout=60.0 |
| 299 |
) as event_collector: |
| 300 |
for t in threads: |
| 301 |
t.start() |
| 302 |
|
| 303 |
for t in threads: |
| 304 |
t.join() |
| 305 |
|
| 306 |
self.assertCountEqual( |
| 307 |
event_collector.events, |
| 308 |
list(range(events_per_thread)) * thread_count, |
| 309 |
) |
| 310 |
|
| 311 |
def test_assert_trait_changes_async_failure(self): |
| 312 |
# Exercise assertTraitChangesAsync. |
| 313 |
thread_count = 10 |
| 314 |
events_per_thread = 10000 |
| 315 |
|
| 316 |
class A(HasTraits): |
| 317 |
event = Event |
| 318 |
|
| 319 |
a = A() |
| 320 |
|
| 321 |
def thread_target(obj, count): |
| 322 |
"Fire obj.event 'count' times." |
| 323 |
for _ in range(count): |
| 324 |
obj.event = True |
| 325 |
|
| 326 |
threads = [ |
| 327 |
threading.Thread(target=thread_target, args=(a, events_per_thread)) |
| 328 |
for _ in range(thread_count) |
| 329 |
] |
| 330 |
|
| 331 |
expected_count = thread_count * events_per_thread |
| 332 |
with self.assertRaises(AssertionError): |
| 333 |
with self.assertTraitChangesAsync(a, "event", expected_count + 1): |
| 334 |
for t in threads: |
| 335 |
t.start() |
| 336 |
|
| 337 |
for t in threads: |
| 338 |
t.join() |
| 339 |
|
| 340 |
def test_assert_eventually_true_fails_on_timeout(self): |
| 341 |
class A(HasTraits): |
| 342 |
foo = Bool(False) |
| 343 |
|
| 344 |
a = A() |
| 345 |
|
| 346 |
def condition(a_object): |
| 347 |
return a_object.foo |
| 348 |
|
| 349 |
with self.assertRaises(self.failureException): |
| 350 |
self.assertEventuallyTrue( |
| 351 |
condition=condition, obj=a, trait="foo", timeout=1.0 |
| 352 |
) |
| 353 |
|
| 354 |
def test_assert_eventually_true_passes_when_condition_becomes_true(self): |
| 355 |
class A(HasTraits): |
| 356 |
foo = Bool(False) |
| 357 |
|
| 358 |
def condition(a_object): |
| 359 |
return a_object.foo |
| 360 |
|
| 361 |
a = A() |
| 362 |
|
| 363 |
def thread_target(a): |
| 364 |
time.sleep(1.0) |
| 365 |
a.foo = True |
| 366 |
|
| 367 |
t = threading.Thread(target=thread_target, args=(a,)) |
| 368 |
t.start() |
| 369 |
self.assertEventuallyTrue( |
| 370 |
condition=condition, obj=a, trait="foo", timeout=10.0 |
| 371 |
) |
| 372 |
t.join() |
| 373 |
|
| 374 |
def test_assert_eventually_true_passes_when_condition_starts_true(self): |
| 375 |
class A(HasTraits): |
| 376 |
foo = Bool(True) |
| 377 |
|
| 378 |
def condition(a_object): |
| 379 |
return a_object.foo |
| 380 |
|
| 381 |
a = A() |
| 382 |
self.assertEventuallyTrue( |
| 383 |
condition=condition, obj=a, trait="foo", timeout=10.0 |
| 384 |
) |
| 385 |
|
| 386 |
def test_assert_deprecated(self): |
| 387 |
with self.assertDeprecated(): |
| 388 |
old_and_dull() |
| 389 |
|
| 390 |
def test_assert_deprecated_failures(self): |
| 391 |
with self.assertRaises(self.failureException): |
| 392 |
with self.assertDeprecated(): |
| 393 |
pass |
| 394 |
|
| 395 |
def test_assert_deprecated_when_warning_already_issued(self): |
| 396 |
# Exercise a problematic case where previous calls to a function or |
| 397 |
# method that issues a DeprecationWarning have already polluted the |
| 398 |
# __warningregistry__. For this, we need a single call-point to |
| 399 |
# old_and_dull, since distinct call-points have separate entries in |
| 400 |
# __warningregistry__. |
| 401 |
def old_and_dull_caller(): |
| 402 |
old_and_dull() |
| 403 |
|
| 404 |
with warnings.catch_warnings(record=True): |
| 405 |
warnings.simplefilter("always", DeprecationWarning) |
| 406 |
|
| 407 |
# Pollute the registry by pre-calling the function. |
| 408 |
old_and_dull_caller() |
| 409 |
|
| 410 |
# Check that we can still detect the DeprecationWarning. |
| 411 |
with self.assertDeprecated(): |
| 412 |
old_and_dull_caller() |
| 413 |
|
| 414 |
def test_assert_not_deprecated_failures(self): |
| 415 |
with self.assertRaises(self.failureException): |
| 416 |
with self.assertNotDeprecated(): |
| 417 |
old_and_dull() |
| 418 |
|
| 419 |
def test_assert_not_deprecated(self): |
| 420 |
with self.assertNotDeprecated(): |
| 421 |
pass |
| 422 |
|
| 423 |
def test_assert_not_deprecated_when_warning_already_issued(self): |
| 424 |
# Exercise a problematic case where previous calls to a function or |
| 425 |
# method that issues a DeprecationWarning have already polluted the |
| 426 |
# __warningregistry__. For this, we need a single call-point to |
| 427 |
# old_and_dull, since distinct call-points have separate entries in |
| 428 |
# __warningregistry__. |
| 429 |
def old_and_dull_caller(): |
| 430 |
old_and_dull() |
| 431 |
|
| 432 |
with warnings.catch_warnings(record=True): |
| 433 |
warnings.simplefilter("always", DeprecationWarning) |
| 434 |
|
| 435 |
# Pollute the registry by pre-calling the function. |
| 436 |
old_and_dull_caller() |
| 437 |
|
| 438 |
# Check that we can still detect the DeprecationWarning. |
| 439 |
with self.assertRaises(self.failureException): |
| 440 |
with self.assertNotDeprecated(): |
| 441 |
old_and_dull_caller() |
| 442 |
|
| 443 |
def test__catch_warnings_deprecated(self): |
| 444 |
with self.assertWarns(DeprecationWarning): |
| 445 |
with self._catch_warnings(): |
| 446 |
pass |