|
| 1 | +from datetime import UTC |
| 2 | +from datetime import datetime |
1 | 3 | from typing import Annotated |
2 | 4 |
|
3 | 5 | import pytest |
|
7 | 9 | from scim2_models import ComplexAttribute |
8 | 10 | from scim2_models import EnterpriseUser |
9 | 11 | from scim2_models import Group |
| 12 | +from scim2_models import Meta |
10 | 13 | from scim2_models import Mutability |
11 | 14 | from scim2_models import Resource |
12 | 15 | from scim2_models import Returned |
@@ -703,6 +706,48 @@ def test_sort_binding_is_none_when_the_model_cannot_sort_on_the_attribute( |
703 | 706 | assert SearchRequest(sort_by=attribute).sort_binding(model) is None |
704 | 707 |
|
705 | 708 |
|
| 709 | +def test_sort_value_without_sort_by_is_none(): |
| 710 | + """There is nothing to order the resource by.""" |
| 711 | + assert SearchRequest().sort_value(User(user_name="bjensen")) is None |
| 712 | + |
| 713 | + |
| 714 | +def test_sort_value_is_the_string_sort_compares(): |
| 715 | + """A keyset storage resumes on the same form of the string as sort() orders by.""" |
| 716 | + request = SearchRequest(sort_by="userName") |
| 717 | + assert request.sort_value(User(user_name="ÉMILE")) == "émile" |
| 718 | + |
| 719 | + |
| 720 | +def test_sort_value_reads_the_primary_entry_of_a_multivalued_attribute(): |
| 721 | + """RFC7644 §3.4.2.3 sorts on "the value of the primary attribute".""" |
| 722 | + user = User( |
| 723 | + user_name="bjensen", |
| 724 | + emails=[ |
| 725 | + User.Emails(value="b@example.com"), |
| 726 | + User.Emails(value="A@example.com", primary=True), |
| 727 | + ], |
| 728 | + ) |
| 729 | + assert SearchRequest(sort_by="emails").sort_value(user) == "a@example.com" |
| 730 | + |
| 731 | + |
| 732 | +def test_sort_value_keeps_the_type_of_a_non_string_value(): |
| 733 | + """A storage gets the boolean or the date as sort() compares it.""" |
| 734 | + when = datetime(2026, 10, 9, tzinfo=UTC) |
| 735 | + user = User(user_name="bjensen", active=True, meta=Meta(last_modified=when)) |
| 736 | + assert SearchRequest(sort_by="active").sort_value(user) is True |
| 737 | + assert SearchRequest(sort_by="meta.lastModified").sort_value(user) == when |
| 738 | + |
| 739 | + |
| 740 | +def test_sort_value_is_none_without_a_value(): |
| 741 | + """sort() puts such a resource last when ascending and first when descending.""" |
| 742 | + assert SearchRequest(sort_by="displayName").sort_value(User(user_name="b")) is None |
| 743 | + |
| 744 | + |
| 745 | +def test_sort_value_is_none_when_the_type_cannot_sort_on_the_attribute(): |
| 746 | + """A resource of a union whose type lacks the attribute is ordered as one without a value.""" |
| 747 | + request = SearchRequest[User | Group](sort_by="userName") |
| 748 | + assert request.sort_value(Group(display_name="admins")) is None |
| 749 | + |
| 750 | + |
706 | 751 | def ids(resources): |
707 | 752 | """Return the ids of the resources, in their order.""" |
708 | 753 | return [resource.id for resource in resources] |
|
0 commit comments