Skip to content

Commit

Permalink
Add test for $elemMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
denis_savitsky committed Jan 27, 2024
1 parent a04623d commit bbb95fa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
29 changes: 10 additions & 19 deletions oolong-mongo-it/src/test/scala/oolong/mongo/OolongMongoSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -218,11 +218,11 @@ class OolongMongoSpec extends AsyncFlatSpec with ForAllTestContainer with Before
}

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" && s.otherField < 10))

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

it should "compile queries with $all" in {
Expand All @@ -234,13 +234,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")))
// }
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions oolong-mongo/src/test/scala/oolong/mongo/TestClass.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bbb95fa

Please sign in to comment.