unknowntpo commented on code in PR #8459: URL: https://github.com/apache/gravitino/pull/8459#discussion_r2338297187
########## clients/client-python/tests/unittests/dto/rel/test_sort_order_serdes.py: ########## @@ -0,0 +1,102 @@ +# 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 dataclasses import dataclass, field +from typing import cast + +from dataclasses_json import DataClassJsonMixin, config + +from gravitino.api.expressions.sorts.null_ordering import NullOrdering +from gravitino.api.expressions.sorts.sort_direction import SortDirection +from gravitino.dto.rel.expressions.field_reference_dto import FieldReferenceDTO +from gravitino.dto.rel.json_serdes.sort_order_serdes import SortOrderSerdes +from gravitino.dto.rel.sort_order_dto import SortOrderDTO +from gravitino.exceptions.base import IllegalArgumentException + + +@dataclass +class MockDataClass(DataClassJsonMixin): + sort_orders: list[SortOrderDTO] = field( + metadata=config( + field_name="sortOrders", + encoder=lambda data: [SortOrderSerdes.serialize(item) for item in data], + decoder=lambda data: [SortOrderSerdes.deserialize(item) for item in data], + ) + ) + + +class TestSortOrderSerdes(unittest.TestCase): + def test_sort_order_serdes_invalid_json(self): + json_strings = [ + '{"sortOrders": [{}]}', + '{"sortOrders": [""]}', + '{"sortOrders": [null]}', + ] + + with self.assertRaisesRegex( + IllegalArgumentException, "Cannot parse sort order from invalid JSON" + ): + for json_string in json_strings: + MockDataClass.from_json(json_string) + + def test_sort_order_serdes_missing_sort_term(self): + json_string = '{"sortOrders": [{"direction": "asc"}]}' + + with self.assertRaisesRegex( + IllegalArgumentException, "Cannot parse sort order from missing sort term" + ): + MockDataClass.from_json(json_string) + + def test_sort_order_serdes_invalid_sort_term(self): Review Comment: ok -- 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: commits-unsubscr...@gravitino.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org