|
9 | 9 | from devcycle_python_sdk import DevCycleLocalClient, DevCycleLocalOptions |
10 | 10 | from devcycle_python_sdk.local_client import _validate_user, _validate_sdk_key |
11 | 11 | from devcycle_python_sdk.exceptions import MalformedConfigError |
| 12 | +from devcycle_python_sdk.models.eval_hook import EvalHook |
12 | 13 | from devcycle_python_sdk.models.event import DevCycleEvent |
13 | 14 | from devcycle_python_sdk.models.feature import Feature |
14 | 15 | from devcycle_python_sdk.api.local_bucketing import LocalBucketing |
@@ -361,6 +362,80 @@ def test_all_variables_exception(self, _): |
361 | 362 | result = self.client.all_variables(user) |
362 | 363 | self.assertEqual(result, {}) |
363 | 364 |
|
| 365 | + def test_hooks(self): |
| 366 | + self.setup_client() |
| 367 | + # Test adding hooks |
| 368 | + hook_called = { |
| 369 | + "before": False, |
| 370 | + "after": False, |
| 371 | + "finally": False, |
| 372 | + "error": False, |
| 373 | + } |
| 374 | + |
| 375 | + def before_hook(context): |
| 376 | + hook_called["before"] = True |
| 377 | + return context |
| 378 | + |
| 379 | + def after_hook(context, variable): |
| 380 | + hook_called["after"] = True |
| 381 | + |
| 382 | + def finally_hook(context, variable): |
| 383 | + hook_called["finally"] = True |
| 384 | + |
| 385 | + def error_hook(context, error): |
| 386 | + hook_called["error"] = True |
| 387 | + |
| 388 | + self.client.add_hook( |
| 389 | + EvalHook(before_hook, after_hook, finally_hook, error_hook) |
| 390 | + ) |
| 391 | + |
| 392 | + # Test hooks called during variable evaluation |
| 393 | + variable = self.client.variable(self.test_user, "strKey", 42) |
| 394 | + self.assertTrue(variable.value == 999) |
| 395 | + self.assertFalse(variable.isDefaulted) |
| 396 | + |
| 397 | + self.assertTrue(hook_called["before"]) |
| 398 | + self.assertTrue(hook_called["after"]) |
| 399 | + self.assertTrue(hook_called["finally"]) |
| 400 | + self.assertFalse(hook_called["error"]) |
| 401 | + |
| 402 | + def test_hook_exceptions(self): |
| 403 | + self.setup_client() |
| 404 | + # Test adding hooks |
| 405 | + hook_called = { |
| 406 | + "before": False, |
| 407 | + "after": False, |
| 408 | + "finally": False, |
| 409 | + "error": False, |
| 410 | + } |
| 411 | + |
| 412 | + def before_hook(context): |
| 413 | + hook_called["before"] = True |
| 414 | + raise Exception("Before hook failed") |
| 415 | + |
| 416 | + def after_hook(context, variable): |
| 417 | + hook_called["after"] = True |
| 418 | + |
| 419 | + def finally_hook(context, variable): |
| 420 | + hook_called["finally"] = True |
| 421 | + |
| 422 | + def error_hook(context, error): |
| 423 | + hook_called["error"] = True |
| 424 | + |
| 425 | + self.client.add_hook( |
| 426 | + EvalHook(before_hook, after_hook, finally_hook, error_hook) |
| 427 | + ) |
| 428 | + |
| 429 | + # Test hooks called during variable evaluation |
| 430 | + variable = self.client.variable(self.test_user, "strKey", 42) |
| 431 | + self.assertTrue(variable.value == 999) |
| 432 | + self.assertFalse(variable.isDefaulted) |
| 433 | + |
| 434 | + self.assertTrue(hook_called["before"]) |
| 435 | + self.assertFalse(hook_called["after"]) |
| 436 | + self.assertTrue(hook_called["finally"]) |
| 437 | + self.assertTrue(hook_called["error"]) |
| 438 | + |
364 | 439 |
|
365 | 440 | def _benchmark_variable_call(client: DevCycleLocalClient, user: DevCycleUser, key: str): |
366 | 441 | return client.variable(user, key, "default_value") |
|
0 commit comments