Skip to content

Commit 781e543

Browse files
committed
RDBC-780 IndexesFromClientTest::canExplain
1 parent e43f93e commit 781e543

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from __future__ import annotations
2+
3+
import json
4+
from typing import Dict, Any, TYPE_CHECKING, Optional, List
5+
6+
import requests
7+
from ravendb import RavenCommand, IndexQuery, ServerNode
8+
from ravendb.extensions.json_extensions import JsonExtensions
9+
10+
if TYPE_CHECKING:
11+
from ravendb.documents.conventions import DocumentConventions
12+
13+
14+
class ExplainQueryResult:
15+
def __init__(self, index: str, reason: str):
16+
self.index = index
17+
self.reason = reason
18+
19+
@classmethod
20+
def from_json(cls, json_dict: Dict[str, Any]) -> ExplainQueryResult:
21+
return cls(json_dict["Index"], json_dict["Reason"])
22+
23+
24+
class ExplainQueryCommand(RavenCommand[List[ExplainQueryResult]]):
25+
def __init__(self, conventions: DocumentConventions, index_query: IndexQuery):
26+
super().__init__(list)
27+
if conventions is None:
28+
raise ValueError("Conventions cannot be None")
29+
if index_query is None:
30+
raise ValueError("Index query cannot be None")
31+
32+
self._conventions = conventions
33+
self._index_query = index_query
34+
35+
def create_request(self, node: ServerNode) -> requests.Request:
36+
path = f"{node.url}/databases/{node.database}/queries?debug=explain"
37+
return requests.Request(
38+
"POST", path, data=JsonExtensions.write_index_query(self._conventions, self._index_query)
39+
)
40+
41+
def set_response(self, response: Optional[str], from_cache: bool) -> None:
42+
if response is None:
43+
return
44+
45+
json_node = json.loads(response)
46+
results = json_node.get("Results", None)
47+
if results is None:
48+
self._throw_invalid_response()
49+
return
50+
51+
self.result = [ExplainQueryResult.from_json(res) for res in results]
52+
53+
def is_read_request(self) -> bool:
54+
return True

ravendb/tests/jvm_migrated_tests/client_tests/indexing_tests/test_indexes_from_client.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import time
22
from typing import Optional
33

4-
from ravendb import QueryStatistics
4+
from ravendb import QueryStatistics, IndexQuery
5+
from ravendb.documents.commands.explain import ExplainQueryCommand
56
from ravendb.documents.operations.statistics import GetStatisticsOperation
67
from ravendb.documents.indexes.index_creation import IndexCreation
78
from ravendb.documents.queries.more_like_this import MoreLikeThisOptions
@@ -218,3 +219,27 @@ def _stats_callback(qs: QueryStatistics) -> None:
218219
index_names = self.store.maintenance.send(GetIndexNamesOperation(0, 10))
219220
self.assertEqual(1, len(index_names))
220221
self.assertIn(index_name, index_names)
222+
223+
def test_can_explain(self):
224+
with self.store.open_session() as session:
225+
session.store(User(name="Fitzchak"))
226+
session.store(User(name="Arek"))
227+
session.save_changes()
228+
229+
with self.store.open_session() as session:
230+
231+
def _stats_callback(qs: QueryStatistics) -> None:
232+
stats = qs
233+
234+
users = list(session.query(object_type=User).statistics(_stats_callback).where_equals("name", "Arek"))
235+
users = list(session.query(object_type=User).statistics(_stats_callback).where_greater_than("age", 10))
236+
237+
index_query = IndexQuery("from users")
238+
command = ExplainQueryCommand(self.store.conventions, index_query)
239+
240+
self.store.get_request_executor().execute_command(command)
241+
242+
explanations = command.result
243+
self.assertEqual(1, len(explanations))
244+
self.assertIsNotNone(explanations[0].index)
245+
self.assertIsNotNone(explanations[0].reason)

0 commit comments

Comments
 (0)