def test_get_returns_original_if_unrelated_injected(self):
     expected = object()
     unrelated = object()
     injected = object()
     sut = DependencyContext()
     sut.inject(unrelated, injected)
     expect(sut.get(expected)).to(be(expected))
Example #2
0
 def test_complains_if_env_not_supplied(self):
     context = DependencyContext()
     try:
         context.set_env(things='0')
         assert False, 'No exception was raised'
     except RuntimeError:
         pass
 def test_spawned_context_returns_value_from_parent(self):
     key = object()
     parent_value = 46
     parent = DependencyContext()
     parent.inject(key, parent_value)
     child = parent.spawn()
     expect(child.get(key)).to(be(parent_value))
Example #4
0
 def test_does_not_affect_unspecified_var(self):
     existing_key = 'dog_milk'
     existing_value = 'Lasts longer than any other milk'
     context = DependencyContext(supply_env=True)
     context.os.environ[existing_key] = existing_value
     context.set_env(goat_milk='and little lambs eat ivy')
     expect(context.os.environ[existing_key]).to(equal(existing_value))
Example #5
0
 def test_can_replace_existing_var(self):
     key = 'quokka'
     old = 'old value'
     new = 'new value'
     context = DependencyContext(supply_env=True)
     context.set_env(**{key: old})
     context.set_env(**{key: new})
     expect(context.os.environ[key]).to(equal(new))
 def test_complains_if_env_not_supplied(self):
     key = 'rubbish-set-by-test-unset-env'
     os.environ[key] = 'rubbish'
     context = DependencyContext()
     try:
         context.unset_env(key)
         assert False, 'Caught nothing'
     except RuntimeError:
         pass
 def test_spawned_context_can_override_parent_value(self):
     key = object()
     parent_value = 37
     child_value = 2
     parent = DependencyContext()
     parent.inject(key, parent_value)
     child = parent.spawn()
     child.inject(key, child_value)
     expect(child.get(key)).to(be(child_value))
Example #8
0
 def test_can_set_multiple_vars(self):
     k1 = 'doe'
     v1 = 'a deer, a female deer'
     k2 = 'ray'
     v2 = 'a drop of golden sun'
     context = DependencyContext(supply_env=True)
     context.set_env(**{k1: v1, k2: v2})
     expect(context.os.environ[k1]).to(equal(v1))
     expect(context.os.environ[k2]).to(equal(v2))
Example #9
0
    def test_inherits_arbitrary_key_from_parent_context(self):
        key = object()
        parent_value = 'something'
        child_value = None

        def fetch():
            nonlocal child_value
            child_value = dependency(key)

        parent_context = DependencyContext()
        parent_context.inject(key, parent_value)
        tc = parent_context.create_time_controller(target=fetch)
        tc.start()
        tc.join()
        expect(child_value).to(equal(parent_value))
 def test_spawned_context_does_not_replace_parent_value(self):
     key = object()
     parent_value = 37
     child_value = 2
     parent = DependencyContext()
     parent.inject(key, parent_value)
     child = parent.spawn()
     child.inject(key, child_value)
     expect(parent.get(key)).to(be(parent_value))
def IndependentTimeController(target,
                              *,
                              daemon=True,
                              parent_context=None,
                              **kwargs):
    """
    Facade for the sake of backward compatibility.

    A TimeController that can be initialized without specifying a parent
    context
    """

    return ContextTimeController(target=target,
                                 daemon=daemon,
                                 parent_context=parent_context
                                 or DependencyContext(),
                                 **kwargs)
Example #12
0
 def test_inherits_parent_context_os(self):
     parent = DependencyContext(supply_env=True)
     with dependency_context(parent=parent):
         expect(dependency(os)).to(be(parent.os))
Example #13
0
 def test_incompatible_with_specifified_parent_context(self):
     try:
         DependencyContext(parent=DependencyContext(), supply_env=True)
         assert False, "No exception was raised"
     except ValueError:
         pass
 def test_complains_about_attempt_to_mix_with_parent_context(self):
     try:
         DependencyContext(parent=DependencyContext(), supply_fs=True)
         assert False, "No exception was raised"
     except ValueError:
         pass
 def test_get_returns_original_if_nothing_injected(self):
     sut = DependencyContext()
     expected = object()
     expect(sut.get(expected)).to(be(expected))
Example #16
0
 def test_converts_number_to_string(self):
     context = DependencyContext(supply_env=True)
     context.set_env(n=13)
     expect(context.os.environ['n']).to(equal(str(13)))
Example #17
0
 def test_converts_bool_to_string(self):
     context = DependencyContext(supply_env=True)
     context.set_env(n=False)
     expect(context.os.environ['n']).to(equal(str(False)))
 def test_get_returns_injected(self):
     dependency = object()
     injected = object()
     sut = DependencyContext()
     sut.inject(dependency, injected)
     expect(sut.get(dependency)).to(be(injected))
Example #19
0
 def test_converts_arbitrary_rubbish_to_string(self):
     context = DependencyContext(supply_env=True)
     rubbish = {'menu': ['spam', 'eggs', 'sausage', 'biggles']}
     context.set_env(rubbish=rubbish)
     expect(context.os.environ['rubbish']).to(equal(str(rubbish)))
 def test_deletes_specified_entry_only(self):
     context = DependencyContext(supply_env=True)
     context.os.environ = {'spam': 1, 'eggs': 2, 'sausage': 3}
     context.unset_env('eggs')
     expect(context.os.environ).to(equal({'spam': 1, 'sausage': 3}))
Example #21
0
 def test_can_set_arbitrary_var(self):
     key = 'some_key'
     value = 'some_value'
     context = DependencyContext(supply_env=True)
     context.set_env(**{key: value})
     expect(context.os.environ[key]).to(equal(value))
Example #22
0
 def test_deletes_specified_entry_only(self):
     context = DependencyContext(supply_env=True)
     context.os.environ = {"spam": 1, "eggs": 2, "sausage": 3}
     context.unset_env("eggs")
     expect(context.os.environ).to(equal({"spam": 1, "sausage": 3}))
 def test_refuses_to_supply_logging_if_parent_specified(self):
     try:
         DependencyContext(parent=DependencyContext(), supply_logging=True)
         assert False, 'Caught nothing'
     except ValueError:
         pass  # Caught what we expected
 def test_inherits_fake_from_parent_context(self):
     parent = DependencyContext(supply_logging=True)
     child = DependencyContext(parent=parent)
     expect(child.logging).to(be(parent.logging))