ibc_testkit/utils/mod.rs
1use ibc::primitives::Timestamp;
2#[cfg(feature = "serde")]
3use serde::{de::DeserializeOwned, Serialize};
4
5/// Returns a `Timestamp` representation of the beginning of year 2023.
6///
7/// This is introduced to initialize [`StoreGenericTestContext`](crate::context::StoreGenericTestContext)s
8/// with the same latest timestamp by default.
9/// If two [`StoreGenericTestContext`](crate::context::StoreGenericTestContext)
10/// are initialized using [`Timestamp::now()`], the second one will have a greater timestamp than the first one.
11/// So, the latest header of the second context cannot be submitted to first one.
12/// We can still set a custom timestamp via [`dummy_store_generic_test_context`](crate::fixtures::core::context::dummy_store_generic_test_context).
13pub fn year_2023() -> Timestamp {
14 // Sun Jan 01 2023 00:00:00 GMT+0000
15 Timestamp::from_unix_timestamp(1_672_531_200, 0).expect("should be a valid time")
16}
17
18/// Utility function that asserts that the given JSON input can be
19/// serialized into and deserialized from the specified type `T`.
20#[cfg(feature = "serde")]
21pub fn test_serialization_roundtrip<T>(json_data: &str)
22where
23 T: core::fmt::Debug + PartialEq + Serialize + DeserializeOwned,
24{
25 let parsed0 = serde_json::from_str::<T>(json_data);
26 assert!(parsed0.is_ok());
27 let parsed0 = parsed0.expect("should not fail");
28
29 let serialized = serde_json::to_string(&parsed0);
30 assert!(serialized.is_ok());
31 let serialized = serialized.expect("should not fail");
32 // We can't assert the following, as JSON string representation is not canonical.
33 // assert_eq!(serialized, json_data);
34
35 let parsed1 = serde_json::from_str::<T>(&serialized);
36 assert!(parsed1.is_ok());
37 let parsed1 = parsed1.expect("should not fail");
38
39 assert_eq!(parsed0, parsed1);
40}