Skip to content

Commit

Permalink
Update test mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Jan 15, 2018
1 parent 647d38c commit 19aa176
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 72 deletions.
@@ -0,0 +1,56 @@
package org.neo4j.internal.kernel.api;

import java.util.Map;

import org.neo4j.values.storable.Value;

class Node
{
final long id;
private final long[] labels;
final Map<Integer,Value> properties;

Node( long id, long[] labels, Map<Integer,Value> properties )
{
this.id = id;
this.labels = labels;
this.properties = properties;
}

LabelSet labelSet()
{
return new LabelSet()
{
@Override
public int numberOfLabels()
{
return labels.length;
}

@Override
public int label( int offset )
{
return labels.length;
}

@Override
public boolean contains( int labelToken )
{
for ( long label : labels )
{
if ( label == labelToken )
{
return true;
}
}
return false;
}

@Override
public long[] all()
{
return labels;
}
};
}
}
Expand Up @@ -158,54 +158,4 @@ public boolean isClosed()
return false;
}

static class Node
{
final long id;
final long[] labels;
final Map<Integer,Value> properties;

Node( long id, long[] labels, Map<Integer,Value> properties )
{
this.id = id;
this.labels = labels;
this.properties = properties;
}

LabelSet labelSet()
{
return new LabelSet()
{
@Override
public int numberOfLabels()
{
return labels.length;
}

@Override
public int label( int offset )
{
return labels.length;
}

@Override
public boolean contains( int labelToken )
{
for ( long label : labels )
{
if ( label == labelToken )
{
return true;
}
}
return false;
}

@Override
public long[] all()
{
return labels;
}
};
}
}
}
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.internal.kernel.api;

import java.util.Map;

public class StubNodeLabelIndexCursor implements NodeLabelIndexCursor
{
private int offset = -1;
private final Map<Integer, long[]> lookup;
private int label;

public StubNodeLabelIndexCursor( Map<Integer,long[]> lookup )
{
this.lookup = lookup;
}

void initalize(int label)
{
this.label = label;
this.offset = -1;
}


@Override
public void node( NodeCursor cursor )
{

}

@Override
public long nodeReference()
{
long[] nodes = lookup.get( label );
if ( nodes == null )
{
return -1L;
}

return offset >= 0 && offset < nodes.length ? nodes[offset] : -1;
}

@Override
public LabelSet labels()
{
return null;
}


@Override
public boolean next()
{
long[] nodes = lookup.get( label );
if ( nodes == null )
{
return false;
}
return ++offset < nodes.length;
}

@Override
public boolean shouldRetry()
{
return false;
}

@Override
public void close()
{

}

@Override
public boolean isClosed()
{
return false;
}
}
Expand Up @@ -40,7 +40,7 @@ public void nodeIndexScan( IndexReference index, NodeValueIndexCursor cursor, In
@Override
public void nodeLabelScan( int label, NodeLabelIndexCursor cursor )
{
throw new UnsupportedOperationException();
((StubNodeLabelIndexCursor) cursor).initalize( label );
}

@Override
Expand Down
Expand Up @@ -53,7 +53,7 @@ import org.neo4j.time.Clocks
import org.neo4j.values.AnyValue
import org.neo4j.values.storable._
import org.neo4j.values.virtual.VirtualValues.EMPTY_MAP
import org.neo4j.values.virtual.{RelationshipValue, ListValue, MapValue, NodeValue}
import org.neo4j.values.virtual.{ListValue, MapValue, NodeValue, RelationshipValue}

import scala.collection.JavaConverters._
import scala.collection.{JavaConverters, mutable}
Expand Down Expand Up @@ -1677,6 +1677,13 @@ abstract class CodeGeneratorTest extends CypherFunSuite with LogicalPlanningTest
else cursor.withNode(n.getId))
cursor
}

private def nodeLabelIndexCursor() = {
val lookup: util.Map[Integer, Array[Long]] = labelTokens.map {
case (label, token) => Int.box(token) -> nodesForLabel(label).map(_.getId).toArray
}.asJava
new StubNodeLabelIndexCursor(lookup)
}
private def propertyCursor() = new StubPropertyCursor


Expand All @@ -1687,23 +1694,16 @@ abstract class CodeGeneratorTest extends CypherFunSuite with LogicalPlanningTest
when(cursors.allocatePropertyCursor()).thenAnswer(new Answer[PropertyCursor] {
override def answer(invocation: InvocationOnMock): PropertyCursor = propertyCursor()
})
when(cursors.allocateNodeLabelIndexCursor()).thenAnswer(new Answer[NodeLabelIndexCursor] {
override def answer(invocation: InvocationOnMock): NodeLabelIndexCursor = nodeLabelIndexCursor()
})
when(transactionalContext.cursors).thenReturn(cursors)
private def read = new StubRead

when(transactionalContext.dataRead).thenAnswer(new Answer[Read] {
override def answer(invocation: InvocationOnMock): Read = read
})
when(queryContext.entityAccessor).thenReturn(nodeManager)
when(ro.nodeGetProperty(anyLong(), anyInt())).thenAnswer(new Answer[Value] {
override def answer(invocationOnMock: InvocationOnMock): Value = {
val id = invocationOnMock.getArguments()(0).asInstanceOf[Long]
if (id < 3) Values.stringValue("value")
else null
}
})
when(ro.nodesGetAll()).thenAnswer(new Answer[PrimitiveLongIterator] {
override def answer(invocationOnMock: InvocationOnMock): PrimitiveLongIterator = primitiveIterator(allNodes.map(_.getId))
})
when(ro.labelGetForName(anyString())).thenAnswer(new Answer[Int] {
override def answer(invocationOnMock: InvocationOnMock): Int = {
val label = invocationOnMock.getArguments.apply(0).asInstanceOf[String]
Expand All @@ -1716,16 +1716,6 @@ abstract class CodeGeneratorTest extends CypherFunSuite with LogicalPlanningTest
relTokens(label)
}
})
when(ro.nodesGetForLabel(anyInt())).thenAnswer(new Answer[PrimitiveLongResourceIterator] {
override def answer(invocationOnMock: InvocationOnMock): PrimitiveLongResourceIterator = {
val labelToken = invocationOnMock.getArguments.apply(0).asInstanceOf[Int]
val (label, _) = labelTokens.find {
case (l, t) => t == labelToken
}.get
val nodeIds = nodesForLabel(label).map(_.getId)
primitiveResourceIterator(null, nodeIds)
}
})
when(ro.nodeGetRelationships(anyLong(), any[Direction])).thenAnswer(new Answer[PrimitiveLongIterator] {
override def answer(invocationOnMock: InvocationOnMock): PrimitiveLongIterator = {
val node = invocationOnMock.getArguments.apply(0).asInstanceOf[Long].toInt
Expand Down

0 comments on commit 19aa176

Please sign in to comment.