- Notifications
You must be signed in to change notification settings - Fork674
feat: addasdict() andto_json() methods to Gitlab Objects#1872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
32 changes: 32 additions & 0 deletionsdocs/api-usage.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
32 changes: 19 additions & 13 deletionsgitlab/base.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletionstests/unit/test_base.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -36,6 +36,16 @@ class FakeManager(base.RESTManager): | ||
| _path = "/tests" | ||
| class FakeParent: | ||
| id = 42 | ||
| class FakeManagerWithParent(base.RESTManager): | ||
| _path = "/tests/{test_id}/cases" | ||
| _obj_cls = FakeObject | ||
| _from_parent_attrs = {"test_id": "id"} | ||
| @pytest.fixture | ||
| def fake_gitlab(): | ||
| return FakeGitlab() | ||
| @@ -46,6 +56,21 @@ def fake_manager(fake_gitlab): | ||
| return FakeManager(fake_gitlab) | ||
| @pytest.fixture | ||
| def fake_manager_with_parent(fake_gitlab): | ||
| return FakeManagerWithParent(fake_gitlab, parent=FakeParent) | ||
| @pytest.fixture | ||
| def fake_object(fake_manager): | ||
| return FakeObject(fake_manager, {"attr1": "foo", "alist": [1, 2, 3]}) | ||
| @pytest.fixture | ||
| def fake_object_with_parent(fake_manager_with_parent): | ||
| return FakeObject(fake_manager_with_parent, {"attr1": "foo", "alist": [1, 2, 3]}) | ||
| class TestRESTManager: | ||
| def test_computed_path_simple(self): | ||
| class MGR(base.RESTManager): | ||
| @@ -306,3 +331,75 @@ def test_repr(self, fake_manager): | ||
| FakeObject._id_attr = None | ||
| assert repr(obj) == "<FakeObject>" | ||
| def test_attributes_get(self, fake_object): | ||
| assert fake_object.attr1 == "foo" | ||
| result = fake_object.attributes | ||
| assert result == {"attr1": "foo", "alist": [1, 2, 3]} | ||
| def test_attributes_shows_updates(self, fake_object): | ||
| # Updated attribute value is reflected in `attributes` | ||
| fake_object.attr1 = "hello" | ||
| assert fake_object.attributes == {"attr1": "hello", "alist": [1, 2, 3]} | ||
| assert fake_object.attr1 == "hello" | ||
| # New attribute is in `attributes` | ||
| fake_object.new_attrib = "spam" | ||
| assert fake_object.attributes == { | ||
| "attr1": "hello", | ||
| "new_attrib": "spam", | ||
| "alist": [1, 2, 3], | ||
| } | ||
| def test_attributes_is_copy(self, fake_object): | ||
| # Modifying the dictionary does not cause modifications to the object | ||
| result = fake_object.attributes | ||
| result["alist"].append(10) | ||
| assert result == {"attr1": "foo", "alist": [1, 2, 3, 10]} | ||
| assert fake_object.attributes == {"attr1": "foo", "alist": [1, 2, 3]} | ||
| def test_attributes_has_parent_attrs(self, fake_object_with_parent): | ||
| assert fake_object_with_parent.attr1 == "foo" | ||
| result = fake_object_with_parent.attributes | ||
| assert result == {"attr1": "foo", "alist": [1, 2, 3], "test_id": "42"} | ||
| def test_asdict(self, fake_object): | ||
| assert fake_object.attr1 == "foo" | ||
| result = fake_object.asdict() | ||
| assert result == {"attr1": "foo", "alist": [1, 2, 3]} | ||
| def test_asdict_no_parent_attrs(self, fake_object_with_parent): | ||
| assert fake_object_with_parent.attr1 == "foo" | ||
| result = fake_object_with_parent.asdict() | ||
| assert result == {"attr1": "foo", "alist": [1, 2, 3]} | ||
| assert "test_id" not in fake_object_with_parent.asdict() | ||
| assert "test_id" not in fake_object_with_parent.asdict(with_parent_attrs=False) | ||
| assert "test_id" in fake_object_with_parent.asdict(with_parent_attrs=True) | ||
| def test_asdict_modify_dict_does_not_change_object(self, fake_object): | ||
| result = fake_object.asdict() | ||
| # Demonstrate modifying the dictionary does not modify the object | ||
JohnVillalovos marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| result["attr1"] = "testing" | ||
| result["alist"].append(4) | ||
| assert result == {"attr1": "testing", "alist": [1, 2, 3, 4]} | ||
| assert fake_object.attr1 == "foo" | ||
| assert fake_object.alist == [1, 2, 3] | ||
| def test_asdict_modify_dict_does_not_change_object2(self, fake_object): | ||
| # Modify attribute and then ensure modifying a list in the returned dict won't | ||
| # modify the list in the object. | ||
| fake_object.attr1 = [9, 7, 8] | ||
| assert fake_object.asdict() == { | ||
| "attr1": [9, 7, 8], | ||
| "alist": [1, 2, 3], | ||
| } | ||
| result = fake_object.asdict() | ||
| result["attr1"].append(1) | ||
| assert fake_object.asdict() == { | ||
| "attr1": [9, 7, 8], | ||
| "alist": [1, 2, 3], | ||
| } | ||
| def test_asdict_modify_object(self, fake_object): | ||
| # asdict() returns the updated value | ||
| fake_object.attr1 = "spam" | ||
| assert fake_object.asdict() == {"attr1": "spam", "alist": [1, 2, 3]} | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.