diff --git a/ravendb/documents/operations/counters.py b/ravendb/documents/operations/counters.py index 14fab8ee..ba7b3f01 100644 --- a/ravendb/documents/operations/counters.py +++ b/ravendb/documents/operations/counters.py @@ -35,7 +35,7 @@ def __init__( if not counter_operation_type: raise ValueError(f"Missing counter_operation_type property in counter {counter_name}") if delta is None and counter_operation_type == CounterOperationType.INCREMENT: - raise ValueError(f"Missing delta property in counter {counter_name} of type {counter_operation_type}") + delta = 1 self.counter_name = counter_name self.delta = delta self.counter_operation_type = counter_operation_type diff --git a/ravendb/tests/counters_tests/test_counter_increment_default_delta.py b/ravendb/tests/counters_tests/test_counter_increment_default_delta.py new file mode 100644 index 00000000..d7a2c2af --- /dev/null +++ b/ravendb/tests/counters_tests/test_counter_increment_default_delta.py @@ -0,0 +1,58 @@ +""" +Counter increment: omitting delta when incrementing defaults to delta=1. + +C# reference: SlowTests.Issues/Issues/RavenDB_22150.cs + IncrementByDefaultValue +""" + +from ravendb.documents.operations.counters import ( + CounterBatch, + CounterBatchOperation, + CounterOperation, + CounterOperationType, + DocumentCountersOperation, +) +from ravendb.infrastructure.entities import User +from ravendb.tests.test_base import TestBase + + +class TestRavenDB22150(TestBase): + def setUp(self): + super().setUp() + + def test_increment_without_delta_defaults_to_one(self): + """ + CounterOperation(INCREMENT) with no delta argument defaults to delta=1 + on the client, matching the C# client's permissive default. + + C# spec: IncrementByDefaultValue — sends a batch with two operations: + one with an explicit delta and one with no delta. The no-delta + operation should increment by 1. + """ + with self.store.open_session() as session: + session.store(User(name="Danielle"), "users/1") + session.save_changes() + + with self.store.open_session() as session: + session.counters_for("users/1").increment("likes", 10) + session.counters_for("users/1").increment("dislikes", 20) + session.save_changes() + + # Single batch: explicit delta=5 for likes, no delta for dislikes (defaults to 1). + result = self.store.operations.send( + CounterBatchOperation( + CounterBatch( + documents=[ + DocumentCountersOperation( + document_id="users/1", + operations=[ + CounterOperation("likes", CounterOperationType.INCREMENT, delta=5), + CounterOperation("dislikes", CounterOperationType.INCREMENT), + ], + ) + ] + ) + ) + ) + + self.assertEqual(21, result.counters[1].total_value)