Skip to content

Commit

Permalink
Add patch implementation for Increment decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticmind committed Oct 14, 2024
1 parent 2adb3a5 commit 5769c5a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Marten/Patching/IPatchExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ public interface IPatchExpression<T>
/// <returns></returns>
IPatchExpression<T> Increment(Expression<Func<T, float>> expression, float increment = 1);

/// <summary>
/// Increment a single field or property by adding the increment value
/// to the persisted value
/// </summary>
/// <param name="expression"></param>
/// <param name="increment"></param>
/// <returns></returns>
IPatchExpression<T> Increment(Expression<Func<T, decimal>> expression, decimal increment = 1);

/// <summary>
/// Append an element to the end of a child collection on the persisted
/// document
Expand Down
11 changes: 11 additions & 0 deletions src/Marten/Patching/PatchExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ public IPatchExpression<T> Increment(Expression<Func<T, float>> expression, floa
_patchSet.Add(new PatchData(Items: patch, false));
return this;
}

public IPatchExpression<T> Increment(Expression<Func<T, decimal>> expression, decimal increment = 1)
{
var patch = new Dictionary<string, object>();
patch.Add("type", "increment_float");
patch.Add("increment", increment);
patch.Add("path", toPath(expression));
_patchSet.Add(new PatchData(Items: patch, false));
return this;
}

//TODO NRT - Annotations are currently inaccurate here due to lack of null guards. Replace with guards in .NET 6+
public IPatchExpression<T> Append<TElement>(Expression<Func<T, IEnumerable<TElement>>> expression, TElement element)
{
Expand Down
30 changes: 30 additions & 0 deletions src/PatchingTests/Patching/PatchExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,36 @@ public void increment_float_with_explicit_interval()
_expression.Patch["increment"].ShouldBe(5);
}

[Fact]
public void increment_decimal_with_default()
{
_expression.Increment(x => x.Decimal);

_expression.Patch["path"].ShouldBe("Decimal");
_expression.Patch["type"].ShouldBe("increment_float");
_expression.Patch["increment"].ShouldBe(1);
}

[Fact]
public void increment_decimal_with_default_deep()
{
_expression.Increment(x => x.Inner.Inner.Decimal);

_expression.Patch["path"].ShouldBe("Inner.Inner.Decimal");
_expression.Patch["type"].ShouldBe("increment_float");
_expression.Patch["increment"].ShouldBe(1);
}

[Fact]
public void increment_decimal_with_explicit_interval()
{
_expression.Increment(x => x.Decimal, 5);

_expression.Patch["path"].ShouldBe("Decimal");
_expression.Patch["type"].ShouldBe("increment_float");
_expression.Patch["increment"].ShouldBe(5);
}

[Fact]
public void append_shallow()
{
Expand Down
23 changes: 22 additions & 1 deletion src/PatchingTests/Patching/patching_api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
using Weasel.Core;
using Weasel.Postgresql.SqlGeneration;
using Xunit;
using Xunit.Abstractions;

namespace PatchingTests.Patching;

public class patching_api: OneOffConfigurationsContext
{
public patching_api()
private readonly ITestOutputHelper _testOutputHelper;

public patching_api(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
StoreOptions(_ =>
{
_.UseDefaultSerialization(EnumStorage.AsString);
Expand Down Expand Up @@ -264,6 +268,23 @@ public void increment_for_float()
}
}

[Fact]
public void increment_for_decimal()
{
var target = Target.Random();
target.Decimal = 11.2m;

theSession.Store(target);
theSession.Patch<Target>(target.Id).Increment(x => x.Decimal, 2.4m);
theSession.SaveChanges();
theSession.SaveChanges();

using (var query = theStore.QuerySession())
{
query.Load<Target>(target.Id).Decimal.ShouldBe(13.6m);
}
}

[Fact]
public void append_to_a_primitive_array()
{
Expand Down

0 comments on commit 5769c5a

Please sign in to comment.