diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unit/test_base.py | 16 | ||||
-rw-r--r-- | tests/unit/test_exceptions.py | 12 | ||||
-rw-r--r-- | tests/unit/test_utils.py | 48 |
3 files changed, 74 insertions, 2 deletions
diff --git a/tests/unit/test_base.py b/tests/unit/test_base.py index 137f480..1e4cb7b 100644 --- a/tests/unit/test_base.py +++ b/tests/unit/test_base.py @@ -78,13 +78,15 @@ class TestRESTManager: class TestRESTObject: def test_instantiate(self, fake_gitlab, fake_manager): - obj = FakeObject(fake_manager, {"foo": "bar"}) + attrs = {"foo": "bar"} + obj = FakeObject(fake_manager, attrs) - assert {"foo": "bar"} == obj._attrs + assert attrs == obj._attrs assert {} == obj._updated_attrs assert obj._create_managers() is None assert fake_manager == obj.manager assert fake_gitlab == obj.manager.gitlab + assert str(obj) == f"{type(obj)} => {attrs}" def test_instantiate_non_dict(self, fake_gitlab, fake_manager): with pytest.raises(gitlab.exceptions.GitlabParsingError): @@ -159,6 +161,7 @@ class TestRESTObject: obj1 = FakeObject(fake_manager, {"id": "foo"}) obj2 = FakeObject(fake_manager, {"id": "foo", "other_attr": "bar"}) assert obj1 == obj2 + assert len(set((obj1, obj2))) == 1 def test_equality_custom_id(self, fake_manager): class OtherFakeObject(FakeObject): @@ -177,3 +180,12 @@ class TestRESTObject: obj1 = FakeObject(fake_manager, {"attr1": "foo"}) obj2 = FakeObject(fake_manager, {"attr1": "bar"}) assert obj1 != obj2 + assert len(set((obj1, obj2))) == 2 + + def test_repr(self, fake_manager): + attrs = {"attr1": "foo"} + obj = FakeObject(fake_manager, attrs) + assert repr(obj) == "<FakeObject id:None>" + + FakeObject._id_attr = None + assert repr(obj) == "<FakeObject>" diff --git a/tests/unit/test_exceptions.py b/tests/unit/test_exceptions.py index 57b394b..6ef0939 100644 --- a/tests/unit/test_exceptions.py +++ b/tests/unit/test_exceptions.py @@ -3,6 +3,18 @@ import pytest from gitlab import exceptions +@pytest.mark.parametrize( + "kwargs,expected", + [ + ({"error_message": "foo"}, "foo"), + ({"error_message": "foo", "response_code": "400"}, "400: foo"), + ], +) +def test_gitlab_error(kwargs, expected): + error = exceptions.GitlabError(**kwargs) + assert str(error) == expected + + def test_error_raises_from_http_error(): """Methods decorated with @on_http_error should raise from GitlabHttpError.""" diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index dbe0838..8506f79 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -15,6 +15,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +import pytest +import requests +import responses + from gitlab import utils @@ -40,3 +44,47 @@ def test_sanitized_url(): src = "http://localhost/foo.bar.baz" dest = "http://localhost/foo%2Ebar%2Ebaz" assert dest == utils.sanitized_url(src) + + +@responses.activate +def test_response_content(capsys): + responses.add( + method="GET", + url="https://example.com", + status=200, + body="test", + content_type="application/octet-stream", + ) + + resp = requests.get("https://example.com", stream=True) + utils.response_content(resp, streamed=True, action=None, chunk_size=1024) + + captured = capsys.readouterr() + assert "test" in captured.out + + +@pytest.mark.parametrize( + "source,expected", + [ + ({"a": "", "b": "spam", "c": None}, {"a": "", "b": "spam", "c": None}), + ({"a": "", "b": {"c": "spam"}}, {"a": "", "b[c]": "spam"}), + ], +) +def test_copy_dict(source, expected): + dest = {} + + utils.copy_dict(dest, source) + assert dest == expected + + +@pytest.mark.parametrize( + "dictionary,expected", + [ + ({"a": None, "b": "spam"}, {"b": "spam"}), + ({"a": "", "b": "spam"}, {"a": "", "b": "spam"}), + ({"a": None, "b": None}, {}), + ], +) +def test_remove_none_from_dict(dictionary, expected): + result = utils.remove_none_from_dict(dictionary) + assert result == expected |