|
| 1 | +import pytest |
| 2 | +from models_library.list_operations import OrderDirection, check_ordering_list |
| 3 | + |
| 4 | + |
| 5 | +def test_check_ordering_list_drops_duplicates_silently(): |
| 6 | + """Test that check_ordering_list silently drops duplicate entries with same field and direction""" |
| 7 | + |
| 8 | + # Input with duplicates (same field and direction) |
| 9 | + order_by = [ |
| 10 | + ("email", OrderDirection.ASC), |
| 11 | + ("created", OrderDirection.DESC), |
| 12 | + ("email", OrderDirection.ASC), # Duplicate - should be dropped |
| 13 | + ("name", OrderDirection.ASC), |
| 14 | + ("created", OrderDirection.DESC), # Duplicate - should be dropped |
| 15 | + ] |
| 16 | + |
| 17 | + result = check_ordering_list(order_by) |
| 18 | + |
| 19 | + # Should return unique entries preserving order of first occurrence |
| 20 | + expected = [ |
| 21 | + ("email", OrderDirection.ASC), |
| 22 | + ("created", OrderDirection.DESC), |
| 23 | + ("name", OrderDirection.ASC), |
| 24 | + ] |
| 25 | + |
| 26 | + assert result == expected |
| 27 | + |
| 28 | + |
| 29 | +def test_check_ordering_list_raises_for_conflicting_directions(): |
| 30 | + """Test that check_ordering_list raises ValueError when same field has different directions""" |
| 31 | + |
| 32 | + # Input with same field but different directions |
| 33 | + order_by = [ |
| 34 | + ("email", OrderDirection.ASC), |
| 35 | + ("created", OrderDirection.DESC), |
| 36 | + ("email", OrderDirection.DESC), # Conflict! Same field, different direction |
| 37 | + ] |
| 38 | + |
| 39 | + with pytest.raises(ValueError, match="conflicting directions") as exc_info: |
| 40 | + check_ordering_list(order_by) |
| 41 | + |
| 42 | + error_msg = str(exc_info.value) |
| 43 | + assert "Field 'email' appears with conflicting directions" in error_msg |
| 44 | + assert "asc" in error_msg |
| 45 | + assert "desc" in error_msg |
| 46 | + |
| 47 | + |
| 48 | +def test_check_ordering_list_empty_input(): |
| 49 | + """Test that check_ordering_list handles empty input correctly""" |
| 50 | + |
| 51 | + result = check_ordering_list([]) |
| 52 | + assert result == [] |
| 53 | + |
| 54 | + |
| 55 | +def test_check_ordering_list_no_duplicates(): |
| 56 | + """Test that check_ordering_list works correctly when there are no duplicates""" |
| 57 | + |
| 58 | + order_by = [ |
| 59 | + ("email", OrderDirection.ASC), |
| 60 | + ("created", OrderDirection.DESC), |
| 61 | + ("name", OrderDirection.ASC), |
| 62 | + ] |
| 63 | + |
| 64 | + result = check_ordering_list(order_by) |
| 65 | + |
| 66 | + # Should return the same list |
| 67 | + assert result == order_by |
0 commit comments