|
| 1 | +use tokio_postgres::types::Type; |
| 2 | +use tokio_postgres::NoTls; |
| 3 | + |
| 4 | +use super::*; |
| 5 | + |
| 6 | +#[test] |
| 7 | +fn prepare() { |
| 8 | + let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap(); |
| 9 | + |
| 10 | + let stmt = client.prepare("SELECT 1::INT, $1::TEXT").unwrap(); |
| 11 | + assert_eq!(stmt.params(), &[Type::TEXT]); |
| 12 | + assert_eq!(stmt.columns().len(), 2); |
| 13 | + assert_eq!(stmt.columns()[0].type_(), &Type::INT4); |
| 14 | + assert_eq!(stmt.columns()[1].type_(), &Type::TEXT); |
| 15 | +} |
| 16 | + |
| 17 | +#[test] |
| 18 | +fn query_prepared() { |
| 19 | + let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap(); |
| 20 | + |
| 21 | + let stmt = client.prepare("SELECT $1::TEXT").unwrap(); |
| 22 | + let rows = client.query(&stmt, &[&"hello"]).unwrap(); |
| 23 | + assert_eq!(rows.len(), 1); |
| 24 | + assert_eq!(rows[0].get::<_, &str>(0), "hello"); |
| 25 | +} |
| 26 | + |
| 27 | +#[test] |
| 28 | +fn query_unprepared() { |
| 29 | + let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap(); |
| 30 | + |
| 31 | + let rows = client.query("SELECT $1::TEXT", &[&"hello"]).unwrap(); |
| 32 | + assert_eq!(rows.len(), 1); |
| 33 | + assert_eq!(rows[0].get::<_, &str>(0), "hello"); |
| 34 | +} |
| 35 | + |
| 36 | +#[test] |
| 37 | +fn transaction_commit() { |
| 38 | + let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap(); |
| 39 | + |
| 40 | + client |
| 41 | + .batch_execute("CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY)") |
| 42 | + .unwrap(); |
| 43 | + |
| 44 | + let mut transaction = client.transaction().unwrap(); |
| 45 | + |
| 46 | + transaction |
| 47 | + .execute("INSERT INTO foo DEFAULT VALUES", &[]) |
| 48 | + .unwrap(); |
| 49 | + |
| 50 | + transaction.commit().unwrap(); |
| 51 | + |
| 52 | + let rows = client.query("SELECT * FROM foo", &[]).unwrap(); |
| 53 | + assert_eq!(rows.len(), 1); |
| 54 | + assert_eq!(rows[0].get::<_, i32>(0), 1); |
| 55 | +} |
| 56 | + |
| 57 | +#[test] |
| 58 | +fn transaction_rollback() { |
| 59 | + let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap(); |
| 60 | + |
| 61 | + client |
| 62 | + .batch_execute("CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY)") |
| 63 | + .unwrap(); |
| 64 | + |
| 65 | + let mut transaction = client.transaction().unwrap(); |
| 66 | + |
| 67 | + transaction |
| 68 | + .execute("INSERT INTO foo DEFAULT VALUES", &[]) |
| 69 | + .unwrap(); |
| 70 | + |
| 71 | + transaction.rollback().unwrap(); |
| 72 | + |
| 73 | + let rows = client.query("SELECT * FROM foo", &[]).unwrap(); |
| 74 | + assert_eq!(rows.len(), 0); |
| 75 | +} |
| 76 | + |
| 77 | +#[test] |
| 78 | +fn transaction_drop() { |
| 79 | + let mut client = Client::connect("host=localhost port=5433 user=postgres", NoTls).unwrap(); |
| 80 | + |
| 81 | + client |
| 82 | + .batch_execute("CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY)") |
| 83 | + .unwrap(); |
| 84 | + |
| 85 | + let mut transaction = client.transaction().unwrap(); |
| 86 | + |
| 87 | + transaction |
| 88 | + .execute("INSERT INTO foo DEFAULT VALUES", &[]) |
| 89 | + .unwrap(); |
| 90 | + |
| 91 | + drop(transaction); |
| 92 | + |
| 93 | + let rows = client.query("SELECT * FROM foo", &[]).unwrap(); |
| 94 | + assert_eq!(rows.len(), 0); |
| 95 | +} |
0 commit comments