def test_format_path_with_incorrect_kwargs(self): foo = apiron.Endpoint(path="/{one}/{two}/") path_kwargs = {"foo": "bar"} with pytest.warns(RuntimeWarning, match="An unknown path kwarg was supplied"): with pytest.raises(KeyError): foo.get_formatted_path(**path_kwargs)
def test_using_path_kwargs_produces_warning(self, mock_send, service): service.foo = apiron.Endpoint(path='/foo/{one}') with warnings.catch_warnings(record=True) as warning_records: warnings.simplefilter('always') _ = service.foo(path_kwargs={'one': 'bar'}) assert 1 == len(warning_records) assert issubclass(warning_records[-1].category, RuntimeWarning)
def test_format_path_with_extra_kwargs(self): foo = apiron.Endpoint(path='/{one}/{two}/') path_kwargs = {'one': 'foo', 'two': 'bar', 'three': 'not used'} with warnings.catch_warnings(record=True) as warning_records: assert '/foo/bar/' == foo.get_formatted_path(**path_kwargs) assert 1 == len(warning_records) assert issubclass(warning_records[-1].category, RuntimeWarning)
def test_format_path_with_incorrect_kwargs(self): foo = apiron.Endpoint(path='/{one}/{two}/') path_kwargs = {'foo': 'bar'} with pytest.raises(KeyError): with warnings.catch_warnings(record=True) as warning_records: warnings.simplefilter('always') foo.get_formatted_path(**path_kwargs) assert 1 == len(warning_records) assert issubclass(warning_records[-1].category, RuntimeWarning)
def test_legacy_endpoint_usage_with_instantiated_service( self, MockSession, mock_timeout, service): service.foo = apiron.Endpoint(path="/foo/") instantiated_service = service() mock_logger = mock.Mock() request = mock.Mock() request.url = "http://host1.biz/foo/" ServiceCaller.call(instantiated_service, instantiated_service.foo, timeout_spec=mock_timeout, logger=mock_logger)
def test_call(self, service): service.foo = apiron.Endpoint() service.foo()
def test_path_placeholders_when_present(self): foo = apiron.Endpoint(path='/foo/{one}/{two}') assert ['one', 'two'] == foo.path_placeholders
def test_format_path_with_correct_kwargs(self): foo = apiron.Endpoint(path="/{one}/{two}/") path_kwargs = {"one": "foo", "two": "bar"} assert "/foo/bar/" == foo.get_formatted_path(**path_kwargs)
def test_path_placeholders_when_none_present(self): foo = apiron.Endpoint() assert [] == foo.path_placeholders
def test_required_headers(self): foo = apiron.Endpoint() assert {} == foo.required_headers
def test_constructor_stores_passed_attributes(self): foo = apiron.Endpoint(path="/foo/", default_method="POST") assert "/foo/" == foo.path assert "POST" == foo.default_method
def test_str_method(self): foo = apiron.Endpoint(path="/bar/baz") assert str(foo) == "/bar/baz"
def test_get_merged_params_with_required_and_default_param(self): foo = apiron.Endpoint(default_params={"foo": "bar"}, required_params={"foo"}) assert {"foo": "bar"} == foo.get_merged_params()
def test_query_parameter_in_path_generates_warning(self): with warnings.catch_warnings(record=True) as warning_records: warnings.simplefilter('always') _ = apiron.Endpoint(path='/?foo=bar') assert 1 == len(warning_records) assert issubclass(warning_records[-1].category, UserWarning)
def test_format_path_with_correct_kwargs(self): foo = apiron.Endpoint(path='/{one}/{two}/') path_kwargs = {'one': 'foo', 'two': 'bar'} assert '/foo/bar/' == foo.get_formatted_path(**path_kwargs)
def test_call_without_service_raises_exception(self): foo = apiron.Endpoint() with pytest.raises(TypeError): foo()
def test_repr_method(self): foo = apiron.Endpoint(path="/bar/baz") assert repr(foo) == "Endpoint(path='/bar/baz')"
def test_default_attributes_from_constructor(self): foo = apiron.Endpoint() assert "/" == foo.path assert "GET" == foo.default_method
def test_get_merged_params(self): foo = apiron.Endpoint(default_params={"foo": "bar"}, required_params={"baz"}) assert {"foo": "bar", "baz": "qux"} == foo.get_merged_params({"baz": "qux"})
def test_format_response(self): foo = apiron.Endpoint() mock_response = mock.Mock() mock_response.text = "foobar" assert "foobar" == foo.format_response(mock_response)
def test_get_merged_params_with_unsupplied_param(self): foo = apiron.Endpoint(default_params={"foo": "bar"}, required_params={"baz"}) with pytest.raises(apiron.UnfulfilledParameterException): foo.get_merged_params()
def test_str_method(self): foo = apiron.Endpoint(path="/foo/bar/") assert "/foo/bar/" == str(foo)
def test_format_path_with_extra_kwargs(self): foo = apiron.Endpoint(path="/{one}/{two}/") path_kwargs = {"one": "foo", "two": "bar", "three": "not used"} with pytest.warns(RuntimeWarning, match="An unknown path kwarg was supplied"): assert "/foo/bar/" == foo.get_formatted_path(**path_kwargs)
def test_path_placeholders_when_present(self): foo = apiron.Endpoint(path="/foo/{one}/{two}") assert ["one", "two"] == foo.path_placeholders
def test_constructor_stores_passed_attributes(self): foo = apiron.Endpoint(path='/foo/', default_method='POST') assert '/foo/' == foo.path assert 'POST' == foo.default_method
def test_get_merged_params_with_empty_param(self): foo = apiron.Endpoint(default_params={"foo": "bar"}, required_params={"baz"}) with pytest.warns(RuntimeWarning, match="endpoint was called with empty parameters"): assert {"foo": "bar", "baz": None} == foo.get_merged_params({"baz": None})
def test_str_method(self): foo = apiron.Endpoint(path='/foo/bar/') assert '/foo/bar/' == str(foo)
def test_query_parameter_in_path_generates_warning(self): with pytest.warns(UserWarning, match="Endpoint path may contain query parameters"): _ = apiron.Endpoint(path="/?foo=bar")
def test_using_path_kwargs_produces_warning(self, mock_send, service): service.foo = apiron.Endpoint(path="/foo/{one}") with pytest.warns(RuntimeWarning, match="path_kwargs is no longer necessary"): _ = service.foo(path_kwargs={"one": "bar"})