|
| 1 | +import unittest |
| 2 | + |
| 3 | +from ravendb.documents.commands.batches import CommandType, CountersBatchCommandData |
| 4 | +from ravendb.documents.operations.counters import CounterOperation, CounterOperationType |
1 | 5 | from ravendb.documents.session.misc import SessionOptions, TransactionMode |
2 | 6 | from ravendb.infrastructure.entities import User |
3 | 7 | from ravendb.tests.test_base import TestBase |
@@ -74,3 +78,64 @@ def test_session_sequence(self): |
74 | 78 | user1.age = 10 |
75 | 79 | session.store(user1, "users/1") |
76 | 80 | session.save_changes() |
| 81 | + |
| 82 | + def test_throw_on_unsupported_operations(self): |
| 83 | + session_options = SessionOptions( |
| 84 | + transaction_mode=TransactionMode.CLUSTER_WIDE, |
| 85 | + disable_atomic_document_writes_in_cluster_wide_transaction=True, |
| 86 | + ) |
| 87 | + |
| 88 | + with self.store.open_session(session_options=session_options) as session: |
| 89 | + from ravendb.documents.session.document_session_operations.in_memory_document_session_operations import ( |
| 90 | + InMemoryDocumentSessionOperations, |
| 91 | + ) |
| 92 | + |
| 93 | + counter_op = CounterOperation("likes", CounterOperationType.INCREMENT, 1) |
| 94 | + counter_cmd = CountersBatchCommandData("docs/1", counter_op) |
| 95 | + |
| 96 | + save_changes_data = InMemoryDocumentSessionOperations.SaveChangesData(session) |
| 97 | + save_changes_data.session_commands.append(counter_cmd) |
| 98 | + |
| 99 | + with self.assertRaises(ValueError) as ctx: |
| 100 | + session.validate_cluster_transaction(save_changes_data) |
| 101 | + |
| 102 | + self.assertIn("not supported", str(ctx.exception)) |
| 103 | + |
| 104 | + def test_compare_exchange_double_create_raises(self): |
| 105 | + session_options = SessionOptions( |
| 106 | + transaction_mode=TransactionMode.CLUSTER_WIDE, |
| 107 | + disable_atomic_document_writes_in_cluster_wide_transaction=True, |
| 108 | + ) |
| 109 | + |
| 110 | + with self.store.open_session(session_options=session_options) as session: |
| 111 | + session.advanced.cluster_transaction.create_compare_exchange_value("users/emails/john", "john@doe.com") |
| 112 | + |
| 113 | + with self.assertRaises(RuntimeError): |
| 114 | + session.advanced.cluster_transaction.create_compare_exchange_value("users/emails/john", "other@doe.com") |
| 115 | + |
| 116 | + |
| 117 | +class TestClusterTransactionValidation(unittest.TestCase): |
| 118 | + def test_cluster_tx_rejects_unsupported_command_types(self): |
| 119 | + import inspect |
| 120 | + from ravendb.documents.session.document_session_operations.in_memory_document_session_operations import ( |
| 121 | + InMemoryDocumentSessionOperations, |
| 122 | + ) |
| 123 | + |
| 124 | + src = inspect.getsource(InMemoryDocumentSessionOperations.validate_cluster_transaction) |
| 125 | + self.assertNotIn( |
| 126 | + "== CommandType.PUT or CommandType.DELETE", |
| 127 | + src, |
| 128 | + "Cluster TX validation uses 'x == A or B' (always True). Must use 'x in (A, B)'.", |
| 129 | + ) |
| 130 | + |
| 131 | + def test_compare_exchange_rejects_double_create(self): |
| 132 | + from ravendb.documents.operations.compare_exchange.compare_exchange import ( |
| 133 | + CompareExchangeSessionValue, |
| 134 | + CompareExchangeValueState, |
| 135 | + ) |
| 136 | + |
| 137 | + value = CompareExchangeSessionValue.__new__(CompareExchangeSessionValue) |
| 138 | + value._key = "test" |
| 139 | + value._state = CompareExchangeValueState.CREATED |
| 140 | + with self.assertRaises(RuntimeError): |
| 141 | + value._CompareExchangeSessionValue__assert_state() |
0 commit comments