Copilot commented on code in PR #11021: URL: https://github.com/apache/gravitino/pull/11021#discussion_r3216710433
########## clients/client-python/tests/unittests/dto/requests/test_statistics_update_request.py: ########## @@ -0,0 +1,89 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +import json +import unittest + +from gravitino.api.stats.json_serdes.statistic_value_serdes import StatisticValueSerdes +from gravitino.api.stats.statistic_values import StatisticValues +from gravitino.dto.requests.statistics_update_request import StatisticsUpdateRequest +from gravitino.exceptions.base import IllegalArgumentException + + +class TestStatisticsUpdateRequest(unittest.TestCase): + @classmethod + def setUpClass(cls) -> None: + cls.test_updates = { + "custom-long-statistic": StatisticValues.long_value(100), + "custom-str-statistic": StatisticValues.string_value("value"), + "custom-list-statistic": StatisticValues.list_value( + [StatisticValues.double_value(1.5), StatisticValues.double_value(2.5)] + ), + "custom-object-statistic": StatisticValues.object_value( + { + "key1": StatisticValues.boolean_value(True), + "key2": StatisticValues.long_value(42), + } + ), + } + + def test_validate_success(self): + request = StatisticsUpdateRequest(_updates=self.test_updates) + self.assertDictEqual(request.updates, self.test_updates) + request.validate() + + def test_validate_failure_empty_name(self): + updates = {"": StatisticValues.long_value(100)} + request = StatisticsUpdateRequest(_updates=updates) + with self.assertRaisesRegex( + IllegalArgumentException, 'statistic "name" must not be null or empty' + ): + request.validate() + + def test_validate_failure_null_value(self): + updates = {"custom-null-value": None} + request = StatisticsUpdateRequest(_updates=updates) + with self.assertRaisesRegex( + IllegalArgumentException, + "statistic \"value\" for 'custom-null-value' must not be null", + ): + request.validate() + Review Comment: Unit tests for `StatisticsUpdateRequest.validate()` cover empty statistic names and null values, but there is no test asserting behavior when `_updates` itself is `None` or an empty dict. Once validation is tightened to reject null/empty updates, please add tests for these cases to prevent regressions. ########## clients/client-python/gravitino/dto/requests/statistics_update_request.py: ########## @@ -0,0 +1,60 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +from dataclasses import dataclass, field +from typing import Any + +from dataclasses_json import config + +from gravitino.api.stats.json_serdes.statistic_value_serdes import StatisticValueSerdes +from gravitino.api.stats.statistic_value import StatisticValue +from gravitino.rest.rest_message import RESTRequest +from gravitino.utils.precondition import Precondition + + +@dataclass +class StatisticsUpdateRequest(RESTRequest): + """Represents a request to update statistics.""" + + _updates: dict[str, StatisticValue[Any]] = field( + metadata=config( + field_name="updates", + encoder=lambda mapping: { + key: StatisticValueSerdes.serialize(value) + for key, value in mapping.items() + }, + decoder=lambda mapping: { + key: StatisticValueSerdes.deserialize(value) + for key, value in mapping.items() + }, + ) + ) + + @property + def updates(self) -> dict[str, StatisticValue[Any]]: + return self._updates + + def validate(self) -> None: + for name, value in self._updates.items(): + Precondition.check_string_not_empty( + name, 'statistic "name" must not be null or empty' + ) + Precondition.check_argument( + value is not None, f"statistic \"value\" for '{name}' must not be null" + ) Review Comment: `StatisticsUpdateRequest.validate()` iterates over `self._updates.items()` without first validating that `_updates` is non-null (and optionally non-empty). If a caller constructs the request with `_updates=None` (or deserialization yields `None`), this will raise `AttributeError` instead of a consistent `IllegalArgumentException` via `Precondition`. Add an explicit `Precondition.check_argument(self._updates is not None and len(self._updates) > 0, ...)` (or at least non-null) before the loop. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
