Skip to content

Commit

Permalink
TestUtils: copy single node (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpollmeier authored Aug 26, 2024
1 parent 9f16c9b commit bb570af
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
12 changes: 9 additions & 3 deletions core/src/main/scala/flatgraph/Accessors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,20 @@ object Accessors {
new ISeq(vals, qty(seq), qty(seq + 1))
}

def getNodeProperties(node: GNode): IterableOnce[(String, IndexedSeq[Any])] = {
def _getNodeProperties(node: GNode): IterableOnce[(Int, IndexedSeq[Any])] = {
val schema = node.graph.schema
for {
propertyKind <- schema.propertyKinds
property = Accessors.getNodeProperty(node, propertyKind)
if property.nonEmpty
propertyLabel = schema.getPropertyLabel(node.nodeKind, propertyKind)
} yield propertyLabel -> property
} yield propertyKind -> property
}

def getNodeProperties(node: GNode): IterableOnce[(String, IndexedSeq[Any])] = {
for {
(propertyKind, value) <- _getNodeProperties(node)
schema = node.graph.schema
} yield schema.getPropertyLabel(node.nodeKind, propertyKind) -> value
}

def getInverseIndex(graph: Graph, nodeKind: Int, propertyKind: Int): MultiDictIndex[GNode] = {
Expand Down
17 changes: 17 additions & 0 deletions core/src/main/scala/flatgraph/misc/TestUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,24 @@ object TestUtils {

newGraph
}
}

/** copies a single GNode into a different graph, including all properties, but excluding edges */
def copyNode(node: GNode, targetGraph: Graph): GNode = {
assert(node.graph.schema == targetGraph.schema, "schemas of the two graphs must be identical, but they are not...")
// first pass: create raw node
val newNode = new GenericDNode(node.nodeKind)
DiffGraphApplier.applyDiff(targetGraph, new DiffGraphBuilder(targetGraph.schema).addNode(newNode))
val nodeInTargetGraph = newNode.storedRef.get // this is set by `applyDiff`

// second pass: set node properties
val diffGraphForProperties = new DiffGraphBuilder(targetGraph.schema)
for {
(propertyKind, value) <- Accessors._getNodeProperties(node)
} diffGraphForProperties._setNodeProperty(nodeInTargetGraph, propertyKind, value)

DiffGraphApplier.applyDiff(targetGraph, diffGraphForProperties)
nodeInTargetGraph
}

}
24 changes: 24 additions & 0 deletions core/src/test/scala/flatgraph/GraphTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package flatgraph
import flatgraph.TestHelpers.withTemporaryFile
import flatgraph.TestSchema.testSerialization
import flatgraph.misc.DebugDump.debugDump
import flatgraph.misc.TestUtils
import flatgraph.storage.Deserialization
import flatgraph.storage.Deserialization.DeserializationException
import flatgraph.traversal.language.*
import flatgraph.util.DiffToolTests
import flatgraph.util.DiffToolTests.sampleSchema
import org.scalatest.matchers.should.Matchers
import org.scalatest.matchers.should.Matchers.shouldBe
import org.scalatest.wordspec.AnyWordSpec
Expand Down Expand Up @@ -1091,4 +1093,26 @@ class GraphTests extends AnyWordSpec with Matchers {
// original graph should be untouched
debugDump(graph) shouldBe debugDumpOriginalGraph
}

"copy a single node (ignoring edges)" in {
val graphA = DiffToolTests.makeSampleGraph()
debugDump(graphA) shouldBe
"""#Node numbers (kindId, nnodes) (0: 2), total 2
|Node kind 0. (eid, nEdgesOut, nEdgesIn): (0, 1 [dense], 1 [dense]),
| V0_0 : 0: [A], 1: [40]
| V0_0 [0] -> (edgePropertyValue) V0_1
| V0_1 : 0: [X, Y], 1: [50, 51]
| V0_1 [0] <- (edgePropertyValue) V0_0
|""".stripMargin

val V0_1 = graphA.node(kind = 0, seq = 1)
val graphB = new Graph(sampleSchema)
TestUtils.copyNode(V0_1, graphB)

debugDump(graphB) shouldBe
"""#Node numbers (kindId, nnodes) (0: 1), total 1
|Node kind 0. (eid, nEdgesOut, nEdgesIn): (0, 0 [NA], 0 [NA]),
| V0_0 : 0: [X, Y], 1: [50, 51]
|""".stripMargin
}
}

0 comments on commit bb570af

Please sign in to comment.