|  | 
|  | 1 | +"""Test the ENABLE_COLLECTIONS_SEARCH environment variable.""" | 
|  | 2 | + | 
|  | 3 | +import os | 
|  | 4 | +import uuid | 
|  | 5 | +from unittest import mock | 
|  | 6 | + | 
|  | 7 | +import pytest | 
|  | 8 | + | 
|  | 9 | +from ..conftest import create_collection, refresh_indices | 
|  | 10 | + | 
|  | 11 | + | 
|  | 12 | +@pytest.mark.asyncio | 
|  | 13 | +@mock.patch.dict(os.environ, {"ENABLE_COLLECTIONS_SEARCH": "false"}) | 
|  | 14 | +async def test_collections_search_disabled(app_client, txn_client, load_test_data): | 
|  | 15 | +    """Test that collection search extensions are disabled when ENABLE_COLLECTIONS_SEARCH=false.""" | 
|  | 16 | +    # Create multiple collections with different ids to test sorting | 
|  | 17 | +    base_collection = load_test_data("test_collection.json") | 
|  | 18 | + | 
|  | 19 | +    # Use unique prefixes to avoid conflicts between tests | 
|  | 20 | +    test_prefix = f"disabled-{uuid.uuid4().hex[:8]}" | 
|  | 21 | +    collection_ids = [f"{test_prefix}-c", f"{test_prefix}-a", f"{test_prefix}-b"] | 
|  | 22 | + | 
|  | 23 | +    for i, coll_id in enumerate(collection_ids): | 
|  | 24 | +        test_collection = base_collection.copy() | 
|  | 25 | +        test_collection["id"] = coll_id | 
|  | 26 | +        test_collection["title"] = f"Test Collection {i}" | 
|  | 27 | +        await create_collection(txn_client, test_collection) | 
|  | 28 | + | 
|  | 29 | +    # Refresh indices to ensure collections are searchable | 
|  | 30 | +    await refresh_indices(txn_client) | 
|  | 31 | + | 
|  | 32 | +    # When collection search is disabled, sortby parameter should be ignored | 
|  | 33 | +    resp = await app_client.get( | 
|  | 34 | +        "/collections", | 
|  | 35 | +        params=[("sortby", "+id")], | 
|  | 36 | +    ) | 
|  | 37 | +    assert resp.status_code == 200 | 
|  | 38 | + | 
|  | 39 | +    # Verify that results are NOT sorted by id (should be in insertion order or default order) | 
|  | 40 | +    resp_json = resp.json() | 
|  | 41 | +    collections = [ | 
|  | 42 | +        c for c in resp_json["collections"] if c["id"].startswith(test_prefix) | 
|  | 43 | +    ] | 
|  | 44 | + | 
|  | 45 | +    # Extract the ids in the order they were returned | 
|  | 46 | +    returned_ids = [c["id"] for c in collections] | 
|  | 47 | + | 
|  | 48 | +    # If sorting was working, they would be in alphabetical order: a, b, c | 
|  | 49 | +    # But since sorting is disabled, they should be in a different order | 
|  | 50 | +    # We can't guarantee the exact order, but we can check they're not in alphabetical order | 
|  | 51 | +    sorted_ids = sorted(returned_ids) | 
|  | 52 | +    assert ( | 
|  | 53 | +        returned_ids != sorted_ids or len(collections) < 2 | 
|  | 54 | +    ), "Collections appear to be sorted despite ENABLE_COLLECTIONS_SEARCH=false" | 
|  | 55 | + | 
|  | 56 | +    # Fields parameter should also be ignored | 
|  | 57 | +    resp = await app_client.get( | 
|  | 58 | +        "/collections", | 
|  | 59 | +        params=[("fields", "id")],  # Request only id field | 
|  | 60 | +    ) | 
|  | 61 | +    assert resp.status_code == 200 | 
|  | 62 | + | 
|  | 63 | +    # Verify that all fields are still returned, not just id | 
|  | 64 | +    resp_json = resp.json() | 
|  | 65 | +    for collection in resp_json["collections"]: | 
|  | 66 | +        if collection["id"].startswith(test_prefix): | 
|  | 67 | +            # If fields filtering was working, only id would be present | 
|  | 68 | +            # Since it's disabled, other fields like title should still be present | 
|  | 69 | +            assert ( | 
|  | 70 | +                "title" in collection | 
|  | 71 | +            ), "Fields filtering appears to be working despite ENABLE_COLLECTIONS_SEARCH=false" | 
|  | 72 | + | 
|  | 73 | + | 
|  | 74 | +@pytest.mark.asyncio | 
|  | 75 | +@mock.patch.dict(os.environ, {"ENABLE_COLLECTIONS_SEARCH": "true"}) | 
|  | 76 | +async def test_collections_search_enabled(app_client, txn_client, load_test_data): | 
|  | 77 | +    """Test that collection search extensions work when ENABLE_COLLECTIONS_SEARCH=true.""" | 
|  | 78 | +    # Create multiple collections with different ids to test sorting | 
|  | 79 | +    base_collection = load_test_data("test_collection.json") | 
|  | 80 | + | 
|  | 81 | +    # Use unique prefixes to avoid conflicts between tests | 
|  | 82 | +    test_prefix = f"enabled-{uuid.uuid4().hex[:8]}" | 
|  | 83 | +    collection_ids = [f"{test_prefix}-c", f"{test_prefix}-a", f"{test_prefix}-b"] | 
|  | 84 | + | 
|  | 85 | +    for i, coll_id in enumerate(collection_ids): | 
|  | 86 | +        test_collection = base_collection.copy() | 
|  | 87 | +        test_collection["id"] = coll_id | 
|  | 88 | +        test_collection["title"] = f"Test Collection {i}" | 
|  | 89 | +        await create_collection(txn_client, test_collection) | 
|  | 90 | + | 
|  | 91 | +    # Refresh indices to ensure collections are searchable | 
|  | 92 | +    await refresh_indices(txn_client) | 
|  | 93 | + | 
|  | 94 | +    # Test that sortby parameter works - sort by id ascending | 
|  | 95 | +    resp = await app_client.get( | 
|  | 96 | +        "/collections", | 
|  | 97 | +        params=[("sortby", "+id")], | 
|  | 98 | +    ) | 
|  | 99 | +    assert resp.status_code == 200 | 
|  | 100 | + | 
|  | 101 | +    # Verify that results are sorted by id in ascending order | 
|  | 102 | +    resp_json = resp.json() | 
|  | 103 | +    collections = [ | 
|  | 104 | +        c for c in resp_json["collections"] if c["id"].startswith(test_prefix) | 
|  | 105 | +    ] | 
|  | 106 | + | 
|  | 107 | +    # Extract the ids in the order they were returned | 
|  | 108 | +    returned_ids = [c["id"] for c in collections] | 
|  | 109 | + | 
|  | 110 | +    # Verify they're in ascending order | 
|  | 111 | +    assert returned_ids == sorted( | 
|  | 112 | +        returned_ids | 
|  | 113 | +    ), "Collections are not sorted by id ascending" | 
|  | 114 | + | 
|  | 115 | +    # Test that sortby parameter works - sort by id descending | 
|  | 116 | +    resp = await app_client.get( | 
|  | 117 | +        "/collections", | 
|  | 118 | +        params=[("sortby", "-id")], | 
|  | 119 | +    ) | 
|  | 120 | +    assert resp.status_code == 200 | 
|  | 121 | + | 
|  | 122 | +    # Verify that results are sorted by id in descending order | 
|  | 123 | +    resp_json = resp.json() | 
|  | 124 | +    collections = [ | 
|  | 125 | +        c for c in resp_json["collections"] if c["id"].startswith(test_prefix) | 
|  | 126 | +    ] | 
|  | 127 | + | 
|  | 128 | +    # Extract the ids in the order they were returned | 
|  | 129 | +    returned_ids = [c["id"] for c in collections] | 
|  | 130 | + | 
|  | 131 | +    # Verify they're in descending order | 
|  | 132 | +    assert returned_ids == sorted( | 
|  | 133 | +        returned_ids, reverse=True | 
|  | 134 | +    ), "Collections are not sorted by id descending" | 
|  | 135 | + | 
|  | 136 | +    # Test that fields parameter works - request only id field | 
|  | 137 | +    resp = await app_client.get( | 
|  | 138 | +        "/collections", | 
|  | 139 | +        params=[("fields", "id")], | 
|  | 140 | +    ) | 
|  | 141 | +    assert resp.status_code == 200 | 
|  | 142 | +    resp_json = resp.json() | 
|  | 143 | + | 
|  | 144 | +    # When fields=id is specified, collections should only have id field | 
|  | 145 | +    for collection in resp_json["collections"]: | 
|  | 146 | +        if collection["id"].startswith(test_prefix): | 
|  | 147 | +            assert "id" in collection, "id field is missing" | 
|  | 148 | +            assert ( | 
|  | 149 | +                "title" not in collection | 
|  | 150 | +            ), "title field should be excluded when fields=id" | 
|  | 151 | + | 
|  | 152 | +    # Test that fields parameter works - request multiple fields | 
|  | 153 | +    resp = await app_client.get( | 
|  | 154 | +        "/collections", | 
|  | 155 | +        params=[("fields", "id,title")], | 
|  | 156 | +    ) | 
|  | 157 | +    assert resp.status_code == 200 | 
|  | 158 | +    resp_json = resp.json() | 
|  | 159 | + | 
|  | 160 | +    # When fields=id,title is specified, collections should have both fields but not others | 
|  | 161 | +    for collection in resp_json["collections"]: | 
|  | 162 | +        if collection["id"].startswith(test_prefix): | 
|  | 163 | +            assert "id" in collection, "id field is missing" | 
|  | 164 | +            assert "title" in collection, "title field is missing" | 
|  | 165 | +            assert ( | 
|  | 166 | +                "description" not in collection | 
|  | 167 | +            ), "description field should be excluded when fields=id,title" | 
0 commit comments