Skip to content

Commit 06466ac

Browse files
authored
chore: more tests (#218)
1 parent 9df9772 commit 06466ac

File tree

7 files changed

+133
-6
lines changed

7 files changed

+133
-6
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ crossbeam = "0.8.4"
2323
enumflags2 = "0.7.10"
2424
ignore = "0.4.23"
2525
indexmap = { version = "2.6.0", features = ["serde"] }
26+
insta = "1.31.0"
2627
line_index = { path = "./lib/line_index", version = "0.0.0" }
2728
pg_query = "6.0.0"
2829
proc-macro2 = "1.0.66"

crates/pglt_cli/src/commands/init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub(crate) fn init(mut session: CliSession) -> Result<(), CliDiagnostic> {
1010
let file_created = ConfigName::pglt_toml();
1111
session.app.console.log(markup! {
1212
"
13-
Welcome to the Postgres Language Server! Let's get you started...
13+
Welcome to the Postgres Language Tools! Let's get you started...
1414
1515
"<Info><Emphasis>"Files created "</Emphasis></Info>"
1616

crates/pglt_lexer/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ cstree = { version = "0.12.0", features = ["derive"] }
2121
text-size.workspace = true
2222

2323
[dev-dependencies]
24-
insta = "1.31.0"
24+
insta.workspace = true
2525

2626
[lib]
2727
doctest = false

crates/pglt_lsp/Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ tower-lsp = { version = "0.20.0" }
3333
tracing = { workspace = true, features = ["attributes"] }
3434

3535
[dev-dependencies]
36-
tokio = { workspace = true, features = ["macros"] }
37-
tower = { version = "0.4.13", features = ["timeout"] }
38-
36+
pglt_test_utils = { workspace = true }
37+
sqlx = { workspace = true }
38+
tokio = { workspace = true, features = ["macros"] }
39+
toml = { workspace = true }
40+
tower = { version = "0.4.13", features = ["timeout"] }
3941

4042
[lib]
4143
doctest = false

crates/pglt_lsp/tests/server.rs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,28 @@ use anyhow::bail;
22
use anyhow::Context;
33
use anyhow::Error;
44
use anyhow::Result;
5+
use biome_deserialize::Merge;
56
use futures::channel::mpsc::{channel, Sender};
67
use futures::Sink;
78
use futures::SinkExt;
89
use futures::Stream;
910
use futures::StreamExt;
11+
use pglt_configuration::database::PartialDatabaseConfiguration;
12+
use pglt_configuration::PartialConfiguration;
13+
use pglt_fs::MemoryFileSystem;
1014
use pglt_lsp::LSPServer;
1115
use pglt_lsp::ServerFactory;
16+
use pglt_test_utils::test_database::get_new_test_db;
17+
use pglt_workspace::DynRef;
1218
use serde::de::DeserializeOwned;
1319
use serde::Serialize;
1420
use serde_json::{from_value, to_value};
21+
use sqlx::Executor;
1522
use std::any::type_name;
1623
use std::fmt::Display;
24+
use std::process::id;
1725
use std::time::Duration;
26+
use tokio::time::sleep;
1827
use tower::timeout::Timeout;
1928
use tower::{Service, ServiceExt};
2029
use tower_lsp::jsonrpc;
@@ -314,3 +323,115 @@ async fn basic_lifecycle() -> Result<()> {
314323

315324
Ok(())
316325
}
326+
327+
#[tokio::test]
328+
async fn test_database_connection() -> Result<()> {
329+
let factory = ServerFactory::default();
330+
let mut fs = MemoryFileSystem::default();
331+
let test_db = get_new_test_db().await;
332+
333+
let setup = r#"
334+
create table public.users (
335+
id serial primary key,
336+
name varchar(255) not null
337+
);
338+
"#;
339+
340+
test_db
341+
.execute(setup)
342+
.await
343+
.expect("Failed to setup test database");
344+
345+
let mut conf = PartialConfiguration::init();
346+
conf.merge_with(PartialConfiguration {
347+
db: Some(PartialDatabaseConfiguration {
348+
database: Some(
349+
test_db
350+
.connect_options()
351+
.get_database()
352+
.unwrap()
353+
.to_string(),
354+
),
355+
..Default::default()
356+
}),
357+
..Default::default()
358+
});
359+
fs.insert(
360+
url!("pglt.toml").to_file_path().unwrap(),
361+
toml::to_string(&conf).unwrap(),
362+
);
363+
364+
let (service, client) = factory
365+
.create_with_fs(None, DynRef::Owned(Box::new(fs)))
366+
.into_inner();
367+
368+
let (stream, sink) = client.split();
369+
let mut server = Server::new(service);
370+
371+
let (sender, mut receiver) = channel(CHANNEL_BUFFER_SIZE);
372+
let reader = tokio::spawn(client_handler(stream, sink, sender));
373+
374+
server.initialize().await?;
375+
server.initialized().await?;
376+
377+
server.load_configuration().await?;
378+
379+
server
380+
.open_document("select unknown from public.users; ")
381+
.await?;
382+
383+
// in this test, we want to ensure a database connection is established and the schema cache is
384+
// loaded. This is the case when the server sends typecheck diagnostics for the query above.
385+
// so we wait for diagnostics to be sent.
386+
let notification = tokio::time::timeout(Duration::from_secs(5), async {
387+
loop {
388+
match receiver.next().await {
389+
Some(ServerNotification::PublishDiagnostics(msg)) => {
390+
if msg
391+
.diagnostics
392+
.iter()
393+
.any(|d| d.message.contains("column \"unknown\" does not exist"))
394+
{
395+
return true;
396+
}
397+
}
398+
_ => continue,
399+
}
400+
}
401+
})
402+
.await
403+
.is_ok();
404+
405+
assert!(notification, "expected diagnostics for unknown column");
406+
407+
server.shutdown().await?;
408+
reader.abort();
409+
410+
Ok(())
411+
}
412+
413+
#[tokio::test]
414+
async fn server_shutdown() -> Result<()> {
415+
let factory = ServerFactory::default();
416+
let (service, client) = factory.create(None).into_inner();
417+
let (stream, sink) = client.split();
418+
let mut server = Server::new(service);
419+
420+
let (sender, _) = channel(CHANNEL_BUFFER_SIZE);
421+
let reader = tokio::spawn(client_handler(stream, sink, sender));
422+
423+
server.initialize().await?;
424+
server.initialized().await?;
425+
426+
let cancellation = factory.cancellation();
427+
let cancellation = cancellation.notified();
428+
429+
// this is called when `pglt stop` is run by the user
430+
server.pglt_shutdown().await?;
431+
432+
cancellation.await;
433+
434+
reader.abort();
435+
436+
Ok(())
437+
}

crates/pglt_typecheck/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ version = "0.0.0"
1212

1313

1414
[dependencies]
15-
insta = "1.31.0"
1615
pglt_console.workspace = true
1716
pglt_diagnostics.workspace = true
1817
pglt_query_ext.workspace = true
@@ -24,6 +23,7 @@ tree-sitter.workspace = true
2423
tree_sitter_sql.workspace = true
2524

2625
[dev-dependencies]
26+
insta.workspace = true
2727
pglt_test_utils.workspace = true
2828

2929
[lib]

0 commit comments

Comments
 (0)