Skip to content

Commit ef589ce

Browse files
author
Montana Low
committed
fix test suite
1 parent b3ae6e5 commit ef589ce

File tree

8 files changed

+74
-36
lines changed

8 files changed

+74
-36
lines changed

sqlx-core/src/postgres/connection/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ impl PgConnection {
100100

101101
Ok(())
102102
}
103+
104+
pub async fn server_version(&mut self) -> Result<String, Error> {
105+
let result = self.fetch_one("SHOW server_version;",).await?;
106+
let server_version: String = result.get("server_version");
107+
108+
Ok(server_version)
109+
}
110+
111+
pub async fn server_major_version(&mut self) -> Result<i32, Error> {
112+
let server_version = self.server_version().await?;
113+
let first = server_version.split(".").next().unwrap();
114+
let major_version = first.parse::<i32>().unwrap();
115+
116+
Ok(major_version)
117+
}
103118
}
104119

105120
impl Debug for PgConnection {

tests/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
### Running Tests
4+
SQLx uses docker to run many compatible database systems for integration testing. You'll need to [install docker](https://docs.docker.com/engine/) to run the full suite. You can validate your docker installation with:
5+
6+
$ docker run hello-world
7+
8+
Start the databases with `docker-compose` before running tests:
9+
10+
$ docker-compose up
11+
12+
Run all tests against all supported databases using:
13+
14+
$ ./x.py
15+
16+
If you see test failures, or want to run a more specific set of tests against a specific database, you can specify both the features to be tests and the DATABASE_URL. e.g.
17+
18+
$ DATABASE_URL=mysql://root:[email protected]:49183/sqlx cargo test --no-default-features --features macros,offline,any,all-types,mysql,runtime-async-std-native-tls

tests/docker.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import subprocess
2+
import sys
23
import time
34
from os import path
45

tests/mysql/macros.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,19 @@ async fn with_test_row<'a>(
190190
conn: &'a mut MySqlConnection,
191191
) -> anyhow::Result<Transaction<'a, MySql>> {
192192
let mut transaction = conn.begin().await?;
193-
sqlx::query!("INSERT INTO tweet(id, text, owner_id) VALUES (1, '#sqlx is pretty cool!', 1)")
193+
sqlx::query!("INSERT INTO tweet(text, owner_id) VALUES ('#sqlx is pretty cool!', 1)")
194194
.execute(&mut transaction)
195195
.await?;
196196
Ok(transaction)
197197
}
198198

