- Notifications
You must be signed in to change notification settings - Fork515
FromSql for type record, closes #310#1261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
myypo wants to merge3 commits intorust-postgres:masterChoose a base branch frommyypo:records
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Draft
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionspostgres-derive-test/src/lib.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
209 changes: 209 additions & 0 deletionspostgres-derive-test/src/records.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
use postgres::{Client, NoTls}; | ||
use postgres_types::{FromSql, ToSql, WrongType}; | ||
use std::error::Error; | ||
#[test] | ||
fn basic() { | ||
#[derive(FromSql, ToSql, Debug, PartialEq)] | ||
struct InventoryItem { | ||
name: String, | ||
supplier_id: i32, | ||
price: Option<f64>, | ||
} | ||
let mut conn = Client::connect("user=postgres host=localhost port=5433", NoTls).unwrap(); | ||
let expected = InventoryItem { | ||
name: "foobar".to_owned(), | ||
supplier_id: 100, | ||
price: Some(15.50), | ||
}; | ||
let got = conn | ||
.query_one("SELECT ('foobar', 100, 15.50::double precision)", &[]) | ||
.unwrap() | ||
.try_get::<_, InventoryItem>(0) | ||
.unwrap(); | ||
assert_eq!(got, expected); | ||
} | ||
#[test] | ||
fn field_count_mismatch() { | ||
#[derive(FromSql, Debug, PartialEq)] | ||
struct InventoryItem { | ||
name: String, | ||
supplier_id: i32, | ||
price: Option<f64>, | ||
} | ||
let mut conn = Client::connect("user=postgres host=localhost port=5433", NoTls).unwrap(); | ||
let err = conn | ||
.query_one("SELECT ('foobar', 100)", &[]) | ||
.unwrap() | ||
.try_get::<_, InventoryItem>(0) | ||
.unwrap_err(); | ||
err.source().unwrap().is::<WrongType>(); | ||
let err = conn | ||
.query_one("SELECT ('foobar', 100, 15.50, 'extra')", &[]) | ||
.unwrap() | ||
.try_get::<_, InventoryItem>(0) | ||
.unwrap_err(); | ||
err.source().unwrap().is::<WrongType>(); | ||
} | ||
#[test] | ||
fn wrong_type() { | ||
#[derive(FromSql, Debug, PartialEq)] | ||
struct InventoryItem { | ||
name: String, | ||
supplier_id: i32, | ||
price: Option<f64>, | ||
} | ||
let mut conn = Client::connect("user=postgres host=localhost port=5433", NoTls).unwrap(); | ||
let err = conn | ||
.query_one("SELECT ('foobar', 'not_an_int', 15.50)", &[]) | ||
.unwrap() | ||
.try_get::<_, InventoryItem>(0) | ||
.unwrap_err(); | ||
assert!(err.source().unwrap().is::<WrongType>()); | ||
let err = conn | ||
.query_one("SELECT (123, 100, 15.50)", &[]) | ||
.unwrap() | ||
.try_get::<_, InventoryItem>(0) | ||
.unwrap_err(); | ||
assert!(err.source().unwrap().is::<WrongType>()); | ||
} | ||
#[test] | ||
fn nested_structs() { | ||
#[derive(FromSql, Debug, PartialEq)] | ||
struct Address { | ||
street: String, | ||
city: Option<String>, | ||
} | ||
#[derive(FromSql, Debug, PartialEq)] | ||
struct Person { | ||
name: String, | ||
age: Option<i32>, | ||
address: Address, | ||
} | ||
let mut conn = Client::connect("user=postgres host=localhost port=5433", NoTls).unwrap(); | ||
let result: Person = conn | ||
.query_one( | ||
"SELECT ('John', 30, ROW('123 Main St', 'Springfield'))", | ||
&[], | ||
) | ||
.unwrap() | ||
.get(0); | ||
let expected = Person { | ||
name: "John".to_owned(), | ||
age: Some(30), | ||
address: Address { | ||
street: "123 Main St".to_owned(), | ||
city: Some("Springfield".to_owned()), | ||
}, | ||
}; | ||
assert_eq!(result, expected); | ||
} | ||
#[test] | ||
fn domains() { | ||
#[derive(FromSql, Debug, PartialEq)] | ||
struct SpecialId(i32); | ||
#[derive(FromSql, Debug, PartialEq)] | ||
struct Person { | ||
name: String, | ||
age: Option<i32>, | ||
id: SpecialId, | ||
} | ||
let mut conn = Client::connect("user=postgres host=localhost port=5433", NoTls).unwrap(); | ||
conn.execute("CREATE DOMAIN pg_temp.\"special_id\" AS integer;", &[]) | ||
.unwrap(); | ||
let result: Person = conn | ||
.query_one("SELECT ('John', 30, 42::special_id)", &[]) | ||
.unwrap() | ||
.get(0); | ||
let expected = Person { | ||
name: "John".to_owned(), | ||
age: Some(30), | ||
id: SpecialId(42), | ||
}; | ||
assert_eq!(result, expected); | ||
} | ||
#[test] | ||
fn enums() { | ||
#[derive(FromSql, Debug, PartialEq)] | ||
enum Employment { | ||
Salaried, | ||
Hourly, | ||
Unemployed, | ||
} | ||
#[derive(FromSql, Debug, PartialEq)] | ||
struct Person { | ||
name: String, | ||
age: Option<i32>, | ||
employment: Employment, | ||
} | ||
let mut conn = Client::connect("user=postgres host=localhost port=5433", NoTls).unwrap(); | ||
conn.execute( | ||
"CREATE TYPE pg_temp.employment AS ENUM ('Salaried', 'Hourly', 'Unemployed')", | ||
&[], | ||
) | ||
.unwrap(); | ||
let result: Person = conn | ||
.query_one("SELECT ('John', 30, 'Hourly'::employment)", &[]) | ||
.unwrap() | ||
.get(0); | ||
let expected = Person { | ||
name: "John".to_owned(), | ||
age: Some(30), | ||
employment: Employment::Hourly, | ||
}; | ||
assert_eq!(result, expected); | ||
} | ||
#[test] | ||
fn generics() { | ||
#[derive(FromSql, ToSql, Debug, PartialEq)] | ||
struct GenericItem<T, U> { | ||
first: T, | ||
second: U, | ||
} | ||
let mut conn = Client::connect("user=postgres host=localhost port=5433", NoTls).unwrap(); | ||
let expected = GenericItem { | ||
first: "test".to_owned(), | ||
second: 42, | ||
}; | ||
let got = conn | ||
.query_one("SELECT ('test', 42)", &[]) | ||
.unwrap() | ||
.try_get::<_, GenericItem<String, i32>>(0) | ||
.unwrap(); | ||
assert_eq!(got, expected); | ||
} |
42 changes: 36 additions & 6 deletionspostgres-derive/src/accepts.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.