Skip to content

Commit 48af741

Browse files
committed
Add a simple_query test
1 parent 32e09db commit 48af741

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

tokio-postgres/tests/test/main.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use tokio::timer::Delay;
1313
use tokio_postgres::error::SqlState;
1414
use tokio_postgres::impls;
1515
use tokio_postgres::types::{Kind, Type};
16-
use tokio_postgres::{AsyncMessage, Client, Connection, NoTls, NoTlsStream};
16+
use tokio_postgres::{AsyncMessage, Client, Connection, NoTls, NoTlsStream, SimpleQueryMessage};
1717

1818
mod parse;
1919
#[cfg(feature = "runtime")]
@@ -737,6 +737,56 @@ fn transaction_builder_around_moved_client() {
737737
runtime.run().unwrap();
738738
}
739739

740+
#[test]
741+
fn simple_query() {
742+
let _ = env_logger::try_init();
743+
let mut runtime = Runtime::new().unwrap();
744+
745+
let (mut client, connection) = runtime.block_on(connect("user=postgres")).unwrap();
746+
let connection = connection.map_err(|e| panic!("{}", e));
747+
runtime.handle().spawn(connection).unwrap();
748+
749+
let f = client
750+
.simple_query(
751+
"CREATE TEMPORARY TABLE foo (
752+
id SERIAL,
753+
name TEXT
754+
);
755+
INSERT INTO foo (name) VALUES ('steven'), ('joe');
756+
SELECT * FROM foo ORDER BY id;",
757+
)
758+
.collect();
759+
let messages = runtime.block_on(f).unwrap();
760+
761+
match messages[0] {
762+
SimpleQueryMessage::CommandComplete(0) => {}
763+
_ => panic!("unexpected message"),
764+
}
765+
match messages[1] {
766+
SimpleQueryMessage::CommandComplete(2) => {}
767+
_ => panic!("unexpected message"),
768+
}
769+
match &messages[2] {
770+
SimpleQueryMessage::Row(row) => {
771+
assert_eq!(row.get(0), Some("1"));
772+
assert_eq!(row.get(1), Some("steven"));
773+
}
774+
_ => panic!("unexpected message"),
775+
}
776+
match &messages[3] {
777+
SimpleQueryMessage::Row(row) => {
778+
assert_eq!(row.get(0), Some("2"));
779+
assert_eq!(row.get(1), Some("joe"));
780+
}
781+
_ => panic!("unexpected message"),
782+
}
783+
match messages[4] {
784+
SimpleQueryMessage::CommandComplete(2) => {}
785+
_ => panic!("unexpected message"),
786+
}
787+
assert_eq!(messages.len(), 5);
788+
}
789+
740790
#[test]
741791
fn poll_idle_running() {
742792
struct DelayStream(Delay);

0 commit comments

Comments
 (0)