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
use anyhow::Result;
use itertools::Itertools;
use sqlx::{
migrate::{MigrationSource, Migrator},
Connection, Executor, PgConnection, PgPool,
};
use std::{path::Path, thread};
use tokio::runtime::Runtime;
use uuid::Uuid;
#[derive(Debug)]
pub struct TestPg {
pub server_url: String,
pub dbname: String,
}
impl TestPg {
pub fn new<S>(server_url: String, migrations: S) -> Self
where
S: MigrationSource<'static> + Send + Sync + 'static,
{
let uuid = Uuid::new_v4();
let dbname = format!("test_{uuid}");
let dbname_cloned = dbname.clone();
let tdb = Self { server_url, dbname };
let server_url = tdb.server_url();
let url = tdb.url();
thread::spawn(move || {
let rt = Runtime::new().unwrap();
rt.block_on(async move {
let mut conn = PgConnection::connect(&server_url).await.unwrap();
conn.execute(format!(r#"CREATE DATABASE "{dbname_cloned}""#).as_str())
.await
.unwrap();
let mut conn = PgConnection::connect(&url).await.unwrap();
let m = Migrator::new(migrations).await.unwrap();
m.run(&mut conn).await.unwrap();
});
})
.join()
.expect("failed to create database");
tdb
}
pub fn server_url(&self) -> String {
self.server_url.clone()
}
pub fn url(&self) -> String {
format!("{}/{}", self.server_url, self.dbname)
}
pub async fn get_pool(&self) -> PgPool {
PgPool::connect(&self.url()).await.unwrap()
}
pub async fn load_csv(&self, table: &str, fields: &[&str], filename: &Path) -> Result<()> {
let pool = self.get_pool().await;
let path = filename.canonicalize()?;
let mut conn = pool.acquire().await?;
let sql = format!(
"COPY {} ({}) FROM '{}' DELIMITER ',' CSV HEADER;",
table,
fields.join(","),
path.display()
);
conn.execute(sql.as_str()).await?;
Ok(())
}
pub async fn load_csv_data(&self, table: &str, csv: &str) -> Result<()> {
let mut rdr = csv::Reader::from_reader(csv.as_bytes());
let headers = rdr.headers()?.iter().join(",");
let mut tx = self.get_pool().await.begin().await?;
for result in rdr.records() {
let record = result?;
let sql = format!(
"INSERT INTO {} ({}) VALUES ({})",
table,
headers,
record.iter().map(|v| format!("'{v}'")).join(",")
);
tx.execute(sql.as_str()).await?;
}
tx.commit().await?;
Ok(())
}
}
impl Drop for TestPg {
fn drop(&mut self) {
let server_url = self.server_url();
let dbname = self.dbname.clone();
thread::spawn(move || {
let rt = Runtime::new().unwrap();
rt.block_on(async move {
let mut conn = PgConnection::connect(&server_url).await.unwrap();
sqlx::query(&format!(r#"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid() AND datname = '{dbname}'"#))
.execute( &mut conn)
.await
.expect("Terminate all other connections");
conn.execute(format!(r#"DROP DATABASE "{dbname}""#).as_str())
.await
.expect("Error while querying the drop database");
});
})
.join()
.expect("failed to drop database");
}
}
impl Default for TestPg {
fn default() -> Self {
Self::new(
"postgres://postgres:postgres@localhost:5432".to_string(),
Path::new("./fixtures/migrations"),
)
}
}
#[cfg(test)]
mod tests {
use std::path::Path;
use crate::postgres::TestPg;
use anyhow::Result;
#[tokio::test]
async fn test_postgres_should_create_and_drop() {
let tdb = TestPg::default();
let pool = tdb.get_pool().await;
sqlx::query("INSERT INTO todos (title) VALUES ('test')")
.execute(&pool)
.await
.unwrap();
let (id, title) = sqlx::query_as::<_, (i32, String)>("SELECT id, title FROM todos")
.fetch_one(&pool)
.await
.unwrap();
assert_eq!(id, 1);
assert_eq!(title, "test");
}
#[tokio::test]
async fn test_postgres_should_load_csv() -> Result<()> {
let filename = Path::new("./fixtures/todos.csv");
let tdb = TestPg::default();
tdb.load_csv("todos", &["title"], filename).await?;
let pool = tdb.get_pool().await;
let (id, title) = sqlx::query_as::<_, (i32, String)>("SELECT id, title FROM todos")
.fetch_one(&pool)
.await
.unwrap();
assert_eq!(id, 1);
assert_eq!(title, "hello world");
Ok(())
}
#[tokio::test]
async fn test_postgres_should_load_csv_data() -> Result<()> {
let csv = include_str!("../fixtures/todos.csv");
let tdb = TestPg::default();
tdb.load_csv_data("todos", csv).await?;
let pool = tdb.get_pool().await;
let (id, title) = sqlx::query_as::<_, (i32, String)>("SELECT id, title FROM todos")
.fetch_one(&pool)
.await
.unwrap();
assert_eq!(id, 1);
assert_eq!(title, "hello world");
Ok(())
}
}