From c9bf7f4cf699a3796e7c8b6ad6fc0d37b7ece137 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Fri, 6 Nov 2020 11:56:51 -0800 Subject: [PATCH] Add tests for new sled impls needed for debug format --- zebra-state/src/sled_state/sled_format.rs | 68 +++++++++++++++++------ 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/zebra-state/src/sled_state/sled_format.rs b/zebra-state/src/sled_state/sled_format.rs index f20355a6..ca5d9d95 100644 --- a/zebra-state/src/sled_state/sled_format.rs +++ b/zebra-state/src/sled_state/sled_format.rs @@ -330,7 +330,6 @@ impl SledDeserialize for sled::Tree { mod tests { use super::*; use proptest::{arbitrary::any, prelude::*}; - use std::ops::Deref; impl Arbitrary for TransactionLocation { type Parameters = (); @@ -352,9 +351,6 @@ mod tests { T::from_ivec(bytes) } - /// The round trip test covers types that are used as value field in a sled - /// Tree. Only these types are ever deserialized, and so they're the only - /// ones that implement both `IntoSled` and `FromSled`. fn assert_round_trip(input: T) where T: IntoSled + FromSled + Clone + PartialEq + std::fmt::Debug, @@ -364,6 +360,52 @@ mod tests { assert_eq!(before, after); } + fn round_trip_ref(input: &T) -> T + where + T: IntoSled + FromSled, + { + let bytes = input.into_ivec(); + T::from_ivec(bytes) + } + + fn assert_round_trip_ref(input: &T) + where + T: IntoSled + FromSled + Clone + PartialEq + std::fmt::Debug, + { + let before = input; + let after = round_trip_ref(input); + assert_eq!(before, &after); + } + + fn round_trip_arc(input: Arc) -> T + where + T: IntoSled + FromSled, + { + let bytes = input.into_ivec(); + T::from_ivec(bytes) + } + + fn assert_round_trip_arc(input: Arc) + where + T: IntoSled + FromSled + Clone + PartialEq + std::fmt::Debug, + { + let before = input.clone(); + let after = round_trip_arc(input); + assert_eq!(*before, after); + } + + /// The round trip test covers types that are used as value field in a sled + /// Tree. Only these types are ever deserialized, and so they're the only + /// ones that implement both `IntoSled` and `FromSled`. + fn assert_value_properties(input: T) + where + T: IntoSled + FromSled + Clone + PartialEq + std::fmt::Debug, + { + assert_round_trip_ref(&input); + assert_round_trip_arc(Arc::new(input.clone())); + assert_round_trip(input); + } + /// This test asserts that types that are used as sled keys behave correctly. /// Any type that implements `IntoIVec` can be used as a sled key. The value /// is serialized via `IntoSled::into_ivec` when the `key`, `value` pair is @@ -382,41 +424,33 @@ mod tests { #[test] fn roundtrip_transaction_location() { zebra_test::init(); - proptest!(|(val in any::())| assert_round_trip(val)); + proptest!(|(val in any::())| assert_value_properties(val)); } #[test] fn roundtrip_block_hash() { zebra_test::init(); - proptest!(|(val in any::())| assert_round_trip(val)); + proptest!(|(val in any::())| assert_value_properties(val)); } #[test] fn roundtrip_block_height() { zebra_test::init(); - proptest!(|(val in any::())| assert_round_trip(val)); + proptest!(|(val in any::())| assert_value_properties(val)); } #[test] fn roundtrip_block() { zebra_test::init(); - proptest!(|(block in any::())| { - let bytes = block.into_ivec(); - let deserialized: Arc = FromSled::from_ivec(bytes); - assert_eq!(&block, deserialized.deref()); - }); + proptest!(|(val in any::())| assert_value_properties(val)); } #[test] fn roundtrip_transparent_output() { zebra_test::init(); - proptest!(|(output in any::())| { - let bytes = output.into_ivec(); - let deserialized: transparent::Output = FromSled::from_ivec(bytes); - assert_eq!(output, deserialized); - }); + proptest!(|(val in any::())| assert_value_properties(val)); } #[test]