Skip to content

Commit

Permalink
[query] regression test for filter & limit (#23773)
Browse files Browse the repository at this point in the history
make sure we don't accidentally mix up the application of Limit and Filter in query pipelines by adding a regression test.

GitOrigin-RevId: fb9ee928779e4c77cf39a930125cd82cabd16220
  • Loading branch information
ldanilek authored and Convex, Inc. committed Mar 21, 2024
1 parent 7b21691 commit 4e899e5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
22 changes: 22 additions & 0 deletions crates/isolate/src/tests/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use must_let::must_let;
use pretty_assertions::assert_eq;
use runtime::testing::TestRuntime;
use value::{
assert_val,
id_v6::DocumentIdV6,
ConvexObject,
};
Expand Down Expand Up @@ -59,6 +60,27 @@ async fn test_full_table_scan(rt: TestRuntime) -> anyhow::Result<()> {
Ok(())
}

#[convex_macro::test_runtime]
async fn test_filter_first(rt: TestRuntime) -> anyhow::Result<()> {
let t = UdfTest::default(rt).await?;
add_index(&t).await?;
// Create two documents with different numbers, and filter for them.
// This tests that Limit and Filter are applied in the correct order,
// because if Limit is applied before Filter, `query().filter().first()`
// would run as `query().first().filter()` and get no results.
t.mutation("query:insert", assert_obj!( "number" => 1))
.await?;
t.mutation("query:insert", assert_obj!( "number" => 2))
.await?;

must_let!(let ConvexValue::Object(r) = t.query("query:filterFirst", assert_obj!( "number" => 1)).await?);
assert_eq!(r.get("hello"), Some(&assert_val!(1)));

must_let!(let ConvexValue::Object(r) = t.query("query:filterFirst", assert_obj!( "number" => 2)).await?);
assert_eq!(r.get("hello"), Some(&assert_val!(2)));
Ok(())
}

#[convex_macro::test_runtime]
async fn test_boolean_value_filters(rt: TestRuntime) -> anyhow::Result<()> {
let t = UdfTest::default(rt).await?;
Expand Down
7 changes: 7 additions & 0 deletions npm-packages/udf-tests/convex/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export const filterScan = query(({ db }, { number }: { number: number }) =>
.collect(),
);

export const filterFirst = query(({ db }, { number }: { number: number }) =>
db
.query("test")
.filter((q) => q.eq(q.field("hello"), number))
.first(),
);

export const explicitScan = query(({ db }, { number }: { number: number }) => {
return db
.query("test")
Expand Down

0 comments on commit 4e899e5

Please sign in to comment.