Skip to content

Commit

Permalink
Merge pull request #74 from neo4j/fix-null-stats
Browse files Browse the repository at this point in the history
Return an empty stats if no stat is included in summary
  • Loading branch information
Zhen Li committed Nov 18, 2015
2 parents 378484b + 2cfd5cf commit 4a58e4d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 7 deletions.
10 changes: 4 additions & 6 deletions driver/src/main/java/org/neo4j/driver/ResultSummary.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
* The result summary of running a statement. The result summary interface can be used to investigate
* details about the result, like the type of query run, how many and which kinds of updates have been executed,
* and query plan and profiling information if available.
* <p>
*
* The result summary is only available after all result records have been consumed.
* <p>
*
* Keeping the result summary around does not influence the lifecycle of any associated session and/or transaction.
*/
public interface ResultSummary
Expand Down Expand Up @@ -59,8 +59,7 @@ public interface ResultSummary
/**
* This describes how the database will execute your statement.
*
* @throws IllegalStateException if {@link #hasPlan()} is false
* @return statement plan for the executed statement if available
* @return statement plan for the executed statement if available, otherwise null
*/
Plan plan();

Expand All @@ -71,8 +70,7 @@ public interface ResultSummary
* information about what each step of the plan did. That more in-depth version of the statement plan becomes
* available here.
*
* @throws IllegalStateException if {@link #hasProfile()} is false
* @return profiled statement plan for the executed statement if available
* @return profiled statement plan for the executed statement if available, otherwise null
*/
ProfiledPlan profile();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

public class SimpleUpdateStatistics implements UpdateStatistics
{
public static final SimpleUpdateStatistics EMPTY_STATS =
new SimpleUpdateStatistics( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
private final int nodesCreated;
private final int nodesDeleted;
private final int relationshipsCreated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import org.neo4j.driver.exceptions.ClientException;
import org.neo4j.driver.internal.spi.StreamCollector;

import static org.neo4j.driver.internal.summary.SimpleUpdateStatistics.EMPTY_STATS;

public class SummaryBuilder implements StreamCollector
{
private final Statement statement;
Expand Down Expand Up @@ -136,7 +138,7 @@ public Statement statement()
@Override
public UpdateStatistics updateStatistics()
{
return statistics;
return statistics == null ? EMPTY_STATS : statistics;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright (c) 2002-2015 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.driver.internal;

import org.junit.Test;

import org.neo4j.driver.ResultSummary;
import org.neo4j.driver.Statement;
import org.neo4j.driver.UpdateStatistics;
import org.neo4j.driver.internal.summary.SimpleUpdateStatistics;
import org.neo4j.driver.internal.summary.SummaryBuilder;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;

public class SummaryBuilderTest
{
@Test
public void shouldReturnEmptyStatisticsIfNotProvided() throws Throwable
{
// Given
SummaryBuilder builder = new SummaryBuilder( mock( Statement.class ) );

// When
ResultSummary summary = builder.build();
UpdateStatistics stats = summary.updateStatistics();

// Then
assertEquals( stats, new SimpleUpdateStatistics( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ) );
}


@Test
public void shouldReturnNullIfNoPlanProfileProvided() throws Throwable
{
// Given
SummaryBuilder builder = new SummaryBuilder( mock( Statement.class ) );

// When
ResultSummary summary = builder.build();

// Then
assertThat( summary.hasPlan(), equalTo( false ) );
assertThat( summary.hasProfile(), equalTo( false ) );
assertNull( summary.plan() );
assertNull( summary.profile() );
}


@Test
public void shouldReturnNullIfNoStatementTypeProvided() throws Throwable
{
// Given
SummaryBuilder builder = new SummaryBuilder( mock( Statement.class ) );

// When
ResultSummary summary = builder.build();

// Then
assertNull( summary.statementType() );
}
}

0 comments on commit 4a58e4d

Please sign in to comment.