Skip to content

Commit

Permalink
Updated tests to:
Browse files Browse the repository at this point in the history
- track current in-mem snapshots
- track in-db pg snapshots
  • Loading branch information
Druue committed Nov 30, 2023
1 parent 0223800 commit 64b8841
Showing 1 changed file with 88 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
use query_engine_tests::*;

/// `distinct on` queries involving `orderBy`
/// are currently forced through in-memory handling as otherwise,
/// they fail with the following error message. This does not affect _all_
/// cases of `distinct on` in conjunction with `orderBy`.
///
/// ```sql
/// SELECT DISTINCT ON expressions must match initial ORDER BY expressions
/// ```
///
/// `distinct on` queries _not_ involving `orderBy` return differently ordered
/// result sets, hence we need to duplicate certain tests to track snapshots
/// for both the in-db pg results, and the in-mem result sets.
#[test_suite(schema(schemas::user_posts))]
mod distinct {
use indoc::indoc;
Expand All @@ -22,8 +34,8 @@ mod distinct {
}

/// Regression test for not selecting the fields the distinct is performed on: https://github.com/prisma/prisma/issues/5969
#[connector_test]
async fn no_panic(runner: Runner) -> TestResult<()> {
#[connector_test(exclude(CockroachDb, MongoDb, SqlServer, MySQL, Sqlite))]
async fn no_panic_pg(runner: Runner) -> TestResult<()> {
test_user(&runner, r#"{ id: 1, first_name: "Joe", last_name: "Doe", email: "1" }"#).await?;
test_user(&runner, r#"{ id: 2, first_name: "Doe", last_name: "Joe", email: "2" }"#).await?;

Expand All @@ -41,6 +53,26 @@ mod distinct {
Ok(())
}

/// Regression test for not selecting the fields the distinct is performed on: https://github.com/prisma/prisma/issues/5969
#[connector_test(exclude(Postgres))]
async fn no_panic_mem(runner: Runner) -> TestResult<()> {
test_user(&runner, r#"{ id: 1, first_name: "Joe", last_name: "Doe", email: "1" }"#).await?;
test_user(&runner, r#"{ id: 2, first_name: "Doe", last_name: "Joe", email: "2" }"#).await?;

insta::assert_snapshot!(
run_query!(
&runner,
indoc!("{
findManyUser(distinct: [first_name, last_name])
{ id }
}")
),
@r###"{"data":{"findManyUser":[{"id":1},{"id":2}]}}"###
);

Ok(())
}

#[connector_test]
async fn shorthand_works(runner: Runner) -> TestResult<()> {
test_user(&runner, r#"{ id: 1, first_name: "Joe", last_name: "Doe", email: "1" }"#).await?;
Expand All @@ -60,8 +92,31 @@ mod distinct {
Ok(())
}

#[connector_test]
async fn with_duplicates(runner: Runner) -> TestResult<()> {
#[connector_test(exclude(Postgres))]
async fn with_duplicates_pg(runner: Runner) -> TestResult<()> {
test_user(&runner, r#"{ id: 1, first_name: "Joe", last_name: "Doe", email: "1" }"#).await?;
test_user(
&runner,
r#"{ id: 2, first_name: "Hans", last_name: "Wurst", email: "2" }"#,
)
.await?;
test_user(&runner, r#"{ id: 3, first_name: "Joe", last_name: "Doe", email: "3" }"#).await?;

insta::assert_snapshot!(run_query!(
&runner,
indoc!("{
findManyUser(distinct: [first_name, last_name])
{ id, first_name, last_name }
}")
),
@r###"{"data":{"findManyUser":[{"id":1,"first_name":"Joe","last_name":"Doe"},{"id":2,"first_name":"Hans","last_name":"Wurst"}]}}"###
);

Ok(())
}

#[connector_test(exclude(CockroachDb, MongoDb, SqlServer, MySQL, Sqlite))]
async fn with_duplicates_mem(runner: Runner) -> TestResult<()> {
test_user(&runner, r#"{ id: 1, first_name: "Joe", last_name: "Doe", email: "1" }"#).await?;
test_user(
&runner,
Expand Down Expand Up @@ -160,14 +215,14 @@ mod distinct {
}

/// Mut return only distinct records for top record, and only for those the distinct relation records.
#[connector_test]
async fn nested_distinct(runner: Runner) -> TestResult<()> {
#[connector_test(exclude(CockroachDb, MongoDb, SqlServer, MySQL, Sqlite))]
async fn nested_distinct_pg(runner: Runner) -> TestResult<()> {
nested_dataset(&runner).await?;

// Returns Users 1, 3, 4, 5 top
// 1 => ["3", "1", "2"]
// 3 => []
// 4 => ["1"]
// 3 => []
// 5 => ["2", "3"]
insta::assert_snapshot!(run_query!(
&runner,
Expand All @@ -186,6 +241,32 @@ mod distinct {
Ok(())
}

#[connector_test(exclude(Postgres))]
async fn nested_distinct_mem(runner: Runner) -> TestResult<()> {
nested_dataset(&runner).await?;

// Returns Users 1, 3, 4, 5 top
// 1 => ["3", "1", "2"]
// 3 => []
// 4 => ["1"]
// 5 => ["2", "3"]
insta::assert_snapshot!(run_query!(
&runner,
indoc!("{
findManyUser(distinct: [first_name, last_name])
{
id
posts(distinct: [title], orderBy: { id: asc }) {
title
}
}}")
),
@r###"{"data":{"findManyUser":[{"id":1,"posts":[{"title":"3"},{"title":"1"},{"title":"2"}]},{"id":3,"posts":[]},{"id":4,"posts":[{"title":"1"}]},{"id":5,"posts":[{"title":"2"},{"title":"3"}]}]}}"###
);

Ok(())
}

/// Mut return only distinct records for top record, and only for those the distinct relation records. Both orderings reversed.
#[connector_test]
async fn nested_distinct_reversed(runner: Runner) -> TestResult<()> {
Expand Down

0 comments on commit 64b8841

Please sign in to comment.