|
| 1 | +import pytest |
| 2 | +from typing import Self |
| 3 | + |
| 4 | +from simple_logger.logger import get_logger |
| 5 | +from tests.model_registry.model_catalog.utils import ( |
| 6 | + get_models_from_catalog_api, |
| 7 | + get_sources_with_sorting, |
| 8 | + get_artifacts_with_sorting, |
| 9 | + validate_items_sorted_correctly, |
| 10 | +) |
| 11 | +from tests.model_registry.model_catalog.constants import VALIDATED_CATALOG_ID |
| 12 | + |
| 13 | +LOGGER = get_logger(name=__name__) |
| 14 | + |
| 15 | +pytestmark = [pytest.mark.usefixtures("updated_dsc_component_state_scope_session", "model_registry_namespace")] |
| 16 | + |
| 17 | + |
| 18 | +class TestModelsSorting: |
| 19 | + """Test sorting functionality for FindModels endpoint""" |
| 20 | + |
| 21 | + @pytest.mark.parametrize( |
| 22 | + "order_by,sort_order", |
| 23 | + [ |
| 24 | + ("ID", "ASC"), |
| 25 | + ("ID", "DESC"), |
| 26 | + pytest.param( |
| 27 | + "NAME", |
| 28 | + "ASC", |
| 29 | + marks=pytest.mark.xfail( |
| 30 | + reason="RHOAIENG-38056: Backend bug - NAME sorting not implemented, falls back to ID sorting" |
| 31 | + ), |
| 32 | + ), |
| 33 | + pytest.param( |
| 34 | + "NAME", |
| 35 | + "DESC", |
| 36 | + marks=pytest.mark.xfail( |
| 37 | + reason="RHOAIENG-38056: Backend bug - NAME sorting not implemented, falls back to ID sorting" |
| 38 | + ), |
| 39 | + ), |
| 40 | + ("CREATE_TIME", "ASC"), |
| 41 | + ("CREATE_TIME", "DESC"), |
| 42 | + ("LAST_UPDATE_TIME", "ASC"), |
| 43 | + ("LAST_UPDATE_TIME", "DESC"), |
| 44 | + ], |
| 45 | + ) |
| 46 | + def test_models_sorting_works_correctly( |
| 47 | + self: Self, |
| 48 | + order_by: str, |
| 49 | + sort_order: str, |
| 50 | + model_catalog_rest_url: list[str], |
| 51 | + model_registry_rest_headers: dict[str, str], |
| 52 | + ): |
| 53 | + """ |
| 54 | + RHOAIENG-37260: Test models endpoint sorts correctly by field and order |
| 55 | + """ |
| 56 | + LOGGER.info(f"Testing models sorting: orderBy={order_by}, sortOrder={sort_order}") |
| 57 | + |
| 58 | + response = get_models_from_catalog_api( |
| 59 | + model_catalog_rest_url=model_catalog_rest_url, |
| 60 | + model_registry_rest_headers=model_registry_rest_headers, |
| 61 | + order_by=order_by, |
| 62 | + sort_order=sort_order, |
| 63 | + ) |
| 64 | + |
| 65 | + assert validate_items_sorted_correctly(response["items"], order_by, sort_order) |
| 66 | + |
| 67 | + |
| 68 | +class TestSourcesSorting: |
| 69 | + """Test sorting functionality for FindSources endpoint""" |
| 70 | + |
| 71 | + @pytest.mark.parametrize( |
| 72 | + "order_by,sort_order", |
| 73 | + [ |
| 74 | + ("ID", "ASC"), |
| 75 | + ("ID", "DESC"), |
| 76 | + ("NAME", "ASC"), |
| 77 | + ("NAME", "DESC"), |
| 78 | + ], |
| 79 | + ) |
| 80 | + def test_sources_sorting_works_correctly( |
| 81 | + self: Self, |
| 82 | + order_by: str, |
| 83 | + sort_order: str, |
| 84 | + model_catalog_rest_url: list[str], |
| 85 | + model_registry_rest_headers: dict[str, str], |
| 86 | + ): |
| 87 | + """ |
| 88 | + RHOAIENG-37260: Test sources endpoint sorts correctly by supported fields |
| 89 | + """ |
| 90 | + LOGGER.info(f"Testing sources sorting: orderBy={order_by}, sortOrder={sort_order}") |
| 91 | + |
| 92 | + response = get_sources_with_sorting( |
| 93 | + model_catalog_rest_url=model_catalog_rest_url, |
| 94 | + model_registry_rest_headers=model_registry_rest_headers, |
| 95 | + order_by=order_by, |
| 96 | + sort_order=sort_order, |
| 97 | + ) |
| 98 | + |
| 99 | + assert validate_items_sorted_correctly(response["items"], order_by, sort_order) |
| 100 | + |
| 101 | + @pytest.mark.parametrize("unsupported_field", ["CREATE_TIME", "LAST_UPDATE_TIME"]) |
| 102 | + def test_sources_rejects_unsupported_fields( |
| 103 | + self: Self, |
| 104 | + unsupported_field: str, |
| 105 | + model_catalog_rest_url: list[str], |
| 106 | + model_registry_rest_headers: dict[str, str], |
| 107 | + ): |
| 108 | + """ |
| 109 | + RHOAIENG-37260: Test sources endpoint rejects fields it doesn't support |
| 110 | + """ |
| 111 | + LOGGER.info(f"Testing sources rejection of unsupported field: {unsupported_field}") |
| 112 | + |
| 113 | + with pytest.raises(Exception, match="unsupported order by field"): |
| 114 | + get_sources_with_sorting( |
| 115 | + model_catalog_rest_url=model_catalog_rest_url, |
| 116 | + model_registry_rest_headers=model_registry_rest_headers, |
| 117 | + order_by=unsupported_field, |
| 118 | + sort_order="ASC", |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +class TestArtifactsSorting: |
| 123 | + """Test sorting functionality for GetAllModelArtifacts endpoint |
| 124 | + Fixed on a random model from the validated catalog since we need more than 1 artifact to test sorting. |
| 125 | + """ |
| 126 | + |
| 127 | + @pytest.mark.parametrize( |
| 128 | + "order_by,sort_order,randomly_picked_model_from_catalog_api_by_source", |
| 129 | + [ |
| 130 | + ("ID", "ASC", {"catalog_id": VALIDATED_CATALOG_ID, "header_type": "registry"}), |
| 131 | + ("ID", "DESC", {"catalog_id": VALIDATED_CATALOG_ID, "header_type": "registry"}), |
| 132 | + pytest.param( |
| 133 | + "NAME", |
| 134 | + "ASC", |
| 135 | + {"catalog_id": VALIDATED_CATALOG_ID, "header_type": "registry"}, |
| 136 | + marks=pytest.mark.xfail(reason="RHOAIENG-38056: falls back to ID sorting"), |
| 137 | + ), |
| 138 | + pytest.param( |
| 139 | + "NAME", |
| 140 | + "DESC", |
| 141 | + {"catalog_id": VALIDATED_CATALOG_ID, "header_type": "registry"}, |
| 142 | + marks=pytest.mark.xfail(reason="RHOAIENG-38056: falls back to ID sorting"), |
| 143 | + ), |
| 144 | + ], |
| 145 | + indirect=["randomly_picked_model_from_catalog_api_by_source"], |
| 146 | + ) |
| 147 | + def test_artifacts_sorting_works_correctly( |
| 148 | + self: Self, |
| 149 | + order_by: str, |
| 150 | + sort_order: str, |
| 151 | + model_catalog_rest_url: list[str], |
| 152 | + model_registry_rest_headers: dict[str, str], |
| 153 | + randomly_picked_model_from_catalog_api_by_source: tuple[dict, str, str], |
| 154 | + ): |
| 155 | + """ |
| 156 | + RHOAIENG-37260: Test artifacts endpoint sorts correctly by supported fields |
| 157 | + """ |
| 158 | + _, model_name, _ = randomly_picked_model_from_catalog_api_by_source |
| 159 | + LOGGER.info(f"Testing artifacts sorting for {model_name}: orderBy={order_by}, sortOrder={sort_order}") |
| 160 | + |
| 161 | + response = get_artifacts_with_sorting( |
| 162 | + model_catalog_rest_url=model_catalog_rest_url, |
| 163 | + model_registry_rest_headers=model_registry_rest_headers, |
| 164 | + source_id=VALIDATED_CATALOG_ID, |
| 165 | + model_name=model_name, |
| 166 | + order_by=order_by, |
| 167 | + sort_order=sort_order, |
| 168 | + ) |
| 169 | + |
| 170 | + assert validate_items_sorted_correctly(response["items"], order_by, sort_order) |
0 commit comments