|
| 1 | +from typing import Type |
| 2 | + |
| 3 | +from ravendb import SorterDefinition, DocumentStore, RawDocumentQuery, DocumentQuery |
| 4 | +from ravendb.documents.operations.sorters import PutSortersOperation |
| 5 | +from ravendb.exceptions.raven_exceptions import RavenException |
| 6 | +from ravendb.infrastructure.orders import Company |
| 7 | +from ravendb.tests.test_base import TestBase |
| 8 | + |
| 9 | +sorter_code = ( |
| 10 | + "using System;\n" |
| 11 | + "using System.Collections.Generic;\n" |
| 12 | + "using Lucene.Net.Index;\n" |
| 13 | + "using Lucene.Net.Search;\n" |
| 14 | + "using Lucene.Net.Store;\n" |
| 15 | + "\n" |
| 16 | + "namespace SlowTests.Data.RavenDB_8355\n" |
| 17 | + "{\n" |
| 18 | + " public class MySorter : FieldComparator\n" |
| 19 | + " {\n" |
| 20 | + " private readonly string _args;\n" |
| 21 | + "\n" |
| 22 | + " public MySorter(string fieldName, int numHits, int sortPos, bool reversed, List<string> diagnostics)\n" |
| 23 | + " {\n" |
| 24 | + ' _args = $"{fieldName}:{numHits}:{sortPos}:{reversed}";\n' |
| 25 | + " }\n" |
| 26 | + "\n" |
| 27 | + " public override int Compare(int slot1, int slot2)\n" |
| 28 | + " {\n" |
| 29 | + ' throw new InvalidOperationException($"Catch me: {_args}");\n' |
| 30 | + " }\n" |
| 31 | + "\n" |
| 32 | + " public override void SetBottom(int slot)\n" |
| 33 | + " {\n" |
| 34 | + ' throw new InvalidOperationException($"Catch me: {_args}");\n' |
| 35 | + " }\n" |
| 36 | + "\n" |
| 37 | + " public override int CompareBottom(int doc, IState state)\n" |
| 38 | + " {\n" |
| 39 | + ' throw new InvalidOperationException($"Catch me: {_args}");\n' |
| 40 | + " }\n" |
| 41 | + "\n" |
| 42 | + " public override void Copy(int slot, int doc, IState state)\n" |
| 43 | + " {\n" |
| 44 | + ' throw new InvalidOperationException($"Catch me: {_args}");\n' |
| 45 | + " }\n" |
| 46 | + "\n" |
| 47 | + " public override void SetNextReader(IndexReader reader, int docBase, IState state)\n" |
| 48 | + " {\n" |
| 49 | + ' throw new InvalidOperationException($"Catch me: {_args}");\n' |
| 50 | + " }\n" |
| 51 | + "\n" |
| 52 | + ' public override IComparable this[int slot] => throw new InvalidOperationException($"Catch me: {_args}");\n' |
| 53 | + " }\n" |
| 54 | + "}\n" |
| 55 | +) |
| 56 | + |
| 57 | + |
| 58 | +class TestRavenDB8355(TestBase): |
| 59 | + def setUp(self): |
| 60 | + super().setUp() |
| 61 | + |
| 62 | + def test_can_use_custom_sorter(self): |
| 63 | + sorter_definition = SorterDefinition("MySorter", sorter_code) |
| 64 | + operation = PutSortersOperation(sorter_definition) |
| 65 | + |
| 66 | + self.store.maintenance.send(operation) |
| 67 | + |
| 68 | + with self.store.open_session() as session: |
| 69 | + company1 = Company(name="C1") |
| 70 | + session.store(company1) |
| 71 | + |
| 72 | + company2 = Company(name="C2") |
| 73 | + session.store(company2) |
| 74 | + |
| 75 | + session.save_changes() |
| 76 | + |
| 77 | + self._can_use_sorter_internal(RuntimeError, self.store, "Catch me: name:2:0:False", "Catch me: name:2:0:True") |
| 78 | + |
| 79 | + def _can_use_sorter_internal(self, expected_class: Type[Exception], store: DocumentStore, asc: str, desc: str) -> None: |
| 80 | + with store.open_session() as session: |
| 81 | + self.assertRaisesWithMessageContaining( |
| 82 | + RawDocumentQuery.__iter__, |
| 83 | + expected_class, |
| 84 | + asc, |
| 85 | + session.advanced.raw_query("from Companies order by custom(name, 'MySorter')"), |
| 86 | + ) |
| 87 | + self.assertRaisesWithMessageContaining( |
| 88 | + DocumentQuery.__iter__, |
| 89 | + expected_class, |
| 90 | + asc, |
| 91 | + session.query(object_type=Company).order_by(field="name", sorter_name_or_ordering_type="MySorter"), |
| 92 | + ) |
| 93 | + |
| 94 | + self.assertRaisesWithMessageContaining( |
| 95 | + RawDocumentQuery.__iter__, |
| 96 | + expected_class, |
| 97 | + desc, |
| 98 | + session.advanced.raw_query("from Companies order by custom(name, 'MySorter') desc"), |
| 99 | + ) |
| 100 | + self.assertRaisesWithMessageContaining( |
| 101 | + DocumentQuery.__iter__, |
| 102 | + expected_class, |
| 103 | + desc, |
| 104 | + session.query(object_type=Company).order_by_descending( |
| 105 | + field="name", sorter_name_or_ordering_type="MySorter" |
| 106 | + ), |
| 107 | + ) |
0 commit comments