Skip to content

Commit

Permalink
Continued prototyping
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Mar 24, 2024
1 parent 124f084 commit 8d24eff
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 117 deletions.
64 changes: 28 additions & 36 deletions all/src/test/scala/spec/SimpleSpec.scala
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
package spec

import cats.effect.IO
import cats.effect.unsafe.IORuntime
import fabric.rw.{RW, ccRW}
import cats.effect.testing.scalatest.AsyncIOSpec
import fabric.rw._
import lightdb.collection.Collection
import lightdb.data.{DataManager, JsonDataManager}
import lightdb.field.Field
import lightdb.index.lucene._
import lightdb.query._
import lightdb.store.halo.SharedHaloSupport
import lightdb.{Document, Id, JsonMapping, LightDB, ObjectMapping}
import testy.{AsyncSupport, Spec}
import lightdb.{Document, Id, JsonMapping, LightDB}
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AsyncWordSpec

import java.nio.file.Paths
import scala.concurrent.Future

class SimpleSpec extends Spec {
private object IOAsyncSupport extends AsyncSupport[IO[Any]] {
override def apply(async: IO[Any]): Future[Unit] = async.unsafeToFuture()(IORuntime.global).asInstanceOf[Future[Unit]]
}
implicit def asyncSupport[T]: AsyncSupport[IO[T]] = IOAsyncSupport.asInstanceOf[AsyncSupport[IO[T]]]

class SimpleSpec extends AsyncWordSpec with AsyncIOSpec with Matchers {
private val id1 = Id[Person]("john")
private val id2 = Id[Person]("jane")

private val p1 = Person("John Doe", 21, id1)
private val p2 = Person("Jane Doe", 19, id2)

"Simple database" should {
"clear data if any exists" async {
"clear data if any exists" in {
for {
_ <- db.truncate()
_ <- db.people.commit()
Expand All @@ -39,40 +31,40 @@ class SimpleSpec extends Spec {
indexCount should be(0)
}
}
"store John Doe" async {
"store John Doe" in {
db.people.put(p1).map { p =>
p._id should be(id1)
}
}
"verify John Doe exists" async {
"verify John Doe exists" in {
db.people.get(id1).map { o =>
o should be(Some(p1))
}
}
"storage Jane Doe" async {
"storage Jane Doe" in {
db.people.put(p2).map { p =>
p._id should be(id2)
}
}
"verify Jane Doe exists" async {
"verify Jane Doe exists" in {
db.people.get(id2).map { o =>
o should be(Some(p2))
}
}
"verify exactly two objects in data" async {
"verify exactly two objects in data" in {
db.people.store.count().map { size =>
size should be(2)
}
}
"flush data" async {
"flush data" in {
db.people.commit()
}
"verify exactly two objects in index" async {
"verify exactly two objects in index" in {
db.people.indexer.count().map { size =>
size should be(2)
}
}
"verify exactly two objects in the store" async {
"verify exactly two objects in the store" in {
db.people.store.all[Person]()
.compile
.toList
Expand All @@ -81,7 +73,7 @@ class SimpleSpec extends Spec {
ids.toSet should be(Set(id1, id2))
}
}
"search by name for positive result" async {
"search by name for positive result" in {
db.people.query.filter(Person.name === "Jane Doe").search().compile.toList.map { results =>
results.length should be(1)
val doc = results.head
Expand All @@ -90,23 +82,23 @@ class SimpleSpec extends Spec {
doc(Person.age) should be(19)
}
}
"delete John" async {
"delete John" in {
db.people.delete(id1)
}
"verify exactly one object in data" async {
"verify exactly one object in data" in {
db.people.store.count().map { size =>
size should be(1)
}
}
"commit data" async {
"commit data" in {
db.people.commit()
}
"verify exactly one object in index" async {
"verify exactly one object in index" in {
db.people.indexer.count().map { size =>
size should be(1)
}
}
"list all documents" async {
"list all documents" in {
db.people.query.search().compile.toList.flatMap { results =>
results.length should be(1)
val doc = results.head
Expand All @@ -121,7 +113,7 @@ class SimpleSpec extends Spec {
}
}
// TODO: search for an item by name and by age range
"replace Jane Doe" async {
"replace Jane Doe" in {
db.people.put(Person("Jan Doe", 20, id2)).map { p =>
p._id should be(id2)
}
Expand All @@ -133,10 +125,10 @@ class SimpleSpec extends Spec {
p.age should be(20)
}
}
"commit data" async {
"commit data" in {
db.people.commit()
}
"list all documents" async {
"list all documents" in {
db.people.query.search().compile.toList.map { results =>
results.length should be(1)
val doc = results.head
Expand All @@ -147,7 +139,7 @@ class SimpleSpec extends Spec {
}
// TODO: support multiple item types (make sure queries don't return different types)
// TODO: test batch operations: insert, replace, and delete
"dispose" async {
"dispose" in {
db.dispose()
}
}
Expand All @@ -161,9 +153,9 @@ class SimpleSpec extends Spec {
case class Person(name: String, age: Int, _id: Id[Person] = Id()) extends Document[Person]

object Person extends JsonMapping[Person] {
override implicit val rw: RW[Person] = ccRW
override implicit val rw: RW[Person] = RW.gen

val name: FD[String] = field("name", _.name).indexed()
val age: FD[Int] = field("age", _.age).indexed()
val name: FD[String] = field("name", _.name)
val age: FD[Int] = field("age", _.age)
}
}
19 changes: 11 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ val collectionCompatVersion: String = "2.11.0"
val haloDBVersion: String = "v0.5.6"
val catsEffectVersion: String = "3.5.4"
val fabricVersion: String = "1.14.1"
//val lucene4sVersion: String = "1.11.1"
val fs2Version: String = "3.10.0"
val scribeVersion: String = "3.13.2"
val luceneVersion: String = "9.10.0"

val scalaTestVersion: String = "3.2.18"
val catsEffectTestingVersion: String = "1.5.0"

lazy val root = project.in(file("."))
.aggregate(core.js, core.jvm, lucene, halo, mapdb, all)
Expand All @@ -69,7 +69,8 @@ lazy val core = crossProject(JSPlatform, JVMPlatform)
"org.typelevel" %%% "cats-effect" % catsEffectVersion,
"org.typelevel" %%% "fabric-io" % fabricVersion,
"co.fs2" %%% "fs2-core" % fs2Version,
"org.scalatest" %%% "scalatest" % scalaTestVersion % Test
"org.scalatest" %%% "scalatest" % scalaTestVersion % Test,
"org.typelevel" %%% "cats-effect-testing-scalatest" % catsEffectTestingVersion % Test
),
libraryDependencies ++= (
if (scalaVersion.value.startsWith("3.")) {
Expand All @@ -96,7 +97,8 @@ lazy val lucene = project.in(file("lucene"))
libraryDependencies ++= Seq(
"org.apache.lucene" % "lucene-core" % luceneVersion,
"org.apache.lucene" % "lucene-queryparser" % luceneVersion,
"org.scalatest" %% "scalatest" % scalaTestVersion % Test
"org.scalatest" %% "scalatest" % scalaTestVersion % Test,
"org.typelevel" %%% "cats-effect-testing-scalatest" % catsEffectTestingVersion % Test
)
)

Expand All @@ -107,7 +109,8 @@ lazy val halo = project.in(file("halo"))
libraryDependencies ++= Seq(
"com.outr" %% "scribe-slf4j" % scribeVersion,
"com.github.yahoo" % "HaloDB" % haloDBVersion,
"org.scalatest" %%% "scalatest" % scalaTestVersion % Test
"org.scalatest" %%% "scalatest" % scalaTestVersion % Test,
"org.typelevel" %%% "cats-effect-testing-scalatest" % catsEffectTestingVersion % Test
),
fork := true
)
Expand All @@ -118,7 +121,8 @@ lazy val mapdb = project.in(file("mapdb"))
name := s"$projectName-mapdb",
libraryDependencies ++= Seq(
"org.mapdb" % "mapdb" % "3.1.0",
"org.scalatest" %% "scalatest" % scalaTestVersion % Test
"org.scalatest" %% "scalatest" % scalaTestVersion % Test,
"org.typelevel" %%% "cats-effect-testing-scalatest" % catsEffectTestingVersion % Test
),
fork := true
)
Expand All @@ -128,7 +132,8 @@ lazy val all = project.in(file("all"))
.settings(
name := s"$projectName-all",
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % scalaTestVersion % Test
"org.scalatest" %% "scalatest" % scalaTestVersion % Test,
"org.typelevel" %%% "cats-effect-testing-scalatest" % catsEffectTestingVersion % Test
),
fork := true
)
Expand All @@ -142,8 +147,6 @@ lazy val benchmark = project.in(file("benchmark"))
"co.fs2" %%% "fs2-io" % fs2Version,
"org.mongodb" % "mongodb-driver-sync" % "4.11.1",
"org.postgresql" % "postgresql" % "42.7.1",
// "com.arangodb" % "arangodb-java-driver" % "7.4.0",
// "com.arangodb" % "jackson-dataformat-velocypack" % "4.2.0",
"com.outr" %% "scarango-driver" % "3.19.1"
)
)
Loading

0 comments on commit 8d24eff

Please sign in to comment.