Skip to content

Commit da00ef9

Browse files
committed
metatests, more readable errors
1 parent 2253576 commit da00ef9

File tree

2 files changed

+78
-13
lines changed

2 files changed

+78
-13
lines changed

lightning/src/util/persist.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -223,27 +223,27 @@ impl From<KVStoreUpdatingPersisterError> for io::Error {
223223
match value {
224224
KVStoreUpdatingPersisterError::BadMonitorName { reason, context } => io::Error::new(
225225
io::ErrorKind::InvalidInput,
226-
format!("{}, context: {}'", reason, context),
226+
format!("BadMonitorName, {}, context: {}'", reason, context),
227227
),
228228
KVStoreUpdatingPersisterError::MonitorDecodeFailed { reason, context } => {
229229
io::Error::new(
230230
io::ErrorKind::InvalidData,
231-
format!("{}, context: {}'", reason, context),
231+
format!("MonitorDecodeFailed, {}, context: {}'", reason, context),
232232
)
233233
}
234234
KVStoreUpdatingPersisterError::UpdateDecodeFailed { reason, context } => {
235235
io::Error::new(
236236
io::ErrorKind::InvalidData,
237-
format!("{}, context: {}'", reason, context),
237+
format!("UpdateDecodeFailed, {}, context: {}'", reason, context),
238238
)
239239
}
240240
KVStoreUpdatingPersisterError::StorageReadFailed { reason, context } => io::Error::new(
241241
io::ErrorKind::Other,
242-
format!("{}, context: {}'", reason, context),
242+
format!("StorageReadFailed, {}, context: {}'", reason, context),
243243
),
244244
KVStoreUpdatingPersisterError::UpdateFailed { reason, context } => io::Error::new(
245245
io::ErrorKind::InvalidData,
246-
format!("{}, context: {}'", reason, context),
246+
format!("UpdateFailed, {}, context: {}'", reason, context),
247247
),
248248
}
249249
}
@@ -451,6 +451,8 @@ where
451451
}
452452
}
453453

454+
455+
454456
/// Deserialize a channel monitor update.
455457
fn deserialize_monitor_update(
456458
&self,
@@ -607,10 +609,10 @@ mod tests {
607609
(21, "00000000000000000021"),
608610
(u64::MAX, "18446744073709551615"),
609611
];
610-
for (input, expected) in cases {
611-
let update_name = UpdateName::from(input);
612-
assert_eq!(update_name.0, expected);
613-
assert_eq!(update_name.storage_key(), expected);
612+
for (input, expected) in &cases {
613+
let update_name = UpdateName::from(*input);
614+
assert_eq!(update_name.0, *expected);
615+
assert_eq!(update_name.storage_key(), *expected);
614616
}
615617
}
616618

@@ -638,10 +640,10 @@ mod tests {
638640
"f33dbeeff33dbeeff33dbeeff33dbeeff33dbeeff33dbeeff33dbeeff33dbeef_65535",
639641
),
640642
];
641-
for (input, expected) in cases {
642-
let monitor_name = MonitorName::from(input);
643-
assert_eq!(monitor_name.0, expected);
644-
assert_eq!(monitor_name.storage_key(), expected);
643+
for (input, expected) in &cases {
644+
let monitor_name = MonitorName::from(*input);
645+
assert_eq!(monitor_name.0, *expected);
646+
assert_eq!(monitor_name.storage_key(), *expected);
645647
}
646648
}
647649

lightning/src/util/test_utils.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,3 +1227,66 @@ impl WalletSource for TestWalletSource {
12271227
Ok(tx)
12281228
}
12291229
}
1230+
1231+
#[cfg(test)]
1232+
mod tests {
1233+
use super::*;
1234+
use std::io::Read;
1235+
1236+
#[test]
1237+
fn test_test_store_new() {
1238+
let store = TestStore::new(true);
1239+
assert!(store.read_only);
1240+
}
1241+
1242+
#[test]
1243+
fn test_test_store_get_persisted_bytes() {
1244+
let store = TestStore::new(false);
1245+
store.write("namespace", "key", &[1, 2, 3]).unwrap();
1246+
assert_eq!(store.get_persisted_bytes("namespace", "key"), Some(vec![1, 2, 3]));
1247+
}
1248+
1249+
#[test]
1250+
fn test_test_store_get_and_clear_did_persist() {
1251+
let store = TestStore::new(false);
1252+
assert!(!store.get_and_clear_did_persist());
1253+
store.write("namespace", "key", &[1, 2, 3]).unwrap();
1254+
assert!(store.get_and_clear_did_persist());
1255+
}
1256+
1257+
#[test]
1258+
fn test_test_store_read() {
1259+
let store = TestStore::new(false);
1260+
store.write("namespace", "key", &[1, 2, 3]).unwrap();
1261+
let mut reader = store.read("namespace", "key").unwrap();
1262+
let mut buf = vec![0; 3];
1263+
reader.read_exact(&mut buf).unwrap();
1264+
assert_eq!(buf, vec![1, 2, 3]);
1265+
}
1266+
1267+
#[test]
1268+
fn test_test_store_write() {
1269+
let store = TestStore::new(false);
1270+
assert_eq!(store.get_persisted_bytes("namespace", "key"), None);
1271+
store.write("namespace", "key", &[1, 2, 3]).unwrap();
1272+
assert_eq!(store.get_persisted_bytes("namespace", "key"), Some(vec![1, 2, 3]));
1273+
}
1274+
1275+
#[test]
1276+
fn test_test_store_remove() {
1277+
let store = TestStore::new(false);
1278+
store.write("namespace", "key", &[1, 2, 3]).unwrap();
1279+
assert_eq!(store.get_persisted_bytes("namespace", "key"), Some(vec![1, 2, 3]));
1280+
store.remove("namespace","key").unwrap();
1281+
assert_eq!(store.get_persisted_bytes("namespace", "key"), None);
1282+
}
1283+
1284+
#[test]
1285+
fn test_test_store_list() {
1286+
let store = TestStore::new(false);
1287+
store.write("namespace", "key1", &[1, 2, 3]).unwrap();
1288+
store.write("namespace", "key2", &[1, 2, 3]).unwrap();
1289+
let keys = store.list("namespace").unwrap();
1290+
assert_eq!(keys, vec!["key1","key2"]);
1291+
}
1292+
}

0 commit comments

Comments
 (0)