Skip to content

Commit 3846d24

Browse files
committed
RDBC-780 RavenDB_8355Test::canUseCustomSorterWithOperations
1 parent 73b1293 commit 3846d24

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

ravendb/documents/operations/sorters.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from ravendb import SorterDefinition, RaftCommand, ServerNode
66
from ravendb.documents.operations.definitions import VoidMaintenanceOperation
77
from ravendb.http.raven_command import VoidRavenCommand
8+
from ravendb.tools.utils import Utils
89
from ravendb.util.util import RaftIdGenerator
910

1011
if TYPE_CHECKING:
@@ -49,3 +50,29 @@ def is_read_request(self) -> bool:
4950

5051
def get_raft_unique_request_id(self) -> str:
5152
return RaftIdGenerator.new_id()
53+
54+
55+
class DeleteSorterOperation(VoidMaintenanceOperation):
56+
def __init__(self, sorter_name: str = None):
57+
if sorter_name is None:
58+
raise ValueError("SorterName cannot be None")
59+
60+
self._sorter_name = sorter_name
61+
62+
def get_command(self, conventions: "DocumentConventions") -> "DeleteSorterOperation.DeleteSorterCommand":
63+
return self.DeleteSorterCommand(self._sorter_name)
64+
65+
class DeleteSorterCommand(VoidRavenCommand, RaftCommand):
66+
def __init__(self, sorter_name: str = None):
67+
if sorter_name is None:
68+
raise ValueError("SorterName cannot be None")
69+
70+
super().__init__()
71+
self._sorter_name = sorter_name
72+
73+
def create_request(self, node: ServerNode) -> requests.Request:
74+
url = f"{node.url}/databases/{node.database}/admin/sorters?name={Utils.quote_key(self._sorter_name)}"
75+
return requests.Request("DELETE", url)
76+
77+
def get_raft_unique_request_id(self) -> str:
78+
return RaftIdGenerator.new_id()

ravendb/tests/jvm_migrated_tests/issues_tests/test_ravenDB_8355.py

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Type
22

33
from ravendb import SorterDefinition, DocumentStore, RawDocumentQuery, DocumentQuery
4-
from ravendb.documents.operations.sorters import PutSortersOperation
4+
from ravendb.documents.operations.sorters import PutSortersOperation, DeleteSorterOperation
55
from ravendb.exceptions.raven_exceptions import RavenException
66
from ravendb.infrastructure.orders import Company
77
from ravendb.tests.test_base import TestBase
@@ -76,7 +76,9 @@ def test_can_use_custom_sorter(self):
7676

7777
self._can_use_sorter_internal(RuntimeError, self.store, "Catch me: name:2:0:False", "Catch me: name:2:0:True")
7878

79-
def _can_use_sorter_internal(self, expected_class: Type[Exception], store: DocumentStore, asc: str, desc: str) -> None:
79+
def _can_use_sorter_internal(
80+
self, expected_class: Type[Exception], store: DocumentStore, asc: str, desc: str
81+
) -> None:
8082
with store.open_session() as session:
8183
self.assertRaisesWithMessageContaining(
8284
RawDocumentQuery.__iter__,
@@ -105,3 +107,59 @@ def _can_use_sorter_internal(self, expected_class: Type[Exception], store: Docum
105107
field="name", sorter_name_or_ordering_type="MySorter"
106108
),
107109
)
110+
111+
def test_can_use_custom_sorter_with_operations(self):
112+
with self.store.open_session() as session:
113+
company1 = Company(name="C1")
114+
session.store(company1)
115+
116+
company2 = Company(name="C2")
117+
session.store(company2)
118+
119+
session.save_changes()
120+
121+
self._can_use_sorter_internal(
122+
RuntimeError,
123+
self.store,
124+
"There is no sorter with 'MySorter' name",
125+
"There is no sorter with 'MySorter' name",
126+
)
127+
128+
sorter_definition = SorterDefinition("MySorter", sorter_code)
129+
130+
operation = PutSortersOperation(sorter_definition)
131+
self.store.maintenance.send(operation)
132+
133+
# checking if we can send again same sorter
134+
self.store.maintenance.send(PutSortersOperation(sorter_definition))
135+
136+
self._can_use_sorter_internal(RuntimeError, self.store, "Catch me: name:2:0:False", "Catch me: name:2:0:True")
137+
138+
_sorter_code = sorter_code.replace("Catch me", "Catch me 2")
139+
140+
# checking if we can update sorter
141+
sorter_definition2 = SorterDefinition("MySorter", _sorter_code)
142+
self.store.maintenance.send(PutSortersOperation(sorter_definition2))
143+
144+
other_definition = SorterDefinition("MySorter_OtherName", _sorter_code)
145+
146+
# We should not be able to add sorter with non-matching name
147+
self.assertRaisesWithMessageContaining(
148+
self.store.maintenance.send,
149+
RuntimeError,
150+
"Could not find type 'MySorter_OtherName' in given assemly.",
151+
PutSortersOperation(other_definition),
152+
)
153+
154+
self._can_use_sorter_internal(
155+
RuntimeError, self.store, "Catch me 2: name:2:0:False", "Catch me 2: name:2:0:True"
156+
)
157+
158+
self.store.maintenance.send(DeleteSorterOperation("MySorter"))
159+
160+
self._can_use_sorter_internal(
161+
RuntimeError,
162+
self.store,
163+
"There is no sorter with 'MySorter' name",
164+
"There is no sorter with 'MySorter' name",
165+
)

0 commit comments

Comments
 (0)