Add basic unit tests for Amount serde

Add a test each for serializing `Amount` as sat, as BTC, and as str.
This commit is contained in:
Tobin C. Harding 2024-12-02 14:25:55 +11:00
parent 22530f6a2b
commit c27f443520
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 67 additions and 0 deletions

View File

@ -385,3 +385,70 @@ pub mod as_str {
} }
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn can_serde_as_sat() {
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct HasAmount {
#[serde(with = "crate::amount::serde::as_sat")]
pub amount: Amount,
}
let orig = HasAmount {
amount: Amount::ONE_BTC,
};
let json = serde_json::to_string(&orig).expect("failed to ser");
let want = "{\"amount\":100000000}";
assert_eq!(json, want);
let rinsed: HasAmount = serde_json::from_str(&json).expect("failed to deser");
assert_eq!(rinsed, orig)
}
#[test]
#[cfg(feature = "alloc")]
fn can_serde_as_btc() {
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct HasAmount {
#[serde(with = "crate::amount::serde::as_btc")]
pub amount: Amount,
}
let orig = HasAmount {
amount: Amount::ONE_BTC,
};
let json = serde_json::to_string(&orig).expect("failed to ser");
let want = "{\"amount\":1.0}";
assert_eq!(json, want);
let rinsed: HasAmount = serde_json::from_str(&json).expect("failed to deser");
assert_eq!(rinsed, orig)
}
#[test]
#[cfg(feature = "alloc")]
fn can_serde_as_str() {
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct HasAmount {
#[serde(with = "crate::amount::serde::as_str")]
pub amount: Amount,
}
let orig = HasAmount {
amount: Amount::ONE_BTC,
};
let json = serde_json::to_string(&orig).expect("failed to ser");
let want = "{\"amount\":\"1\"}";
assert_eq!(json, want);
let rinsed: HasAmount = serde_json::from_str(&json).expect("failed to deser");
assert_eq!(rinsed, orig);
}
}