use kcode_credits::{Balance, Credits, Error};
#[test]
fn awards_persist_and_advance_the_small_global_revision() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("credits.sqlite3");
let credits = Credits::open(&path).unwrap();
assert_eq!(
credits.balance("user-a").unwrap(),
Balance {
amount: 0,
revision: 0
}
);
assert_eq!(
credits.award("user-a", 7).unwrap(),
Balance {
amount: 7,
revision: 1
}
);
assert_eq!(
Credits::open(path)
.unwrap()
.balance("user-a")
.unwrap()
.amount,
7
);
}
#[test]
fn rejects_empty_users_and_overflows() {
let directory = tempfile::tempdir().unwrap();
let credits = Credits::open(directory.path().join("credits.sqlite3")).unwrap();
assert!(matches!(credits.balance(" "), Err(Error::EmptyUserId)));
credits.award("user-a", i64::MAX as u64).unwrap();
assert!(matches!(
credits.award("user-a", 1),
Err(Error::AmountOutOfRange)
));
}