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