|
31 | 31 | import java.util.function.Supplier;
|
32 | 32 |
|
33 | 33 | import org.neo4j.driver.v1.Record;
|
| 34 | +import org.neo4j.driver.v1.StatementResult; |
34 | 35 | import org.neo4j.driver.v1.Value;
|
35 | 36 | import org.neo4j.driver.v1.types.IsoDuration;
|
36 | 37 | import org.neo4j.driver.v1.util.Function;
|
|
43 | 44 | import static org.junit.Assume.assumeTrue;
|
44 | 45 | import static org.neo4j.driver.internal.util.ServerVersion.v3_4_0;
|
45 | 46 | import static org.neo4j.driver.v1.Values.isoDuration;
|
| 47 | +import static org.neo4j.driver.v1.Values.parameters; |
46 | 48 |
|
47 | 49 | public class TemporalTypesIT
|
48 | 50 | {
|
@@ -245,6 +247,29 @@ public void shouldSendAndReceiveRandomDuration()
|
245 | 247 | testSendAndReceiveRandomValues( TemporalUtil::randomDuration, Value::asIsoDuration );
|
246 | 248 | }
|
247 | 249 |
|
| 250 | + @Test |
| 251 | + public void shouldFormatDurationToString() |
| 252 | + { |
| 253 | + testDurationToString( 1, 0, "P0M0DT1S" ); |
| 254 | + testDurationToString( -1, 0, "P0M0DT-1S" ); |
| 255 | + |
| 256 | + testDurationToString( 0, 5, "P0M0DT0.000000005S" ); |
| 257 | + testDurationToString( 0, -5, "P0M0DT-0.000000005S" ); |
| 258 | + testDurationToString( 0, 999_999_999, "P0M0DT0.999999999S" ); |
| 259 | + testDurationToString( 0, -999_999_999, "P0M0DT-0.999999999S" ); |
| 260 | + |
| 261 | + testDurationToString( 1, 5, "P0M0DT1.000000005S" ); |
| 262 | + testDurationToString( -1, -5, "P0M0DT-1.000000005S" ); |
| 263 | + testDurationToString( 1, -5, "P0M0DT0.999999995S" ); |
| 264 | + testDurationToString( -1, 5, "P0M0DT-0.999999995S" ); |
| 265 | + testDurationToString( 1, 999999999, "P0M0DT1.999999999S" ); |
| 266 | + testDurationToString( -1, -999999999, "P0M0DT-1.999999999S" ); |
| 267 | + testDurationToString( 1, -999999999, "P0M0DT0.000000001S" ); |
| 268 | + testDurationToString( -1, 999999999, "P0M0DT-0.000000001S" ); |
| 269 | + |
| 270 | + testDurationToString( -78036, -143000000, "P0M0DT-78036.143000000S" ); |
| 271 | + } |
| 272 | + |
248 | 273 | private <T> void testSendAndReceiveRandomValues( Supplier<T> supplier, Function<Value,T> converter )
|
249 | 274 | {
|
250 | 275 | for ( int i = 0; i < RANDOM_VALUES_TO_TEST; i++ )
|
@@ -274,6 +299,13 @@ private <T> void testSendAndReceiveValue( T value, Function<Value,T> converter )
|
274 | 299 | assertEquals( value, converter.apply( record.get( 0 ) ) );
|
275 | 300 | }
|
276 | 301 |
|
| 302 | + private void testDurationToString( long seconds, int nanoseconds, String expectedValue ) |
| 303 | + { |
| 304 | + StatementResult result = session.run( "RETURN duration({seconds: $s, nanoseconds: $n})", parameters( "s", seconds, "n", nanoseconds ) ); |
| 305 | + IsoDuration duration = result.single().get( 0 ).asIsoDuration(); |
| 306 | + assertEquals( expectedValue, duration.toString() ); |
| 307 | + } |
| 308 | + |
277 | 309 | private static IsoDuration newDuration( long months, long days, long seconds, int nanoseconds )
|
278 | 310 | {
|
279 | 311 | return isoDuration( months, days, seconds, nanoseconds ).asIsoDuration();
|
|
0 commit comments