Skip to content

Commit

Permalink
Add some tests that QueryableByName compiles with multiple SQL types …
Browse files Browse the repository at this point in the history
…for one output type.

This tests that `QueryableByName` compiles, and outputs some data, for one example
where the types are pulled from a `table!` macro; and another example where the
values are specified as annotations in the struct.

It's difficult to find a type that has the required properties across all three
databases; these tests are compiled only for Postgres. To do something similar for
the other databases would probably require compiling with one of the time libraries.
  • Loading branch information
fiadliel committed Jul 26, 2024
1 parent ecf87ef commit 99546fd
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions diesel_derives/tests/queryable_by_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ table! {
}
}

#[cfg(feature = "postgres")]
table! {
multiple_sql_types_for_text {
id -> Integer,
name -> Text,
email -> Citext
}
}

#[test]
fn named_struct_definition() {
#[derive(Debug, Clone, Copy, PartialEq, Eq, QueryableByName)]
Expand Down Expand Up @@ -86,6 +95,55 @@ fn struct_with_path_in_name() {
);
}

#[cfg(feature = "postgres")]
#[test]
fn struct_with_multiple_sql_types_for_text() {
#[derive(Debug, PartialEq, QueryableByName)]
struct MultipleSqlTypesForText {
#[diesel(sql_type = diesel::sql_types::Text)]
name: String,
#[diesel(sql_type = diesel::sql_types::Citext)]
email: String,
}

let conn = &mut connection();
sql_query("CREATE EXTENSION IF NOT EXISTS citext")
.execute(conn)
.unwrap();
let data = sql_query("SELECT 'name'::text AS name, 'email'::citext AS email").get_result(conn);
assert_eq!(
Ok(MultipleSqlTypesForText {
name: "name".into(),
email: "email".into()
}),
data
);
}

#[cfg(feature = "postgres")]
#[test]
fn struct_with_multiple_sql_types_for_text_from_table() {
#[derive(Debug, PartialEq, QueryableByName)]
#[diesel(table_name = multiple_sql_types_for_text)]
struct MultipleSqlTypesForText {
name: String,
email: String,
}

let conn = &mut connection();
sql_query("CREATE EXTENSION IF NOT EXISTS citext")
.execute(conn)
.unwrap();
let data = sql_query("SELECT 'name'::text AS name, 'email'::citext AS email").get_result(conn);
assert_eq!(
Ok(MultipleSqlTypesForText {
name: "name".into(),
email: "email".into()
}),
data
);
}

// FIXME: Test usage with renamed columns

#[test]
Expand Down

0 comments on commit 99546fd

Please sign in to comment.