public void EqualityAndComparable() { TimeUnitExpression oneAndHalfYear = "1.5y"; TimeUnitExpression twoWeeks = TimeSpan.FromDays(14); TimeUnitExpression twoDays = 1000 * 60 * 60 * 24 * 2; /** * Milliseconds are calculated even when values are not passed as long */ oneAndHalfYear.Milliseconds.Should().BeGreaterThan(1); twoWeeks.Milliseconds.Should().BeGreaterThan(1); /** * This allows you to do comparisons on the expressions */ oneAndHalfYear.Should().BeGreaterThan(twoWeeks); (oneAndHalfYear > twoWeeks).Should().BeTrue(); (oneAndHalfYear >= twoWeeks).Should().BeTrue(); (twoDays >= new TimeUnitExpression("2d")).Should().BeTrue(); twoDays.Should().BeLessThan(twoWeeks); (twoDays < twoWeeks).Should().BeTrue(); (twoDays <= twoWeeks).Should().BeTrue(); (twoDays <= new TimeUnitExpression("2d")).Should().BeTrue(); /** * And assert equality */ twoDays.Should().Be(new TimeUnitExpression("2d")); (twoDays == new TimeUnitExpression("2d")).Should().BeTrue(); (twoDays != new TimeUnitExpression("2.1d")).Should().BeTrue(); (new TimeUnitExpression("2.1d") == new TimeUnitExpression(TimeSpan.FromDays(2.1))).Should().BeTrue(); }
/** # Time units * Whenever durations need to be specified, eg for a timeout parameter, the duration can be specified * as a whole number representing time in milliseconds, or as a time value like `2d` for 2 days. * * ## Using Time units in NEST * NEST uses `TimeUnitExpression` to strongly type this and there are several ways to construct one. * * ### Constructor * The most straight forward way to construct a `TimeUnitExpression` is through its constructor */ [U] public void Constructor() { var unitString = new TimeUnitExpression("2d"); var unitComposed = new TimeUnitExpression(2, Nest.TimeUnit.Day); var unitTimeSpan = new TimeUnitExpression(TimeSpan.FromDays(2)); var unitMilliseconds = new TimeUnitExpression(1000 * 60 * 60 * 24 * 2); /** * When serializing TimeUnitExpression constructed from a string, composition of factor and interval, or a `TimeSpan` * the expression will be serialized as time unit string */ Expect("2d") .WhenSerializing(unitString) .WhenSerializing(unitComposed) .WhenSerializing(unitTimeSpan); /** * When constructed from a long representing milliseconds, a long will be serialized */ Expect(172800000).WhenSerializing(unitMilliseconds); /** * Milliseconds are always calculated even when not using the constructor that takes a long */ unitMilliseconds.Milliseconds.Should().Be(1000 * 60 * 60 * 24 * 2); unitComposed.Milliseconds.Should().Be(1000 * 60 * 60 * 24 * 2); unitTimeSpan.Milliseconds.Should().Be(1000 * 60 * 60 * 24 * 2); unitString.Milliseconds.Should().Be(1000 * 60 * 60 * 24 * 2); }
public void ImplicitConversion() { TimeUnitExpression oneAndHalfYear = "1.5y"; TimeUnitExpression twoWeeks = TimeSpan.FromDays(14); TimeUnitExpression twoDays = 1000 * 60 * 60 * 24 * 2; Expect("1.5y").WhenSerializing(oneAndHalfYear); Expect("2w").WhenSerializing(twoWeeks); Expect(172800000).WhenSerializing(twoDays); }