Skip to content

Commit

Permalink
Extending DateTimeValueTest
Browse files Browse the repository at this point in the history
  • Loading branch information
sherfert authored and thobe committed Jan 31, 2018
1 parent ddccbb6 commit ce5ff00
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 33 deletions.
Expand Up @@ -415,7 +415,7 @@ <Input> void assign( Builder<Input,?> builder, Input value )
{
if ( builder.timezone != null )
{
throw new IllegalArgumentException( "cannot assign timezone twice" );
throw new IllegalArgumentException( "Cannot assign timezone twice." );
}
builder.timezone = value;
}
Expand Down Expand Up @@ -526,19 +526,19 @@ private static final class SelectDateTime<Input> extends BuilderState<Input>
@Override
BuilderState<Input> assign( Field field, Input value )
{
throw new IllegalArgumentException( "cannot assign " + field + " when selecting datetime" );
throw new IllegalArgumentException( "Cannot assign " + field + " when selecting datetime." );
}

@Override
BuilderState<Input> date( Input date )
{
throw new IllegalArgumentException( "cannot select date when selecting datetime" );
throw new IllegalArgumentException( "Cannot select date when selecting datetime." );
}

@Override
BuilderState<Input> time( Input time )
{
throw new IllegalArgumentException( "cannot select time when selecting datetime" );
throw new IllegalArgumentException( "Cannot select time when selecting datetime." );
}

@Override
Expand Down
Expand Up @@ -19,18 +19,22 @@
*/
package org.neo4j.values.storable;

import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;

import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

import org.junit.Rule;
import org.junit.Test;

import static java.time.ZoneOffset.UTC;
import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.StringStartsWith.startsWith;
import static org.junit.Assert.assertEquals;
import static org.neo4j.values.storable.AssertingStructureBuilder.asserting;
import static org.neo4j.values.storable.DateTimeValue.builder;
Expand All @@ -46,6 +50,7 @@
import static org.neo4j.values.storable.TimeValueTest.inUTC;
import static org.neo4j.values.storable.TimeValueTest.orFail;
import static org.neo4j.values.storable.Values.stringValue;
import static org.neo4j.values.utils.AnyValueTestUtil.assertThrows;

public class DateTimeValueTest
{
Expand All @@ -64,6 +69,65 @@ public void shouldParseDateTime() throws Exception
assertEquals(
datetime( date( 2017, 12, 17 ), time( 17, 14, 35, 123456789, UTC ) ),
parse( "2017-12-17T17:14:35.123456789+0000", orFail ) );
assertEquals(
datetime( date( 10000, 12, 17 ), time( 17, 14, 35, 123456789, UTC ) ),
parse( "+10000-12-17T17:14:35.123456789+0000", orFail ) );
assertEquals(
datetime( date( -1, 12, 17 ), time( 17, 14, 35, 123456789, UTC ) ),
parse( "-1-12-17T17:14:35.123456789+0000", orFail ) );
}

@Ignore
public void shouldSupportLeadSeconds() throws Exception
{
// Leap second according to https://www.timeanddate.com/time/leap-seconds-future.html
assertEquals( datetime( 2016, 12, 31, 23, 59, 60, 0, UTC ), parse( "2016-12-31T23:59:60Z", orFail ) );
}

@Test
public void shouldRejectInvalidDateTimeString() throws Exception
{
// Wrong year
assertThrows( DateTimeException.class, () -> parse( "10000-12-17T17:14:35", inUTC ) );
assertThrows( DateTimeException.class, () -> parse( "10000-12-17T17:14:35Z", orFail ) );

// Wrong month
assertThat( assertThrows( DateTimeException.class, () -> parse( "2017-13-17T17:14:35", inUTC ) ).getMessage(),
startsWith( "Invalid value for MonthOfYear" ) );
assertThat( assertThrows( DateTimeException.class, () -> parse( "2017-00-17T17:14:35", inUTC ) ).getMessage(),
startsWith( "Invalid value for MonthOfYear" ) );
assertThat( assertThrows( DateTimeException.class, () -> parse( "2017-13-17T17:14:35Z", orFail ) ).getMessage(),
startsWith( "Invalid value for MonthOfYear" ) );
assertThat( assertThrows( DateTimeException.class, () -> parse( "2017-00-17T17:14:35Z", orFail ) ).getMessage(),
startsWith( "Invalid value for MonthOfYear" ) );

// Wrong day of month
assertThat( assertThrows( DateTimeException.class, () -> parse( "2017-12-32T17:14:35", inUTC ) ).getMessage(),
startsWith( "Invalid value for DayOfMonth" ) );
assertThat( assertThrows( DateTimeException.class, () -> parse( "2017-12-00T17:14:35", inUTC ) ).getMessage(),
startsWith( "Invalid value for DayOfMonth" ) );
assertThat( assertThrows( DateTimeException.class, () -> parse( "2017-12-32T17:14:35Z", orFail ) ).getMessage(),
startsWith( "Invalid value for DayOfMonth" ) );
assertThat( assertThrows( DateTimeException.class, () -> parse( "2017-12-00T17:14:35Z", orFail ) ).getMessage(),
startsWith( "Invalid value for DayOfMonth" ) );

// Wrong hour
assertThat( assertThrows( DateTimeException.class, () -> parse( "2017-12-17T24:14:35", inUTC ) ).getMessage(),
startsWith( "Invalid value for HourOfDay" ) );
assertThat( assertThrows( DateTimeException.class, () -> parse( "2017-12-17T24:14:35Z", orFail ) ).getMessage(),
startsWith( "Invalid value for HourOfDay" ) );

// Wrong minute
assertThat( assertThrows( DateTimeException.class, () -> parse( "2017-12-17T17:60:35", inUTC ) ).getMessage(),
startsWith( "Invalid value for MinuteOfHour" ) );
assertThat( assertThrows( DateTimeException.class, () -> parse( "2017-12-17T17:60:35Z", orFail ) ).getMessage(),
startsWith( "Invalid value for MinuteOfHour" ) );

// Wrong second
assertThat( assertThrows( DateTimeException.class, () -> parse( "2017-12-17T17:14:61", inUTC ) ).getMessage(),
startsWith( "Invalid value for SecondOfMinute" ) );
assertThat( assertThrows( DateTimeException.class, () -> parse( "2017-12-17T17:14:61Z", orFail ) ).getMessage(),
startsWith( "Invalid value for SecondOfMinute" ) );
}

