def test_attribute_requested_returns_true_if_tracking_and_requested(self): stub = EmptyFake() stub.thing = None spy = MasterSpy(stub, affect_only_functions=False) spy.thing assert spy.attribute_was_requested( "thing"), "Spy did not report that attribute was requested"
def test_last_call_to_returns_last_call_to_given_func(self): stub = EmptyFake() stub.func = lambda *a, **k: None spy = MasterSpy(stub) planted_args = (4, 5) planted_kwargs = {'foo': 77, 'bar': 'soap'} spy.func(spam=1, eggs=2) spy.func(*planted_args, **planted_kwargs) expect(spy.last_call_to('func')).to( equal((planted_args, planted_kwargs)))
def test_saves_first_function_call(self): planted_args = (1, 4, 5) planted_kwargs = {"spam": 32, "eggs": object()} stub = EmptyFake() stub.func = lambda *a, **k: None spy = MasterSpy(stub) spy.func(*planted_args, **planted_kwargs) spy.func(5, 7, 2, bruces=4, michaels=1) expect(spy.attribute_spies["func"].call_history[0]).to( equal((planted_args, planted_kwargs)))
def test_last_call_to_returns_last_call_to_given_func(self): stub = EmptyFake() stub.func = lambda *a, **k: None spy = MasterSpy(stub) planted_args = (4, 5) planted_kwargs = {"foo": 77, "bar": "soap"} spy.func(spam=1, eggs=2) spy.func(*planted_args, **planted_kwargs) expect(spy.last_call_to("func")).to( equal((planted_args, planted_kwargs)))
def test_saves_last_function_call(self): planted_args = (1, 4, 5) planted_kwargs = {'spam': 32, 'eggs': object()} stub = EmptyFake() stub.func = lambda *a, **k: None spy = MasterSpy(stub) spy.func(5, 7, 2, bruces=4, michaels=1) spy.func() spy.func(*planted_args, **planted_kwargs) expect(spy.attribute_spies['func'].call_history[-1]).to( equal((planted_args, planted_kwargs)))
def test_respects_hasattr_for_pattern_objects(self): class Pattern: def __getattr__(self, name): return "spam" fake = EmptyFake(pattern_obj=Pattern()) assert hasattr(fake, "anything"), "hasattr returned False"
def setUp(self): EventBroker.reset() self.context = open_dependency_context() self.fake_now = datetime.fromtimestamp(1515429469.61845) self.fake_datetime_module = EmptyFake() self.fake_datetime_module.now = lambda: self.fake_now self.context.inject(datetime, self.fake_datetime_module)
def setUp(self): self.context = open_dependency_context(supply_fs=True, supply_logging=True) self.context.inject(StreamHandler, EmptyFake()) EventBroker.reset() subscribe_event_handlers(self) self.skip_events = 0
def test_returns_empty_fake_when_obj_pattern_satisfied(self): pattern = MutableObject() attr = "yup" setattr(pattern, attr, None) fake = EmptyFake(pattern_obj=pattern) returned = getattr(fake, attr) expect(returned).to(be_a(EmptyFake)) # Verify that the pattern restriction is not inherited expect(returned.spam).to(be_a(EmptyFake))
def setUp(self): self.context = open_dependency_context(supply_env=True, supply_fs=True, supply_logging=True) self.context.set_env( DELAY_BETWEEN_CHECKS_FOR_PARALLEL_SUITE_COMPLETION=0) self.fake_process = EmptyFake() self.context.inject_as_class(Popen, self.fake_process) self.pretend_process_is_running()
def test_complains_when_cannot_parse_line(self): reporters_filename = "/here/is/something/silly" self.set_filename(reporters_filename) module_name = "fake_module" self.fake_importer.add_module(module_name, EmptyFake()) self.fake_files.create( reporters_filename, """ Stop being silly """, ) expect(activate_reporters).to(raise_ex(InvalidConfiguration))
def setUp(self): self.context = open_dependency_context(supply_env=True, supply_fs=True, supply_logging=True) self.context.inject(StreamHandler, EmptyFake()) EventBroker.reset() subscribe_event_handlers(self) self.skip_events = 0 self.skip_msg = "intentional" self.suite_teardown_ran = False self.test_setup_ran = False self.test_teardown_ran = False
def test_ignores_comment_line(self): reporters_filename = "/here/is/something/silly" self.set_filename(reporters_filename) module_name = "fake_module" self.fake_importer.add_module(module_name, EmptyFake()) self.fake_files.create( reporters_filename, """ # This should be ignored %s.FakeClass # This should be ignored too """ % module_name, ) expect(activate_reporters).not_to(raise_ex(Exception))
def test_applies_affect_only_functions_flag_recursively(self): child = EmptyFake() child.value = 7 parent = EmptyFake() parent.get_child = lambda: child parent_spy = MasterSpy(parent, affect_only_functions=False) parent_spy.get_child() func_spy = parent_spy.attribute_spies["get_child"] ret_spy = func_spy.return_value_spies[0] ret_spy.value expect(ret_spy.attribute_spies.keys()).to(contain("value"))
def test_activates_specified_class(self): reporters_filename = "/here/is/something/silly" self.set_filename(reporters_filename) module_name = "fake_module" class_name = "FakeClass" module = EmptyFake() spy = HandlerSpy() setattr(module, class_name, spy) self.fake_importer.add_module(module_name, module) self.fake_files.create( reporters_filename, """ %s.%s """ % (module_name, class_name), ) activate_reporters() assert spy.was_activated, "Handler was not activated"
def test_gt_returns_false(self): expect(EmptyFake()).not_to(be_above(42))
def test_call_accepts_arbitary_args_and_kwargs(self): expect(lambda: EmptyFake()(4, spams=1)).not_to(complain(TypeError))
def test_different_fake_has_different_hash(self): expect(hash(EmptyFake())).not_to(equal(hash(EmptyFake())))
def test_same_fake_has_same_hash(self): fake = EmptyFake() expect(hash(fake)).to(equal(hash(fake)))
def test_le_returns_true_if_other_is_empty_fake(self): expect(EmptyFake()).to(be_below_or_equal(EmptyFake()))
def test_iter_returns_self(self): fake = EmptyFake() expect(iter(fake)).to(be(fake))
def test_does_not_wrap_non_functions_by_default(self): target = EmptyFake() target.non_func = 42 spy = MasterSpy(target) expect(spy.non_func).to(be(target.non_func))
def test_can_wrap_non_functions(self): target = EmptyFake() target.non_func = 42 spy = MasterSpy(target, affect_only_functions=False) expect(spy.non_func).to(be_a(MasterSpy))
def test_returns_false_if_other_is_not_empty_fake(self): expect(EmptyFake()).not_to(be_below_or_equal(42))
def test_getattr_returns_requested_attribute(self): stub = EmptyFake() stub.thing = 42 spy = MasterSpy(stub) expect(spy.thing).to(equal(stub.thing))
def test_last_call_to_raises_function_not_called(self): stub = EmptyFake() stub.func = lambda: None spy = MasterSpy(stub) expect(lambda: spy.last_call_to("func")).to( complain(FunctionNotCalled))
def test_ne_returns_false_if_other_is_empty_fake(self): assert not (EmptyFake() != EmptyFake()), "__ne__ returned true for another EmptyFake"
def test_ne_returns_true_if_other_is_not_empty_fake(self): assert EmptyFake() != 42, "__ne__ returned false for 42"
def test_len_is_0(self): expect(EmptyFake()).to(have_length(0))
def test_lt_returns_false(self): expect(EmptyFake()).not_to(be_below(42))