diff options
author | John L. Villalovos <john@sodarock.com> | 2022-07-27 16:08:25 -0700 |
---|---|---|
committer | John L. Villalovos <john@sodarock.com> | 2022-07-27 16:08:25 -0700 |
commit | 1af44ce8761e6ee8a9467a3e192f6c4d19e5cefe (patch) | |
tree | da3116aa356818cb2a865b047de9ef160bbfffa7 /tests | |
parent | 194ee0100c2868c1a9afb161c15f3145efb01c7c (diff) | |
download | gitlab-1af44ce8761e6ee8a9467a3e192f6c4d19e5cefe.tar.gz |
fix: use the [] after key names for array variables in `params`
1. If a value is of type ArrayAttribute then append '[]' to the name
of the value for query parameters (`params`).
This is step 3 in a series of steps of our goal to add full
support for the GitLab API data types[1]:
* array
* hash
* array of hashes
Step one was: commit 5127b1594c00c7364e9af15e42d2e2f2d909449b
Step two was: commit a57334f1930752c70ea15847a39324fa94042460
Fixes: #1698
[1] https://docs.gitlab.com/ee/api/#encoding-api-parameters-of-array-and-hash-types
Diffstat (limited to 'tests')
-rw-r--r-- | tests/functional/api/test_groups.py | 5 | ||||
-rw-r--r-- | tests/unit/mixins/test_mixin_methods.py | 38 | ||||
-rw-r--r-- | tests/unit/test_types.py | 18 | ||||
-rw-r--r-- | tests/unit/test_utils.py | 28 |
4 files changed, 77 insertions, 12 deletions
diff --git a/tests/functional/api/test_groups.py b/tests/functional/api/test_groups.py index 88e5a36..7688273 100644 --- a/tests/functional/api/test_groups.py +++ b/tests/functional/api/test_groups.py @@ -99,6 +99,11 @@ def test_groups(gl): assert len(group1.members.list()) == 3 assert len(group2.members.list()) == 2 + # Test `user_ids` array + result = group1.members.list(user_ids=[user.id, 99999]) + assert len(result) == 1 + assert result[0].id == user.id + group1.members.delete(user.id) assert user not in group1.members.list() assert group1.members_all.list() diff --git a/tests/unit/mixins/test_mixin_methods.py b/tests/unit/mixins/test_mixin_methods.py index 68b59a2..004d190 100644 --- a/tests/unit/mixins/test_mixin_methods.py +++ b/tests/unit/mixins/test_mixin_methods.py @@ -206,6 +206,25 @@ def test_list_mixin(gl): @responses.activate +def test_list_mixin_with_attributes(gl): + class M(ListMixin, FakeManager): + _types = {"my_array": gl_types.ArrayAttribute} + + url = "http://localhost/api/v4/tests" + responses.add( + method=responses.GET, + headers={}, + url=url, + json=[], + status=200, + match=[responses.matchers.query_param_matcher({"my_array[]": ["1", "2", "3"]})], + ) + + mgr = M(gl) + mgr.list(iterator=True, my_array=[1, 2, 3]) + + +@responses.activate def test_list_other_url(gl): class M(ListMixin, FakeManager): pass @@ -295,6 +314,25 @@ def test_create_mixin_custom_path(gl): assert responses.assert_call_count(url, 1) is True +@responses.activate +def test_create_mixin_with_attributes(gl): + class M(CreateMixin, FakeManager): + _types = {"my_array": gl_types.ArrayAttribute} + + url = "http://localhost/api/v4/tests" + responses.add( + method=responses.POST, + headers={}, + url=url, + json={}, + status=200, + match=[responses.matchers.json_params_matcher({"my_array": [1, 2, 3]})], + ) + + mgr = M(gl) + mgr.create({"my_array": [1, 2, 3]}) + + def test_update_mixin_missing_attrs(gl): class M(UpdateMixin, FakeManager): _update_attrs = gl_types.RequiredOptional( diff --git a/tests/unit/test_types.py b/tests/unit/test_types.py index c06e9d0..4f18c51 100644 --- a/tests/unit/test_types.py +++ b/tests/unit/test_types.py @@ -73,7 +73,7 @@ def test_gitlab_attribute_get(): o.set_from_cli("whatever2") assert o.get() == "whatever2" - assert o.get_for_api() == "whatever2" + assert o.get_for_api(key="spam") == ("spam", "whatever2") o = types.GitlabAttribute() assert o._value is None @@ -100,42 +100,42 @@ def test_array_attribute_empty_input(): def test_array_attribute_get_for_api_from_cli(): o = types.ArrayAttribute() o.set_from_cli("foo,bar,baz") - assert o.get_for_api() == "foo,bar,baz" + assert o.get_for_api(key="spam") == ("spam[]", ["foo", "bar", "baz"]) def test_array_attribute_get_for_api_from_list(): o = types.ArrayAttribute(["foo", "bar", "baz"]) - assert o.get_for_api() == "foo,bar,baz" + assert o.get_for_api(key="spam") == ("spam[]", ["foo", "bar", "baz"]) def test_array_attribute_get_for_api_from_int_list(): o = types.ArrayAttribute([1, 9, 7]) - assert o.get_for_api() == "1,9,7" + assert o.get_for_api(key="spam") == ("spam[]", [1, 9, 7]) def test_array_attribute_does_not_split_string(): o = types.ArrayAttribute("foo") - assert o.get_for_api() == "foo" + assert o.get_for_api(key="spam") == ("spam[]", "foo") # CommaSeparatedListAttribute tests def test_csv_string_attribute_get_for_api_from_cli(): o = types.CommaSeparatedListAttribute() o.set_from_cli("foo,bar,baz") - assert o.get_for_api() == "foo,bar,baz" + assert o.get_for_api(key="spam") == ("spam", "foo,bar,baz") def test_csv_string_attribute_get_for_api_from_list(): o = types.CommaSeparatedListAttribute(["foo", "bar", "baz"]) - assert o.get_for_api() == "foo,bar,baz" + assert o.get_for_api(key="spam") == ("spam", "foo,bar,baz") def test_csv_string_attribute_get_for_api_from_int_list(): o = types.CommaSeparatedListAttribute([1, 9, 7]) - assert o.get_for_api() == "1,9,7" + assert o.get_for_api(key="spam") == ("spam", "1,9,7") # LowercaseStringAttribute tests def test_lowercase_string_attribute_get_for_api(): o = types.LowercaseStringAttribute("FOO") - assert o.get_for_api() == "foo" + assert o.get_for_api(key="spam") == ("spam", "foo") diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index ce2e776..6038d84 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -155,7 +155,7 @@ def test_remove_none_from_dict(dictionary, expected): def test_transform_types_copies_data_with_empty_files(): data = {"attr": "spam"} - new_data, files = utils._transform_types(data, {}) + new_data, files = utils._transform_types(data, {}, transform_data=True) assert new_data is not data assert new_data == data @@ -165,7 +165,7 @@ def test_transform_types_copies_data_with_empty_files(): def test_transform_types_with_transform_files_populates_files(): custom_types = {"attr": types.FileAttribute} data = {"attr": "spam"} - new_data, files = utils._transform_types(data, custom_types) + new_data, files = utils._transform_types(data, custom_types, transform_data=True) assert new_data == {} assert files["attr"] == ("attr", "spam") @@ -174,7 +174,29 @@ def test_transform_types_with_transform_files_populates_files(): def test_transform_types_without_transform_files_populates_data_with_empty_files(): custom_types = {"attr": types.FileAttribute} data = {"attr": "spam"} - new_data, files = utils._transform_types(data, custom_types, transform_files=False) + new_data, files = utils._transform_types( + data, custom_types, transform_files=False, transform_data=True + ) assert new_data == {"attr": "spam"} assert files == {} + + +def test_transform_types_params_array(): + data = {"attr": [1, 2, 3]} + custom_types = {"attr": types.ArrayAttribute} + new_data, files = utils._transform_types(data, custom_types, transform_data=True) + + assert new_data is not data + assert new_data == {"attr[]": [1, 2, 3]} + assert files == {} + + +def test_transform_types_not_params_array(): + data = {"attr": [1, 2, 3]} + custom_types = {"attr": types.ArrayAttribute} + new_data, files = utils._transform_types(data, custom_types, transform_data=False) + + assert new_data is not data + assert new_data == data + assert files == {} |