import pytest import requests import responses from gitlab import base from gitlab import types as gl_types from gitlab.mixins import ( CreateMixin, DeleteMixin, GetMixin, GetWithoutIdMixin, ListMixin, RefreshMixin, SaveMixin, SetMixin, UpdateMixin, ) class FakeObject(base.RESTObject): pass class FakeManager(base.RESTManager): _path = "/tests" _obj_cls = FakeObject @responses.activate def test_get_mixin(gl): class M(GetMixin, FakeManager): pass url = "http://localhost/api/v4/tests/42" responses.add( method=responses.GET, url=url, json={"id": 42, "foo": "bar"}, status=200, match=[responses.matchers.query_param_matcher({})], ) mgr = M(gl) obj = mgr.get(42) assert isinstance(obj, FakeObject) assert obj.foo == "bar" assert obj.id == 42 assert obj._lazy is False assert responses.assert_call_count(url, 1) is True def test_get_mixin_lazy(gl): class M(GetMixin, FakeManager): pass url = "http://localhost/api/v4/tests/42" mgr = M(gl) with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: rsps.add( method=responses.GET, url=url, json={"id": 42, "foo": "bar"}, status=200, match=[responses.matchers.query_param_matcher({})], ) obj = mgr.get(42, lazy=True) assert isinstance(obj, FakeObject) assert not hasattr(obj, "foo") assert obj.id == 42 assert obj._lazy is True # a `lazy` get does not make a network request assert not rsps.calls def test_get_mixin_lazy_missing_attribute(gl): class FakeGetManager(GetMixin, FakeManager): pass manager = FakeGetManager(gl) obj = manager.get(1, lazy=True) assert obj.id == 1 with pytest.raises(AttributeError) as exc: obj.missing_attribute # undo `textwrap.fill()` message = str(exc.value).replace("\n", " ") assert "'FakeObject' object has no attribute 'missing_attribute'" in message assert ( "note that was " "created as a `lazy` object and was not initialized with any data." ) in message @responses.activate def test_head_mixin(gl): class M(GetMixin, FakeManager): pass url = "http://localhost/api/v4/tests/42" responses.add( method=responses.HEAD, url=url, headers={"X-GitLab-Header": "test"}, status=200, match=[responses.matchers.query_param_matcher({})], ) manager = M(gl) result = manager.head(42) assert isinstance(result, requests.structures.CaseInsensitiveDict) assert result["x-gitlab-header"] == "test" @responses.activate def test_refresh_mixin(gl): class TestClass(RefreshMixin, FakeObject): pass url = "http://localhost/api/v4/tests/42" responses.add( method=responses.GET, url=url, json={"id": 42, "foo": "bar"}, status=200, match=[responses.matchers.query_param_matcher({})], ) mgr = FakeManager(gl) obj = TestClass(mgr, {"id": 42}) res = obj.refresh() assert res is None assert obj.foo == "bar" assert obj.id == 42 assert responses.assert_call_count(url, 1) is True @responses.activate def test_get_without_id_mixin(gl): class M(GetWithoutIdMixin, FakeManager): pass url = "http://localhost/api/v4/tests" responses.add( method=responses.GET, url=url, json={"foo": "bar"}, status=200, match=[responses.matchers.query_param_matcher({})], ) mgr = M(gl) obj = mgr.get() assert isinstance(obj, FakeObject) assert obj.foo == "bar" assert not hasattr(obj, "id") assert responses.assert_call_count(url, 1) is True @responses.activate def test_list_mixin(gl): class M(ListMixin, FakeManager): pass url = "http://localhost/api/v4/tests" headers = { "X-Page": "1", "X-Next-Page": "2", "X-Per-Page": "1", "X-Total-Pages": "2", "X-Total": "2", "Link": ("