|
1 | 1 | from typing import Type |
2 | 2 |
|
3 | 3 | from ravendb import SorterDefinition, DocumentStore, RawDocumentQuery, DocumentQuery |
4 | | -from ravendb.documents.operations.sorters import PutSortersOperation |
| 4 | +from ravendb.documents.operations.sorters import PutSortersOperation, DeleteSorterOperation |
5 | 5 | from ravendb.exceptions.raven_exceptions import RavenException |
6 | 6 | from ravendb.infrastructure.orders import Company |
7 | 7 | from ravendb.tests.test_base import TestBase |
@@ -76,7 +76,9 @@ def test_can_use_custom_sorter(self): |
76 | 76 |
|
77 | 77 | self._can_use_sorter_internal(RuntimeError, self.store, "Catch me: name:2:0:False", "Catch me: name:2:0:True") |
78 | 78 |
|
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: |
80 | 82 | with store.open_session() as session: |
81 | 83 | self.assertRaisesWithMessageContaining( |
82 | 84 | RawDocumentQuery.__iter__, |
@@ -105,3 +107,59 @@ def _can_use_sorter_internal(self, expected_class: Type[Exception], store: Docum |
105 | 107 | field="name", sorter_name_or_ordering_type="MySorter" |
106 | 108 | ), |
107 | 109 | ) |
| 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