Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc9da397

Browse files
committed
added comments to tests and more cases
1 parentd6c9d63 commitc9da397

File tree

1 file changed

+56
-17
lines changed

1 file changed

+56
-17
lines changed

‎tokio-postgres/tests/test/main.rs‎

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -905,34 +905,53 @@ async fn query_typed_one() {
905905
.batch_execute(
906906
"
907907
CREATE TEMPORARY TABLE foo (
908-
name TEXT
908+
name TEXT,
909+
age INT
909910
);
910-
INSERT INTO foo (name) VALUES ('alice'), ('bob'), ('carol');
911+
INSERT INTO foo (name, age) VALUES ('alice', 20), ('bob', 30), ('carol', 40);
911912
",
912913
)
913914
.await
914915
.unwrap();
915916

916-
client
917+
// should return exactly one row
918+
let row = client
917919
.query_typed_one(
918-
"SELECT*FROM foo WHERE name = $1",
919-
&[(&"dave",Type::TEXT)],
920+
"SELECTname, ageFROM foo WHERE name = $1 AND age = $2",
921+
&[(&"alice",Type::TEXT),(&20i32,Type::INT4)],
920922
)
921923
.await
922-
.err()
923924
.unwrap();
925+
926+
assert_eq!(row.get::<_,&str>(0),"alice");
927+
assert_eq!(row.get::<_,i32>(1),20);
928+
929+
// dave doesn't exist, hence should return no row
924930
client
925931
.query_typed_one(
926932
"SELECT * FROM foo WHERE name = $1",
927-
&[(&"alice",Type::TEXT)],
933+
&[(&"dave",Type::TEXT)],
928934
)
929935
.await
936+
.err()
930937
.unwrap();
938+
939+
// should return error if the number of rows returned is not exactly one or zero
931940
client
932941
.query_typed_one("SELECT * FROM foo",&[])
933942
.await
934943
.err()
935944
.unwrap();
945+
946+
// should be none because no row is returned
947+
client
948+
.query_typed_one(
949+
"INSERT INTO foo (name, age) VALUES ($1, $2)",
950+
&[(&"dave",Type::TEXT),(&45i32,Type::INT4)],
951+
)
952+
.await
953+
.err()
954+
.unwrap();
936955
}
937956

938957
#[tokio::test]
@@ -967,6 +986,7 @@ async fn query_opt() {
967986
.err()
968987
.unwrap();
969988
}
989+
970990
#[tokio::test]
971991
asyncfnquery_typed_opt(){
972992
let client =connect("user=postgres").await;
@@ -975,35 +995,54 @@ async fn query_typed_opt() {
975995
.batch_execute(
976996
"
977997
CREATE TEMPORARY TABLE foo (
978-
name TEXT
998+
name TEXT,
999+
age INT
9791000
);
980-
INSERT INTO foo (name) VALUES ('alice'), ('bob'), ('carol');
1001+
INSERT INTO foo (name, age) VALUES ('alice', 20), ('bob', 30), ('carol', 40);
9811002
",
9821003
)
9831004
.await
9841005
.unwrap();
9851006

986-
assert!(client
1007+
// should return exactly one row
1008+
let row = client
9871009
.query_typed_opt(
988-
"SELECT*FROM foo WHERE name = $1",
989-
&[(&"dave",Type::TEXT)]
1010+
"SELECTname, ageFROM foo WHERE name = $1 AND age = $2",
1011+
&[(&"alice",Type::TEXT),(&20i32,Type::INT4)],
9901012
)
9911013
.await
9921014
.unwrap()
993-
.is_none());
994-
client
1015+
.unwrap();
1016+
1017+
assert_eq!(row.get::<_,&str>(0),"alice");
1018+
assert_eq!(row.get::<_,i32>(1),20);
1019+
1020+
// dave doesn't exist, hence should return no row
1021+
assert!(client
9951022
.query_typed_opt(
9961023
"SELECT * FROM foo WHERE name = $1",
997-
&[(&"alice",Type::TEXT)],
1024+
&[(&"dave",Type::TEXT)]
9981025
)
9991026
.await
10001027
.unwrap()
1001-
.unwrap();
1028+
.is_none());
1029+
1030+
// should return error if the number of rows returned is not exactly one or zero
10021031
client
1003-
.query_typed_one("SELECT * FROM foo",&[])
1032+
.query_typed_opt("SELECT * FROM foo",&[])
10041033
.await
10051034
.err()
10061035
.unwrap();
1036+
1037+
// should be none because no row is returned
1038+
assert!(client
1039+
.query_typed_opt(
1040+
"INSERT INTO foo (name, age) VALUES ($1, $2)",
1041+
&[(&"dave",Type::TEXT),(&45i32,Type::INT4)],
1042+
)
1043+
.await
1044+
.unwrap()
1045+
.is_none());
10071046
}
10081047

10091048
#[tokio::test]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp