| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1ecbc7c commit 50b1f12
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -63,8 +63,8 @@ def get(self) -> Any: | |||
| 63 | 63 | def set_from_cli(self, cli_value: Any) -> None: | |
| 64 | 64 | self._value = cli_value | |
| 65 | 65 | ||
| 66 | - def get_for_api(self) -> Any: | ||
| 67 | - return self._value | ||
| 66 | + def get_for_api(self, *, key: str) -> Tuple[str, Any]: | ||
| 67 | + return (key, self._value) | ||
| 68 | 68 | ||
| 69 | 69 | ||
| 70 | 70 | class _ListArrayAttribute(GitlabAttribute): | |
@@ -76,30 +76,40 @@ def set_from_cli(self, cli_value: str) -> None: | |||
| 76 | 76 | else: | |
| 77 | 77 | self._value = [item.strip() for item in cli_value.split(",")] | |
| 78 | 78 | ||
| 79 | - def get_for_api(self) -> str: | ||
| 79 | + def get_for_api(self, *, key: str) -> Tuple[str, str]: | ||
| 80 | 80 | # Do not comma-split single value passed as string | |
| 81 | 81 | if isinstance(self._value, str): | |
| 82 | - return self._value | ||
| 82 | + return (key, self._value) | ||
| 83 | 83 | ||
| 84 | 84 | if TYPE_CHECKING: | |
| 85 | 85 | assert isinstance(self._value, list) | |
| 86 | - return ",".join([str(x) for x in self._value]) | ||
| 86 | + return (key, ",".join([str(x) for x in self._value])) | ||
| 87 | 87 | ||
| 88 | 88 | ||
| 89 | 89 | class ArrayAttribute(_ListArrayAttribute): | |
| 90 | 90 | """To support `array` types as documented in | |
| 91 | 91 | https://docs.gitlab.com/ee/api/#array""" | |
| 92 | 92 | ||
| 93 | + def get_for_api(self, *, key: str) -> Tuple[str, Any]: | ||
| 94 | + if isinstance(self._value, str): | ||
| 95 | + return (f"{key}[]", self._value) | ||
| 96 | + | ||
| 97 | + if TYPE_CHECKING: | ||
| 98 | + assert isinstance(self._value, list) | ||
| 99 | + return (f"{key}[]", self._value) | ||
| 100 | + | ||
| 93 | 101 | ||
| 94 | 102 | class CommaSeparatedListAttribute(_ListArrayAttribute): | |
| 95 | 103 | """For values which are sent to the server as a Comma Separated Values | |
| 96 | 104 | (CSV) string. We allow them to be specified as a list and we convert it | |
| 97 | 105 | into a CSV""" | |
| 98 | 106 | ||
| 107 | + pass | ||
| 108 | + | ||
| 99 | 109 | ||
| 100 | 110 | class LowercaseStringAttribute(GitlabAttribute): | |
| 101 | - def get_for_api(self) -> str: | ||
| 102 | - return str(self._value).lower() | ||
| 111 | + def get_for_api(self, *, key: str) -> Tuple[str, str]: | ||
| 112 | + return (key, str(self._value).lower()) | ||
| 103 | 113 | ||
| 104 | 114 | ||
| 105 | 115 | class FileAttribute(GitlabAttribute): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -76,13 +76,19 @@ def _transform_types( | |||
| 76 | 76 | continue | |
| 77 | 77 | ||
| 78 | 78 | type_obj = type_cls(data[attr_name]) | |
| 79 | - | ||
| 80 | - # if the type if FileAttribute we need to pass the data as file | ||
| 81 | - if transform_files and isinstance(type_obj, types.FileAttribute): | ||
| 82 | - key = type_obj.get_file_name(attr_name) | ||
| 83 | - files[attr_name] = (key, data.pop(attr_name)) | ||
| 84 | - else: | ||
| 85 | - data[attr_name] = type_obj.get_for_api() | ||
| 79 | + assert isinstance(type_obj, types.GitlabAttribute) | ||
| 80 | + | ||
| 81 | + if isinstance(type_obj, types.GitlabAttribute): | ||
| 82 | + # if the type is FileAttribute we need to pass the data as file | ||
| 83 | + if isinstance(type_obj, types.FileAttribute): | ||
| 84 | + if transform_files: | ||
| 85 | + key = type_obj.get_file_name(attr_name) | ||
| 86 | + files[attr_name] = (key, data.pop(attr_name)) | ||
| 87 | + continue | ||
| 88 | + key, value = type_obj.get_for_api(key=attr_name) | ||
| 89 | + if key != attr_name: | ||
| 90 | + del data[attr_name] | ||
| 91 | + data[key] = value | ||
| 86 | 92 | ||
| 87 | 93 | return data, files | |
| 88 | 94 | ||
@@ -94,6 +100,8 @@ def copy_dict( | |||
| 94 | 100 | ) -> None: | |
| 95 | 101 | for k, v in src.items(): | |
| 96 | 102 | if isinstance(v, dict): | |
| 103 | + # NOTE(jlvillal): This provides some support for the `hash` type | ||
| 104 | + # https://docs.gitlab.com/ee/api/#hash | ||
| 97 | 105 | # Transform dict values to new attributes. For example: | |
| 98 | 106 | # custom_attributes: {'foo', 'bar'} => | |
| 99 | 107 | # "custom_attributes['foo']": "bar" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -99,6 +99,11 @@ def test_groups(gl): | |||
| 99 | 99 | assert len(group1.members.list()) == 3 | |
| 100 | 100 | assert len(group2.members.list()) == 2 | |
| 101 | 101 | ||
| 102 | + # Test `user_ids` array | ||
| 103 | + result = group1.members.list(user_ids=[user.id, 99999]) | ||
| 104 | + assert len(result) == 1 | ||
| 105 | + assert result[0].id == user.id | ||
| 106 | + | ||
| 102 | 107 | group1.members.delete(user.id) | |
| 103 | 108 | assert user not in group1.members.list() | |
| 104 | 109 | assert group1.members_all.list() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -205,6 +205,25 @@ class M(ListMixin, FakeManager): | |||
| 205 | 205 | assert responses.assert_call_count(url, 2) is True | |
| 206 | 206 | ||
| 207 | 207 | ||
| 208 | + @responses.activate | ||
| 209 | + def test_list_mixin_with_attributes(gl): | ||
| 210 | + class M(ListMixin, FakeManager): | ||
| 211 | + _types = {"my_array": gl_types.ArrayAttribute} | ||
| 212 | + | ||
| 213 | + url = "http://localhost/api/v4/tests?my_array%5B%5D=1&my_array%5B%5D=2&my_array%5B%5D=3" | ||
| 214 | + responses.add( | ||
| 215 | + method=responses.GET, | ||
| 216 | + headers={}, | ||
| 217 | + url=url, | ||
| 218 | + json=[], | ||
| 219 | + status=200, | ||
| 220 | + match=[responses.matchers.query_param_matcher({"my_array[]": ["1", "2", "3"]})], | ||
| 221 | + ) | ||
| 222 | + | ||
| 223 | + mgr = M(gl) | ||
| 224 | + mgr.list(iterator=True, my_array=[1, 2, 3]) | ||
| 225 | + | ||
| 226 | + | ||
| 208 | 227 | @responses.activate | |
| 209 | 228 | def test_list_other_url(gl): | |
| 210 | 229 | class M(ListMixin, FakeManager): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -73,7 +73,7 @@ def test_gitlab_attribute_get(): | |||
| 73 | 73 | ||
| 74 | 74 | o.set_from_cli("whatever2") | |
| 75 | 75 | assert o.get() == "whatever2" | |
| 76 | - assert o.get_for_api() == "whatever2" | ||
| 76 | + assert o.get_for_api(key="spam") == ("spam", "whatever2") | ||
| 77 | 77 | ||
| 78 | 78 | o = types.GitlabAttribute() | |
| 79 | 79 | assert o._value is None | |
@@ -100,42 +100,42 @@ def test_array_attribute_empty_input(): | |||
| 100 | 100 | def test_array_attribute_get_for_api_from_cli(): | |
| 101 | 101 | o = types.ArrayAttribute() | |
| 102 | 102 | o.set_from_cli("foo,bar,baz") | |
| 103 | - assert o.get_for_api() == "foo,bar,baz" | ||
| 103 | + assert o.get_for_api(key="spam") == ("spam[]", ["foo", "bar", "baz"]) | ||
| 104 | 104 | ||
| 105 | 105 | ||
| 106 | 106 | def test_array_attribute_get_for_api_from_list(): | |
| 107 | 107 | o = types.ArrayAttribute(["foo", "bar", "baz"]) | |
| 108 | - assert o.get_for_api() == "foo,bar,baz" | ||
| 108 | + assert o.get_for_api(key="spam") == ("spam[]", ["foo", "bar", "baz"]) | ||
| 109 | 109 | ||
| 110 | 110 | ||
| 111 | 111 | def test_array_attribute_get_for_api_from_int_list(): | |
| 112 | 112 | o = types.ArrayAttribute([1, 9, 7]) | |
| 113 | - assert o.get_for_api() == "1,9,7" | ||
| 113 | + assert o.get_for_api(key="spam") == ("spam[]", [1, 9, 7]) | ||
| 114 | 114 | ||
| 115 | 115 | ||
| 116 | 116 | def test_array_attribute_does_not_split_string(): | |
| 117 | 117 | o = types.ArrayAttribute("foo") | |
| 118 | - assert o.get_for_api() == "foo" | ||
| 118 | + assert o.get_for_api(key="spam") == ("spam[]", "foo") | ||
| 119 | 119 | ||
| 120 | 120 | ||
| 121 | 121 | # CommaSeparatedListAttribute tests | |
| 122 | 122 | def test_csv_string_attribute_get_for_api_from_cli(): | |
| 123 | 123 | o = types.CommaSeparatedListAttribute() | |
| 124 | 124 | o.set_from_cli("foo,bar,baz") | |
| 125 | - assert o.get_for_api() == "foo,bar,baz" | ||
| 125 | + assert o.get_for_api(key="spam") == ("spam", "foo,bar,baz") | ||
| 126 | 126 | ||
| 127 | 127 | ||
| 128 | 128 | def test_csv_string_attribute_get_for_api_from_list(): | |
| 129 | 129 | o = types.CommaSeparatedListAttribute(["foo", "bar", "baz"]) | |
| 130 | - assert o.get_for_api() == "foo,bar,baz" | ||
| 130 | + assert o.get_for_api(key="spam") == ("spam", "foo,bar,baz") | ||
| 131 | 131 | ||
| 132 | 132 | ||
| 133 | 133 | def test_csv_string_attribute_get_for_api_from_int_list(): | |
| 134 | 134 | o = types.CommaSeparatedListAttribute([1, 9, 7]) | |
| 135 | - assert o.get_for_api() == "1,9,7" | ||
| 135 | + assert o.get_for_api(key="spam") == ("spam", "1,9,7") | ||
| 136 | 136 | ||
| 137 | 137 | ||
| 138 | 138 | # LowercaseStringAttribute tests | |
| 139 | 139 | def test_lowercase_string_attribute_get_for_api(): | |
| 140 | 140 | o = types.LowercaseStringAttribute("FOO") | |
| 141 | - assert o.get_for_api() == "foo" | ||
| 141 | + assert o.get_for_api(key="spam") == ("spam", "foo") | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments