Skip to content

Commit 42557d6

Browse files
author
denis_savitsky
committed
Increase IT coverage for queries
1 parent 53ce389 commit 42557d6

File tree

3 files changed

+75
-13
lines changed

3 files changed

+75
-13
lines changed

oolong-core/src/main/scala/oolong/AstParser.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ private[oolong] class DefaultAstParser(using quotes: Quotes) extends AstParser {
159159
case '{ ($x: Option[_]).isDefined } =>
160160
QExpr.Exists(parse(x), QExpr.Constant(true))
161161

162+
case '{ ($x: Option[_]).nonEmpty } =>
163+
QExpr.Exists(parse(x), QExpr.Constant(true))
164+
162165
case PropSelector(name, path) if name == paramName =>
163166
QExpr.Prop(path.mkString("."))
164167

oolong-mongo-it/src/test/scala/oolong/mongo/OolongMongoSpec.scala

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ class OolongMongoSpec extends AsyncFlatSpec with ForAllTestContainer with Before
2828
container.start()
2929

3030
val client = MongoClient(container.replicaSetUrl)
31-
val collection = client.getDatabase("test").getCollection[BsonDocument]("testColection")
31+
val collection = client.getDatabase("test").getCollection[BsonDocument]("testCollection")
3232

3333
override def beforeAll(): Unit = {
3434
val documents = List(
35-
TestClass("0", 0, InnerClass("sdf"), Nil),
36-
TestClass("1", 1, InnerClass("qwe"), Nil),
37-
TestClass("2", 2, InnerClass("asd"), Nil),
38-
TestClass("3", 12, InnerClass("sdf"), Nil)
35+
TestClass("0", 0, InnerClass("sdf"), List(1, 2), None),
36+
TestClass("1", 1, InnerClass("qwe"), Nil, Some(2L)),
37+
TestClass("2", 2, InnerClass("asd"), Nil, None),
38+
TestClass("3", 12, InnerClass("sdf"), Nil, None),
39+
TestClass("12345", 12, InnerClass("sdf"), Nil, None),
3940
)
4041

4142
implicit val ec = ExecutionContext.global
@@ -55,14 +56,14 @@ class OolongMongoSpec extends AsyncFlatSpec with ForAllTestContainer with Before
5556
case Failure(exception) => Future.failed(exception)
5657
case Success(value) => Future.successful(value)
5758
}
58-
} yield assert(v == TestClass("1", 1, InnerClass("qwe"), Nil))
59+
} yield assert(v == TestClass("1", 1, InnerClass("qwe"), Nil, Some(2L)))
5960
}
6061

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

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

9495
for {
9596
res <- collection.find(q).toFuture()
96-
} yield assert(res.size == 4)
97+
} yield assert(res.size == 5)
9798
}
9899

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

140+
it should "compile queries with `.contains` #4" in {
141+
val q = query[TestClass](x => !List(1, 2, 3).contains(x.field2))
142+
143+
for {
144+
res <- collection.find(q).toFuture()
145+
} yield assert(res.size == 3)
146+
}
147+
139148
it should "compile queries with nested objects" in {
140149
val q = query[TestClass](_.field3 == lift(InnerClass("qwe")))
141150

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

150159
for {
151160
res <- collection.find(q).toFuture()
152-
} yield assert(res.size == 4)
161+
} yield assert(res.size == 5)
153162
}
154163

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

158167
for {
159168
res <- collection.find(q).toFuture()
160-
} yield assert(res.size == 4)
169+
} yield assert(res.size == 5)
161170
}
162171

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

166175
for {
167176
res <- collection.find(q).toFuture()
168-
} yield assert(res.size == 2)
177+
} yield assert(res.size == 3)
169178
}
170179

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

183+
for {
184+
res <- collection.find(q).toFuture()
185+
} yield assert(res.size == 3)
186+
}
187+
188+
it should "compile queries with $type" in {
189+
val q = query[TestClass](_.field2.isInstanceOf[MongoType.INT32])
190+
191+
for {
192+
res <- collection.find(q).toFuture()
193+
} yield assert(res.size == 5)
194+
}
195+
196+
it should "compile queries with $exists" in {
197+
val q = query[TestClass](_.field5.isDefined)
198+
199+
for {
200+
res <- collection.find(q).toFuture()
201+
} yield assert(res.size == 1)
202+
}
203+
204+
it should "compile queries with $regex" in {
205+
val q = query[TestClass](_.field1.matches("\\d{2,5}"))
206+
207+
for {
208+
res <- collection.find(q).toFuture()
209+
} yield assert(res.size == 1)
210+
}
211+
212+
it should "compile queries with $size" in {
213+
val q = query[TestClass](_.field4.size == 2)
214+
215+
for {
216+
res <- collection.find(q).toFuture()
217+
} yield assert(res.size == 1)
218+
}
219+
220+
it should "compile queries with $elemMatch" in {
221+
val q = query[TestClass](_.field2 >= 10)
222+
174223
for {
175224
res <- collection.find(q).toFuture()
176225
} yield assert(res.size == 2)
177226
}
178227

228+
it should "compile queries with $all" in {
229+
inline def variants = List(1, 2)
230+
val q = query[TestClass](ins => variants.forall(ins.field4.contains))
231+
232+
for {
233+
res <- collection.find(q).toFuture()
234+
} yield assert(res.size == 1)
235+
}
236+
179237
// TODO: test updates
180238
// it should "compile updates" in {
181239
// val upd = compileUpdate {

oolong-mongo/src/test/scala/oolong/mongo/TestClass.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ case class TestClass(
88
field2: Int,
99
field3: InnerClass,
1010
field4: List[Int],
11+
field5: Option[Long]
1112
) derives BsonEncoder,
1213
BsonDecoder
1314

0 commit comments

Comments
 (0)