def test_direct_with_context_manager(self): real_spam = 'real spam' fake_spam = 'injected spam' with dependency_context() as outer: with dependency_context(parent=outer) as inner: inner.inject(real_spam, fake_spam) expect(dependency(real_spam)).to(equal(fake_spam))
def test_inherited_with_context_manager(self): real_spam = 'real spam' fake_spam = 'injected spam' with dependency_context() as outer: outer.inject(real_spam, fake_spam) with dependency_context(parent=outer): expect(dependency(real_spam)).to(equal(fake_spam))
def test_parent_unaffected_by_child_injection(self): real_spam = 'real spam' fake_spam = 'injected spam' with dependency_context() as outer: with dependency_context(parent=outer) as inner: inner.inject(real_spam, fake_spam) expect(dependency(real_spam)).to(equal(real_spam))
def test_popped_layer_is_no_longer_used(self): bottom_canary = Canary() top_canary = Canary() with dependency_context() as bottom_context: bottom_context.inject(Canary, SingletonClass(bottom_canary)) with dependency_context() as top_context: top_context.inject(Canary, SingletonClass(top_canary)) touch_a_canary() expect(top_canary.touched).to(be_false)
def test_pops_top_layer_off_the_stack(self): bottom_canary = Canary() top_canary = Canary() with dependency_context() as bottom_context: bottom_context.inject(Canary, SingletonClass(bottom_canary)) with dependency_context() as top_context: top_context.inject(Canary, SingletonClass(top_canary)) touch_a_canary() expect(bottom_canary.touched).to(be_true)
def test_does_not_use_object_injected_lower_in_stack(self): bottom_canary = Canary() top_canary = Canary() with dependency_context() as bottom_context: bottom_context.inject(Canary, SingletonClass(bottom_canary)) with dependency_context() as top_context: top_context.inject(Canary, SingletonClass(top_canary)) touch_a_canary() expect(bottom_canary.touched).to(be_false)
def test_uses_object_injected_at_top_of_stack(self): bottom_canary = Canary() top_canary = Canary() with dependency_context() as bottom_context: bottom_context.inject(Canary, SingletonClass(bottom_canary)) with dependency_context() as top_context: top_context.inject(Canary, SingletonClass(top_canary)) touch_a_canary() expect(top_canary.touched).to(be_true)
def test_precedence(self): spam = 'SPAM' inner_fake = 'inner fake' outer_fake = 'outer fake' with dependency_context() as outer: outer.inject(spam, outer_fake) with dependency_context(parent=outer) as inner: inner.inject(spam, inner_fake) expect(dependency(spam)).to(equal(inner_fake))
def test_stores_return_value(self): expected = 42 with dependency_context() as context: sut = context.create_time_controller(target=lambda: expected, daemon=True) sut.start() sut.join() expect(sut.value_returned).to(equal(expected))
def test_survives_mutation(self): with dependency_context() as context: dep = ['spam', 'eggs', 'sausage'] thing = object() context.inject(dep, thing) dep.append('spam') expect(dependency(dep)).to(be(thing))
def test_provides_create_file_convenience(self): planted = b"yummy fig leaves" with dependency_context(supply_fs=True) as context: filename = context.os.path.join("a", "b", "c.this") context.create_file(filename, content=planted) f = context.os.open(filename, flags=os.O_RDONLY) expect(context.os.read(f, len(planted))).to(equal(planted))
def test_survives_mutation(self): with dependency_context() as context: dep = ["spam", "eggs", "sausage"] thing = object() context.inject(dep, thing) dep.append("spam") expect(dependency(dep)).to(be(thing))
def test_create_file_accepts_strings(self): planted = "some text" filename = "yad.dah" with dependency_context(supply_fs=True) as context: context.create_file(filename, text=planted) with dependency(open)(filename, "r") as f: expect(f.read()).to(equal(planted))
def test_complains_if_thread_not_started(self): with dependency_context() as context: def attempt(): context.attach_to_thread(Thread(target=print)) expect(attempt).to(raise_error(RuntimeError))
def test_complains_if_both_text_and_content(self): with dependency_context(supply_fs=True) as context: caught = None try: context.create_file("s", content=b"a", text="b") except TypeError as e: caught = e expect(caught).not_to(equal(None))
def test_injection_does_not_affect_another_thread(self): my_canary = Canary() with dependency_context() as context: context.inject(Canary, SingletonClass(my_canary)) t = Thread(target=touch_a_canary) t.start() t.join() expect(my_canary.touched).to(be_false)
def test_advance_complains_if_not_started(self): with dependency_context() as context: sut = context.create_time_controller(target=lambda: None) def attempt(): sut.advance(seconds=1) expect(attempt).to(raise_error(RuntimeError))
def test_inherits_origin_context(self): with dependency_context() as context: key = "dennis" value = 37 context.inject(key, "something else") ctl = context.create_time_controller(target=lambda: dependency(key)) context.inject(key, value) ctl.start() ctl.join() expect(ctl.value_returned).to(equal(value))
def test_stores_exception(self): exception_raised = Exception('intentional') def boom(): raise exception_raised with dependency_context() as context: sut = context.create_time_controller(target=boom, daemon=True) sut.start() sut.join() expect(sut.exception_caught).to(equal(exception_raised))
def test_injects_open(self): fname = "reginald" planted = b"some rubbish from test_fake_filesystem" with dependency_context(supply_fs=True) as context: # Write f = context.os.open(fname, flags=os.O_CREAT | os.O_WRONLY) context.os.write(f, planted) context.os.close(f) # Read try: with dependency(open)(fname, "rb") as f: expect(f.read()).to(equal(planted)) except FileNotFoundError: assert False, '"open" not find fake file'
def test_create_file_can_create_two_files_in_same_path(self): """ Verifies that os.makedirs gets called with exist_ok=True """ with dependency_context(supply_fs=True) as context: join = context.os.path.join path = join("somewhere", "nice") context.create_file(join(path, "a"), content=b"a") caught = None try: context.create_file(join(path, "b"), content=b"b") except FileExistsError as e: caught = e expect(caught).to(equal(None))
def test_does_not_affect_origin_context(self): keep_running = True def forrest(): while keep_running: sleep(0.001) with dependency_context() as context: original_context_time = datetime.fromtimestamp(2) context_dt = FakeDatetime(fixed_time=original_context_time) context.inject(datetime, context_dt) tc = context.create_time_controller(target=forrest) tc.start() sleep(0.01) expect(dependency(datetime).now()).to(equal(original_context_time)) keep_running = False tc.join()
def test_advance_advances_time_by_specified_delta(self): reported_time = None def canary(): nonlocal reported_time while True: reported_time = dependency(datetime).now() with dependency_context() as context: sut = context.create_time_controller(target=canary, daemon=True) sut.start() sleep(0.05) # Give SUT a chance to get started start_time = sut.fake_datetime.now() advance_delta = 42 sut.advance(seconds=advance_delta) sleep(0.05) # Give SUT a chance to cycle expect(reported_time).to(equal(start_time + timedelta(seconds=advance_delta)))
def test_stops_seeing_injection_when_context_ends(self): real_thing = "real thing" injected_thing = "injected thing" thing_seen_by_thread = None def canary(): nonlocal thing_seen_by_thread thing_seen_by_thread = dependency(real_thing) t = TwoStageThread(target=canary, daemon=True) t.start() with dependency_context() as context: context.inject(real_thing, injected_thing) context.attach_to_thread(t) t.really_start() t.join() expect(thing_seen_by_thread).to(be(real_thing))
def test_operation_failed_passes_args_to_super_correctly( self, expected_args): fake_requests_response = requests.Response() fake_requests_response.json = lambda: {"errors": "who cares"} fake_init = EndlessFake() class FakeSupered: def __init__(*args, **kwargs): fake_init.inited_args = args fake_init.inited_kwargs = kwargs with dependency_context(supply_env=True, supply_logging=True) as temporary_context: temporary_context.inject(super, lambda *args: FakeSupered) OperationFailed(*expected_args, requests_response=fake_requests_response) for index, arg in enumerate(expected_args): expect(fake_init.inited_args[index]).to(equal(arg))
def test_inherits_parent_context_os(self): parent = DependencyContext(supply_env=True) with dependency_context(parent=parent): expect(dependency(os)).to(be(parent.os))
def test_supplies_real_environment_if_unset(self): with dependency_context(): expect(dependency(os).environ).to(equal(os.environ))
def test_plays_nicely_with_fake_fs(self): key = "something_i_made_up" value = "Playdoh(tm)" with dependency_context(supply_env=True, supply_fs=True) as context: context.os.environ[key] = value expect(dependency(os).environ[key]).to(equal(value))
def test_also_initially_empty_with_fake_fs(self): with dependency_context(supply_env=True, supply_fs=True): expect(dependency(os).environ).to(equal({}))
def test_does_not_affect_other_os_attributes(self): with dependency_context(supply_env=True): expect(dependency(os).getpid).to(be(os.getpid))