diff --git a/crates/isolate/src/tests/query.rs b/crates/isolate/src/tests/query.rs index 5ee71e1d..2f33edf8 100644 --- a/crates/isolate/src/tests/query.rs +++ b/crates/isolate/src/tests/query.rs @@ -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, }; @@ -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?; diff --git a/npm-packages/udf-tests/convex/query.ts b/npm-packages/udf-tests/convex/query.ts index 6f7a6383..eb9cb8a2 100644 --- a/npm-packages/udf-tests/convex/query.ts +++ b/npm-packages/udf-tests/convex/query.ts @@ -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")