Skip to content

Commit

Permalink
allow conversion from StreamPosition to StreamRevision
Browse files Browse the repository at this point in the history
  • Loading branch information
thefringeninja committed Jul 20, 2020
1 parent dccc52d commit d4848af
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/EventStore.Client.Streams/IWriteResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public interface IWriteResult {
/// <summary>
/// The version the stream is currently at.
/// </summary>
[Obsolete]
[Obsolete("Please use NextExpectedStreamRevision instead. This property will be removed in a future version.",
true)]
long NextExpectedVersion { get; }
/// <summary>
/// The <see cref="Position"/> of the <see cref="IWriteResult"/> in the transaction file.
Expand Down
16 changes: 16 additions & 0 deletions src/EventStore.Client/StreamPosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ namespace EventStore.Client {
public static StreamPosition FromInt64(long value) =>
value == -1 ? End : new StreamPosition(Convert.ToUInt64(value));

/// <summary>
/// Creates a <see cref="StreamPosition"/> from a <see cref="StreamRevision"/>.
/// </summary>
/// <param name="revision"></param>
/// <returns></returns>
public static StreamPosition FromStreamRevision(StreamRevision revision) => revision.ToUInt64() switch {
ulong.MaxValue => throw new ArgumentOutOfRangeException(nameof(revision)),
_ => new StreamPosition(revision.ToUInt64())
};

/// <summary>
/// Constructs a new <see cref="StreamPosition"/>.
/// </summary>
Expand All @@ -39,6 +49,12 @@ public StreamPosition(ulong value) {
_value = value;
}

/// <summary>
/// Advance to the next <see cref="StreamPosition"/>.
/// </summary>
/// <returns></returns>
public StreamPosition Next() => this + 1;

/// <inheritdoc />
public int CompareTo(StreamPosition other) => _value.CompareTo(other._value);

Expand Down
6 changes: 6 additions & 0 deletions src/EventStore.Client/StreamRevision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public StreamRevision(ulong value) {
_value = value;
}

/// <summary>
/// Advances the <see cref="StreamRevision"/> to the next revision.
/// </summary>
/// <returns></returns>
public StreamRevision Next() => this + 1;

/// <inheritdoc />
public int CompareTo(StreamRevision other) => _value.CompareTo(other._value);

Expand Down
24 changes: 21 additions & 3 deletions test/EventStore.Client.Tests/StreamPositionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public void AdditionOperator() {
Assert.Equal(new StreamPosition(1), 1 + sut);
}

[Fact]
public void NextReturnsExpectedResult() {
var sut = StreamPosition.Start;
Assert.Equal(sut + 1, sut.Next());
}

public static IEnumerable<object[]> AdditionOutOfBoundsCases() {
yield return new object[] {StreamPosition.End, 1};
yield return new object[] {new StreamPosition(long.MaxValue), long.MaxValue + 2UL};
Expand All @@ -59,9 +65,9 @@ public static IEnumerable<object[]> SubtractionOutOfBoundsCases() {
}

[Theory, MemberData(nameof(SubtractionOutOfBoundsCases))]
public void SubtractionOutOfBoundsThrows(StreamPosition StreamPosition, ulong operand) {
Assert.Throws<OverflowException>(() => StreamPosition - operand);
Assert.Throws<OverflowException>(() => (ulong)StreamPosition - new StreamPosition(operand));
public void SubtractionOutOfBoundsThrows(StreamPosition streamPosition, ulong operand) {
Assert.Throws<OverflowException>(() => streamPosition - operand);
Assert.Throws<OverflowException>(() => (ulong)streamPosition - new StreamPosition(operand));
}

public static IEnumerable<object[]> ArgumentOutOfRangeTestCases() {
Expand All @@ -75,6 +81,18 @@ public void ArgumentOutOfRange(ulong value) {
Assert.Equal(nameof(value), ex.ParamName);
}

[Fact]
public void FromStreamPositionEndThrows() {
Assert.Throws<ArgumentOutOfRangeException>(() => StreamRevision.FromStreamPosition(StreamPosition.End));
}

[Fact]
public void FromStreamPositionReturnsExpectedResult() {
var result = StreamPosition.FromStreamRevision(new StreamRevision(0));

Assert.Equal(new StreamPosition(0), result);
}

public static IEnumerable<object[]> ComparableTestCases() {
yield return new object[] {StreamPosition.Start, StreamPosition.Start, 0};
yield return new object[] {StreamPosition.Start, StreamPosition.End, -1};
Expand Down
6 changes: 6 additions & 0 deletions test/EventStore.Client.Tests/StreamRevisionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public void AdditionOperator() {
Assert.Equal(new StreamRevision(1), 1 + sut);
}

[Fact]
public void NextReturnsExpectedResult() {
var sut = new StreamRevision(0);
Assert.Equal(sut + 1, sut.Next());
}

public static IEnumerable<object[]> AdditionOutOfBoundsCases() {
yield return new object[] {new StreamRevision(long.MaxValue), long.MaxValue + 2UL};
}
Expand Down

0 comments on commit d4848af

Please sign in to comment.