def test_args_from_last_call_complains_when_there_are_no_calls(self): expect(FunctionSpy().args_from_last_call).to( complain(FunctionNotCalled))
def test_complains_if_there_were_no_calls(self): expect(lambda: FunctionSpy()[0]).to(complain(FunctionNotCalled))
def test_kwargs_from_last_call_returns_kwargs_from_last_call(self): planted = {'do': 1, 're': 2, 'mi': 3} spy = FunctionSpy() spy(foo=2, bar=7) spy(**planted) expect(spy.kwargs_from_last_call()).to(equal(planted))
def test_get_numeric_item_returns_arg_from_last_call(self): spy = FunctionSpy() planted = 'your card' spy(1, 23, 44) spy(2, planted, 6) expect(spy[1]).to(equal(planted))
def test_get_non_numeric_item_returns_kwarg_from_last_call(self): spy = FunctionSpy() planted = 'roger' spy(1, 22, 44) spy(planted=planted) expect(spy['planted']).to(equal(planted))
def test_can_specify_return_value(self): planted = 42 spy = FunctionSpy(return_value=planted) expect(spy()).to(equal(planted))
def test_publishes_suite_ended_last(self): spy = FunctionSpy() EventBroker.subscribe(events=ALL_EVENTS, func=spy) execute() args, kwargs = spy.call_history[-1] expect(kwargs["event"]).to(equal(TestEvent.suite_ended))
def setUp(self): self.context = open_dependency_context(supply_env=True, supply_logging=True) self.webdriver_stub = EndlessFake(pattern_obj=webdriver) self.remote_spy = FunctionSpy() self.webdriver_stub.Remote = self.remote_spy self.context.inject(webdriver, self.webdriver_stub)
def test_complains_when_requested_kwarg_is_absent(self): spy = FunctionSpy() spy(things=2) expect(lambda: spy['stuff']).to(complain(KwargNotSpecified))
def test_publishes_test_ended_with_suite_name(self): spy = FunctionSpy() EventBroker.subscribe(event=TestEvent.test_ended, func=spy) execute() expect(spy["suite_name"]).to(equal(SUITE_NAME))
def test_publishes_test_failed_when_expected_and_absent(self): table = (("expect exception", ), (ValueError, )) spy = FunctionSpy() EventBroker.subscribe(event=TestEvent.test_failed, func=spy) execute_test_table(table=table, func=lambda: None) spy.assert_was_called()
def test_publishes_test_ended_with_test_name(self): spy = FunctionSpy() EventBroker.subscribe(event=TestEvent.test_ended, func=spy) execute() expect([call[1]["test_name"] for call in spy.call_history]).to(equal(list(TEST_NAMES)))
def test_publishes_test_started_for_each_row(self): spy = FunctionSpy() EventBroker.subscribe(event=TestEvent.test_started, func=spy) execute() expect(spy.call_history).to(have_length(len(TEST_NAMES)))
def test_publishes_suite_ended_exactly_once(self): spy = FunctionSpy() EventBroker.subscribe(events=ALL_EVENTS, func=spy) execute() expect(events_with_type(spy.call_history, TestEvent.suite_ended)).to(have_length(1))
def test_assert_was_called_does_not_complain_when_called(self): spy = FunctionSpy() spy() expect(spy.assert_was_called).not_to(complain(FunctionNotCalled))
def test_complains_when_requested_arg_is_absent(self): spy = FunctionSpy() spy(0, 1) expect(lambda: spy[2]).to(complain(ArgNotSpecified))
class TestExecute(TestCase): def setUp(self): self.context = open_dependency_context(supply_env=True, supply_logging=True) self.fake_http_client = EndlessFake() self.fake_requests_response = requests.Response() self.fake_requests_response.json = lambda: {} self.post_spy = FunctionSpy(return_value=self.fake_requests_response) self.fake_http_client.post = self.post_spy self.graph_client = GraphqlClient(http_client=self.fake_http_client, url="ignore me") def tearDown(self): self.context.close() def default_execute(self): return self.graph_client.execute("a not important operation", **{"dingle": "dangle"}) @parameterized.expand(["a real operation", "mutation is cool"]) def test_sends_operation_through_http_client(self, expected_operation): graph_client = GraphqlClient(http_client=self.fake_http_client, url="a real url") graph_client.execute(expected_operation) formatted_data = json.loads( self.post_spy.kwargs_from_last_call()["data"]) expect(formatted_data).to(have_keys(query=expected_operation)) @parameterized.expand(["send it here", "the correct url now"]) def test_sends_operation_to_correct_url_through_http_client( self, expected_url): graph_client = GraphqlClient(http_client=self.fake_http_client, url=expected_url) graph_client.execute("a not important operation") expect(self.post_spy.kwargs_from_last_call()).to( have_keys(url=expected_url)) @parameterized.expand([({ "dingle": "dangle" }, ), ({ "dangle": ["dingle", "dongle"] }, )]) def test_sends_kwargs_as_variables(self, expected_variables): self.graph_client.execute("a not important operation", **expected_variables) formatted_data = json.loads( self.post_spy.kwargs_from_last_call()["data"]) expect(formatted_data).to(have_keys(variables=expected_variables)) def test_sends_data_as_a_json_formatted_string(self): self.default_execute() data = self.post_spy.kwargs_from_last_call()["data"] def json_loads(): json.loads(data) expect(json_loads).to_not(complain(json.decoder.JSONDecodeError)) def test_sends_content_type_application_json_header(self): self.default_execute() headers = self.post_spy.kwargs_from_last_call()["headers"] expect(headers["Content-Type"]).to(equal("application/json")) def test_returns_a_graph_ql_response_object(self): response = self.default_execute() expect(response).to(be_a(GraphqlResponse)) def test_graph_ql_response_object_is_passed_response_from_operation(self): expected_url = "a fake url" self.fake_requests_response.url = expected_url post_spy = FunctionSpy(return_value=self.fake_requests_response) self.fake_http_client.post = post_spy response = self.default_execute() assert response.http_response == self.fake_requests_response, ( f"Was not passed the fake_requests_response " f"with url: '{expected_url}'") def test_raises_operation_failed_exception(self): self.fake_requests_response.json = lambda: { "errors": ["this did not go well"] } expect(self.default_execute).to(complain(OperationFailed)) def test_operation_failed_exception_is_passed_response_from_operation( self): self.fake_requests_response.json = lambda: { "errors": ["this did not go well"] } expected_url = "this is a unique identifier" self.fake_requests_response.url = expected_url try: self.default_execute() assert False, "The operation did not fail" except OperationFailed as exception: assert exception.http_response == self.fake_requests_response, ( f"Was not passed the fake_requests_response " f"with url: '{expected_url}'") def test_operation_does_not_catch_http_client_exceptions(self): def fake_post(): raise fake_http_error fake_http_error = Exception() self.fake_http_client.post = lambda **kwargs: fake_post() try: self.default_execute() assert False, "The exception was not raised" except Exception as actual: expect(actual).to(equal(fake_http_error)) @parameterized.expand([("str", "this is a string with the word errors in it"), ("list", ["errors", "this should not happen"])]) def test_execute_finds_errors_in_response_through_keys( self, object_type, bad_response): self.fake_requests_response.json = lambda: bad_response try: self.default_execute() assert False, "Expected #execute to raise an an AttributeError" except AttributeError as error: expect(error.args[0]).to( equal(f"'{object_type}' object has no attribute 'keys'"))
def test_return_value_is_none_if_not_specified(self): expect(FunctionSpy()()).to(be_none)
def test_last_call_complains_when_not_called(self): expect(FunctionSpy().last_call).to(complain(FunctionNotCalled))
def test_publishes_sample_measured_with_test_name(self): spy = FunctionSpy() EventBroker.subscribe(event=TestEvent.sample_measured, func=spy) execute() expect(spy["test_name"]).to(equal(TEST_NAMES[-1]))