|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +import unittest |
| 18 | +from unittest.mock import MagicMock |
| 19 | + |
| 20 | +from gravitino.api.expressions.function_expression import FunctionExpression |
| 21 | +from gravitino.api.expressions.named_reference import NamedReference |
| 22 | +from gravitino.api.expressions.sorts.sort_direction import SortDirection |
| 23 | +from gravitino.api.expressions.sorts.null_ordering import NullOrdering |
| 24 | +from gravitino.api.expressions.sorts.sort_orders import SortImpl, SortOrders |
| 25 | +from gravitino.api.expressions.expression import Expression |
| 26 | + |
| 27 | + |
| 28 | +class TestSortOrder(unittest.TestCase): |
| 29 | + def test_sort_direction_from_string(self): |
| 30 | + self.assertEqual(SortDirection.from_string("asc"), SortDirection.ASCENDING) |
| 31 | + self.assertEqual(SortDirection.from_string("desc"), SortDirection.DESCENDING) |
| 32 | + with self.assertRaises(ValueError): |
| 33 | + SortDirection.from_string("invalid") |
| 34 | + |
| 35 | + def test_null_ordering(self): |
| 36 | + self.assertEqual(str(NullOrdering.NULLS_FIRST), "nulls_first") |
| 37 | + self.assertEqual(str(NullOrdering.NULLS_LAST), "nulls_last") |
| 38 | + |
| 39 | + def test_sort_impl_initialization(self): |
| 40 | + mock_expression = MagicMock(spec=Expression) |
| 41 | + sort_impl = SortImpl( |
| 42 | + expression=mock_expression, |
| 43 | + direction=SortDirection.ASCENDING, |
| 44 | + null_ordering=NullOrdering.NULLS_FIRST, |
| 45 | + ) |
| 46 | + self.assertEqual(sort_impl.expression(), mock_expression) |
| 47 | + self.assertEqual(sort_impl.direction(), SortDirection.ASCENDING) |
| 48 | + self.assertEqual(sort_impl.null_ordering(), NullOrdering.NULLS_FIRST) |
| 49 | + |
| 50 | + def test_sort_impl_equality(self): |
| 51 | + mock_expression1 = MagicMock(spec=Expression) |
| 52 | + mock_expression2 = MagicMock(spec=Expression) |
| 53 | + |
| 54 | + sort_impl1 = SortImpl( |
| 55 | + expression=mock_expression1, |
| 56 | + direction=SortDirection.ASCENDING, |
| 57 | + null_ordering=NullOrdering.NULLS_FIRST, |
| 58 | + ) |
| 59 | + sort_impl2 = SortImpl( |
| 60 | + expression=mock_expression1, |
| 61 | + direction=SortDirection.ASCENDING, |
| 62 | + null_ordering=NullOrdering.NULLS_FIRST, |
| 63 | + ) |
| 64 | + sort_impl3 = SortImpl( |
| 65 | + expression=mock_expression2, |
| 66 | + direction=SortDirection.ASCENDING, |
| 67 | + null_ordering=NullOrdering.NULLS_FIRST, |
| 68 | + ) |
| 69 | + |
| 70 | + self.assertEqual(sort_impl1, sort_impl2) |
| 71 | + self.assertNotEqual(sort_impl1, sort_impl3) |
| 72 | + |
| 73 | + def test_sort_orders(self): |
| 74 | + mock_expression = MagicMock(spec=Expression) |
| 75 | + ascending_order = SortOrders.ascending(mock_expression) |
| 76 | + self.assertEqual(ascending_order.direction(), SortDirection.ASCENDING) |
| 77 | + self.assertEqual(ascending_order.null_ordering(), NullOrdering.NULLS_FIRST) |
| 78 | + |
| 79 | + descending_order = SortOrders.descending(mock_expression) |
| 80 | + self.assertEqual(descending_order.direction(), SortDirection.DESCENDING) |
| 81 | + self.assertEqual(descending_order.null_ordering(), NullOrdering.NULLS_LAST) |
| 82 | + |
| 83 | + def test_sort_impl_string_representation(self): |
| 84 | + mock_expression = MagicMock(spec=Expression) |
| 85 | + sort_impl = SortImpl( |
| 86 | + expression=mock_expression, |
| 87 | + direction=SortDirection.ASCENDING, |
| 88 | + null_ordering=NullOrdering.NULLS_FIRST, |
| 89 | + ) |
| 90 | + expected_str = ( |
| 91 | + f"SortImpl(expression={mock_expression}, " |
| 92 | + f"direction=asc, null_ordering=nulls_first)" |
| 93 | + ) |
| 94 | + self.assertEqual(str(sort_impl), expected_str) |
| 95 | + |
| 96 | + def test_sort_order(self): |
| 97 | + field_reference = NamedReference.field(["field1"]) |
| 98 | + sort_order = SortOrders.of( |
| 99 | + field_reference, SortDirection.ASCENDING, NullOrdering.NULLS_FIRST |
| 100 | + ) |
| 101 | + |
| 102 | + self.assertEqual(NullOrdering.NULLS_FIRST, sort_order.null_ordering()) |
| 103 | + self.assertEqual(SortDirection.ASCENDING, sort_order.direction()) |
| 104 | + self.assertIsInstance(sort_order.expression(), NamedReference) |
| 105 | + self.assertEqual(["field1"], sort_order.expression().field_name()) |
| 106 | + |
| 107 | + date = FunctionExpression.of("date", NamedReference.field(["b"])) |
| 108 | + sort_order = SortOrders.of( |
| 109 | + date, SortDirection.DESCENDING, NullOrdering.NULLS_LAST |
| 110 | + ) |
| 111 | + self.assertEqual(NullOrdering.NULLS_LAST, sort_order.null_ordering()) |
| 112 | + self.assertEqual(SortDirection.DESCENDING, sort_order.direction()) |
| 113 | + |
| 114 | + self.assertIsInstance(sort_order.expression(), FunctionExpression) |
| 115 | + self.assertEqual("date", sort_order.expression().function_name()) |
| 116 | + self.assertEqual( |
| 117 | + ["b"], sort_order.expression().arguments()[0].references()[0].field_name() |
| 118 | + ) |
0 commit comments