Skip to content

Commit b1395c5

Browse files
authored
Merge pull request #1636 from saireddythfc/codegen-bot/sort-collections-list-all
Order return of collections.list_all() alphabetically by key
2 parents 5f8ef1a + c784f15 commit b1395c5

3 files changed

Lines changed: 101 additions & 2 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pytest
2+
from integration.conftest import ClientFactory
3+
4+
5+
@pytest.fixture
6+
def sorting_test_client(client_factory: ClientFactory):
7+
client = client_factory()
8+
try:
9+
yield client
10+
finally:
11+
client.close()
12+
13+
14+
def test_collections_list_all_sorting(sorting_test_client):
15+
"""Test that collections.list_all() returns collections sorted alphabetically by key."""
16+
client = sorting_test_client
17+
18+
try:
19+
# Create collections with names in non-alphabetical order
20+
client.collections.create(name="SortingTestCollectionC")
21+
client.collections.create(name="SortingTestCollectionA")
22+
client.collections.create(name="SortingTestCollectionB")
23+
24+
# Get all collections
25+
collections = client.collections.list_all()
26+
27+
# Get the keys and filter only our test collections
28+
collection_keys = list(collections.keys())
29+
test_collections = [k for k in collection_keys if k.startswith("SortingTestCollection")]
30+
31+
# Verify they are in alphabetical order
32+
assert test_collections == sorted(test_collections)
33+
34+
# Test with simple=False as well
35+
collections = client.collections.list_all(simple=False)
36+
collection_keys = list(collections.keys())
37+
test_collections = [k for k in collection_keys if k.startswith("SortingTestCollection")]
38+
assert test_collections == sorted(test_collections)
39+
40+
finally:
41+
# Clean up
42+
client.collections.delete(
43+
["SortingTestCollectionA", "SortingTestCollectionB", "SortingTestCollectionC"]
44+
)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from unittest.mock import MagicMock, patch
2+
from weaviate.collections.classes.config_methods import (
3+
_collection_configs_from_json,
4+
_collection_configs_simple_from_json,
5+
)
6+
7+
8+
def test_collection_configs_from_json_sorting():
9+
"""Test that _collection_configs_from_json returns collections sorted by key."""
10+
# Mock schema with collections in non-alphabetical order
11+
mock_schema = {
12+
"classes": [
13+
{"class": "CollectionC", "description": "Collection C"},
14+
{"class": "CollectionA", "description": "Collection A"},
15+
{"class": "CollectionB", "description": "Collection B"},
16+
]
17+
}
18+
19+
# Mock the _collection_config_from_json function to return a simple object
20+
with patch(
21+
"weaviate.collections.classes.config_methods._collection_config_from_json",
22+
return_value=MagicMock(),
23+
):
24+
# Call the function
25+
result = _collection_configs_from_json(mock_schema)
26+
27+
# Check that the keys are in alphabetical order
28+
assert list(result.keys()) == ["CollectionA", "CollectionB", "CollectionC"]
29+
30+
31+
def test_collection_configs_simple_from_json_sorting():
32+
"""Test that _collection_configs_simple_from_json returns collections sorted by key."""
33+
# Mock schema with collections in non-alphabetical order
34+
mock_schema = {
35+
"classes": [
36+
{"class": "CollectionC", "description": "Collection C"},
37+
{"class": "CollectionA", "description": "Collection A"},
38+
{"class": "CollectionB", "description": "Collection B"},
39+
]
40+
}
41+
42+
# Mock the _collection_config_simple_from_json function to return a simple object
43+
with patch(
44+
"weaviate.collections.classes.config_methods._collection_config_simple_from_json",
45+
return_value=MagicMock(),
46+
):
47+
# Call the function
48+
result = _collection_configs_simple_from_json(mock_schema)
49+
50+
# Check that the keys are in alphabetical order
51+
assert list(result.keys()) == ["CollectionA", "CollectionB", "CollectionC"]

weaviate/collections/classes/config_methods.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,19 @@ def _collection_config_from_json(schema: Dict[str, Any]) -> _CollectionConfig:
343343

344344

345345
def _collection_configs_from_json(schema: Dict[str, Any]) -> Dict[str, _CollectionConfig]:
346-
return {schema["class"]: _collection_config_from_json(schema) for schema in schema["classes"]}
346+
configs = {
347+
schema["class"]: _collection_config_from_json(schema) for schema in schema["classes"]
348+
}
349+
return dict(sorted(configs.items()))
347350

348351

349352
def _collection_configs_simple_from_json(
350353
schema: Dict[str, Any]
351354
) -> Dict[str, _CollectionConfigSimple]:
352-
return {
355+
configs = {
353356
schema["class"]: _collection_config_simple_from_json(schema) for schema in schema["classes"]
354357
}
358+
return dict(sorted(configs.items()))
355359

356360

357361
def _nested_properties_from_config(props: List[Dict[str, Any]]) -> List[_NestedProperty]:

0 commit comments

Comments
 (0)