Lines
100 %
Functions
Branches
use rpc::compile_script;
use server::user::User;
use sqlx::PgPool;
use super::*;
use crate::flat::load_values;
use crate::render::reparse_list;
use crate::transaction::create as create_transaction;
use crate::transaction::tests::{lunch, seeded_session};
const SOURCE: &str = "(+ 1 2)";
const BOOM: &str = "(error 'boom \"no\")";
async fn listed(session: &mut Session) -> anyhow::Result<Vec<ScriptInfo>> {
Ok(list(session).await?)
}
async fn bytecode_only(session: &Session) -> anyhow::Result<String> {
let id = User {
id: session.ctx().user_id,
.create_script(compile_script(SOURCE)?, Some("old".to_string()))
.await?;
Ok(id.to_string())
#[test]
fn a_script_line_names_state_and_source() {
let info = ScriptInfo {
id: "i1".to_string(),
name: "sync".to_string(),
enabled: false,
has_source: true,
};
assert_eq!(info.line(), "i1 sync disabled source");
assert_eq!(
ScriptInfo {
enabled: true,
has_source: false,
..info
.line(),
"i1 sync enabled bytecode-only"
);
fn a_run_report_lists_counts_then_failures() -> anyhow::Result<()> {
let reply = reparse_list(
r#"(:RAN 2 :COMMITTED 1 :FAILURES ((:TRANSACTION "t1" :SCRIPT "s1" :CODE "boom" :MESSAGE "no")))"#,
)?;
parse_report(&reply)?.lines(),
[
"ran: 2",
"committed: 1",
"failures: 1",
"failed: transaction t1: boom: no"
]
Ok(())
fn a_reply_that_is_not_a_run_is_malformed() -> anyhow::Result<()> {
let reply = reparse_list("(:RAN 2)")?;
assert!(matches!(
parse_report(&reply),
Err(CommandError::Command(_))
));
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn create_then_list_shows_the_script(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let mut session = seeded_session(&pool).await?;
let id = create(&mut session, Some("sync"), SOURCE).await?;
listed(&mut session).await?,
[ScriptInfo {
id,
has_source: true
}]
async fn edit_keeps_the_fields_it_is_not_given(pool: PgPool) -> anyhow::Result<()> {
edit(&mut session, &id, Some("renamed"), None, None).await?;
edit(&mut session, &id, None, Some("(+ 2 3)"), None).await?;
let scripts = listed(&mut session).await?;
assert_eq!(scripts.len(), 1);
assert_eq!(scripts[0].name, "renamed");
assert!(scripts[0].enabled && scripts[0].has_source);
async fn a_disabled_script_can_be_enabled_again(pool: PgPool) -> anyhow::Result<()> {
edit(&mut session, &id, None, None, Some(false)).await?;
assert!(!listed(&mut session).await?[0].enabled);
edit(&mut session, &id, None, None, Some(true)).await?;
assert!(listed(&mut session).await?[0].enabled);
async fn a_source_that_does_not_compile_is_a_field_error(pool: PgPool) -> anyhow::Result<()> {
let created = create(&mut session, None, "(unclosed").await.map(|_| ());
let edited = edit(&mut session, &id, None, Some("(unclosed"), None)
.await
.map(|_| ());
for failed in [created, edited] {
match failed {
Err(CommandError::FormInvalid(lines)) => {
assert!(
lines.iter().any(|line| line.starts_with("source: ")),
"{lines:?}"
)
other => anyhow::bail!("expected a field error, got {other:?}"),
assert_eq!(listed(&mut session).await?.len(), 1);
async fn a_stale_edit_is_refused_at_the_revision(pool: PgPool) -> anyhow::Result<()> {
let stale = load_values(&mut session, &SCRIPT_EDIT, &id).await?;
edit(&mut session, &id, Some("newer"), None, None).await?;
match submit(&mut session, &SCRIPT_EDIT, &stale).await {
lines.iter().any(|line| line.starts_with("revision: ")),
other => anyhow::bail!("expected a stale revision, got {other:?}"),
assert_eq!(listed(&mut session).await?[0].name, "newer");
async fn a_bytecode_only_script_cannot_be_toggled(pool: PgPool) -> anyhow::Result<()> {
let id = bytecode_only(&session).await?;
let refused = edit(&mut session, &id, None, None, Some(false)).await;
matches!(refused, Err(CommandError::FormInvalid(_))),
"{refused:?}"
async fn run_reports_the_transactions_it_covered(pool: PgPool) -> anyhow::Result<()> {
create_transaction(&mut session, &lunch()).await?;
let report = run(&mut session, &id, "Cash").await?;
assert_eq!((report.ran.as_str(), report.failures.len()), ("1", 0));
async fn run_names_each_failure_and_commits_nothing(pool: PgPool) -> anyhow::Result<()> {
let id = create(&mut session, Some("boom"), BOOM).await?;
assert_eq!(report.committed, "0");
assert_eq!(report.failures.len(), 1);
report.failures[0].ends_with(": BOOM: no"),
"{:?}",
report.failures
async fn run_refuses_an_unknown_account(pool: PgPool) -> anyhow::Result<()> {
let refused = run(&mut session, &id, "nowhere").await;
matches!(refused, Err(CommandError::Argument(_))),