-
Notifications
You must be signed in to change notification settings - Fork 113
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
503aac0
cf511d2
6a3aed7
35eae23
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,19 @@ | ||
| from typing import Any | ||
|
|
||
| from camel_converter.pydantic_base import CamelBase | ||
|
|
||
|
|
||
| class DynamicSearchRule(CamelBase): | ||
| uid: str | ||
| description: str | None = None | ||
| priority: int | None = None | ||
| active: bool | ||
| conditions: list[dict[str, Any]] | ||
| actions: list[dict[str, Any]] | ||
|
|
||
|
|
||
| class DynamicSearchRuleResults(CamelBase): | ||
| results: list[DynamicSearchRule] | ||
| offset: int | ||
| limit: int | ||
| total: int |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| """Tests for dynamic search rule management endpoints.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from meilisearch.errors import MeilisearchApiError | ||
|
|
||
| pytestmark = pytest.mark.usefixtures("enable_dynamic_search_rules", "clear_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): | ||
|
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. Split this into seperate tests, one for offset and one for limit. With the current setup it can take two test run to know all issues. Splitting them allows 1 test frun to find both issues if present. You can make a fixture in this file to create the same rules for both tests. |
||
| """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) | ||
|
|
||
|
|
||
| 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") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,22 @@ def clear_webhooks(client): | |
| client.delete_webhook(webhook.uuid) | ||
|
|
||
|
|
||
| @fixture | ||
| def clear_dynamic_search_rules(client): | ||
| """ | ||
| Clears the dynamic search rules after each test function run. | ||
| Must be explicitly used alongside enable_dynamic_search_rules, | ||
| since the dynamic search rules feature requires experimental feature toggle. | ||
| """ | ||
| # Yields back to the test function. | ||
|
|
||
| yield | ||
| # Deletes all the dynamic search rules in the Meilisearch instance. | ||
| rules = client.get_dynamic_search_rules() | ||
| for rule in rules.results: | ||
| client.delete_dynamic_search_rule(rule.uid) | ||
|
|
||
|
|
||
|
Comment on lines
+64
to
+79
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. Let add the cleanup to |
||
| @fixture(autouse=True) | ||
| def clear_all_tasks(client, client2): | ||
| """ | ||
|
|
@@ -354,6 +370,23 @@ def enable_network_options(): | |
| ) | ||
|
|
||
|
|
||
| @fixture | ||
| def enable_dynamic_search_rules(): | ||
| requests.patch( | ||
| f"{common.BASE_URL}/experimental-features", | ||
| headers={"Authorization": f"Bearer {common.MASTER_KEY}"}, | ||
| json={"dynamicSearchRules": True}, | ||
| timeout=10, | ||
| ) | ||
| yield | ||
| requests.patch( | ||
| f"{common.BASE_URL}/experimental-features", | ||
| headers={"Authorization": f"Bearer {common.MASTER_KEY}"}, | ||
| json={"dynamicSearchRules": False}, | ||
| timeout=10, | ||
|
w1ndcn marked this conversation as resolved.
|
||
| ) | ||
|
|
||
|
|
||
| @fixture | ||
| def enable_render_route(): | ||
| requests.patch( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
Let's not list the parameters that are accepted here, this will be hard to maintain. Not listing them matches other parameters like this.