use serde::Serialize;
use crate::{common::test_support::AccountTypeWire, utils::tests::DateTimeUtcWire};
#[derive(Debug, Serialize, proptest_derive::Arbitrary)]
pub struct GetUserProfileResponseWire {
profile: UserProfileWire,
}
#[derive(Debug, Serialize, proptest_derive::Arbitrary)]
pub struct UserProfileWire {
id: String,
name: String,
account: Vec<AccountWire>,
}
#[derive(Debug, Serialize, proptest_derive::Arbitrary)]
pub struct AccountWire {
account_number: String,
classification: ClassificationWire,
#[serde(rename = "date_created")]
date_created: DateTimeUtcWire,
day_trader: bool,
option_level: u8,
status: AccountStatusWire,
#[serde(rename = "type")]
account_type: AccountTypeWire,
#[serde(rename = "last_update_date")]
last_update_date: DateTimeUtcWire,
}
#[derive(Debug, Serialize, proptest_derive::Arbitrary)]
#[serde(rename_all = "lowercase")]
pub enum ClassificationWire {
Individual,
Corporate,
Joint,
Ira,
RothIra,
Entity,
}
#[derive(Debug, Serialize, proptest_derive::Arbitrary)]
#[serde(rename_all = "lowercase")]
pub enum AccountStatusWire {
Active,
Closed,
}
#[cfg(test)]
mod test {
use super::*;
use proptest::prelude::*;
use serde_json::{json, Value};
use std::fs::OpenOptions;
use tracing::debug;
static PATH: &str = "src/user/get_user_profile_schema.json";
#[test]
fn should_fail_to_process_an_empty_object() {
let reader = OpenOptions::new()
.read(true)
.open(PATH)
.expect("schema file to exist and be readable");
let reader = std::io::BufReader::new(reader);
let schema: Value =
serde_json::from_reader(reader).expect("parsing the schema as a Value object to work");
let validator =
jsonschema::validator_for(&schema).expect("validator in test to work as expected");
assert!(!validator.is_valid(
&serde_json::to_value(json!({})).expect("serde to serialize the object correctly")
));
}
proptest! {
#[test]
fn serialized_wire_objects_should_conform_to_schema(wire_object in any::<GetUserProfileResponseWire>()) {
let reader = OpenOptions::new().read(true).open(PATH).expect("schema file to exist and be readable");
let reader = std::io::BufReader::new(reader);
let schema: Value = serde_json::from_reader(reader)
.expect("parsing the schema as a Value object to work");
let validator = jsonschema::validator_for(&schema)
.expect("validator in test to work as expected");
let actual_serialized_value = serde_json::to_value(&wire_object)
.expect("serde to serialize the object correctly");
debug!("{:#?}", &actual_serialized_value);
prop_assert!(validator.is_valid(&actual_serialized_value));
}
}
}