199+
async fn last_insert_id(conn: &mut MySqlConnection) -> anyhow::Result<MyInt> {
200+
let result = sqlx::query!("SELECT last_insert_id() AS last_insert_id")
201+
.fetch_one(conn)
202+
.await?;
203+
Ok(MyInt(result.last_insert_id as i64))
204+
}
205+
199206
#[derive(PartialEq, Eq, Debug, sqlx::Type)]
200207
#[sqlx(transparent)]
201208
struct MyInt(i64);
@@ -212,12 +219,13 @@ struct OptionalRecord {
212219
async fn test_column_override_wildcard() -> anyhow::Result<()> {
213220
let mut conn = new::<MySql>().await?;
214221
let mut conn = with_test_row(&mut conn).await?;
222+
let id = last_insert_id(&mut conn).await?;
215223

216224
let record = sqlx::query_as!(Record, "select id as `id: _` from tweet")
217225
.fetch_one(&mut conn)
218226
.await?;
219227

220-
assert_eq!(record.id, MyInt(1));
228+
assert_eq!(record.id, id);
221229

222230
// this syntax is also useful for expressions
223231
let record = sqlx::query_as!(Record, "select * from (select 1 as `id: _`) records")
@@ -253,12 +261,13 @@ async fn test_column_override_wildcard_not_null() -> anyhow::Result<()> {
253261
async fn test_column_override_wildcard_nullable() -> anyhow::Result<()> {
254262
let mut conn = new::<MySql>().await?;
255263
let mut conn = with_test_row(&mut conn).await?;
264+
let id = last_insert_id(&mut conn).await?;
256265

257266
let record = sqlx::query_as!(OptionalRecord, "select id as `id?: _` from tweet")
258267
.fetch_one(&mut conn)
259268
.await?;
260269

261-
assert_eq!(record.id, Some(MyInt(1)));
270+
assert_eq!(record.id, Some(id));
262271

263272
Ok(())
264273
}
@@ -267,12 +276,13 @@ async fn test_column_override_wildcard_nullable() -> anyhow::Result<()> {
267276
async fn test_column_override_exact() -> anyhow::Result<()> {
268277
let mut conn = new::<MySql>().await?;
269278
let mut conn = with_test_row(&mut conn).await?;
279+
let id = last_insert_id(&mut conn).await?;
270280

271281
let record = sqlx::query!("select id as `id: MyInt` from tweet")
272282
.fetch_one(&mut conn)
273283
.await?;
274284

275-
assert_eq!(record.id, MyInt(1));
285+
assert_eq!(record.id, id);
276286

277287
// we can also support this syntax for expressions
278288
let record = sqlx::query!("select * from (select 1 as `id: MyInt`) records")
@@ -308,12 +318,13 @@ async fn test_column_override_exact_not_null() -> anyhow::Result<()> {
308318
async fn test_column_override_exact_nullable() -> anyhow::Result<()> {
309319
let mut conn = new::<MySql>().await?;
310320
let mut conn = with_test_row(&mut conn).await?;
321+
let id = last_insert_id(&mut conn).await?;
311322

312323
let record = sqlx::query!("select id as `id?: MyInt` from tweet")
313324
.fetch_one(&mut conn)
314325
.await?;
315326

316-
assert_eq!(record.id, Some(MyInt(1)));
327+
assert_eq!(record.id, Some(id));
317328

318329
Ok(())
319330
}

tests/postgres/postgres.rs

+15-26
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,12 @@ async fn test_listener_cleanup() -> anyhow::Result<()> {
968968

969969
#[sqlx_macros::test]
970970
async fn it_supports_domain_types_in_composite_domain_types() -> anyhow::Result<()> {
971+
// Only supported in Postgres 11+
972+
let mut conn = new::<Postgres>().await?;
973+
if !(conn.server_major_version().await? >= 11) {
974+
return Ok(());
975+
}
976+
971977
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
972978
struct MonthId(i16);
973979

@@ -1040,40 +1046,23 @@ async fn it_supports_domain_types_in_composite_domain_types() -> anyhow::Result<
10401046
}
10411047
}
10421048

1043-
let mut conn = new::<Postgres>().await?;
1044-
1045-
{
1046-
let result = sqlx::query("DELETE FROM heating_bills;")
1047-
.execute(&mut conn)
1048-
.await;
1049-
1050-
let result = result.unwrap();
1051-
assert_eq!(result.rows_affected(), 1);
1052-
}
1049+
// Ensure the table is empty to begin to avoid CPK violations from repeat runs
1050+
sqlx::query("DELETE FROM heating_bills;")
1051+
.execute(&mut conn)
1052+
.await;
10531053

1054-
{
1055-
let result = sqlx::query(
1056-
"INSERT INTO heating_bills(month, cost) VALUES($1::winter_year_month, 100);",
1057-
)
1054+
let result = sqlx::query(
1055+
"INSERT INTO heating_bills(month, cost) VALUES($1::winter_year_month, 100);",
1056+
)
10581057
.bind(WinterYearMonth {
10591058
year: 2021,
10601059
month: MonthId(1),
10611060
})
10621061
.execute(&mut conn)
10631062
.await;
10641063

1065-
let result = result.unwrap();
1066-
assert_eq!(result.rows_affected(), 1);
1067-
}
1068-
1069-
{
1070-
let result = sqlx::query("DELETE FROM heating_bills;")
1071-
.execute(&mut conn)
1072-
.await;
1073-
1074-
let result = result.unwrap();
1075-
assert_eq!(result.rows_affected(), 1);
1076-
}
1064+
let result = result.unwrap();
1065+
assert_eq!(result.rows_affected(), 1);
10771066

10781067
Ok(())
10791068
}

tests/postgres/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ test_type!(ipnetwork_vec<Vec<sqlx::types::ipnetwork::IpNetwork>>(Postgres,
211211

212212
#[cfg(feature = "mac_address")]
213213
test_type!(mac_address_vec<Vec<sqlx::types::mac_address::MacAddress>>(Postgres,
214-
"'{01:02:03:04:05:06,FF:FF:FF:FF:FF:FF}'::inet[]"
214+
"'{01:02:03:04:05:06,FF:FF:FF:FF:FF:FF}'::macaddr[]"
215215
== vec![
216216
"01:02:03:04:05:06".parse::<sqlx::types::mac_address::MacAddress>().unwrap(),
217217
"FF:FF:FF:FF:FF:FF".parse::<sqlx::types::mac_address::MacAddress>().unwrap()

tests/sqlite/sqlite.db

0 Bytes
Binary file not shown.

tests/x.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ def run(command, comment=None, env=None, service=None, tag=None, args=None, data
8888
# check
8989
#
9090

91-
run("cargo c", comment="check with a default set of features", tag="check")
92-
9391
run(
9492
"cargo c --no-default-features --features runtime-async-std-native-tls,all-databases,all-types,offline,macros",
9593
comment="check with async-std",
@@ -113,9 +111,9 @@ def run(command, comment=None, env=None, service=None, tag=None, args=None, data
113111
#
114112

115113
run(
116-
"cargo test --manifest-path sqlx-core/Cargo.toml --features all-databases,all-types",
114+
"cargo test --no-default-features --manifest-path sqlx-core/Cargo.toml --features all-databases,all-types,runtime-async-std-native-tls",
117115
comment="unit test core",
118-
tag="unit"
116+
tag="unit_async_std"
119117
)
120118

121119
run(
@@ -124,6 +122,12 @@ def run(command, comment=None, env=None, service=None, tag=None, args=None, data
124122
tag="unit_tokio"
125123
)
126124

125+
run(
126+
"cargo test --no-default-features --manifest-path sqlx-core/Cargo.toml --features all-databases,all-types,runtime-actix-native-tls",
127+
comment="unit test core",
128+
tag="unit_actix"
129+
)
130+
127131
#
128132
# integration tests
129133
#

0 commit comments

Comments
 (0)