sqlite-clone/sqlite_core/tests/acceptance_test.rs
kjuulh 4fd34f973f
All checks were successful
continuous-integration/drone/push Build is passing
Add test
2022-04-02 21:58:23 +02:00

119 lines
4.2 KiB
Rust

use std::ops::Deref;
use std::sync::{Arc, Mutex};
use sqlite_core::app::App;
#[derive(Clone, Debug, Default)]
struct EmptyDatabaseEnv {
input: Arc<Mutex<Vec<String>>>,
output: Arc<Mutex<Vec<String>>>,
app: Arc<Mutex<sqlite_core::app::App>>,
}
#[test]
fn integration_test() {
let env = EmptyDatabaseEnv {
input: Arc::new(Mutex::new(Vec::new())),
output: Arc::new(Mutex::new(Vec::new())),
app: Arc::new(Mutex::new(App::new())),
};
rspec::run(&rspec::given("a sqlite database", env, |ctx| {
ctx.before_each(|env| {
env.input = Arc::new(Mutex::new(Vec::new()));
env.output = Arc::new(Mutex::new(Vec::new()));
env.app = Arc::new(Mutex::new(App::new()));
});
ctx.when("it runs with empty items", |ctx| {
ctx.then("it returns input error", |value| {
let result = value
.app
.lock()
.as_mut()
.unwrap()
.with_buffer_interaction(value.input.clone(), value.output.clone())
.run();
assert_eq!(result, Err(String::from("could not handle input")));
assert_eq!(0, value.input.lock().unwrap().deref().len());
assert_eq!(1, value.output.lock().unwrap().deref().len());
assert_eq!(
String::from("db > "),
value.output.lock().unwrap().deref()[0]
);
})
});
ctx.when("it runs with insert items", |ctx| {
ctx.before_all(|value| {
let mut input = value.input.lock().unwrap();
input.push(String::from("insert 1 username user@email.com"));
input.push(String::from("insert 2 username2 user2@email.com"));
input.push(String::from(".exit"));
input.reverse();
});
ctx.then("returns success", |value| {
let result = value
.app
.lock()
.as_mut()
.unwrap()
.with_buffer_interaction(value.input.clone(), value.output.clone())
.run();
assert_eq!(result, Err(String::from("could not handle input")));
assert_eq!(6, value.output.lock().unwrap().deref().len());
assert_eq!(
String::from("db > "),
value.output.lock().unwrap().deref()[0]
);
assert_eq!(
String::from("insert success\n"),
value.output.lock().unwrap().deref()[1]
);
assert_eq!(
String::from("db > "),
value.output.lock().unwrap().deref()[2]
);
assert_eq!(
String::from("insert success\n"),
value.output.lock().unwrap().deref()[3]
);
assert_eq!(
String::from("db > "),
value.output.lock().unwrap().deref()[4]
);
assert_eq!(
String::from("db > "),
value.output.lock().unwrap().deref()[5]
);
});
});
ctx.when("it runs with select items", |ctx| {
ctx.before_all(|value| {
let mut input = value.input.lock().unwrap();
input.push(String::from("insert 1 username user@email.com"));
input.push(String::from("insert 2 username2 user2@email.com"));
input.push(String::from("select"));
input.push(String::from(".exit"));
input.reverse();
});
ctx.then("returns success", |value| {
let result = value
.app
.lock()
.as_mut()
.unwrap()
.with_buffer_interaction(value.input.clone(), value.output.clone())
.run();
assert_eq!(result, Err(String::from("could not handle input")));
assert_eq!(10, value.output.lock().unwrap().deref().len());
});
});
}))
}