#![cfg(unix)]
use std::process::Command;
use rexpect::session::{spawn_command, PtySession};
const TIMEOUT_MS: u64 = 30_000;
fn shell(home: &std::path::Path, args: &[&str]) -> PtySession {
let mut command = Command::new(env!("CARGO_BIN_EXE_ursa"));
command
.args(args)
.current_dir(home)
.env("HOME", home)
.env_remove("URSA_HISTORY")
.env("TERM", "xterm")
.env("NO_COLOR", "1");
spawn_command(command, Some(TIMEOUT_MS)).expect("ursa starts in a pseudo-terminal")
}
#[test]
fn colour_in_a_colour_terminal() {
let home = tempfile::tempdir().unwrap();
let mut command = Command::new(env!("CARGO_BIN_EXE_ursa"));
command
.args(["--no-history"])
.current_dir(home.path())
.env("HOME", home.path())
.env("TERM", "xterm-256color")
.env_remove("NO_COLOR");
let mut p = spawn_command(command, Some(TIMEOUT_MS)).expect("ursa starts in a pseudo-terminal");
p.exp_string("\x1b[1;38;5;208mmemory U \x1b[0m").unwrap();
p.send_line("SELECT 42 AS answer;").unwrap();
p.exp_string("\x1b[1mSELECT\x1b[0m").unwrap();
p.exp_string(&bytes("│ \x1b[90mInt64 \x1b[0m │")).unwrap();
p.send_line("SELECT 'a").unwrap();
p.exp_string("\x1b[33m'a\x1b[0m").unwrap();
p.send_line("b' AS s;").unwrap();
p.exp_string("\x1b[33mb'\x1b[0m \x1b[1mAS\x1b[0m").unwrap();
p.send_line(".quit").unwrap();
p.exp_eof().unwrap();
}
fn bytes(s: &str) -> String {
s.bytes().map(char::from).collect()
}
#[test]
fn a_session_in_the_shell() {
let home = tempfile::tempdir().unwrap();
std::fs::write(home.path().join(".ursarc"), ".mode csv\n").unwrap();
let mut p = shell(home.path(), &["club.ursa"]);
p.exp_string(&format!(
"ursa {}, database club.ursa.",
env!("URSA_VERSION")
))
.unwrap();
p.exp_string("club U ").unwrap();
p.send_line("SELECT 1 AS x,").unwrap();
p.exp_string(" ").unwrap();
p.send_line(" 'a;b' AS y;").unwrap();
p.exp_string("x,y\r\n1,a;b\r\n").unwrap();
p.exp_string("club U ").unwrap();
p.send_line("CREATE VIEW ties AS SELECT * FROM (VALUES (1, 2)) AS t(src, dst);")
.unwrap();
p.exp_string("club U ").unwrap();
p.send_line(
"CREATE PROPERTY GRAPH g EDGE TABLES (ties SOURCE KEY (src) DESTINATION KEY (dst));",
)
.unwrap();
p.exp_string("club U ").unwrap();
p.send_line(".graphs").unwrap();
p.exp_string("g,ties,src,dst").unwrap();
p.send_line(".tabels").unwrap();
p.exp_string("did you mean '.tables'?").unwrap();
p.send_line("SELECT nope;").unwrap();
p.exp_string("Error: ").unwrap();
p.exp_string("club U ").unwrap();
p.send_line("SELECT 'dropped'").unwrap();
p.exp_string(" ").unwrap();
p.send_control('c').unwrap();
p.exp_string("club U ").unwrap();
p.send_line("SELECT 2 AS z;").unwrap();
p.exp_string("z\r\n2\r\n").unwrap();
p.exp_string("club U ").unwrap();
p.send_control('d').unwrap();
p.exp_eof().unwrap();
let history = std::fs::read_to_string(home.path().join(".ursa_history")).unwrap();
assert!(history.contains("SELECT 2 AS z;"), "{history}");
assert!(history.contains(".graphs"), "{history}");
assert!(!history.contains("dropped"), "{history}");
let catalog = std::fs::read_to_string(home.path().join("club.ursa")).unwrap();
assert!(catalog.contains("CREATE PROPERTY GRAPH g"), "{catalog}");
}
#[test]
fn ctrl_c_cancels_a_running_query() {
let home = tempfile::tempdir().unwrap();
let mut p = shell(home.path(), &["--no-history"]);
p.exp_string("in memory.").unwrap();
p.exp_string("memory U ").unwrap();
p.send_line(
"SELECT count(*) FROM generate_series(1, 100000000000) a CROSS JOIN generate_series(1, 10) b;",
)
.unwrap();
std::thread::sleep(std::time::Duration::from_millis(500));
let sent = std::time::Instant::now();
p.send_control('c').unwrap();
p.exp_string("Interrupted\r\n").unwrap();
p.exp_string("memory U ").unwrap();
let waited = sent.elapsed();
assert!(waited.as_secs() < 5, "the prompt took {waited:?}");
p.send_line(
"CREATE VIEW e AS SELECT value % 2000000 AS src, (value * 7919 + 13) % 2000000 AS dst \
FROM generate_series(1, 20000000); \
CREATE PROPERTY GRAPH g EDGE TABLES (e SOURCE KEY (src) DESTINATION KEY (dst)); \
SELECT count(*) FROM louvain(g);",
)
.unwrap();
std::thread::sleep(std::time::Duration::from_millis(2000));
p.send_control('c').unwrap();
p.exp_string("Interrupted\r\n").unwrap();
p.exp_string("memory U ").unwrap();
p.send_line("SELECT 41 + 1 AS answer;").unwrap();
p.exp_string(&bytes("│ 42 │")).unwrap();
p.send_line(".quit").unwrap();
p.exp_eof().unwrap();
assert!(!home.path().join(".ursa_history").exists());
}