1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! Integration test for the Diesel `connection::Instrumentation` front-end
//! against a real PostgreSQL server.
//!
//! Runs the `test-diesel` `basic_postgres` example as a subprocess and asserts
//! on its report. Requires the PostgreSQL container from the repo-root
//! compose.yml (`docker compose up -d postgres`, host port 5439). Skips locally
//! when nothing listens there; on CI the postgres service is mandatory, so a
//! missing server fails instead.
//!
//! The example uses `$1` positional placeholders, so this pins their `?`
//! normalization for the Diesel path, plus the ` -- binds: [..]` stripping on
//! Pg's `DebugQuery` Display output. The example writes to `diesel_users`
//! (not `users`) because the database persists across runs and is shared with
//! the sqlx postgres test binaries, which drop/recreate `users` - so the
//! asserted bucket texts differ from the sqlite ones by table name only.
#[cfg(test)]
pub mod tests {
use std::process::Command;
fn postgres_available() -> bool {
let addr = "127.0.0.1:5439".parse().unwrap();
let timeout = std::time::Duration::from_millis(500);
std::net::TcpStream::connect_timeout(&addr, timeout).is_ok()
}
fn skip_without_postgres(test: &str) -> bool {
if postgres_available() {
return false;
}
assert!(
std::env::var_os("CI").is_none(),
"no PostgreSQL on localhost:5439 - the CI postgres service is required"
);
eprintln!(
"skipping {test}: no PostgreSQL on localhost:5439 \
(start it with `docker compose up -d postgres`)"
);
true
}
fn run_basic(format: Option<&str>) -> String {
let mut cmd = Command::new("cargo");
cmd.args([
"run",
"-p",
"test-diesel",
"--example",
"basic_postgres",
"--features",
"hotpath,pg",
]);
if let Some(fmt) = format {
cmd.env("HOTPATH_OUTPUT_FORMAT", fmt);
}
let output = cmd.output().expect("Failed to execute command");
assert!(
output.status.success(),
"Command failed with status: {}",
output.status
);
String::from_utf8_lossy(&output.stdout).into_owned()
}
#[test]
fn test_table_output_postgres() {
if skip_without_postgres("test_table_output_postgres") {
return;
}
let stdout = run_basic(None);
let all_expected = [
"Diesel postgres instrumentation example completed!",
"sql - SQL query execution time statistics.",
// $1/$2 placeholders normalize to ?, so all 50 inserts share one bucket.
"INSERT INTO diesel_users (name, age) VALUES (?, ?)",
"SELECT id, name, age FROM diesel_users WHERE id = ?",
// Inline literals normalized into one bucket.
"SELECT name FROM diesel_users WHERE age = ?",
"SELECT COUNT(*) FROM diesel_users",
// Different-arity IN lists collapse to one bucket.
"SELECT * FROM diesel_users WHERE id IN (?)",
];
for expected in all_expected {
assert!(
stdout.contains(expected),
"Expected:\n{expected}\n\nGot:\n{stdout}",
);
}
// Transaction-control statements must not surface as query buckets.
for control in ["| BEGIN", "| COMMIT", "| ROLLBACK"] {
assert!(
!stdout.contains(control),
"Unexpected transaction-control bucket {control:?} in:\n{stdout}",
);
}
}
#[test]
fn test_transaction_query_captured_postgres() {
if skip_without_postgres("test_transaction_query_captured_postgres") {
return;
}
// 50 loop inserts + 1 transaction-internal insert = 51. The
// transaction-internal query is captured; BEGIN/COMMIT are not.
let stdout = run_basic(Some("json"));
let all_expected = [
"\"sql\"",
"\"INSERT INTO diesel_users (name, age) VALUES (?, ?)\"",
"\"count\":51",
];
for expected in all_expected {
assert!(
stdout.contains(expected),
"Expected:\n{expected}\n\nGot:\n{stdout}",
);
}
}
}