-
Notifications
You must be signed in to change notification settings - Fork 110
Add support for Dynamic Search Rules #1238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
68831d5
7e2fd46
67639b6
dd1a55a
9e7a7af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| from typing import Any, Dict, List, Optional | ||
|
|
||
| from camel_converter.pydantic_base import CamelBase | ||
| from pydantic import ConfigDict | ||
|
|
||
|
|
||
| class DynamicSearchRule(CamelBase): | ||
| """Model for a Meilisearch dynamic search rule.""" | ||
|
|
||
| model_config = ConfigDict(arbitrary_types_allowed=True) | ||
|
|
||
| uid: str | ||
| description: Optional[str] = None | ||
| priority: Optional[int] = None | ||
| active: Optional[bool] = None | ||
| conditions: Optional[List[Dict[str, Any]]] = None | ||
| actions: Optional[List[Dict[str, Any]]] = None | ||
|
Comment on lines
+13
to
+17
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these really optional? In your comment about failing tests you said actions was requried. @Strift the Meilisearch documentation here isn't great unless I'm missing somthing, but I'm assuming none of these should be optional. |
||
|
|
||
|
|
||
| class DynamicSearchRuleResults(CamelBase): | ||
| """Model for dynamic search rules list results.""" | ||
|
|
||
| model_config = ConfigDict(arbitrary_types_allowed=True) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why allow arbitrary types, there aren't any here? |
||
| results: List[DynamicSearchRule] | ||
| offset: int | ||
| limit: int | ||
| total: int | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| """Tests for dynamic search rule management endpoints.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from meilisearch.errors import MeilisearchApiError | ||
|
|
||
| pytestmark = pytest.mark.usefixtures("enable_dynamic_search_rules") | ||
|
|
||
|
|
||
| def test_get_dynamic_search_rules_empty(client): | ||
| """Test getting dynamic search rules when none exist.""" | ||
| rules = client.get_dynamic_search_rules() | ||
| assert rules.results is not None | ||
| assert isinstance(rules.results, list) | ||
| assert len(rules.results) == 0 | ||
|
|
||
|
w1ndcn marked this conversation as resolved.
|
||
|
|
||
| def test_get_dynamic_search_rules_with_parameters(client): | ||
| """Test getting dynamic search rules with offset, limit, and filter parameters.""" | ||
| # Create a few rules to paginate/filter | ||
| for i in range(3): | ||
| client.create_or_update_dynamic_search_rule( | ||
| f"test-rule-{i}", | ||
| { | ||
| "description": f"Rule {i}", | ||
| "active": i != 2, | ||
| "conditions": [{"scope": "query", "isEmpty": True}], | ||
| "actions": [ | ||
| { | ||
| "selector": {"indexUid": "movies", "id": str(i)}, | ||
| "action": {"type": "pin", "position": i + 1}, | ||
| } | ||
| ], | ||
| }, | ||
| ) | ||
|
|
||
| # Test with limit | ||
| rules = client.get_dynamic_search_rules({"limit": 1}) | ||
| assert len(rules.results) == 1 | ||
|
|
||
| # Verify total count before testing offset | ||
| all_rules = client.get_dynamic_search_rules() | ||
| assert len(all_rules.results) == 3, "Offset test requires exactly 3 rules" | ||
| rules = client.get_dynamic_search_rules({"offset": 1}) | ||
| assert len(rules.results) == 2 | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| # Test with filter on active status | ||
| rules = client.get_dynamic_search_rules({"filter": {"active": True}}) | ||
| assert all(r.active is True for r in rules.results) | ||
|
|
||
| # Clean up | ||
| for i in range(3): | ||
| client.delete_dynamic_search_rule(f"test-rule-{i}") | ||
|
Comment on lines
+52
to
+53
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can cause false failures. If this test fails all subsequent tests will fail because the rules will be left behind. Instead create a fixture that cleans up the search rules after each test that creates them. @pytest.fixture
def cleanup_search_rules():
yield
# do cleanup here |
||
|
|
||
|
|
||
| def test_create_or_update_dynamic_search_rule(client): | ||
| """Test creating a dynamic search rule.""" | ||
| rule_data = { | ||
| "description": "Test rule for promotion", | ||
| "priority": 10, | ||
| "active": True, | ||
| "conditions": [{"scope": "query", "isEmpty": True}], | ||
| "actions": [ | ||
| { | ||
| "selector": {"indexUid": "movies", "id": "123"}, | ||
| "action": {"type": "pin", "position": 1}, | ||
| } | ||
| ], | ||
| } | ||
|
|
||
| rule = client.create_or_update_dynamic_search_rule("test-rule", rule_data) | ||
|
|
||
| assert rule.uid == "test-rule" | ||
| assert rule.description == rule_data["description"] | ||
| assert rule.priority == 10 | ||
| assert rule.active is True | ||
|
|
||
|
|
||
| def test_get_dynamic_search_rule(client): | ||
| """Test getting a single dynamic search rule.""" | ||
| # Create a rule first | ||
| rule_data = { | ||
| "description": "Test rule", | ||
| "active": True, | ||
| "conditions": [{"scope": "query", "isEmpty": True}], | ||
| "actions": [ | ||
| { | ||
| "selector": {"indexUid": "movies", "id": "123"}, | ||
| "action": {"type": "pin", "position": 1}, | ||
| } | ||
| ], | ||
| } | ||
|
|
||
| created_rule = client.create_or_update_dynamic_search_rule("test-rule", rule_data) | ||
|
|
||
| # Get the rule | ||
| rule = client.get_dynamic_search_rule(created_rule.uid) | ||
|
|
||
| assert rule.uid == created_rule.uid | ||
| assert rule.description == rule_data["description"] | ||
|
|
||
|
|
||
| def test_get_dynamic_search_rule_not_found(client): | ||
| """Test getting a dynamic search rule that doesn't exist.""" | ||
| with pytest.raises(MeilisearchApiError): | ||
| client.get_dynamic_search_rule("non-existent-uid") | ||
|
|
||
|
|
||
| def test_update_dynamic_search_rule(client): | ||
| """Test updating a dynamic search rule.""" | ||
| # Create a rule first | ||
| rule_data = { | ||
| "description": "Original description", | ||
| "priority": 10, | ||
| "active": True, | ||
| "conditions": [{"scope": "query", "isEmpty": True}], | ||
| "actions": [ | ||
| { | ||
| "selector": {"indexUid": "movies", "id": "123"}, | ||
| "action": {"type": "pin", "position": 1}, | ||
| } | ||
| ], | ||
| } | ||
|
|
||
| created_rule = client.create_or_update_dynamic_search_rule("test-rule", rule_data) | ||
|
|
||
| # Update the rule | ||
| update_data = { | ||
| "description": "Updated description", | ||
| "priority": 5, | ||
| } | ||
|
|
||
| updated_rule = client.create_or_update_dynamic_search_rule(created_rule.uid, update_data) | ||
|
|
||
| assert updated_rule.uid == created_rule.uid | ||
| assert updated_rule.description == update_data["description"] | ||
| assert updated_rule.priority == 5 | ||
|
|
||
|
|
||
| def test_delete_dynamic_search_rule(client): | ||
| """Test deleting a dynamic search rule.""" | ||
| # Create a rule first | ||
| rule_data = { | ||
| "description": "Rule to delete", | ||
| "active": True, | ||
| "conditions": [{"scope": "query", "isEmpty": True}], | ||
| "actions": [ | ||
| { | ||
| "selector": {"indexUid": "movies", "id": "123"}, | ||
| "action": {"type": "pin", "position": 1}, | ||
| } | ||
| ], | ||
| } | ||
|
|
||
| created_rule = client.create_or_update_dynamic_search_rule("test-rule", rule_data) | ||
|
|
||
| # Delete the rule | ||
| status_code = client.delete_dynamic_search_rule(created_rule.uid) | ||
|
|
||
| assert status_code == 204 | ||
|
|
||
| # Verify it's deleted | ||
| with pytest.raises(MeilisearchApiError): | ||
| client.get_dynamic_search_rule(created_rule.uid) | ||
|
|
||
|
|
||
| def test_delete_dynamic_search_rule_not_found(client): | ||
| """Test deleting a dynamic search rule that doesn't exist.""" | ||
| with pytest.raises(MeilisearchApiError): | ||
| client.delete_dynamic_search_rule("non-existent-uid") | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why allow arbitrary types, there aren't any here?