From 6d49b895584b027496af92d12aff963dec8f8b69 Mon Sep 17 00:00:00 2001 From: Ritik Raj <76646497+ritikraj26@users.noreply.github.com> Date: Fri, 28 Jun 2024 14:47:11 +0530 Subject: [PATCH] [GSoC2024] - add/rest-api-tests-for-attribute-rename (#7754) Added rest-api tests for attribute rename feature. These rest-api tests are to validate the changes introduced in the PR [here](https://github.com/cvat-ai/cvat/pull/7670) --------- Co-authored-by: Maria Khrustaleva --- tests/python/rest_api/test_labels.py | 53 ++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/tests/python/rest_api/test_labels.py b/tests/python/rest_api/test_labels.py index 023d833c78dc..dfcae7a8635e 100644 --- a/tests/python/rest_api/test_labels.py +++ b/tests/python/rest_api/test_labels.py @@ -593,12 +593,25 @@ def _get_patch_data( if overrides: payload = deepcopy(overrides) - if overrides.get("attributes"): - payload["attributes"] = (original_data.get("attributes") or []) + overrides[ - "attributes" - ] - result["attributes"] = deepcopy(payload["attributes"]) - ignore_fields.append("attributes.id") + if overridden_attributes := deepcopy(overrides.get("attributes", [])): + combined_attributes = deepcopy(original_data.get("attributes", [])) + + mapping = {attr["id"]: attr for attr in overridden_attributes if "id" in attr} + + # no attributes to update + if not mapping: + ignore_fields.append("attributes.id") + + for attr in combined_attributes: + if attr["id"] in mapping: + attr.update(mapping[attr["id"]]) + + for attr in overridden_attributes: + if attr not in combined_attributes: + combined_attributes.append(attr) + + payload["attributes"] = deepcopy(combined_attributes) + result["attributes"] = deepcopy(combined_attributes) # Changing skeletons is not supported if overrides.get("type") == "skeleton": @@ -663,6 +676,34 @@ def test_can_patch_label_field(self, source, admin_user, param, newvalue): user, label["id"], patch_data, expected_data=expected_data, ignore_fields=ignore_fields ) + @parametrize("source", _TestLabelsPermissionsBase.source_types) + def test_can_patch_attribute_name(self, source: str, admin_user: str): + source_key = self._get_source_info(source).label_source_key + label = next( + ( + l + for l in self.labels + if l.get(source_key) and not l["has_parent"] and l.get("attributes") + ) + ) + + attributes = deepcopy(label["attributes"]) + + for attribute in attributes: + attribute["name"] += "_updated" + + expected_data, patch_data, ignore_fields = self._get_patch_data( + label, attributes=attributes + ) + + self._test_update_ok( + admin_user, + label["id"], + patch_data, + expected_data=expected_data, + ignore_fields=ignore_fields, + ) + @parametrize("source", _TestLabelsPermissionsBase.source_types) def test_cannot_patch_sublabel_directly(self, admin_user, source): user = admin_user