Skip to content

Commit

Permalink
Looking for an infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
hohonuuli committed May 11, 2024
1 parent 6cca23c commit fab3c8e
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 6 deletions.
13 changes: 7 additions & 6 deletions oni/src/main/scala/org/mbari/oni/jdbc/ConceptRow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ package org.mbari.oni.jdbc
*/

import java.time.Instant

import org.mbari.oni.domain.ConceptNameTypes

import scala.collection.mutable


/**
* @author Brian Schlining
* @since 2018-02-11T11:34:00
Expand All @@ -36,11 +37,11 @@ case class ConceptRow(
id: Long,
parentId: Option[Long],
name: String,
rankLevel: Option[String],
rankName: Option[String],
nameType: String,
conceptTimestamp: Instant,
conceptNameTimestamp: Instant
rankLevel: Option[String] = None,
rankName: Option[String] = None,
nameType: String = ConceptNameTypes.PRIMARY.getType,
conceptTimestamp: Instant = Instant.EPOCH,
conceptNameTimestamp: Instant = Instant.EPOCH
) {

lazy val rank: Option[String] = rankName.map(n => rankLevel.getOrElse("") + n)
Expand Down
31 changes: 31 additions & 0 deletions oni/src/test/scala/org/mbari/oni/jdbc/ConceptRowSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) Monterey Bay Aquarium Research Institute 2024
*
* oni code is non-public software. Unauthorized copying of this file,
* via any medium is strictly prohibited. Proprietary and confidential.
*/

package org.mbari.oni.jdbc

import java.time.Instant
import org.mbari.oni.domain.ConceptNameTypes

class ConceptRowSuite extends munit.FunSuite:
test("ConceptRow constructor works as expected") {
val now = Instant.now()
val conceptRow =
ConceptRow(
1,
None,
"name",
Some("rankLevel"),
Some("rankName"),
ConceptNameTypes.PRIMARY.getType,
Instant.EPOCH,
now
)
assertEquals(conceptRow.id, 1L)
assertEquals(conceptRow.rank, Some("rankLevelrankName"))
assertEquals(conceptRow.nameType, ConceptNameTypes.PRIMARY.getType)
assertEquals(conceptRow.lastUpdate, now)
}
87 changes: 87 additions & 0 deletions oni/src/test/scala/org/mbari/oni/jdbc/MutableConceptSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.mbari.oni.jdbc

import org.mbari.oni.domain.ConceptNameTypes

class MutableConceptSuite extends munit.FunSuite {


/*
1 - root, object
`- 2 - child2
|- 4 - child4
| |- 8 - child8
| `- 9 - child9
| `- 10 - child10
`- 5 - child5
`- 3 - child3
|- 6 - child6
`- 7 - child7
*/
val rows = Seq(
ConceptRow(1, None, "root"),
ConceptRow(1, None, "object", nameType = ConceptNameTypes.ALTERNATE.getType),
ConceptRow(2, Some(1), "child2"),
ConceptRow(3, Some(1), "child3"),
ConceptRow(4, Some(2), "child4"),
ConceptRow(5, Some(2), "child5"),
ConceptRow(6, Some(3), "child6"),
ConceptRow(7, Some(3), "child7"),
ConceptRow(8, Some(4), "child8"),
ConceptRow(9, Some(4), "child9"),
ConceptRow(10, Some(9), "child10")
)

test("toTree") {
val (rootOpt, nodes) = MutableConcept.toTree(rows)
assert(rootOpt.isDefined)
val root = rootOpt.get
assertEquals(root.id.get, 1L)
assertEquals(root.children.size, 2)
assertEquals(nodes.size, 10)
assertEquals(root.primaryName, Some("root"))
assertEquals(root.names.map(_.name).sorted, Seq("object", "root"))
}

test("root") {
val (rootOpt, nodes) = MutableConcept.toTree(rows)
val root = rootOpt.get
val child2 = root.children.head
val child4 = child2.children.head
val child8 = child4.children.head
val child9 = child4.children(1)
val child5 = root.children(1)
val child3 = root.children(1)
val child6 = child3.children.head
val child7 = child3.children(1)
assertEquals(child8.root().id.get, 1L)
assertEquals(child9.root().id.get, 1L)
assertEquals(child5.root().id.get, 1L)
assertEquals(child6.root().id.get, 1L)
assertEquals(child7.root().id.get, 1L)
}

test("copyUp") {
val (rootOpt, nodes) = MutableConcept.toTree(rows)
val child9 = nodes.find(_.id.get == 9).get
val child9Copy = child9.copyUp()
assertEquals(child9Copy.id, child9.id)
assertEquals(child9Copy.rank, child9.rank)
assertEquals(child9Copy.names, child9.names)
assertEquals(child9Copy.children, Nil)
assertEquals(child9Copy.parent.get.id, child9.parent.get.id)
assertEquals(child9Copy.root().id.get, 1L)
}

test("toImmutable") {
val (rootOpt, nodes) = MutableConcept.toTree(rows)
val root = rootOpt.get
val concept = root.toImmutable
assertEquals(concept.name, "root")
assertEquals(concept.rank, None)
assertEquals(concept.alternativeNames, Seq("object"))
assertEquals(concept.children.size, 2)
assertEquals(concept.children.head.name, "child2")
assertEquals(concept.children(1).name, "child3")
}

}

0 comments on commit fab3c8e

Please sign in to comment.