-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
lib.rs
224 lines (184 loc) 路 6.94 KB
/
lib.rs
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use sqlx::pool::PoolOptions;
use sqlx::{Connection, Database, Pool};
use std::env;
pub fn setup_if_needed() {
let _ = dotenv::dotenv();
let _ = env_logger::builder().is_test(true).try_init();
}
// Make a new connection
// Ensure [dotenv] and [env_logger] have been setup
pub async fn new<DB>() -> anyhow::Result<DB::Connection>
where
DB: Database,
{
setup_if_needed();
Ok(DB::Connection::connect(&env::var("DATABASE_URL")?).await?)
}
// Make a new pool
// Ensure [dotenv] and [env_logger] have been setup
pub async fn pool<DB>() -> anyhow::Result<Pool<DB>>
where
DB: Database,
{
setup_if_needed();
let pool = PoolOptions::<DB>::new()
.min_connections(0)
.max_connections(5)
.test_before_acquire(true)
.connect(&env::var("DATABASE_URL")?)
.await?;
Ok(pool)
}
// Test type encoding and decoding
#[macro_export]
macro_rules! test_type {
($name:ident<$ty:ty>($db:ident, $sql:literal, $($text:literal == $value:expr),+ $(,)?)) => {
$crate::__test_prepared_type!($name<$ty>($db, $sql, $($text == $value),+));
$crate::test_unprepared_type!($name<$ty>($db, $($text == $value),+));
};
($name:ident<$ty:ty>($db:ident, $($text:literal == $value:expr),+ $(,)?)) => {
paste::item! {
$crate::__test_prepared_type!($name<$ty>($db, $crate::[< $db _query_for_test_prepared_type >]!(), $($text == $value),+));
$crate::test_unprepared_type!($name<$ty>($db, $($text == $value),+));
}
};
($name:ident($db:ident, $($text:literal == $value:expr),+ $(,)?)) => {
$crate::test_type!($name<$name>($db, $($text == $value),+));
};
}
// Test type decoding only
#[macro_export]
macro_rules! test_decode_type {
($name:ident<$ty:ty>($db:ident, $($text:literal == $value:expr),+ $(,)?)) => {
$crate::__test_prepared_decode_type!($name<$ty>($db, $($text == $value),+));
$crate::test_unprepared_type!($name<$ty>($db, $($text == $value),+));
};
($name:ident($db:ident, $($text:literal == $value:expr),+ $(,)?)) => {
$crate::test_decode_type!($name<$name>($db, $($text == $value),+));
};
}
// Test type encoding and decoding
#[macro_export]
macro_rules! test_prepared_type {
($name:ident<$ty:ty>($db:ident, $sql:literal, $($text:literal == $value:expr),+ $(,)?)) => {
$crate::__test_prepared_type!($name<$ty>($db, $sql, $($text == $value),+));
};
($name:ident<$ty:ty>($db:ident, $($text:literal == $value:expr),+ $(,)?)) => {
paste::item! {
$crate::__test_prepared_type!($name<$ty>($db, $crate::[< $db _query_for_test_prepared_type >]!(), $($text == $value),+));
}
};
($name:ident($db:ident, $($text:literal == $value:expr),+ $(,)?)) => {
$crate::__test_prepared_type!($name<$name>($db, $($text == $value),+));
};
}
// Test type decoding for the simple (unprepared) query API
#[macro_export]
macro_rules! test_unprepared_type {
($name:ident<$ty:ty>($db:ident, $($text:literal == $value:expr),+ $(,)?)) => {
paste::item! {
#[sqlx_macros::test]
async fn [< test_unprepared_type_ $name >] () -> anyhow::Result<()> {
use sqlx::prelude::*;
use futures::TryStreamExt;
let mut conn = sqlx_test::new::<$db>().await?;
$(
let query = format!("SELECT {}", $text);
let mut s = conn.fetch(&*query);
let row = s.try_next().await?.unwrap();
let rec = row.try_get::<$ty, _>(0)?;
assert_eq!($value, rec);
drop(s);
)+
Ok(())
}
}
}
}
// Test type decoding only for the prepared query API
#[macro_export]
macro_rules! __test_prepared_decode_type {
($name:ident<$ty:ty>($db:ident, $($text:literal == $value:expr),+ $(,)?)) => {
paste::item! {
#[sqlx_macros::test]
async fn [< test_prepared_decode_type_ $name >] () -> anyhow::Result<()> {
use sqlx::Row;
let mut conn = sqlx_test::new::<$db>().await?;
$(
let query = format!("SELECT {}", $text);
let row = sqlx::query(&query)
.fetch_one(&mut conn)
.await?;
let rec: $ty = row.try_get(0)?;
assert_eq!($value, rec);
)+
Ok(())
}
}
};
}
// Test type encoding and decoding for the prepared query API
#[macro_export]
macro_rules! __test_prepared_type {
($name:ident<$ty:ty>($db:ident, $sql:expr, $($text:literal == $value:expr),+ $(,)?)) => {
paste::item! {
#[sqlx_macros::test]
async fn [< test_prepared_type_ $name >] () -> anyhow::Result<()> {
use sqlx::Row;
let mut conn = sqlx_test::new::<$db>().await?;
$(
let query = format!($sql, $text);
let row = sqlx::query(&query)
.bind($value)
.bind($value)
.fetch_one(&mut conn)
.await?;
let matches: i32 = row.try_get(0)?;
let returned: $ty = row.try_get(1)?;
let round_trip: $ty = row.try_get(2)?;
assert!(matches != 0,
"[1] DB value mismatch; given value: {:?}\n\
as returned: {:?}\n\
round-trip: {:?}",
$value, returned, round_trip);
assert_eq!($value, returned,
"[2] DB value mismatch; given value: {:?}\n\
as returned: {:?}\n\
round-trip: {:?}",
$value, returned, round_trip);
assert_eq!($value, round_trip,
"[3] DB value mismatch; given value: {:?}\n\
as returned: {:?}\n\
round-trip: {:?}",
$value, returned, round_trip);
)+
Ok(())
}
}
};
}
#[macro_export]
macro_rules! MySql_query_for_test_prepared_type {
() => {
// MySQL 8.0.27 changed `<=>` to return an unsigned integer
"SELECT CAST({0} <=> ? AS SIGNED INTEGER), {0}, ?"
};
}
#[macro_export]
macro_rules! Mssql_query_for_test_prepared_type {
() => {
"SELECT CASE WHEN {0} IS NULL AND @p1 IS NULL THEN 1 WHEN {0} = @p1 THEN 1 ELSE 0 END, {0}, @p2"
};
}
#[macro_export]
macro_rules! Sqlite_query_for_test_prepared_type {
() => {
"SELECT {0} is ?, {0}, ?"
};
}
#[macro_export]
macro_rules! Postgres_query_for_test_prepared_type {
() => {
"SELECT ({0} is not distinct from $1)::int4, {0}, $2"
};
}