Skip to content

Commit

Permalink
test: expose serialize methods used in integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dkijania committed Jun 27, 2022
1 parent 390b8b6 commit c47c2e8
Showing 1 changed file with 41 additions and 35 deletions.
76 changes: 41 additions & 35 deletions catalyst-toolbox/src/snapshot/registration.rs
@@ -1,6 +1,6 @@
use jormungandr_lib::crypto::account::Identifier;
use jormungandr_lib::interfaces::Value;
use serde::{de::Error, Deserialize};
use serde::{de::Error, Deserialize, Serialize};

pub type MainnetRewardAddress = String;
pub type MainnetStakeAddress = String;
Expand All @@ -9,7 +9,7 @@ pub type MainnetStakeAddress = String;
/// which is a generalizatin of CIP-15, allowing to distribute
/// voting power among multiple keys in a single transaction and
/// to tag the purpose of the vote.
#[derive(Deserialize, Clone, Debug, PartialEq)]
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
pub struct VotingRegistration {
pub stake_public_key: MainnetStakeAddress,
pub voting_power: Value,
Expand Down Expand Up @@ -136,15 +136,52 @@ mod deser {
}
}

#[cfg(feature = "test-api")]
mod test_api {
use super::deser::IdentifierDef;
use super::*;
use serde::Serializer;

// This is only to make it easier to test the Deserialize impl
impl Serialize for IdentifierDef {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if serializer.is_human_readable() {
serializer.serialize_str(&format!("0x{}", self.0.to_hex()))
} else {
serializer.serialize_bytes(self.0.as_ref().as_ref())
}
}
}

// This is only to make it easier to test the Deserialize impl
impl Serialize for Delegations {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
Self::Legacy(key) => IdentifierDef(key.clone()).serialize(serializer),
Self::New(vec) => vec
.iter()
.map(|(vk, weight)| (IdentifierDef(vk.clone()), weight))
.collect::<Vec<_>>()
.serialize(serializer),
}
}
}
}

#[cfg(test)]
#[cfg(feature = "test-api")]
mod tests {
use super::deser::IdentifierDef;
use super::*;
use bech32::ToBase32;
use chain_crypto::{Ed25519, SecretKey};
use proptest::collection::vec;
use proptest::prelude::*;
use serde::{Serialize, Serializer};
use serde_test::{assert_de_tokens, Configure, Token};
use test_strategy::proptest;

Expand Down Expand Up @@ -245,37 +282,6 @@ mod tests {
);
}

// This is only to make it easier to test the Deserialize impl
impl Serialize for IdentifierDef {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if serializer.is_human_readable() {
serializer.serialize_str(&format!("0x{}", self.0.to_hex()))
} else {
serializer.serialize_bytes(self.0.as_ref().as_ref())
}
}
}

// This is only to make it easier to test the Deserialize impl
impl Serialize for Delegations {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
Self::Legacy(key) => IdentifierDef(key.clone()).serialize(serializer),
Self::New(vec) => vec
.iter()
.map(|(vk, weight)| (IdentifierDef(vk.clone()), weight))
.collect::<Vec<_>>()
.serialize(serializer),
}
}
}

#[proptest]
fn serde_json(d: Delegations) {
assert_eq!(
Expand Down

0 comments on commit c47c2e8

Please sign in to comment.