| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1efb123 commit 7ed5620
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,6 +62,7 @@ class RESTObject: | |||
| 62 | 62 | _parent_attrs: Dict[str, Any] | |
| 63 | 63 | _repr_attr: Optional[str] = None | |
| 64 | 64 | _updated_attrs: Dict[str, Any] | |
| 65 | + _lazy: bool | ||
| 65 | 66 | manager: "RESTManager" | |
| 66 | 67 | ||
| 67 | 68 | def __init__( | |
@@ -70,6 +71,7 @@ def __init__( | |||
| 70 | 71 | attrs: Dict[str, Any], | |
| 71 | 72 | *, | |
| 72 | 73 | created_from_list: bool = False, | |
| 74 | + lazy: bool = False, | ||
| 73 | 75 | ) -> None: | |
| 74 | 76 | if not isinstance(attrs, dict): | |
| 75 | 77 | raise GitlabParsingError( | |
@@ -84,6 +86,7 @@ def __init__( | |||
| 84 | 86 | "_updated_attrs": {}, | |
| 85 | 87 | "_module": importlib.import_module(self.__module__), | |
| 86 | 88 | "_created_from_list": created_from_list, | |
| 89 | + "_lazy": lazy, | ||
| 87 | 90 | } | |
| 88 | 91 | ) | |
| 89 | 92 | self.__dict__["_parent_attrs"] = self.manager.parent_attrs | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -106,11 +106,11 @@ def get( | |||
| 106 | 106 | if lazy is True: | |
| 107 | 107 | if TYPE_CHECKING: | |
| 108 | 108 | assert self._obj_cls._id_attr is not None | |
| 109 | - return self._obj_cls(self, {self._obj_cls._id_attr: id}) | ||
| 109 | + return self._obj_cls(self, {self._obj_cls._id_attr: id}, lazy=lazy) | ||
| 110 | 110 | server_data = self.gitlab.http_get(path, **kwargs) | |
| 111 | 111 | if TYPE_CHECKING: | |
| 112 | 112 | assert not isinstance(server_data, requests.Response) | |
| 113 | - return self._obj_cls(self, server_data) | ||
| 113 | + return self._obj_cls(self, server_data, lazy=lazy) | ||
| 114 | 114 | ||
| 115 | 115 | ||
| 116 | 116 | class GetWithoutIdMixin(_RestManagerBase): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -44,9 +44,34 @@ class M(GetMixin, FakeManager): | |||
| 44 | 44 | assert isinstance(obj, FakeObject) | |
| 45 | 45 | assert obj.foo == "bar" | |
| 46 | 46 | assert obj.id == 42 | |
| 47 | + assert obj._lazy is False | ||
| 47 | 48 | assert responses.assert_call_count(url, 1) is True | |
| 48 | 49 | ||
| 49 | 50 | ||
| 51 | + def test_get_mixin_lazy(gl): | ||
| 52 | + class M(GetMixin, FakeManager): | ||
| 53 | + pass | ||
| 54 | + | ||
| 55 | + url = "http://localhost/api/v4/tests/42" | ||
| 56 | + | ||
| 57 | + mgr = M(gl) | ||
| 58 | + with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: | ||
| 59 | + rsps.add( | ||
| 60 | + method=responses.GET, | ||
| 61 | + url=url, | ||
| 62 | + json={"id": 42, "foo": "bar"}, | ||
| 63 | + status=200, | ||
| 64 | + match=[responses.matchers.query_param_matcher({})], | ||
| 65 | + ) | ||
| 66 | + obj = mgr.get(42, lazy=True) | ||
| 67 | + assert isinstance(obj, FakeObject) | ||
| 68 | + assert not hasattr(obj, "foo") | ||
| 69 | + assert obj.id == 42 | ||
| 70 | + assert obj._lazy is True | ||
| 71 | + # a `lazy` get does not make a network request | ||
| 72 | + assert len(rsps.calls) == 0 | ||
| 73 | + | ||
| 74 | + | ||
| 50 | 75 | @responses.activate | |
| 51 | 76 | def test_refresh_mixin(gl): | |
| 52 | 77 | class TestClass(RefreshMixin, FakeObject): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments