Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup/consolidate: use generated domain for tests #183

Merged
merged 8 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -358,32 +358,33 @@ class DomainClassesGenerator(schema: Schema) {
}
}

val productElementNames = productElements.zipWithIndex
.map { case (name, index) =>
s"""case $index => "$name""""
}
.mkString("\n")
// format: off
val productElementNames = productElements.zipWithIndex.map { case (name, index) =>
s"""case $index => "$name""""
}.mkString("\n")

val productElementAccessors = productElements.zipWithIndex
.map { case (name, index) =>
s"case $index => this.$name"
}
.mkString("\n")
val productElementAccessors = productElements.zipWithIndex.map { case (name, index) =>
s"case $index => this.$name"
}.mkString("\n")

val propertyNames = nodeType.properties
.map(_.name)
.map { name =>
val camelCase = camelCaseCaps(name)
s"""val $camelCase = $basePackage.PropertyNames.$name"""
}
.mkString("\n")
val propertyNames = {
val sourceLines = Seq.newBuilder[String]
nodeType.properties.map { property =>
s"""${scaladocMaybe(property.comment)}
|val ${camelCaseCaps(property.name)} = "${property.name}" """.stripMargin
}.map(sourceLines.addOne)
nodeType.containedNodes.map { containedNode =>
s"""${scaladocMaybe(containedNode.comment)}
|val ${camelCaseCaps(containedNode.localName)} = "${containedNode.localName}" """.stripMargin
}.map(sourceLines.addOne)
sourceLines.result().mkString("")
}

val propertyDefaults = nodeType.properties
.collect {
case p if p.hasDefault =>
s"""val ${p.className} = ${Helpers.defaultValueImpl(p.default.get)}"""
}
.mkString("\n")
val propertyDefaults = nodeType.properties.collect {
case p if p.hasDefault =>
s"""val ${p.className} = ${Helpers.defaultValueImpl(p.default.get)}"""
}.mkString("\n")
// format: on

def neighborEdgeStr(es: Map[String, Set[String]]): String =
es.toSeq.sortBy(_._1).map { case (k, vs) => s"$k -> Set(${vs.toSeq.sorted.mkString(", ")})" }.mkString(", ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ object Helpers {
def quote(string: String): String =
s""""$string""""

def scaladocMaybe(comment: Option[String]): String =
comment.map(text => s"/** $text */").getOrElse("")

def typeFor[A](property: Property[A]): String = {
val isMandatory = property.isMandatory
property.valueType match {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
0,node_a,,42,,node 3 c1;node 3 c2,node 2 a,node 2 b
0,node_a,,42,,node 1 c1;node 1 c2,node 1 a,node 1 b
1,node_a,10;11;12,1,2,,<empty>,
57 changes: 0 additions & 57 deletions formats-tests/src/test/scala/flatgraph/TestDomainSimple.scala

This file was deleted.

26 changes: 26 additions & 0 deletions formats-tests/src/test/scala/flatgraph/TestGraphSimple.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package flatgraph

import flatgraph.testdomains.generic.GenericDomain
import flatgraph.testdomains.generic.edges.ConnectedTo
import flatgraph.testdomains.generic.nodes.NewNodeA

import java.nio.file.Path

object TestGraphSimple {
def create(storageMaybe: Option[Path] = None): GenericDomain = {
val genericDomain = storageMaybe match {
case None => GenericDomain.empty
case Some(storagePath) => GenericDomain.withStorage(storagePath)
}
val graph = genericDomain.graph
val node1 = NewNodeA().stringMandatory("node 1 a").stringOptional("node 1 b").stringList(Seq("node 1 c1", "node 1 c2"))
val node2 = NewNodeA().intMandatory(1).intOptional(2).intList(Seq(10, 11, 12))

DiffGraphApplier.applyDiff(
graph,
GenericDomain.newDiffGraphBuilder
.addEdge(node1, node2, ConnectedTo.Label, "edge property")
)
genericDomain
}
}
16 changes: 6 additions & 10 deletions formats-tests/src/test/scala/flatgraph/formats/dot/DotTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,27 @@ package flatgraph.formats.dot

import better.files.*
import flatgraph.*
import flatgraph.TestDomainSimple.*
import flatgraph.TestDomainSimple.PropertyNames.*
import org.scalatest.matchers.should.Matchers.*
import org.scalatest.wordspec.AnyWordSpec

class DotTests extends AnyWordSpec {

"Exporter should export valid dot" in {
val graph = newGraphSimple()
val graph = TestGraphSimple.create().graph

File.usingTemporaryDirectory(getClass.getName) { exportRootDirectory =>
val exportResult = DotExporter.runExport(graph, exportRootDirectory.pathAsString)
exportResult.nodeCount shouldBe 3
exportResult.edgeCount shouldBe 2
exportResult.nodeCount shouldBe 2
exportResult.edgeCount shouldBe 1
val Seq(exportedFile) = exportResult.files

val result = better.files.File(exportedFile).contentAsString.trim
withClue(s"actual result was: `$result`") {
result.trim shouldBe
"""digraph {
| 0[label=testNode StringProperty="<stringProp1>" StringListProperty="stringListProp1a;stringList\Prop1b" IntProperty="11" IntListProperty="21;31;41"]
| 1[label=testNode StringProperty="string"Prop2\"]
| 2[label=testNode IntProperty="13"]
| 0 -> 1 [label=testEdge property=9223372036854775807]
| 1 -> 2 [label=testEdge property=0]
| 0[label=node_a int_mandatory="42" string_list="node 1 c1;node 1 c2" string_mandatory="node 1 a" string_optional="node 1 b"]
| 1[label=node_a int_list="10;11;12" int_mandatory="1" int_optional="2" string_mandatory="<empty>"]
| 0 -> 1 [label=connected_to property="edge property"]
|}
|""".stripMargin.trim
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package flatgraph.formats.graphson

import better.files.File
import flatgraph.GenericDNode
import flatgraph.TestDomainSimple.*
import flatgraph.TestDomainSimple.PropertyNames.{ContainedTestNodeProperty, IntProperty}
import flatgraph.{DiffGraphApplier, GenericDNode, TestGraphSimple}
import flatgraph.testdomains.generic.Language.*
import flatgraph.testdomains.generic.nodes.NodeA
import flatgraph.misc.TestUtils.applyDiff
import flatgraph.testdomains.generic.GenericDomain
import flatgraph.testdomains.generic.nodes.NewNodeB
import flatgraph.util.DiffTool
import org.scalatest.matchers.should.Matchers.*
import org.scalatest.wordspec.AnyWordSpec
Expand All @@ -15,16 +17,16 @@ import scala.jdk.CollectionConverters.CollectionHasAsScala
class GraphSONTests extends AnyWordSpec {

"export to GraphSON and back" in {
val graph = newGraphSimple()
val graph = TestGraphSimple.create().graph

File.usingTemporaryDirectory(getClass.getName) { exportRootDirectory =>
val exportResult = GraphSONExporter.runExport(graph, exportRootDirectory.pathAsString)
exportResult.nodeCount shouldBe 3
exportResult.edgeCount shouldBe 2
exportResult.nodeCount shouldBe 2
exportResult.edgeCount shouldBe 1
val Seq(graphMLFile) = exportResult.files

// import graphml into new graph, use difftool for round trip of conversion
val reimported = newGraphEmpty()
val reimported = GenericDomain.empty.graph
GraphSONImporter.runImport(reimported, graphMLFile)
val diff = DiffTool.compare(graph, reimported)
withClue(
Expand All @@ -38,26 +40,23 @@ class GraphSONTests extends AnyWordSpec {
}

"using 'contained node' property" in {
val graph = newGraphEmpty()
val v0New = new GenericDNode(0)
val v1New = new GenericDNode(0)

graph.applyDiff(_.addNode(v0New).addNode(v1New))
val v0 = v0New.storedRef.get
val v1 = v1New.storedRef.get

graph.applyDiff(
_.setNodeProperty(v1, ContainedTestNodeProperty, v0)
.setNodeProperty(v1, IntProperty, 11)
val genericDomain = TestGraphSimple.create()
val graph = genericDomain.graph
val Seq(node2) = genericDomain.nodeA.intMandatory(1).l
val newNodeB = NewNodeB().stringOptional("node b stringOptional")
DiffGraphApplier.applyDiff(
graph,
GenericDomain.newDiffGraphBuilder
.setNodeProperty(node2, NodeA.PropertyNames.NodeB, newNodeB)
)

File.usingTemporaryDirectory(getClass.getName) { exportRootDirectory =>
val exportResult = GraphSONExporter.runExport(graph, exportRootDirectory.pathAsString)
exportResult.nodeCount shouldBe 2
exportResult.nodeCount shouldBe 3
val Seq(graphJsonFile) = exportResult.files

// import graphml into new graph, use difftool for round trip of conversion
val reimported = newGraphEmpty()
val reimported = GenericDomain.empty.graph
GraphSONImporter.runImport(reimported, graphJsonFile)
val diff = DiffTool.compare(graph, reimported)
val diffString = diff.asScala.mkString(lineSeparator)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package flatgraph.formats.neo4jcsv

import better.files.*
import flatgraph.DiffGraphApplier
import flatgraph.TestGraphSimple
import org.scalatest.matchers.should.Matchers.*
import org.scalatest.wordspec.AnyWordSpec
import flatgraph.formats.{ExportResult, ExporterMain, ImporterMain}
import flatgraph.util.DiffTool
import flatgraph.testdomains.generic.GenericDomain
import flatgraph.testdomains.generic.Language.*
import flatgraph.testdomains.generic.edges.ConnectedTo
import flatgraph.testdomains.generic.nodes.{NewNodeA, NodeA}
import flatgraph.testdomains.generic.nodes.NodeA

import java.io.FileNotFoundException
import java.nio.file.Paths
Expand All @@ -20,15 +20,7 @@ class Neo4jCsvTests extends AnyWordSpec {
val neo4jcsvRoot = Paths.get(subprojectRoot, "src/test/resources/neo4jcsv")

"Exporter should export valid csv" in {
val graph = GenericDomain.empty.graph
val node1 = NewNodeA().stringMandatory("node 2 a").stringOptional("node 2 b").stringList(Seq("node 3 c1", "node 3 c2"))
val node2 = NewNodeA().intMandatory(1).intOptional(2).intList(Seq(10, 11, 12))

DiffGraphApplier.applyDiff(
graph,
GenericDomain.newDiffGraphBuilder
.addEdge(node1, node2, ConnectedTo.Label, "edge property")
)
val graph = TestGraphSimple.create().graph

File.usingTemporaryDirectory(getClass.getName) { exportRootDirectory =>
val ExportResult(nodeCount, edgeCount, exportedFiles0, additionalInfo) =
Expand All @@ -46,7 +38,7 @@ class Neo4jCsvTests extends AnyWordSpec {

val nodeDataFileLines = fuzzyFindFile(exportedFiles, NodeA.Label, DataFileSuffix).lines.toSeq
nodeDataFileLines.size shouldBe 2
nodeDataFileLines should contain("0,node_a,,42,,node 3 c1;node 3 c2,node 2 a,node 2 b")
nodeDataFileLines should contain("0,node_a,,42,,node 1 c1;node 1 c2,node 1 a,node 1 b")
nodeDataFileLines should contain("1,node_a,10;11;12,1,2,,<empty>,")

val edgeHeaderFile = fuzzyFindFile(exportedFiles, ConnectedTo.Label, HeaderFileSuffix)
Expand Down Expand Up @@ -106,7 +98,7 @@ class Neo4jCsvTests extends AnyWordSpec {

graph.nodeCount shouldBe 2

val Seq(node1) = genericDomain.nodeA.stringMandatory("node 2 a").l
val Seq(node1) = genericDomain.nodeA.stringMandatory("node 1 a").l
val Seq(node2) = genericDomain.nodeA.intMandatory(1).l

node1.intMandatory shouldBe 42
Expand All @@ -115,11 +107,11 @@ class Neo4jCsvTests extends AnyWordSpec {
node2.intOptional shouldBe Some(2)
node1.intList shouldBe Seq.empty
node2.intList shouldBe Seq(10, 11, 12)
node1.stringMandatory shouldBe "node 2 a"
node1.stringMandatory shouldBe "node 1 a"
node2.stringMandatory shouldBe "<empty>"
node1.stringOptional shouldBe Some("node 2 b")
node1.stringOptional shouldBe Some("node 1 b")
node2.stringOptional shouldBe None
node1.stringList shouldBe Seq("node 3 c1", "node 3 c2")
node1.stringList shouldBe Seq("node 1 c1", "node 1 c2")
node2.stringList shouldBe Seq.empty

graph.edgeCount shouldBe 1
Expand Down Expand Up @@ -158,24 +150,16 @@ class Neo4jCsvTests extends AnyWordSpec {

"main apps for cli export/import" in {
File.usingTemporaryDirectory(getClass.getName) { tmpDir =>
val graphPath = tmpDir / "original.fg"
val exportPath = tmpDir / "export"
val genericDomain = GenericDomain.withStorage(graphPath.path)

val node1 = NewNodeA().stringMandatory("node 2 a").stringOptional("node 2 b").stringList(Seq("node 3 c1", "node 3 c2"))
val node2 = NewNodeA().intMandatory(1).intOptional(2).intList(Seq(10, 11, 12))

DiffGraphApplier.applyDiff(
genericDomain.graph,
GenericDomain.newDiffGraphBuilder.addEdge(node1, node2, ConnectedTo.Label, "edge property")
)
val graphPath = tmpDir / "original.fg"
val exportPath = tmpDir / "export"

val genericDomain = TestGraphSimple.create(Some(graphPath.path))
genericDomain.close()

val exporterMain = ExporterMain()
exporterMain(Array("--format=neo4jcsv", s"--out=${exportPath.pathAsString}", graphPath.pathAsString))
val exportedFiles = exportPath.list.toArray
exportedFiles.size shouldBe 6
exportedFiles.length shouldBe 6

// use importer for round trip
val importerMain = ImporterMain(flatgraph.testdomains.generic.GraphSchema)
Expand All @@ -188,7 +172,7 @@ class Neo4jCsvTests extends AnyWordSpec {
genericDomainReimported.graph.edgeCount shouldBe 1

genericDomainReimported.nodeA.intMandatory.l.sorted shouldBe List(1, 42)
genericDomainReimported.nodeA.stringMandatory("node 2 a").connectedTo.intOptional.head shouldBe 2
genericDomainReimported.nodeA.stringMandatory("node 1 a").connectedTo.intOptional.head shouldBe 2
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ object Accessors {
case newNode: nodes.NewNodeA => newNode.stringOptional
}
}
final class Access_NodeBBase(val node: nodes.NodeBBase) extends AnyVal {
def stringOptional: Option[String] = node match {
case stored: nodes.StoredNode => new Access_Property_string_optional(stored).stringOptional
case newNode: nodes.NewNodeB => newNode.stringOptional
}
}
/* accessors for base nodes end */
}

Expand All @@ -82,4 +88,5 @@ trait ConcreteStoredConversions extends ConcreteBaseConversions {
trait ConcreteBaseConversions {
import Accessors.*
implicit def access_NodeABase(node: nodes.NodeABase): Access_NodeABase = new Access_NodeABase(node)
implicit def access_NodeBBase(node: nodes.NodeBBase): Access_NodeBBase = new Access_NodeBBase(node)
}
Loading
Loading