From 6d6821b7309a5c515842a1bf4d137a24f28c5d77 Mon Sep 17 00:00:00 2001 From: denis_savitsky Date: Sat, 27 Jan 2024 17:07:03 +0100 Subject: [PATCH] Add test for $elemMatch --- .../scala/oolong/mongo/OolongMongoSpec.scala | 39 +++++++++---------- .../oolong/mongo/OolongMongoUpdateSpec.scala | 10 ++--- .../test/scala/oolong/mongo/TestClass.scala | 6 ++- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/oolong-mongo-it/src/test/scala/oolong/mongo/OolongMongoSpec.scala b/oolong-mongo-it/src/test/scala/oolong/mongo/OolongMongoSpec.scala index 3b15f10..caddf4d 100644 --- a/oolong-mongo-it/src/test/scala/oolong/mongo/OolongMongoSpec.scala +++ b/oolong-mongo-it/src/test/scala/oolong/mongo/OolongMongoSpec.scala @@ -32,11 +32,11 @@ class OolongMongoSpec extends AsyncFlatSpec with ForAllTestContainer with Before override def beforeAll(): Unit = { val documents = List( - 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), + TestClass("0", 0, InnerClass("sdf", 1), List(1, 2), None, List(InnerClass("abc", 1))), + TestClass("1", 1, InnerClass("qwe", 2), Nil, Some(2L), List(InnerClass("cde", 10))), + TestClass("2", 2, InnerClass("asd", 3), Nil, None, List.empty), + TestClass("3", 12, InnerClass("sdf", 4), List(10, 25), None, List.empty), + TestClass("12345", 12, InnerClass("sdf", 5), Nil, None, List.empty), ) implicit val ec = ExecutionContext.global @@ -56,7 +56,7 @@ 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, Some(2L))) + } yield assert(v == TestClass("1", 1, InnerClass("qwe", 2), Nil, Some(2L), List(InnerClass("cde", 10)))) } it should "find documents in a collection with query with runtime constant" in { @@ -130,7 +130,7 @@ class OolongMongoSpec extends AsyncFlatSpec with ForAllTestContainer with Before } it should "compile queries with `.contains` #3" in { - val q = query[TestClass](x => lift(Set(InnerClass("qwe"), InnerClass("asd"))).contains(x.field3)) + val q = query[TestClass](x => lift(Set(InnerClass("qwe", 2), InnerClass("asd", 3))).contains(x.field3)) for { res <- collection.find(q).toFuture() @@ -146,7 +146,7 @@ class OolongMongoSpec extends AsyncFlatSpec with ForAllTestContainer with Before } it should "compile queries with nested objects" in { - val q = query[TestClass](_.field3 == lift(InnerClass("qwe"))) + val q = query[TestClass](_.field3 == lift(InnerClass("qwe", 2))) for { res <- collection.find(q).toFuture() @@ -214,15 +214,23 @@ class OolongMongoSpec extends AsyncFlatSpec with ForAllTestContainer with Before for { res <- collection.find(q).toFuture() - } yield assert(res.size == 1) + } yield assert(res.size == 2) } it should "compile queries with $elemMatch" in { - val q = query[TestClass](_.field2 >= 10) + val q = query[TestClass](tc => tc.field6.exists(s => s.innerField == "cde")) for { res <- collection.find(q).toFuture() - } yield assert(res.size == 2) + } yield assert(res.size == 1) + } + + it should "compile queries with $elemMatch #2" in { + val q = query[TestClass](tc => tc.field6.exists(s => s.innerField != "cde" && s.otherField < 10)) + + for { + res <- collection.find(q).toFuture() + } yield assert(res.size == 1) } it should "compile queries with $all" in { @@ -234,13 +242,4 @@ class OolongMongoSpec extends AsyncFlatSpec with ForAllTestContainer with Before } yield assert(res.size == 1) } - // TODO: test updates -// it should "compile updates" in { -// val upd = compileUpdate { -// update[CompanySuccess] -// .set(_.from, 2) -// .set(_.field4, liftU(Field("qweasd"))) -// } -// } - } diff --git a/oolong-mongo-it/src/test/scala/oolong/mongo/OolongMongoUpdateSpec.scala b/oolong-mongo-it/src/test/scala/oolong/mongo/OolongMongoUpdateSpec.scala index e81e850..5eb3189 100644 --- a/oolong-mongo-it/src/test/scala/oolong/mongo/OolongMongoUpdateSpec.scala +++ b/oolong-mongo-it/src/test/scala/oolong/mongo/OolongMongoUpdateSpec.scala @@ -23,11 +23,11 @@ class OolongMongoUpdateSpec extends AsyncFlatSpec with ForAllTestContainer with override def beforeAll(): Unit = { val documents = List( - 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), + TestClass("0", 0, InnerClass("sdf", 2.0), List(1, 2), None, List.empty), + TestClass("1", 1, InnerClass("qwe", 3), Nil, Some(2L), List.empty), + TestClass("2", 2, InnerClass("asd", 0), Nil, None, List.empty), + TestClass("3", 12, InnerClass("sdf", 1), Nil, None, List.empty), + TestClass("12345", 12, InnerClass("sdf", 11), Nil, None, List.empty), ) implicit val ec = ExecutionContext.global diff --git a/oolong-mongo/src/test/scala/oolong/mongo/TestClass.scala b/oolong-mongo/src/test/scala/oolong/mongo/TestClass.scala index a92e93f..06ee939 100644 --- a/oolong-mongo/src/test/scala/oolong/mongo/TestClass.scala +++ b/oolong-mongo/src/test/scala/oolong/mongo/TestClass.scala @@ -8,11 +8,13 @@ case class TestClass( field2: Int, field3: InnerClass, field4: List[Int], - field5: Option[Long] + field5: Option[Long], + field6: List[InnerClass] ) derives BsonEncoder, BsonDecoder case class InnerClass( - innerField: String + innerField: String, + otherField: Double ) derives BsonEncoder, BsonDecoder