unknowntpo commented on code in PR #8363: URL: https://github.com/apache/gravitino/pull/8363#discussion_r2318140819
########## clients/client-python/tests/unittests/dto/rel/test_partitioning_serdes.py: ########## @@ -0,0 +1,569 @@ +# 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 enum import Enum +from unittest.mock import patch + +from gravitino.api.types.types import Types +from gravitino.dto.rel.expressions.field_reference_dto import FieldReferenceDTO +from gravitino.dto.rel.expressions.literal_dto import LiteralDTO +from gravitino.dto.rel.partitioning.day_partitioning_dto import DayPartitioningDTO +from gravitino.dto.rel.partitioning.function_partitioning_dto import ( + FunctionPartitioningDTO, +) +from gravitino.dto.rel.partitioning.hour_partitioning_dto import HourPartitioningDTO +from gravitino.dto.rel.partitioning.identity_partitioning_dto import ( + IdentityPartitioningDTO, +) +from gravitino.dto.rel.partitioning.json_serdes.partitioning_serdes import ( + PartitioningSerdes, +) +from gravitino.dto.rel.partitioning.month_partitioning_dto import MonthPartitioningDTO +from gravitino.dto.rel.partitioning.partitioning import Partitioning +from gravitino.dto.rel.partitioning.year_partitioning_dto import YearPartitioningDTO +from gravitino.exceptions.base import IllegalArgumentException + + +class MockPartitionStrategy(str, Enum): + INVALID_STRATEGY = "invalid_partitioning_strategy" + + +class TestPartitioningSerdes(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.field_name = [f"dummy_field_{i}" for i in range(1)] + cls.single_field_partitioning_dtos = { + Partitioning.Strategy.IDENTITY: IdentityPartitioningDTO(*cls.field_name), + Partitioning.Strategy.YEAR: YearPartitioningDTO(*cls.field_name), + Partitioning.Strategy.MONTH: MonthPartitioningDTO(*cls.field_name), + Partitioning.Strategy.DAY: DayPartitioningDTO(*cls.field_name), + Partitioning.Strategy.HOUR: HourPartitioningDTO(*cls.field_name), + } + + def test_serialize_invalid_strategy(self): + mock_dto = IdentityPartitioningDTO(*self.field_name) + with patch.object( + mock_dto, "strategy", return_value=MockPartitionStrategy.INVALID_STRATEGY + ): + self.assertRaisesRegex( + IOError, + "Unknown partitioning strategy", + PartitioningSerdes.serialize, + mock_dto, + ) + + def test_deserialize_invalid_json(self): + invalid_partitioning_data = (None, "invalid_data", {}) Review Comment: can we make `invalid_partitioning_data` a JSON string, and use json.load to load it ? Just like what `test_deserialize_identity_partitioning_dto` did. ########## clients/client-python/tests/unittests/dto/rel/test_partitioning_serdes.py: ########## @@ -0,0 +1,569 @@ +# 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 enum import Enum +from unittest.mock import patch + +from gravitino.api.types.types import Types +from gravitino.dto.rel.expressions.field_reference_dto import FieldReferenceDTO +from gravitino.dto.rel.expressions.literal_dto import LiteralDTO +from gravitino.dto.rel.partitioning.day_partitioning_dto import DayPartitioningDTO +from gravitino.dto.rel.partitioning.function_partitioning_dto import ( + FunctionPartitioningDTO, +) +from gravitino.dto.rel.partitioning.hour_partitioning_dto import HourPartitioningDTO +from gravitino.dto.rel.partitioning.identity_partitioning_dto import ( + IdentityPartitioningDTO, +) +from gravitino.dto.rel.partitioning.json_serdes.partitioning_serdes import ( + PartitioningSerdes, +) +from gravitino.dto.rel.partitioning.month_partitioning_dto import MonthPartitioningDTO +from gravitino.dto.rel.partitioning.partitioning import Partitioning +from gravitino.dto.rel.partitioning.year_partitioning_dto import YearPartitioningDTO +from gravitino.exceptions.base import IllegalArgumentException + + +class MockPartitionStrategy(str, Enum): + INVALID_STRATEGY = "invalid_partitioning_strategy" + + +class TestPartitioningSerdes(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.field_name = [f"dummy_field_{i}" for i in range(1)] + cls.single_field_partitioning_dtos = { + Partitioning.Strategy.IDENTITY: IdentityPartitioningDTO(*cls.field_name), + Partitioning.Strategy.YEAR: YearPartitioningDTO(*cls.field_name), + Partitioning.Strategy.MONTH: MonthPartitioningDTO(*cls.field_name), + Partitioning.Strategy.DAY: DayPartitioningDTO(*cls.field_name), + Partitioning.Strategy.HOUR: HourPartitioningDTO(*cls.field_name), + } + + def test_serialize_invalid_strategy(self): + mock_dto = IdentityPartitioningDTO(*self.field_name) + with patch.object( + mock_dto, "strategy", return_value=MockPartitionStrategy.INVALID_STRATEGY + ): + self.assertRaisesRegex( + IOError, + "Unknown partitioning strategy", + PartitioningSerdes.serialize, + mock_dto, + ) + + def test_deserialize_invalid_json(self): + invalid_partitioning_data = (None, "invalid_data", {}) + + for invalid_data in invalid_partitioning_data: + with self.assertRaisesRegex( + IllegalArgumentException, "Cannot parse partitioning from invalid JSON" + ): + PartitioningSerdes.deserialize(invalid_data) + + def test_deserialize_invalid_strategy(self): + """Tests missing strategy and unknown partitioning strategy.""" + + invalid_data_base = { + PartitioningSerdes.FIELD_NAME: self.field_name, + } + invalid_strategy_data = { + PartitioningSerdes.STRATEGY: "invalid_strategy", + **invalid_data_base, + } + + with self.assertRaisesRegex( + IllegalArgumentException, + "Cannot parse partitioning from missing strategy", + ): + PartitioningSerdes.deserialize(invalid_data_base) + + with self.assertRaisesRegex( + IOError, + "Unknown partitioning strategy", + ): + PartitioningSerdes.deserialize(invalid_strategy_data) + + def test_serialize_single_field_partitioning_dto(self): + for partitioning_dto in self.single_field_partitioning_dtos.values(): + serialized = PartitioningSerdes.serialize(partitioning_dto) Review Comment: dtto. ########## clients/client-python/tests/unittests/dto/rel/test_partitioning_serdes.py: ########## @@ -0,0 +1,569 @@ +# 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 enum import Enum +from unittest.mock import patch + +from gravitino.api.types.types import Types +from gravitino.dto.rel.expressions.field_reference_dto import FieldReferenceDTO +from gravitino.dto.rel.expressions.literal_dto import LiteralDTO +from gravitino.dto.rel.partitioning.day_partitioning_dto import DayPartitioningDTO +from gravitino.dto.rel.partitioning.function_partitioning_dto import ( + FunctionPartitioningDTO, +) +from gravitino.dto.rel.partitioning.hour_partitioning_dto import HourPartitioningDTO +from gravitino.dto.rel.partitioning.identity_partitioning_dto import ( + IdentityPartitioningDTO, +) +from gravitino.dto.rel.partitioning.json_serdes.partitioning_serdes import ( + PartitioningSerdes, +) +from gravitino.dto.rel.partitioning.month_partitioning_dto import MonthPartitioningDTO +from gravitino.dto.rel.partitioning.partitioning import Partitioning +from gravitino.dto.rel.partitioning.year_partitioning_dto import YearPartitioningDTO +from gravitino.exceptions.base import IllegalArgumentException + + +class MockPartitionStrategy(str, Enum): + INVALID_STRATEGY = "invalid_partitioning_strategy" + + +class TestPartitioningSerdes(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.field_name = [f"dummy_field_{i}" for i in range(1)] + cls.single_field_partitioning_dtos = { + Partitioning.Strategy.IDENTITY: IdentityPartitioningDTO(*cls.field_name), + Partitioning.Strategy.YEAR: YearPartitioningDTO(*cls.field_name), + Partitioning.Strategy.MONTH: MonthPartitioningDTO(*cls.field_name), + Partitioning.Strategy.DAY: DayPartitioningDTO(*cls.field_name), + Partitioning.Strategy.HOUR: HourPartitioningDTO(*cls.field_name), + } + + def test_serialize_invalid_strategy(self): + mock_dto = IdentityPartitioningDTO(*self.field_name) + with patch.object( + mock_dto, "strategy", return_value=MockPartitionStrategy.INVALID_STRATEGY + ): + self.assertRaisesRegex( + IOError, + "Unknown partitioning strategy", + PartitioningSerdes.serialize, + mock_dto, + ) + + def test_deserialize_invalid_json(self): + invalid_partitioning_data = (None, "invalid_data", {}) + + for invalid_data in invalid_partitioning_data: + with self.assertRaisesRegex( + IllegalArgumentException, "Cannot parse partitioning from invalid JSON" + ): + PartitioningSerdes.deserialize(invalid_data) + + def test_deserialize_invalid_strategy(self): + """Tests missing strategy and unknown partitioning strategy.""" + + invalid_data_base = { Review Comment: ditto -- 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]
