|
| 1 | +from dependency_injection.container import DependencyContainer |
| 2 | +from unit_test.unit_test_case import UnitTestCase |
| 3 | + |
| 4 | + |
| 5 | +class TestConfigureDefaultScopeName(UnitTestCase): |
| 6 | + def test_scoped_same_within_configured_default_scope_string(self): |
| 7 | + # arrange |
| 8 | + DependencyContainer.configure_default_scope_name("alpha") |
| 9 | + |
| 10 | + class Svc: |
| 11 | + pass |
| 12 | + |
| 13 | + c = DependencyContainer.get_instance() |
| 14 | + c.register_scoped(Svc, Svc) |
| 15 | + |
| 16 | + # act |
| 17 | + a1 = c.resolve(Svc) |
| 18 | + a2 = c.resolve(Svc) |
| 19 | + |
| 20 | + # assert |
| 21 | + self.assertIs(a1, a2) |
| 22 | + |
| 23 | + def test_scoped_new_instance_after_default_scope_string_changes(self): |
| 24 | + # arrange |
| 25 | + DependencyContainer.configure_default_scope_name("alpha") |
| 26 | + |
| 27 | + class Svc: |
| 28 | + pass |
| 29 | + |
| 30 | + c = DependencyContainer.get_instance() |
| 31 | + c.register_scoped(Svc, Svc) |
| 32 | + a1 = c.resolve(Svc) |
| 33 | + DependencyContainer.configure_default_scope_name("beta") |
| 34 | + |
| 35 | + # act |
| 36 | + b1 = c.resolve(Svc) |
| 37 | + |
| 38 | + # assert |
| 39 | + self.assertIsNot(b1, a1) |
| 40 | + |
| 41 | + def test_scoped_old_instance_still_resolvable_via_explicit_old_scope(self): |
| 42 | + # arrange |
| 43 | + DependencyContainer.configure_default_scope_name("alpha") |
| 44 | + |
| 45 | + class Svc: |
| 46 | + pass |
| 47 | + |
| 48 | + c = DependencyContainer.get_instance() |
| 49 | + c.register_scoped(Svc, Svc) |
| 50 | + a1 = c.resolve(Svc) |
| 51 | + DependencyContainer.configure_default_scope_name("beta") # switch default |
| 52 | + |
| 53 | + # act |
| 54 | + a2 = c.resolve(Svc, scope_name="alpha") |
| 55 | + |
| 56 | + # assert |
| 57 | + self.assertIs(a2, a1) |
| 58 | + |
| 59 | + def test_scoped_same_when_default_scope_callable_returns_same_value(self): |
| 60 | + # arrange |
| 61 | + def scope_a(): |
| 62 | + return "scope-a" |
| 63 | + |
| 64 | + DependencyContainer.configure_default_scope_name(scope_a) |
| 65 | + |
| 66 | + class Repo: |
| 67 | + pass |
| 68 | + |
| 69 | + c = DependencyContainer.get_instance() |
| 70 | + c.register_scoped(Repo, Repo) |
| 71 | + |
| 72 | + # act |
| 73 | + r1 = c.resolve(Repo) |
| 74 | + r2 = c.resolve(Repo) |
| 75 | + |
| 76 | + # assert |
| 77 | + self.assertIs(r1, r2) |
| 78 | + |
| 79 | + def test_scoped_new_instance_after_switching_default_scope_callable(self): |
| 80 | + # arrange |
| 81 | + def scope_a(): |
| 82 | + return "scope-a" |
| 83 | + |
| 84 | + def scope_b(): |
| 85 | + return "scope-b" |
| 86 | + |
| 87 | + DependencyContainer.configure_default_scope_name(scope_a) |
| 88 | + |
| 89 | + class Repo: |
| 90 | + pass |
| 91 | + |
| 92 | + c = DependencyContainer.get_instance() |
| 93 | + c.register_scoped(Repo, Repo) |
| 94 | + r_a = c.resolve(Repo) |
| 95 | + DependencyContainer.configure_default_scope_name(scope_b) |
| 96 | + |
| 97 | + # act |
| 98 | + r_b = c.resolve(Repo) |
| 99 | + |
| 100 | + # assert |
| 101 | + self.assertIsNot(r_b, r_a) |
0 commit comments