| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d5de4b1 commit 8ba00fc
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 | |
@@ -137,6 +140,11 @@ def __getattr__(self, name: str) -> Any: | |||
| 137 | 140 | ) | |
| 138 | 141 | + f"\n\n{_URL_ATTRIBUTE_ERROR}" | |
| 139 | 142 | ) | |
| 143 | + elif self._lazy: | ||
| 144 | + message = f"{message}\n\n" + textwrap.fill( | ||
| 145 | + f"{self.__class__!r} was created as a `lazy` object and " | ||
| 146 | + f"was not initialzed with any data." | ||
| 147 | + ) | ||
| 140 | 148 | raise AttributeError(message) | |
| 141 | 149 | ||
| 142 | 150 | def __setattr__(self, name: str, value: Any) -> None: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -135,11 +135,11 @@ def get( | |||
| 135 | 135 | if lazy is True: | |
| 136 | 136 | if TYPE_CHECKING: | |
| 137 | 137 | assert self._obj_cls._id_attr is not None | |
| 138 | - return self._obj_cls(self, {self._obj_cls._id_attr: id}) | ||
| 138 | + return self._obj_cls(self, {self._obj_cls._id_attr: id}, lazy=lazy) | ||
| 139 | 139 | server_data = self.gitlab.http_get(path, **kwargs) | |
| 140 | 140 | if TYPE_CHECKING: | |
| 141 | 141 | assert not isinstance(server_data, requests.Response) | |
| 142 | - return self._obj_cls(self, server_data) | ||
| 142 | + return self._obj_cls(self, server_data, lazy=lazy) | ||
| 143 | 143 | ||
| 144 | 144 | ||
| 145 | 145 | class GetWithoutIdMixin(HeadMixin, _RestManagerBase): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -45,9 +45,47 @@ class M(GetMixin, FakeManager): | |||
| 45 | 45 | assert isinstance(obj, FakeObject) | |
| 46 | 46 | assert obj.foo == "bar" | |
| 47 | 47 | assert obj.id == 42 | |
| 48 | + assert obj._lazy is False | ||
| 48 | 49 | assert responses.assert_call_count(url, 1) is True | |
| 49 | 50 | ||
| 50 | 51 | ||
| 52 | + def test_get_mixin_lazy(gl): | ||
| 53 | + class M(GetMixin, FakeManager): | ||
| 54 | + pass | ||
| 55 | + | ||
| 56 | + url = "http://localhost/api/v4/tests/42" | ||
| 57 | + | ||
| 58 | + mgr = M(gl) | ||
| 59 | + with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: | ||
| 60 | + rsps.add( | ||
| 61 | + method=responses.GET, | ||
| 62 | + url=url, | ||
| 63 | + json={"id": 42, "foo": "bar"}, | ||
| 64 | + status=200, | ||
| 65 | + match=[responses.matchers.query_param_matcher({})], | ||
| 66 | + ) | ||
| 67 | + obj = mgr.get(42, lazy=True) | ||
| 68 | + assert isinstance(obj, FakeObject) | ||
| 69 | + assert not hasattr(obj, "foo") | ||
| 70 | + assert obj.id == 42 | ||
| 71 | + assert obj._lazy is True | ||
| 72 | + # a `lazy` get does not make a network request | ||
| 73 | + assert len(rsps.calls) == 0 | ||
| 74 | + | ||
| 75 | + | ||
| 76 | + def test_get_mixin_lazy_missing_attribute(gl): | ||
| 77 | + class FakeGetManager(GetMixin, FakeManager): | ||
| 78 | + pass | ||
| 79 | + | ||
| 80 | + manager = FakeGetManager(gl) | ||
| 81 | + obj = manager.get(1, lazy=True) | ||
| 82 | + assert obj.id == 1 | ||
| 83 | + with pytest.raises(AttributeError) as exc: | ||
| 84 | + obj.missing_attribute | ||
| 85 | + assert "as a `lazy` object" in str(exc) | ||
| 86 | + assert "'missing_attribute'" in str(exc) | ||
| 87 | + | ||
| 88 | + | ||
| 51 | 89 | @responses.activate | |
| 52 | 90 | def test_head_mixin(gl): | |
| 53 | 91 | class M(GetMixin, FakeManager): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments