1
use super::*;
2
use crate::db::DBError;
3
use crate::logical::STALE_REVISION;
4
use crate::telemetry::recorder::build_recorder;
5

            
6
3
fn refusal(tx_id: Option<Uuid>) -> RunError {
7
3
    write_failure(BatchError {
8
3
        tx_id,
9
3
        error: ServerError::Script(STALE_REVISION.to_string()),
10
3
    })
11
3
}
12

            
13
#[test]
14
1
fn a_refused_write_names_the_transaction_and_writes_nothing() {
15
1
    let tx_id = Uuid::from_u128(7);
16
1
    assert_eq!(
17
1
        refusal(Some(tx_id)).to_string(),
18
1
        format!("{tx_id}: refused: {STALE_REVISION} (nothing was written)")
19
    );
20
1
}
21

            
22
#[test]
23
1
fn a_batch_wide_refusal_names_the_batch() {
24
1
    let message = refusal(None).to_string();
25
1
    assert!(message.starts_with("batch: refused: "), "{message}");
26
1
}
27

            
28
#[test]
29
1
fn any_other_write_failure_stays_a_server_error() {
30
1
    let creation = write_failure(BatchError {
31
1
        tx_id: Some(Uuid::from_u128(7)),
32
1
        error: ServerError::Creation,
33
1
    });
34
1
    let conflict = write_failure(BatchError {
35
1
        tx_id: None,
36
1
        error: ServerError::DB(DBError::WriteConflict),
37
1
    });
38
1
    assert!(matches!(
39
1
        creation,
40
        RunError::Failed {
41
            source: ServerError::Creation,
42
            ..
43
        }
44
    ));
45
1
    assert!(matches!(
46
1
        conflict,
47
        RunError::Failed {
48
            source: ServerError::DB(DBError::WriteConflict),
49
            ..
50
        }
51
    ));
52
1
}
53

            
54
#[test]
55
1
fn a_refused_write_counts_one_refused_item() {
56
1
    let recorder = build_recorder().expect("recorder");
57
1
    let _scope = metrics::set_default_local_recorder(&recorder);
58
1
    let _ = refusal(Some(Uuid::from_u128(7)));
59
1
    let rendered = recorder.handle().render();
60
1
    assert!(
61
1
        rendered.contains(&format!("{SCRIPT_BATCH_ITEMS}{{outcome=\"refused\"}} 1")),
62
        "{rendered}"
63
    );
64
1
    assert!(!rendered.contains("outcome=\"failed\""), "{rendered}");
65
1
}
66

            
67
#[test]
68
1
fn a_failed_write_names_the_item_and_writes_nothing() {
69
1
    let tx_id = Uuid::from_u128(7);
70
1
    let err = write_failure(BatchError {
71
1
        tx_id: Some(tx_id),
72
1
        error: ServerError::Creation,
73
1
    });
74
1
    assert_eq!(
75
1
        err.to_string(),
76
1
        format!(
77
1
            "{tx_id}: failed: {} (nothing was written)",
78
1
            ServerError::Creation
79
        )
80
    );
81
1
}
82

            
83
#[test]
84
1
fn a_row_not_found_after_the_lookup_is_not_a_missing_script() {
85
1
    let err = write_failure(BatchError {
86
1
        tx_id: Some(Uuid::from_u128(7)),
87
1
        error: ServerError::DB(DBError::Sqlx(sqlx::Error::RowNotFound)),
88
1
    });
89
1
    assert!(matches!(err, RunError::Failed { .. }), "{err}");
90
1
}