Skip to content

Commit

Permalink
Increase IT coverage for queries
Browse files Browse the repository at this point in the history
  • Loading branch information
denis_savitsky committed Jan 23, 2024
1 parent 53ce389 commit 687c368
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 12 deletions.
3 changes: 3 additions & 0 deletions oolong-core/src/main/scala/oolong/AstParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ private[oolong] class DefaultAstParser(using quotes: Quotes) extends AstParser {
case '{ ($x: Option[_]).isDefined } =>
QExpr.Exists(parse(x), QExpr.Constant(true))

case '{ ($x: Option[_]).nonEmpty } =>
QExpr.Exists(parse(x), QExpr.Constant(true))

case PropSelector(name, path) if name == paramName =>
QExpr.Prop(path.mkString("."))

Expand Down
82 changes: 70 additions & 12 deletions oolong-mongo-it/src/test/scala/oolong/mongo/OolongMongoSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ class OolongMongoSpec extends AsyncFlatSpec with ForAllTestContainer with Before
container.start()

val client = MongoClient(container.replicaSetUrl)
val collection = client.getDatabase("test").getCollection[BsonDocument]("testColection")
val collection = client.getDatabase("test").getCollection[BsonDocument]("testCollection")

override def beforeAll(): Unit = {
val documents = List(
TestClass("0", 0, InnerClass("sdf"), Nil),
TestClass("1", 1, InnerClass("qwe"), Nil),
TestClass("2", 2, InnerClass("asd"), Nil),
TestClass("3", 12, InnerClass("sdf"), Nil)
TestClass("0", 0, InnerClass("sdf"), List(1, 2), None),
TestClass("1", 1, InnerClass("qwe"), Nil, Some(2L)),
TestClass("2", 2, InnerClass("asd"), Nil, None),
TestClass("3", 12, InnerClass("sdf"), Nil, None),
TestClass("12345", 12, InnerClass("sdf"), Nil, None),
)

implicit val ec = ExecutionContext.global
Expand All @@ -55,14 +56,14 @@ class OolongMongoSpec extends AsyncFlatSpec with ForAllTestContainer with Before
case Failure(exception) => Future.failed(exception)
case Success(value) => Future.successful(value)
}
} yield assert(v == TestClass("1", 1, InnerClass("qwe"), Nil))
} yield assert(v == TestClass("1", 1, InnerClass("qwe"), Nil, None))
}

it should "find documents in a collection with query with runtime constant" in {
val q = query[TestClass](_.field2 <= lift(Random.between(13, 100)))
for {
res <- collection.find(q).toFuture()
} yield assert(res.size == 4)
} yield assert(res.size == 5)
}

it should "find both documents with OR operator" in {
Expand Down Expand Up @@ -93,7 +94,7 @@ class OolongMongoSpec extends AsyncFlatSpec with ForAllTestContainer with Before

for {
res <- collection.find(q).toFuture()
} yield assert(res.size == 4)
} yield assert(res.size == 5)
}

it should "compile queries with `unchecked`" in {
Expand Down Expand Up @@ -136,6 +137,14 @@ class OolongMongoSpec extends AsyncFlatSpec with ForAllTestContainer with Before
} yield assert(res.size == 2)
}

it should "compile queries with `.contains` #4" in {
val q = query[TestClass](x => !List(1, 2, 3).contains(x.field2))

for {
res <- collection.find(q).toFuture()
} yield assert(res.size == 1)
}

it should "compile queries with nested objects" in {
val q = query[TestClass](_.field3 == lift(InnerClass("qwe")))

Expand All @@ -149,33 +158,82 @@ class OolongMongoSpec extends AsyncFlatSpec with ForAllTestContainer with Before

for {
res <- collection.find(q).toFuture()
} yield assert(res.size == 4)
} yield assert(res.size == 5)
}

it should "compile queries with `.isInstance` #2" in {
val q = query[TestClass](_.field3.isInstanceOf[MongoType.DOCUMENT])

for {
res <- collection.find(q).toFuture()
} yield assert(res.size == 4)
} yield assert(res.size == 5)
}

it should "compile queries with `.mod` #1" in {
it should "compile queries with `%` #1" in {
val q = query[TestClass](_.field2 % 4 == 0)

for {
res <- collection.find(q).toFuture()
} yield assert(res.size == 2)
}

it should "compile queries with `.mod` #2" in {
it should "compile queries with `%` #2" in {
val q = query[TestClass](_.field2 % 4.99 == 0)

for {
res <- collection.find(q).toFuture()
} yield assert(res.size == 2)
}

it should "compile queries with $type" in {
val q = query[TestClass](_.field2.isInstanceOf[MongoType.INT32])

for {
res <- collection.find(q).toFuture()
} yield assert(res.size == 5)
}

it should "compile queries with $exists" in {
val q = query[TestClass](_.field5.isDefined)

for {
res <- collection.find(q).toFuture()
} yield assert(res.size == 1)
}

it should "compile queries with $regex" in {
val q = query[TestClass](_.field1.matches("\\d{2,5}"))

for {
res <- collection.find(q).toFuture()
} yield assert(res.size == 1)
}

it should "compile queries with $size" in {
val q = query[TestClass](_.field4.size == 2)

for {
res <- collection.find(q).toFuture()
} yield assert(res.size == 1)
}

it should "compile queries with $elemMatch" in {
val q = query[TestClass](_.field2 >= 10)

for {
res <- collection.find(q).toFuture()
} yield assert(res.size == 2)
}

it should "compile queries with $all" in {
inline def variants = List(1, 2)
val q = query[TestClass](ins => variants.forall(ins.field4.contains))

for {
res <- collection.find(q).toFuture()
} yield assert(res.size == 1)
}

// TODO: test updates
// it should "compile updates" in {
// val upd = compileUpdate {
Expand Down
1 change: 1 addition & 0 deletions oolong-mongo/src/test/scala/oolong/mongo/TestClass.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ case class TestClass(
field2: Int,
field3: InnerClass,
field4: List[Int],
field5: Option[Long]
) derives BsonEncoder,
BsonDecoder

Expand Down

0 comments on commit 687c368

Please sign in to comment.