Skip to content

Commit 9d783ed

Browse files
kvs: 100 % function coverage
Functions: 100.00% Lines: 99.28% Regions: 97.63% Branches: 95.65% Signed-off-by: Sven Bachmann <sven.bachmann.ext@qorix.ai>
1 parent e4c04d7 commit 9d783ed

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

src/lib.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ const KVS_MAX_SNAPSHOTS: usize = 3;
158158
pub struct InstanceId(usize);
159159

160160
/// Snapshot ID
161+
#[derive(PartialEq, Debug)]
161162
pub struct SnapshotId(usize);
162163

163164
/// Runtime Error Codes
@@ -1541,4 +1542,107 @@ mod tests {
15411542
// default value
15421543
let _ = kvs.get_value("bool1");
15431544
}
1545+
1546+
#[test]
1547+
fn test_snapshot_id() {
1548+
let snapshot_id: SnapshotId = 123.into();
1549+
assert_eq!(snapshot_id, SnapshotId(123));
1550+
}
1551+
1552+
#[test]
1553+
fn test_set_default_value() {
1554+
let instance_id = InstanceId::new(0);
1555+
let temp_dir = test_dir();
1556+
1557+
std::fs::copy(
1558+
"tests/kvs_0_default.json",
1559+
format!("{}/kvs_0_default.json", temp_dir.1.clone()),
1560+
)
1561+
.unwrap();
1562+
1563+
std::fs::copy(
1564+
"tests/kvs_0_default.hash",
1565+
format!("{}/kvs_0_default.hash", temp_dir.1.clone()),
1566+
)
1567+
.unwrap();
1568+
1569+
let kvs = Arc::new(
1570+
KvsBuilder::new(instance_id.clone())
1571+
.dir(temp_dir.1.clone())
1572+
.need_defaults(true)
1573+
.build()
1574+
.unwrap(),
1575+
);
1576+
1577+
assert_eq!(
1578+
kvs.get_value("string1"),
1579+
Ok(KvsValue::String("Hello".to_string()))
1580+
);
1581+
assert!(kvs
1582+
.set_value("string1", KvsValue::String("Bye".to_string()))
1583+
.is_ok());
1584+
assert_eq!(
1585+
kvs.get_value("string1"),
1586+
Ok(KvsValue::String("Bye".to_string()))
1587+
);
1588+
assert!(kvs.set_default_value("string1").is_ok());
1589+
assert_eq!(
1590+
kvs.get_value("string1"),
1591+
Ok(KvsValue::String("Hello".to_string()))
1592+
);
1593+
assert_eq!(
1594+
kvs.set_default_value("string2").err(),
1595+
Some(ErrorCode::KeyDefaultNotFound)
1596+
);
1597+
}
1598+
1599+
#[test]
1600+
fn test_has_default_value_with_mutex_lock_fail() {
1601+
let instance_id = InstanceId::new(0);
1602+
let temp_dir = test_dir();
1603+
1604+
std::fs::copy(
1605+
"tests/kvs_0_default.json",
1606+
format!("{}/kvs_0_default.json", temp_dir.1.clone()),
1607+
)
1608+
.unwrap();
1609+
1610+
std::fs::copy(
1611+
"tests/kvs_0_default.hash",
1612+
format!("{}/kvs_0_default.hash", temp_dir.1.clone()),
1613+
)
1614+
.unwrap();
1615+
1616+
let kvs = Arc::new(
1617+
KvsBuilder::new(instance_id.clone())
1618+
.dir(temp_dir.1.clone())
1619+
.need_defaults(true)
1620+
.build()
1621+
.unwrap(),
1622+
);
1623+
1624+
// test from: https://doc.rust-lang.org/std/sync/struct.PoisonError.html
1625+
let c_kvs = kvs.clone();
1626+
let _ = thread::spawn(move || {
1627+
let _unused = c_kvs.kvs.lock().unwrap();
1628+
panic!();
1629+
})
1630+
.join();
1631+
1632+
assert!(!kvs.has_default_value("string1"));
1633+
}
1634+
1635+
#[test]
1636+
fn test_try_from_kvs_value() {
1637+
assert!(f64::try_from(&KvsValue::Number(123.0)).is_ok());
1638+
assert!(<()>::try_from(&KvsValue::Null).is_ok());
1639+
assert_eq!(
1640+
<()>::try_from(&KvsValue::Number(123.0)),
1641+
Err(ErrorCode::InvalidValueType)
1642+
);
1643+
assert_eq!(
1644+
u64::try_from(&KvsValue::Number(123.0)),
1645+
Err(ErrorCode::ConversionFailed)
1646+
);
1647+
}
15441648
}

0 commit comments

Comments
 (0)