@Test
Expand Down Expand Up @@ -168,12 +232,51 @@ public void shouldRejectInvalidFieldCombinations() throws Exception
.add( "month", 12 )
.add( "dayOfWeek", 5 )
.assertThrows( IllegalArgumentException.class, "Cannot assign dayOfWeek to calendar date." );
// TODO: more assertions
asserting( fromValues( builder( clock ) ) )
.add( "datetime", localDateTime( LocalDateTime.now( clock ) ) )
.add( "day", 12 )
.assertThrows( IllegalArgumentException.class, "Cannot assign day when selecting datetime." );
asserting( fromValues( builder( clock ) ) )
.add( "datetime", localDateTime( LocalDateTime.now( clock ) ) )
.add( "hour", 12 )
.assertThrows( IllegalArgumentException.class, "Cannot assign hour when selecting datetime." );
asserting( fromValues( builder( clock ) ) )
.add( "year", 2018 )
.add( "week", 12 )
.add( "day", 12 )
.assertThrows( IllegalArgumentException.class, "Cannot assign day to week date." );
asserting( fromValues( builder( clock ) ) )
.add( "year", 2018 )
.add( "ordinalDay", 12 )
.add( "dayOfWeek", 1 )
.assertThrows( IllegalArgumentException.class, "Cannot assign dayOfWeek to ordinal date." );
asserting( fromValues( builder( clock ) ) )
.add( "year", 2018 )
.add( "month", 12 )
.add( "day", 5 )
.add( "hour", 5 )
.add( "minute", 5 )
.add( "second", 5 )
.add( "millisecond", 1 )
.add( "nanosecond", 1 )
.assertThrows( IllegalArgumentException.class, "Cannot assign ..." );
}

@Test
public void shouldRejectInvalidComponentValues() throws Exception
{
// TODO: add assertions
asserting( fromValues( builder( clock ) ) )
.add( "year", 2018 )
.add( "moment", 12 )
.assertThrows( IllegalArgumentException.class, "No such field: moment" );
asserting( fromValues( builder( clock ) ) )
.add( "year", 2018 )
.add( "month", 12 )
.add( "day", 5 )
.add( "hour", 5 )
.add( "minute", 5 )
.add( "second", 5 )
.add( "picosecond", 12 )
.assertThrows( IllegalArgumentException.class, "No such field: picosecond" );
}
}
Expand Up @@ -19,15 +19,14 @@
*/
package org.neo4j.values.storable;

import org.junit.Test;

import java.time.DateTimeException;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.IsoFields;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

import org.junit.Test;

import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
Expand All @@ -39,6 +38,7 @@
import static org.neo4j.values.storable.DateValue.parse;
import static org.neo4j.values.storable.DateValue.quarterDate;
import static org.neo4j.values.storable.DateValue.weekDate;
import static org.neo4j.values.utils.AnyValueTestUtil.assertThrows;

@SuppressWarnings( "ThrowableNotThrown" )
public class DateValueTest
Expand Down Expand Up @@ -205,25 +205,4 @@ private DateTimeException assertCannotParse( String text )
}
throw new AssertionError( String.format( "'%s' parsed to %s", text, value ) );
}

private static <X extends Exception, T> X assertThrows( Class<X> exception, Supplier<T> thunk )
{
T value;
try
{
value = thunk.get();
}
catch ( Exception e )
{
if ( exception.isInstance( e ) )
{
return exception.cast( e );
}
else
{
throw new AssertionError( "Expected " + exception.getName(), e );
}
}
throw new AssertionError( "Expected " + exception.getName() + " but returned: " + value );
}
}
Expand Up @@ -19,6 +19,8 @@
*/
package org.neo4j.values.utils;

import java.util.function.Supplier;

import org.neo4j.values.AnyValue;

import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -68,4 +70,25 @@ public static void assertIncomparable( AnyValue a, AnyValue b )
assertNull( a + " should be incomparable to " + b, a.ternaryEquals( b ) );
assertNull( b + " should be incomparable to " + a, b.ternaryEquals( a ) );
}

public static <X extends Exception, T> X assertThrows( Class<X> exception, Supplier<T> thunk )
{
T value;
try
{
value = thunk.get();
}
catch ( Exception e )
{
if ( exception.isInstance( e ) )
{
return exception.cast( e );
}
else
{
throw new AssertionError( "Expected " + exception.getName(), e );
}
}
throw new AssertionError( "Expected " + exception.getName() + " but returned: " + value );
}
}

0 comments on commit ce5ff00

Please sign in to comment.