Skip to content

Commit db3612b

Browse files
change env var, add dotenv, load into test_db
1 parent 75d6acb commit db3612b

File tree

5 files changed

+41
-28
lines changed

5 files changed

+41
-28
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
DATABASE_URL=postgresql://postgres:postgres@127.0.0.1:5432/postgres
1+
DB_CONNECTION_STRING=postgresql://postgres:postgres@127.0.0.1:5432/postgres

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pg_completions/src/lib.rs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,18 @@ pub fn complete<'a>(params: &'a CompletionParams<'a>) -> CompletionResult<'a> {
6363

6464
#[cfg(test)]
6565
mod tests {
66-
use async_std::task::block_on;
6766
use pg_schema_cache::SchemaCache;
6867
use pg_test_utils::test_database::*;
6968

70-
use sqlx::PgPool;
69+
use sqlx::Executor;
7170

7271
use crate::{complete, CompletionParams};
7372

74-
#[test]
75-
fn test_complete() {
76-
let input = "select id from c;";
77-
78-
let conn_string = std::env::var("DB_CONNECTION_STRING").unwrap();
73+
#[tokio::test]
74+
async fn test_complete() {
75+
let pool = get_new_test_db().await;
7976

80-
let pool = block_on(PgPool::connect(conn_string.as_str())).unwrap();
77+
let input = "select id from c;";
8178

8279
let mut parser = tree_sitter::Parser::new();
8380
parser
@@ -86,7 +83,7 @@ mod tests {
8683

8784
let tree = parser.parse(input, None).unwrap();
8885

89-
let schema_cache = block_on(SchemaCache::load(&pool));
86+
let schema_cache = SchemaCache::load(&pool).await;
9087

9188
let p = CompletionParams {
9289
position: 15.into(),
@@ -100,21 +97,19 @@ mod tests {
10097
assert!(result.items.len() > 0);
10198
}
10299

103-
#[test]
104-
fn test_complete_two() {
105-
let input = "select id, name, test1231234123, unknown from co;";
106-
107-
let conn_string = std::env::var("DB_CONNECTION_STRING").unwrap();
100+
#[tokio::test]
101+
async fn test_complete_two() {
102+
let pool = get_new_test_db().await;
108103

109-
let pool = block_on(PgPool::connect(conn_string.as_str())).unwrap();
104+
let input = "select id, name, test1231234123, unknown from co;";
110105

111106
let mut parser = tree_sitter::Parser::new();
112107
parser
113108
.set_language(tree_sitter_sql::language())
114109
.expect("Error loading sql language");
115110

116111
let tree = parser.parse(input, None).unwrap();
117-
let schema_cache = block_on(SchemaCache::load(&pool));
112+
let schema_cache = SchemaCache::load(&pool).await;
118113

119114
let p = CompletionParams {
120115
position: 47.into(),
@@ -128,8 +123,10 @@ mod tests {
128123
assert!(result.items.len() > 0);
129124
}
130125

131-
#[test]
132-
fn test_complete_with_db() {
126+
#[tokio::test]
127+
async fn test_complete_with_db() {
128+
let test_db = get_new_test_db().await;
129+
133130
let setup = r#"
134131
create table users (
135132
id serial primary key,
@@ -138,23 +135,23 @@ mod tests {
138135
);
139136
"#;
140137

141-
let input = "select * from u";
142-
143-
let conn_string = std::env::var("DB_CONNECTION_STRING").unwrap();
144-
let password = std::env::var("DB_PASSWORD").unwrap_or("postgres".into());
138+
test_db
139+
.execute(setup)
140+
.await
141+
.expect("Failed to execute setup query");
145142

146-
let test_db = block_on(get_new_test_db(conn_string, password));
143+
let input = "select * from u";
147144

148145
let mut parser = tree_sitter::Parser::new();
149146
parser
150147
.set_language(tree_sitter_sql::language())
151148
.expect("Error loading sql language");
152149

153150
let tree = parser.parse(input, None).unwrap();
154-
let schema_cache = block_on(SchemaCache::load(&pool));
151+
let schema_cache = SchemaCache::load(&test_db).await;
155152

156153
let p = CompletionParams {
157-
position: 47.into(),
154+
position: ((input.len() - 1) as u32).into(),
158155
schema: &schema_cache,
159156
text: input,
160157
tree: Some(&tree),

crates/pg_test_utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ anyhow = "1.0.81"
88
uuid = { version = "1.11.0", features = ["v4"] }
99

1010
sqlx.workspace = true
11+
dotenv = "0.15.0"

crates/pg_test_utils/src/test_database.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ use uuid::Uuid;
33

44
// TODO: Work with proper config objects instead of a connection_string.
55
// With the current implementation, we can't parse the password from the connection string.
6-
pub async fn get_new_test_db(connection_string: String, database_password: String) -> PgPool {
6+
pub async fn get_new_test_db() -> PgPool {
7+
dotenv::dotenv()
8+
.ok()
9+
.expect("Unable to load .env file for tests");
10+
11+
let connection_string =
12+
std::env::var("DB_CONNECTION_STRING").expect("DB_CONNECTION_STRING not set");
13+
let password = std::env::var("DB_PASSWORD").unwrap_or("postgres".into());
14+
715
let options_from_conn_str: PgConnectOptions = connection_string
816
.parse()
917
.expect("Invalid Connection String");
@@ -18,7 +26,7 @@ pub async fn get_new_test_db(connection_string: String, database_password: Strin
1826
.host(host)
1927
.port(options_from_conn_str.get_port())
2028
.username(options_from_conn_str.get_username())
21-
.password(&database_password);
29+
.password(&password);
2230

2331
let postgres = sqlx::PgPool::connect_with(options_without_db_name.clone())
2432
.await

0 commit comments

Comments
 (0)