Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add patch implementation for Increment decimal #3479

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 18 additions & 0 deletions src/PatchingTests/Patching/patching_api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Weasel.Core;
using Weasel.Postgresql.SqlGeneration;
using Xunit;
using Xunit.Abstractions;

namespace PatchingTests.Patching;

Expand Down Expand Up @@ -264,6 +265,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
Loading