use std::{
assert_matches, env, fs,
path::{Path, PathBuf},
};
use easyfix_core::fix_str;
use quick_xml::de::from_str;
use uuid::Uuid;
use super::*;
const BASIC_FIX_DICT: &str = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
<field name='BodyLength' required='Y'/>
<field name='MsgType' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<field name='TestReqID' required='N'/>
</message>
<message msgcat='admin' msgtype='1' name='TestRequest'>
<field name='TestReqID' required='Y'/>
<component name='TestComponent' required='Y'/>
</message>
</messages>
<components>
<component name='TestComponent'>
<field name='ComponentField' required='Y'/>
</component>
</components>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='BodyLength' number='9' type='LENGTH'/>
<field name='MsgType' number='35' type='STRING'>
<value enum='0' description='HEARTBEAT'/>
<value enum='1' description='TEST_REQUEST'/>
</field>
<field name='CheckSum' number='10' type='STRING'/>
<field name='TestReqID' number='112' type='STRING'/>
<field name='ComponentField' number='999' type='STRING'/>
</fields>
</fix>
"#;
const BASIC_FIXT_DICT: &str = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIXT' major='1' minor='1' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
<field name='BodyLength' required='Y'/>
<field name='MsgType' required='Y'/>
<field name='SenderCompID' required='Y'/>
<field name='TargetCompID' required='Y'/>
<field name='MsgSeqNum' required='Y'/>
<field name='SendingTime' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<field name='TestReqID' required='N'/>
<component name='TestComponent' required='N'/>
</message>
</messages>
<components>
<component name='TestComponent'>
<field name='TestReqID' required='Y'/>
</component>
</components>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='BodyLength' number='9' type='LENGTH'/>
<field name='MsgType' number='35' type='STRING'/>
<field name='CheckSum' number='10' type='STRING'/>
<field name='TestReqID' number='112' type='STRING'/>
<field name='SenderCompID' number='49' type='STRING'/>
<field name='TargetCompID' number='56' type='STRING'/>
<field name='MsgSeqNum' number='34' type='SEQNUM'/>
<field name='SendingTime' number='52' type='UTCTIMESTAMP'/>
</fields>
</fix>
"#;
const BASIC_FIX50_DICT: &str = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='5' minor='0' servicepack='0'>
<header/>
<trailer/>
<messages>
<message msgcat='app' msgtype='D' name='NewOrderSingle'>
<field name='ClOrdID' required='Y'/>
<component name='TestComponent' required='N'/>
</message>
</messages>
<components>
<component name='TestComponent'>
<field name='ClOrdID' required='Y'/>
</component>
</components>
<fields>
<field name='ClOrdID' number='11' type='STRING'/>
</fields>
</fix>
"#;
const COMPLEX_DICT: &str = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
<field name='BodyLength' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='app' msgtype='E' name='NewOrderList'>
<component name='OrderListComponent' required='Y'/>
<field name='ListID' required='Y'/>
</message>
</messages>
<components>
<!-- Complex component hierarchy for testing flattening -->
<component name='OrderListComponent'>
<field name='ListSeqNo' required='Y'/>
<component name='InstrumentComponent' required='Y'/>
<component name='OrderComponent' required='N'/>
<group name='OrderListGroup' required='Y'>
<field name='OrderListGroupField' required='Y'/>
<component name='NestedComponent' required='Y'/>
</group>
</component>
<component name='InstrumentComponent'>
<field name='Symbol' required='Y'/>
<field name='SecurityID' required='N'/>
<component name='SecurityIDComponent' required='N'/>
</component>
<component name='SecurityIDComponent'>
<field name='SecurityIDSource' required='Y'/>
<field name='SecurityDesc' required='N'/>
</component>
<component name='OrderComponent'>
<field name='OrderID' required='Y'/>
<field name='OrderQty' required='Y'/>
<field name='Price' required='N'/>
<group name='OrderParties' required='N'>
<field name='PartyID' required='Y'/>
<field name='PartyRole' required='Y'/>
</group>
</component>
<component name='NestedComponent'>
<field name='NestedField1' required='Y'/>
<field name='NestedField2' required='N'/>
</component>
</components>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='BodyLength' number='9' type='LENGTH'/>
<field name='CheckSum' number='10' type='STRING'/>
<field name='ListID' number='66' type='STRING'/>
<field name='ListSeqNo' number='67' type='INT'/>
<field name='Symbol' number='55' type='STRING'/>
<field name='SecurityID' number='48' type='STRING'/>
<field name='SecurityIDSource' number='22' type='INT'/>
<field name='SecurityDesc' number='107' type='STRING'/>
<field name='OrderID' number='37' type='STRING'/>
<field name='OrderQty' number='38' type='QTY'/>
<field name='Price' number='44' type='PRICE'/>
<field name='OrderListGroup' number='73' type='NUMINGROUP'/>
<field name='OrderListGroupField' number='74' type='STRING'/>
<field name='OrderParties' number='453' type='NUMINGROUP'/>
<field name='PartyID' number='448' type='STRING'/>
<field name='PartyRole' number='452' type='INT'/>
<field name='NestedField1' number='1001' type='STRING'/>
<field name='NestedField2' number='1002' type='STRING'/>
</fields>
</fix>
"#;
struct TestFile {
test_dir: PathBuf,
path: PathBuf,
}
impl TestFile {
fn new(file_name: &str, data: &str) -> TestFile {
let test_dir = env::temp_dir().join(Uuid::new_v4().hyphenated().to_string());
fs::create_dir(test_dir.as_path()).unwrap_or_else(|err| {
panic!(
"Failed to crate temporary directory {}: {err}",
test_dir.display()
)
});
let path = test_dir.join(file_name);
fs::write(&path, data).unwrap_or_else(|err| {
panic!("Failed to write temporary file {}: {err}", path.display())
});
TestFile { test_dir, path }
}
fn path(&self) -> &Path {
&self.path
}
}
impl Drop for TestFile {
fn drop(&mut self) {
if self.test_dir.exists() {
if self.path.exists()
&& let Err(err) = fs::remove_file(&self.path)
{
eprintln!(
"Warning: Failed to clean up temporary file {}: {err}",
self.path.display(),
);
}
if let Err(err) = fs::remove_dir(&self.test_dir) {
eprintln!(
"Warning: Failed to clean up temporary directory {}: {err}",
self.test_dir.display(),
);
}
}
}
}
fn setup_fix44_file() -> TestFile {
TestFile::new("FIX44.xml", BASIC_FIX_DICT)
}
fn setup_fixt11_file() -> TestFile {
TestFile::new("FIXT11.xml", BASIC_FIXT_DICT)
}
fn setup_fix50_file() -> TestFile {
TestFile::new("FIXT50.xml", BASIC_FIX50_DICT)
}
#[test]
fn test_simple_builder() {
let fix44_file = setup_fix44_file();
let result = DictionaryBuilder::new()
.with_fix_xml(fix44_file.path())
.build();
let dictionary = result.expect("Failed to build dictionary");
assert_eq!(dictionary.version(), Version::FIX44);
}
#[test]
fn test_builder_with_fixt_and_fix() {
let fixt11_file = setup_fixt11_file();
let fix50_file = setup_fix50_file();
let result = DictionaryBuilder::new()
.with_fixt_xml(fixt11_file.path())
.with_fix_xml(fix50_file.path())
.build();
let dictionary = result.expect("Failed to build combined FIXT/FIX dictionary");
assert_eq!(dictionary.version(), Version::FIXT11);
assert!(dictionary.subdictionary(Version::FIX50).is_some());
assert!(dictionary.message_by_name(fix_str!("Heartbeat")).is_some());
assert!(
dictionary
.message_by_name(fix_str!("NewOrderSingle"))
.is_none()
);
let subdictionary = dictionary.subdictionary(Version::FIX50).unwrap();
assert!(
subdictionary
.message_by_name(fix_str!("NewOrderSingle"))
.is_some()
);
}
#[test]
fn test_builder_with_multiple_fix_apps() {
let fixt11_file = setup_fixt11_file();
let fix50_file = setup_fix50_file();
let fix50sp2_dict = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='5' minor='0' servicepack='2'>
<header/>
<trailer/>
<messages>
<message msgcat='app' msgtype='8' name='ExecutionReport'>
<field name='OrderID' required='Y'/>
</message>
</messages>
<components>
<component name='TestComponent'>
<field name='OrderID' required='Y'/>
</component>
</components>
<fields>
<field name='OrderID' number='37' type='STRING'/>
</fields>
</fix>
"#;
let fix50sp2_file = TestFile::new("FIX50SP2.xml", fix50sp2_dict);
let result = DictionaryBuilder::new()
.with_fixt_xml(fixt11_file.path())
.with_fix_xmls([
fix50_file.path().to_path_buf(),
fix50sp2_file.path().to_path_buf(),
])
.build();
let dictionary = result.expect("Failed to build dictionary with multiple app layers");
assert_eq!(dictionary.version(), Version::FIXT11);
assert!(dictionary.subdictionary(Version::FIX50).is_some());
assert!(dictionary.subdictionary(Version::FIX50SP2).is_some());
let fix50_subdict = dictionary.subdictionary(Version::FIX50).unwrap();
assert!(
fix50_subdict
.message_by_name(fix_str!("NewOrderSingle"))
.is_some()
);
let fix50sp2_subdict = dictionary.subdictionary(Version::FIX50SP2).unwrap();
assert!(
fix50sp2_subdict
.message_by_name(fix_str!("ExecutionReport"))
.is_some()
);
}
#[test]
fn test_from_file_constructor() {
let fix44_file = setup_fix44_file();
let result = Dictionary::new(&fix44_file.path().display().to_string());
let dictionary = result.expect("Failed to create dictionary from file");
assert_eq!(dictionary.version(), Version::FIX44);
assert!(dictionary.field_by_name(fix_str!("BeginString")).is_some());
assert!(dictionary.message_by_name(fix_str!("Heartbeat")).is_some());
}
#[test]
fn test_incompatible_versions() {
let fixt11_file = setup_fixt11_file();
let fix44_file = setup_fix44_file();
let result = DictionaryBuilder::new()
.with_fixt_xml(fixt11_file.path())
.with_fix_xml(fix44_file.path())
.build();
assert_matches!(
result,
Err(Error::Builder(BuilderError::IncompatibleVersion))
);
}
#[test]
fn test_unknown_version_error() {
let invalid_version_xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='9' minor='9' servicepack='9'>
<header>
<field name='BeginString' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<field name='TestReqID' required='N'/>
</message>
</messages>
<components/>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='CheckSum' number='10' type='STRING'/>
<field name='TestReqID' number='112' type='STRING'/>
</fields>
</fix>
"#;
let test_file = TestFile::new("invalid_version.xml", invalid_version_xml);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.build();
assert_matches!(result, Err(Error::Builder(BuilderError::UnknownVersion(_))));
}
#[test]
fn test_unknown_field_error() {
let unknown_field_xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<field name='UnknownField' required='Y'/>
</message>
</messages>
<components/>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='CheckSum' number='10' type='STRING'/>
<!-- UnknownField is referenced but not defined -->
</fields>
</fix>
"#;
let test_file = TestFile::new("unknown_field.xml", unknown_field_xml);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.build();
assert_matches!(result, Err(Error::Validation(ValidationError::UnknownField(name))) if name == "UnknownField");
}
#[test]
fn test_unknown_component_error() {
let unknown_component_xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<component name='UnknownComponent' required='Y'/>
</message>
</messages>
<components/>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='CheckSum' number='10' type='STRING'/>
</fields>
</fix>
"#;
let test_file = TestFile::new("unknown_component.xml", unknown_component_xml);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.build();
assert_matches!(result, Err(Error::Validation(ValidationError::UnknownComponent(name))) if name == "UnknownComponent");
}
#[test]
fn test_duplicated_field_error() {
let duplicated_field_xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<field name='TestReqID' required='N'/>
</message>
</messages>
<components/>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='CheckSum' number='10' type='STRING'/>
<field name='TestReqID' number='112' type='STRING'/>
<!-- Duplicated field with same name -->
<field name='TestReqID' number='113' type='INT'/>
</fields>
</fix>
"#;
let test_file = TestFile::new("duplicated_field.xml", duplicated_field_xml);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.build();
assert_matches!(result, Err(Error::Validation(ValidationError::DuplicatedField(name))) if name == "TestReqID");
}
#[test]
fn test_duplicated_message_name_error() {
let duplicated_message_xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<field name='TestReqID' required='N'/>
</message>
<!-- Same message name, different type -->
<message msgcat='admin' msgtype='1' name='Heartbeat'>
<field name='TestReqID' required='Y'/>
</message>
</messages>
<components/>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='CheckSum' number='10' type='STRING'/>
<field name='TestReqID' number='112' type='STRING'/>
</fields>
</fix>
"#;
let test_file = TestFile::new("duplicated_message.xml", duplicated_message_xml);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.build();
assert_matches!(result, Err(Error::Validation(ValidationError::DuplicatedMessageName(name))) if name == "Heartbeat");
}
#[test]
fn test_duplicated_message_type_error() {
let duplicated_msgtype_xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<field name='TestReqID' required='N'/>
</message>
<!-- Same message type, different name -->
<message msgcat='admin' msgtype='0' name='TestHeartbeat'>
<field name='TestReqID' required='Y'/>
</message>
</messages>
<components/>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='CheckSum' number='10' type='STRING'/>
<field name='TestReqID' number='112' type='STRING'/>
</fields>
</fix>
"#;
let test_file = TestFile::new("duplicated_msgtype.xml", duplicated_msgtype_xml);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.build();
assert_matches!(result, Err(Error::Validation(ValidationError::DuplicatedMessageType(msg_type))) if msg_type.as_str() == "0");
}
#[test]
fn test_empty_container_error() {
let empty_component_xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<component name='EmptyComponent' required='Y'/>
</message>
</messages>
<components>
<!-- Empty component with no members -->
<component name='EmptyComponent'>
</component>
</components>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='CheckSum' number='10' type='STRING'/>
</fields>
</fix>
"#;
let test_file = TestFile::new("empty_component.xml", empty_component_xml);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.build();
assert_matches!(result, Err(Error::Validation(ValidationError::EmptyContainer(name))) if name == "EmptyComponent");
}
#[test]
fn test_empty_message_error() {
let empty_message_xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<!-- Empty message with no members -->
<message msgcat='admin' msgtype='0' name='Heartbeat'>
</message>
</messages>
<components/>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='CheckSum' number='10' type='STRING'/>
</fields>
</fix>
"#;
let test_file = TestFile::new("empty_message.xml", empty_message_xml);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.build();
assert_matches!(result, Err(Error::Validation(ValidationError::EmptyMessage(name))) if name == "Heartbeat");
}
#[test]
fn test_unexpected_message_category_error() {
let unexpected_category_xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIXT' major='1' minor='1' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<field name='TestReqID' required='N'/>
</message>
<!-- App message in FIXT dictionary (should be admin only) -->
<message msgcat='app' msgtype='D' name='NewOrder'>
<field name='TestReqID' required='N'/>
</message>
</messages>
<components/>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='CheckSum' number='10' type='STRING'/>
<field name='TestReqID' number='112' type='STRING'/>
</fields>
</fix>
"#;
let test_file = TestFile::new("unexpected_category.xml", unexpected_category_xml);
let result = DictionaryBuilder::new()
.with_fixt_xml(test_file.path())
.build();
assert_matches!(
result,
Err(Error::Validation(ValidationError::UnexpectedMessageCategory(MsgCat::App, name))) if name == "NewOrder"
)
}
#[test]
fn test_missing_required_fields() {
let missing_required = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header/>
<trailer/>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
</message>
</messages>
<components/>
<fields>
<field name='BeginString' number='8' type='STRING'/>
</fields>
</fix>
"#;
let test_file = TestFile::new("invalid_dict.xml", missing_required);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.build();
assert!(result.is_err())
}
#[test]
fn test_builder_with_unspecified_dictionary() {
let result = DictionaryBuilder::new().build();
assert!(result.is_err());
}
#[test]
fn test_component_flattening() {
let xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
<field name='BodyLength' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<component name='TestComponent' required='Y'/>
</message>
</messages>
<components>
<component name='TestComponent'>
<field name='TestField1' required='Y'/>
<component name='NestedComponent' required='N'/>
</component>
<component name='NestedComponent'>
<field name='TestField2' required='Y'/>
</component>
</components>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='BodyLength' number='9' type='LENGTH'/>
<field name='CheckSum' number='10' type='STRING'/>
<field name='TestField1' number='1000' type='STRING'/>
<field name='TestField2' number='1001' type='STRING'/>
</fields>
</fix>
"#
.trim();
let normal_dict_raw: xml::Dictionary = from_str(xml).unwrap();
let normal_dict = Dictionary::from_raw_dictionary(normal_dict_raw, false, false).unwrap();
let heartbeat = normal_dict.message_by_name(fix_str!("Heartbeat")).unwrap();
assert_eq!(heartbeat.members().len(), 1);
if !matches!(
heartbeat.members()[0].definition(),
MemberDefinition::Component(_)
) {
panic!("Expected component member but found something else");
}
let flattened_dict_raw: xml::Dictionary = from_str(xml).unwrap();
let flattened_dict = Dictionary::from_raw_dictionary(flattened_dict_raw, true, false)
.unwrap()
.flatten()
.unwrap();
let flattened_heartbeat = flattened_dict
.message_by_name(fix_str!("Heartbeat"))
.unwrap();
assert_eq!(flattened_heartbeat.members().len(), 2);
assert!(
flattened_heartbeat
.members()
.iter()
.all(|m| matches!(m.definition(), MemberDefinition::Field(_)))
);
assert!(flattened_heartbeat.members().iter().all(|m| m.required()));
}
fn complex_dictionary(flatten: bool) -> Dictionary {
let file = TestFile::new("FIX44.xml", COMPLEX_DICT);
DictionaryBuilder::new()
.with_fix_xml(file.path())
.flatten_components(flatten)
.build()
.unwrap()
}
#[test]
fn test_deep_component_flattening() {
let normal_dict = complex_dictionary(false);
let flattened_dict = complex_dictionary(true);
let normal_msg = normal_dict
.message_by_name(fix_str!("NewOrderList"))
.expect("Message not found");
let flattened_msg = flattened_dict
.message_by_name(fix_str!("NewOrderList"))
.expect("Message not found");
assert_eq!(normal_msg.members().len(), 2);
assert!(flattened_msg.members().len() > 2);
let flattened_fields = flattened_msg
.members()
.iter()
.filter_map(|m| match m.definition() {
MemberDefinition::Field(field) => Some(field.name.as_utf8()),
_ => None,
})
.collect::<Vec<_>>();
assert!(flattened_fields.contains(&"ListID"), "ListID missing");
assert!(
flattened_fields.contains(&"ListSeqNo"),
"ListSeqNo missing (from OrderListComponent)"
);
assert!(
flattened_fields.contains(&"Symbol"),
"Symbol missing (from InstrumentComponent)"
);
let has_order_list_group = flattened_msg
.members()
.iter()
.any(|m| match m.definition() {
MemberDefinition::Group(group) => group.name() == "OrderListGroup",
_ => false,
});
assert!(
has_order_list_group,
"OrderListGroup missing after flattening"
);
let flattened_group = flattened_msg
.members()
.iter()
.find_map(|m| match m.definition() {
MemberDefinition::Group(group) if group.name() == "OrderListGroup" => Some(group),
_ => None,
})
.expect("OrderListGroup not found");
let group_field_names = flattened_group
.members()
.iter()
.filter_map(|m| match m.definition() {
MemberDefinition::Field(field) => Some(field.name.as_utf8()),
_ => None,
})
.collect::<Vec<_>>();
assert!(
group_field_names.contains(&"OrderListGroupField"),
"OrderListGroupField missing"
);
assert!(
group_field_names.contains(&"NestedField1"),
"NestedField1 missing from flattened group"
);
assert!(
group_field_names.contains(&"NestedField2"),
"NestedField2 missing from flattened group"
);
let has_order_component_fields = flattened_msg
.members()
.iter()
.any(|m| match m.definition() {
MemberDefinition::Field(field) => field.name == "OrderID",
_ => false,
});
assert!(
has_order_component_fields,
"OrderID missing from flattened message"
);
let has_order_parties_group = flattened_msg
.members()
.iter()
.any(|m| match m.definition() {
MemberDefinition::Group(group) => group.name() == "OrderParties",
_ => false,
});
assert!(
has_order_parties_group,
"OrderParties group missing from flattened message"
);
}
#[test]
fn test_nested_required_flag_propagation() {
let flattened_dict = complex_dictionary(true);
let flattened_msg = flattened_dict
.message_by_name(fix_str!("NewOrderList"))
.expect("Message not found");
for member in flattened_msg.members() {
match member.definition() {
MemberDefinition::Field(field) => {
match field.name.as_utf8() {
"ListSeqNo" => assert!(member.required(), "ListSeqNo should be required"),
"Symbol" => assert!(member.required(), "Symbol should be required"),
"SecurityID" => {
assert!(!member.required(), "SecurityID should not be required")
}
"OrderID" => assert!(
member.required(),
"OrderID should be required (from its component definition)"
),
"Price" => assert!(!member.required(), "Price should not be required"),
"SecurityIDSource" => assert!(
member.required(),
"SecurityIDSource should be required (from its component definition)"
),
"ListID" => assert!(member.required(), "ListID should be required"),
_ => {}
}
}
MemberDefinition::Group(group) => {
match group.name().as_utf8() {
"OrderListGroup" => {
assert!(member.required(), "OrderListGroup should be required")
}
"OrderParties" => {
assert!(!member.required(), "OrderParties should not be required")
}
_ => {}
}
}
_ => {}
}
}
for member in flattened_msg.members() {
if let MemberDefinition::Group(group) = member.definition()
&& group.name() == "OrderListGroup"
{
for group_member in group.members() {
if let MemberDefinition::Field(field) = group_member.definition() {
match field.name.as_utf8() {
"OrderListGroupField" => assert!(
group_member.required(),
"OrderListGroupField should be required"
),
"NestedField1" => {
assert!(group_member.required(), "NestedField1 should be required")
}
"NestedField2" => assert!(
!group_member.required(),
"NestedField2 should not be required"
),
_ => {}
}
}
}
}
}
}
const CIRCULAR_COMPONENTS_DICT: &str = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
<field name='BodyLength' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='app' msgtype='X' name='TestMessage'>
<component name='ComponentA' required='Y'/>
</message>
</messages>
<components>
<!-- Circular reference: A -> B -> A -->
<component name='ComponentA'>
<field name='FieldA' required='Y'/>
<component name='ComponentB' required='Y'/>
</component>
<component name='ComponentB'>
<field name='FieldB' required='Y'/>
<component name='ComponentA' required='N'/>
</component>
</components>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='BodyLength' number='9' type='LENGTH'/>
<field name='CheckSum' number='10' type='STRING'/>
<field name='FieldA' number='101' type='STRING'/>
<field name='FieldB' number='102' type='STRING'/>
</fields>
</fix>
"#;
#[test]
fn test_circular_references() {
let test_file = TestFile::new("circular_dict.xml", CIRCULAR_COMPONENTS_DICT);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.flatten_components(true)
.build();
assert_matches!(
result,
Err(Error::Validation(ValidationError::CircularReference(_))),
"Circular component references should cause an error"
);
}
#[test]
fn parse_full_xml() {
let xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIXT' major='1' minor='1' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
<field name='BodyLength' required='Y'/>
<field name='MsgType' required='Y'/>
<field name='SenderCompID' required='Y'/>
<field name='TargetCompID' required='Y'/>
<group name='NoHops' required='N'>
<field name='HopCompID' required='N' />
<field name='HopSendingTime' required='N' />
<field name='HopRefID' required='N' />
</group>
<field name='ApplVerID' required='N'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<field name='TestReqID' required='N'/>
</message>
<message msgcat='admin' msgtype='1' name='TestRequest'>
<field name='TestReqID' required='Y'/>
</message>
</messages>
<components>
<component name='MsgTypeGrp'>
<group name='NoMsgTypes' required='N'>
<field name='RefMsgType' required='N'/>
<field name='MsgDirection' required='N'/>
</group>
</component>
</components>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='BodyLength' number='9' type='LENGTH'/>
<field name='CheckSum' number='10' type='STRING'/>
<field number='35' name='MsgType' type='STRING'>
<value enum='0' description='HEARTBEAT'/>
<value enum='1' description='TEST_REQUEST'/>
</field>
<field name='RefMsgType' number='372' type='STRING'/>
<field name='PossDupFlag' number='43' type='BOOLEAN'/>
<field name='RefSeqNum' number='45' type='SEQNUM'/>
<field name='SenderCompID' number='49' type='STRING'/>
<field name='SenderSubID' number='50' type='STRING'/>
<field name='SendingTime' number='52' type='UTCTIMESTAMP'/>
<field name='TargetCompID' number='56' type='STRING'/>
<field name='TargetSubID' number='57' type='STRING'/>
<field name='Text' number='58' type='STRING'/>
<field name='RawDataLength' number='95' type='LENGTH'/>
<field name='RawData' number='96' type='DATA'/>
<field name='HeartBtInt' number='108' type='INT'/>
<field name='TestReqID' number='112' type='STRING'/>
<field name='OnBehalfOfCompID' number='115' type='STRING'/>
<field name='OnBehalfOfSubID' number='116' type='STRING'/>
<field name='OrigSendingTime' number='122' type='UTCTIMESTAMP'/>
<field name='GapFillFlag' number='123' type='BOOLEAN'/>
<field name='DeliverToCompID' number='128' type='STRING'/>
<field name='DeliverToSubID' number='129' type='STRING'/>
<field name='ResetSeqNumFlag' number='141' type='BOOLEAN'/>
<field name='SenderLocationID' number='142' type='STRING'/>
<field name='TargetLocationID' number='143' type='STRING'/>
<field name='OnBehalfOfLocationID' number='144' type='STRING'/>
<field name='DeliverToLocationID' number='145' type='STRING'/>
<field name='SessionRejectReason' number='373' type='INT'>
<value description='INVALID_TAG_NUMBER' enum='0'/>
<value description='REQUIRED_TAG_MISSING' enum='1'/>
</field>
<field name='MaxMessageSize' number='383' type='LENGTH'/>
<field name='NoMsgTypes' number='384' type='NUMINGROUP'/>
<field name='MsgDirection' number='385' type='CHAR'>
<value description='RECEIVE' enum='R'/>
<value description='SEND' enum='S'/>
</field>
<field name='TestMessageIndicator' number='464' type='BOOLEAN'/>
<field name='Username' number='553' type='STRING'/>
<field name='Password' number='554' type='STRING'/>
<field name='NoHops' number='627' type='NUMINGROUP'/>
<field name='HopCompID' number='628' type='STRING'/>
<field name='HopSendingTime' number='629' type='UTCTIMESTAMP'/>
<field name='HopRefID' number='630' type='SEQNUM'/>
<field name='NextExpectedMsgSeqNum' number='789' type='SEQNUM'/>
<field name='ApplVerID' number='1128' type='STRING'>
<value description='FIX44' enum='6'/>
<value description='FIX50SP2' enum='9'/>
</field>
</fields>
</fix>
"#
.trim();
{
let raw_dictionary: xml::Dictionary = from_str(xml).unwrap();
let _dictionary = Dictionary::from_raw_dictionary(raw_dictionary, false, false).unwrap();
}
{
let raw_dictionary: xml::Dictionary = from_str(xml).unwrap();
let _dictionary = Dictionary::from_raw_dictionary(raw_dictionary, true, false).unwrap();
}
}
#[test]
fn test_field_lookups() {
let fix44_file = setup_fix44_file();
let result = DictionaryBuilder::new()
.with_fix_xml(fix44_file.path())
.build();
let dictionary = result.expect("Failed to build dictionary");
let begin_string = dictionary
.field_by_name(fix_str!("BeginString"))
.expect("Field not found");
assert_eq!(begin_string.number, 8);
assert_eq!(begin_string.name, "BeginString");
assert_eq!(begin_string.data_type, BasicType::String);
let found = dictionary.field_by_id(9).expect("Field not found");
assert_eq!(found.name, "BodyLength");
assert_eq!(found.data_type, BasicType::Length);
let msg_type = dictionary
.field_by_name(fix_str!("MsgType"))
.expect("Field not found");
let variants = msg_type.variants();
assert_eq!(variants.len(), 2);
assert_eq!(variants[0].value(), "0");
assert_eq!(variants[0].name(), "HEARTBEAT");
assert_eq!(variants[1].value(), "1");
assert_eq!(variants[1].name(), "TEST_REQUEST");
assert!(
dictionary
.field_by_name(fix_str!("NonExistentField"))
.is_none()
);
assert!(dictionary.field_by_id(65535).is_none());
let all_fields = dictionary.fields().count();
assert_eq!(all_fields, 6);
}
#[test]
fn test_message_lookups() {
let fix44_file = setup_fix44_file();
let result = DictionaryBuilder::new()
.with_fix_xml(fix44_file.path())
.build();
let dictionary = result.expect("Failed to build dictionary");
let Some(heartbeat) = dictionary.message_by_name(fix_str!("Heartbeat")) else {
panic!("Heartbeat message not found");
};
assert_eq!(heartbeat.name(), "Heartbeat");
assert_eq!(heartbeat.msg_type(), "0".parse::<MsgType>().unwrap());
assert_matches!(heartbeat.msg_cat(), MsgCat::Admin);
let Some(test_req) = dictionary.message_by_type(b"1") else {
panic!("MsgType '1' message not found");
};
assert_eq!(test_req.name(), "TestRequest");
assert_matches!(test_req.msg_cat(), MsgCat::Admin);
let members = heartbeat.members();
assert_eq!(members.len(), 1);
let member = &members[0];
assert!(!member.required());
assert_matches!(
member.definition(),
MemberDefinition::Field(field) if field.name == "TestReqID" && field.number == 112
);
assert!(
dictionary
.message_by_name(fix_str!("NonExistentMessage"))
.is_none()
);
assert!(dictionary.message_by_type(b"X").is_none());
let all_messages = dictionary.messages().count();
assert_eq!(all_messages, 2);
}
#[test]
fn test_component_lookups() {
let fix44_file = setup_fix44_file();
let result = DictionaryBuilder::new()
.with_fix_xml(fix44_file.path())
.build();
let dictionary = result.expect("Failed to build dictionary");
let Some(component) = dictionary.component(fix_str!("TestComponent")) else {
panic!("TestComponent component not found");
};
assert_eq!(component.name(), "TestComponent");
let members = component.members();
assert_eq!(members.len(), 1);
let member = &members[0];
assert!(member.required());
assert_matches!(
member.definition(),
MemberDefinition::Field(field) if field.name == "ComponentField" && field.number == 999
);
assert!(
dictionary
.component(fix_str!("NonExistentComponent"))
.is_none()
);
let all_components = dictionary.components().count();
assert_eq!(all_components, 1);
}
#[test]
fn test_builder_with_strict_check_unused_field() {
let unused_field_xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
<field name='BodyLength' required='Y'/>
<field name='MsgType' required='Y'/>
<field name='SenderCompID' required='Y'/>
<field name='TargetCompID' required='Y'/>
<field name='MsgSeqNum' required='Y'/>
<field name='SendingTime' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<field name='TestReqID' required='N'/>
</message>
</messages>
<components/>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='BodyLength' number='9' type='LENGTH'/>
<field name='MsgType' number='35' type='STRING'/>
<field name='CheckSum' number='10' type='STRING'/>
<field name='TestReqID' number='112' type='STRING'/>
<field name='SenderCompID' number='49' type='STRING'/>
<field name='TargetCompID' number='56' type='STRING'/>
<field name='MsgSeqNum' number='34' type='SEQNUM'/>
<field name='SendingTime' number='52' type='UTCTIMESTAMP'/>
<!-- This field is defined but not used anywhere -->
<field name='UnusedField' number='999' type='STRING'/>
</fields>
</fix>
"#;
let test_file = TestFile::new("unused_field.xml", unused_field_xml);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.with_strict_check(false)
.build();
assert!(
result.is_ok(),
"Dictionary without strict check should succeed with unused field"
);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.with_strict_check(true)
.build();
assert_matches!(result, Err(Error::Validation(ValidationError::UnusedField(name, tag))) if name == "UnusedField" && tag == 999);
}
#[test]
fn test_builder_with_strict_check_unused_component() {
let unused_component_xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
<field name='BodyLength' required='Y'/>
<field name='MsgType' required='Y'/>
<field name='SenderCompID' required='Y'/>
<field name='TargetCompID' required='Y'/>
<field name='MsgSeqNum' required='Y'/>
<field name='SendingTime' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<field name='TestReqID' required='N'/>
<!-- Only using UsedComponent -->
<component name='UsedComponent' required='Y'/>
</message>
</messages>
<components>
<component name='UsedComponent'>
<field name='UsedField' required='Y'/>
</component>
<!-- This component is defined but not referenced anywhere -->
<component name='UnusedComponent'>
<field name='UnusedCompField' required='Y'/>
</component>
</components>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='BodyLength' number='9' type='LENGTH'/>
<field name='MsgType' number='35' type='STRING'/>
<field name='CheckSum' number='10' type='STRING'/>
<field name='TestReqID' number='112' type='STRING'/>
<field name='SenderCompID' number='49' type='STRING'/>
<field name='TargetCompID' number='56' type='STRING'/>
<field name='MsgSeqNum' number='34' type='SEQNUM'/>
<field name='SendingTime' number='52' type='UTCTIMESTAMP'/>
<field name='UsedField' number='998' type='STRING'/>
<field name='UnusedCompField' number='999' type='STRING'/>
</fields>
</fix>
"#;
let test_file = TestFile::new("unused_component.xml", unused_component_xml);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.with_strict_check(false)
.build();
assert!(
result.is_ok(),
"Dictionary without strict check should succeed with unused component"
);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.with_strict_check(true)
.build();
assert_matches!(result, Err(Error::Validation(ValidationError::UnusedField(name, _))) if name == "UnusedCompField");
}
#[test]
fn test_builder_with_strict_invalid_header() {
let invalid_header_xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<!-- First field is wrong (should be BeginString) -->
<field name='WrongField' required='Y'/>
<field name='BodyLength' required='Y'/>
<field name='MsgType' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<field name='TestReqID' required='N'/>
</message>
</messages>
<components/>
<fields>
<field name='WrongField' number='8' type='STRING'/>
<field name='BodyLength' number='9' type='LENGTH'/>
<field name='MsgType' number='35' type='STRING'/>
<field name='CheckSum' number='10' type='STRING'/>
<field name='TestReqID' number='112' type='STRING'/>
</fields>
</fix>
"#;
let test_file = TestFile::new("invalid_header.xml", invalid_header_xml);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.with_strict_check(false)
.build();
assert!(
result.is_ok(),
"Dictionary without strict check should succeed with invalid header"
);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.with_strict_check(true)
.build();
assert_matches!(
result,
Err(Error::Validation(ValidationError::InvalidRequiredField(name, _, _))) if name == "BeginString",
"Expected InvalidRequiredField error for BeginString"
);
}
#[test]
fn test_builder_with_strict_check_out_of_order_header_fields() {
let reordered_header_xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
<field name='BodyLength' required='Y'/>
<field name='MsgType' required='Y'/>
<field name='SendingTime' required='Y'/>
<field name='TargetCompID' required='Y'/>
<field name='SenderCompID' required='Y'/>
<field name='MsgSeqNum' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<field name='TestReqID' required='N'/>
</message>
</messages>
<components/>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='BodyLength' number='9' type='LENGTH'/>
<field name='MsgType' number='35' type='STRING'/>
<field name='CheckSum' number='10' type='STRING'/>
<field name='TestReqID' number='112' type='STRING'/>
<field name='SenderCompID' number='49' type='STRING'/>
<field name='TargetCompID' number='56' type='STRING'/>
<field name='MsgSeqNum' number='34' type='SEQNUM'/>
<field name='SendingTime' number='52' type='UTCTIMESTAMP'/>
</fields>
</fix>
"#;
let test_file = TestFile::new("reordered_header.xml", reordered_header_xml);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.with_strict_check(true)
.build();
assert!(
result.is_ok(),
"Strict check should accept required header fields in any order: {:?}",
result.err()
);
}
#[test]
fn test_builder_with_strict_check_header_without_sender_comp_id() {
let no_sender_comp_id_xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
<field name='BodyLength' required='Y'/>
<field name='MsgType' required='Y'/>
<!-- SenderCompID is missing -->
<field name='TargetCompID' required='Y'/>
<field name='MsgSeqNum' required='Y'/>
<field name='SendingTime' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<field name='TestReqID' required='N'/>
</message>
</messages>
<components/>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='BodyLength' number='9' type='LENGTH'/>
<field name='MsgType' number='35' type='STRING'/>
<field name='CheckSum' number='10' type='STRING'/>
<field name='TestReqID' number='112' type='STRING'/>
<field name='TargetCompID' number='56' type='STRING'/>
<field name='MsgSeqNum' number='34' type='SEQNUM'/>
<field name='SendingTime' number='52' type='UTCTIMESTAMP'/>
</fields>
</fix>
"#;
let test_file = TestFile::new("no_sender_comp_id.xml", no_sender_comp_id_xml);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.with_strict_check(false)
.build();
assert!(
result.is_ok(),
"Dictionary without strict check should succeed without SenderCompID"
);
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.with_strict_check(true)
.build();
assert_matches!(
result,
Err(Error::Validation(ValidationError::MissingRequiredField(name, tag)))
if name == "SenderCompID" && tag == 49,
"Expected MissingRequiredField error for SenderCompID"
);
}
fn setup_optional_header_field_file(field: &str) -> TestFile {
let xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
<field name='BodyLength' required='Y'/>
<field name='MsgType' required='Y'/>
<field name='SenderCompID' required='Y'/>
<field name='TargetCompID' required='Y'/>
<field name='MsgSeqNum' required='Y'/>
<field name='SendingTime' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<field name='TestReqID' required='N'/>
</message>
</messages>
<components/>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='BodyLength' number='9' type='LENGTH'/>
<field name='MsgType' number='35' type='STRING'/>
<field name='CheckSum' number='10' type='STRING'/>
<field name='TestReqID' number='112' type='STRING'/>
<field name='SenderCompID' number='49' type='STRING'/>
<field name='TargetCompID' number='56' type='STRING'/>
<field name='MsgSeqNum' number='34' type='SEQNUM'/>
<field name='SendingTime' number='52' type='UTCTIMESTAMP'/>
</fields>
</fix>
"#;
let required = format!("<field name='{field}' required='Y'/>");
let optional = format!("<field name='{field}' required='N'/>");
assert_eq!(xml.matches(&required).count(), 1);
let xml = xml.replace(&required, &optional);
TestFile::new("optional_header_field.xml", &xml)
}
#[test]
fn test_builder_with_strict_check_optional_body_length() {
let test_file = setup_optional_header_field_file("BodyLength");
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.with_strict_check(true)
.build();
assert_matches!(
result,
Err(Error::Validation(ValidationError::OptionalRequiredField(name, tag)))
if name == "BodyLength" && tag == 9,
"Expected OptionalRequiredField error for optional BodyLength"
);
}
#[test]
fn test_builder_with_strict_check_optional_sending_time() {
let test_file = setup_optional_header_field_file("SendingTime");
let result = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.with_strict_check(true)
.build();
assert_matches!(
result,
Err(Error::Validation(ValidationError::OptionalRequiredField(name, tag)))
if name == "SendingTime" && tag == 52,
"Expected OptionalRequiredField error for optional SendingTime"
);
}
#[test]
fn test_header_trailer() {
let fix44_file = setup_fix44_file();
let result = DictionaryBuilder::new()
.with_fix_xml(fix44_file.path())
.build();
let dictionary = result.expect("Failed to build dictionary");
let header = dictionary.header();
assert_eq!(header.name(), "Header");
assert_eq!(header.members().len(), 3);
let first_member = &header.members()[0];
assert!(first_member.required());
match first_member.definition() {
MemberDefinition::Field(field) => {
assert_eq!(field.name, "BeginString");
}
_ => panic!("Expected field member"),
}
let trailer = dictionary.trailer();
assert_eq!(trailer.name(), "Trailer");
assert_eq!(trailer.members().len(), 1);
let first_member = &trailer.members()[0];
assert!(first_member.required());
match first_member.definition() {
MemberDefinition::Field(field) => {
assert_eq!(field.name, "CheckSum");
}
_ => panic!("Expected field member"),
}
}
#[test]
fn test_raw_data_pairing() {
let raw_data_xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
<field name='BodyLength' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='admin' msgtype='0' name='Heartbeat'>
<field name='TestReqID' required='N'/>
<field name='EncodedTextLen' required='N'/>
<field name='EncodedText' required='N'/>
</message>
<message msgcat='app' msgtype='D' name='NewOrderSingle'>
<field name='ClOrdID' required='Y'/>
<field name='MaxMessageSize' required='N'/>
<field name='XmlDataLen' required='N'/>
<field name='XmlData' required='N'/>
</message>
</messages>
<components/>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='BodyLength' number='9' type='LENGTH'/>
<field name='CheckSum' number='10' type='STRING'/>
<field name='ClOrdID' number='11' type='STRING'/>
<field name='TestReqID' number='112' type='STRING'/>
<field name='EncodedTextLen' number='354' type='LENGTH'/>
<field name='EncodedText' number='355' type='DATA'/>
<field name='MaxMessageSize' number='383' type='LENGTH'/>
<field name='XmlDataLen' number='212' type='LENGTH'/>
<field name='XmlData' number='213' type='XMLDATA'/>
</fields>
</fix>
"#;
let test_file = TestFile::new("raw_data.xml", raw_data_xml);
let dictionary = DictionaryBuilder::new()
.with_fix_xml(test_file.path())
.build()
.expect("Failed to build dictionary");
let header = dictionary.header();
let body_length_member = &header.members()[1];
assert_matches!(
body_length_member.definition(),
MemberDefinition::Field(f) if f.name() == "BodyLength" && f.data_type() == BasicType::Length,
"BodyLength should be a plain Field, not RawData"
);
let heartbeat = dictionary.message_by_name(fix_str!("Heartbeat")).unwrap();
assert_eq!(
heartbeat.members().len(),
2,
"TestReqID + RawData(EncodedText)"
);
let test_req = &heartbeat.members()[0];
assert_matches!(
test_req.definition(),
MemberDefinition::Field(f) if f.name() == "TestReqID"
);
let encoded_text = &heartbeat.members()[1];
assert_matches!(
encoded_text.definition(),
MemberDefinition::RawData { length, data }
if length.name() == "EncodedTextLen" && data.name() == "EncodedText"
&& length.data_type() == BasicType::Length
&& data.data_type() == BasicType::Data,
"EncodedTextLen + EncodedText should be paired as RawData"
);
assert!(!encoded_text.required());
assert_eq!(encoded_text.name(), "EncodedText");
let (len_field, data_field) = encoded_text.as_raw_data().unwrap();
assert_eq!(len_field.number(), 354);
assert_eq!(data_field.number(), 355);
assert!(encoded_text.is_raw_data());
assert!(!encoded_text.is_field());
let nos = dictionary
.message_by_name(fix_str!("NewOrderSingle"))
.unwrap();
assert_eq!(
nos.members().len(),
3,
"ClOrdID + MaxMessageSize + RawData(XmlData)"
);
let max_msg_size = &nos.members()[1];
assert_matches!(
max_msg_size.definition(),
MemberDefinition::Field(f) if f.name() == "MaxMessageSize" && f.data_type() == BasicType::Length,
"MaxMessageSize should remain a plain Field (not followed by Data)"
);
let xml_data = &nos.members()[2];
assert_matches!(
xml_data.definition(),
MemberDefinition::RawData { length, data }
if length.name() == "XmlDataLen" && data.name() == "XmlData"
&& data.data_type() == BasicType::XmlData,
"XmlDataLen + XmlData should be paired as RawData"
);
}
#[test]
fn test_builder_with_strict_check_fix50_empty_header() {
let fixt11_file = setup_fixt11_file();
let fix50_file = setup_fix50_file();
let result = DictionaryBuilder::new()
.with_fixt_xml(fixt11_file.path())
.with_fix_xml(fix50_file.path())
.with_strict_check(true)
.build();
assert!(
result.is_ok(),
"Strict check should pass for FIX 5.0+ with empty header/trailer: {:?}",
result.err()
);
}
#[test]
fn test_builder_with_strict_check_fix50_msg_type_not_unused() {
let fixt11_file = setup_fixt11_file();
let fix50_with_msg_type = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='5' minor='0' servicepack='0'>
<header/>
<trailer/>
<messages>
<message msgcat='app' msgtype='D' name='NewOrderSingle'>
<field name='ClOrdID' required='Y'/>
</message>
</messages>
<components/>
<fields>
<field name='MsgType' number='35' type='STRING'/>
<field name='ClOrdID' number='11' type='STRING'/>
</fields>
</fix>
"#;
let fix50_file = TestFile::new("FIX50_msg_type.xml", fix50_with_msg_type);
let result = DictionaryBuilder::new()
.with_fixt_xml(fixt11_file.path())
.with_fix_xml(fix50_file.path())
.with_strict_check(true)
.build();
assert!(
result.is_ok(),
"MsgType should not be flagged as unused in FIX 5.0+ with empty header: {:?}",
result.err()
);
}
const DOC_DICT: &str = r#"
<?xml version='1.0' encoding='UTF-8'?>
<fix type='FIX' major='4' minor='4' servicepack='0'>
<header>
<field name='BeginString' required='Y'/>
<field name='BodyLength' required='Y'/>
</header>
<trailer>
<field name='CheckSum' required='Y'/>
</trailer>
<messages>
<message msgcat='app' msgtype='D' name='NewOrderSingle' doc='Submits a new order.'>
<field name='ClOrdID' required='Y'/>
<field name='Side' required='Y'/>
<component name='Instrument' required='Y'/>
<component name='Parties' required='N'/>
<component name='Undocumented' required='N'/>
<group name='NoAllocs' required='N' doc='Allocations of the order.'>
<field name='AllocAccount' required='N'/>
</group>
</message>
</messages>
<components>
<component name='Instrument' doc='Instrument identification.'>
<field name='Symbol' required='Y'/>
</component>
<component name='Parties' doc='Parties of the order.'>
<group name='NoPartyIDs' required='N' doc='Party group own doc.'>
<field name='PartyID' required='Y'/>
</group>
</component>
<component name='Undocumented'>
<group name='NoUndocEntries' required='N' doc='Group own doc.'>
<field name='UndocEntry' required='Y'/>
</group>
</component>
</components>
<fields>
<field name='BeginString' number='8' type='STRING'/>
<field name='BodyLength' number='9' type='LENGTH'/>
<field name='CheckSum' number='10' type='STRING'/>
<field name='ClOrdID' number='11' type='STRING' doc='Unique order id.'/>
<field name='Side' number='54' type='CHAR' doc='Side of order.'>
<value enum='1' description='BUY' doc='Buy order.'/>
<value enum='2' description='SELL'/>
</field>
<field name='Symbol' number='55' type='STRING'/>
<field name='NoAllocs' number='78' type='NUMINGROUP'/>
<field name='AllocAccount' number='79' type='STRING'/>
<field name='NoPartyIDs' number='453' type='NUMINGROUP'/>
<field name='PartyID' number='448' type='STRING'/>
<field name='NoUndocEntries' number='1001' type='NUMINGROUP'/>
<field name='UndocEntry' number='1002' type='STRING'/>
</fields>
</fix>
"#;
fn doc_dictionary() -> Dictionary {
let raw: xml::Dictionary = from_str(DOC_DICT).unwrap();
Dictionary::from_raw_dictionary(raw, false, false).unwrap()
}
#[test]
fn test_doc_propagation() {
let dictionary = doc_dictionary();
let cl_ord_id = dictionary.field_by_id(11).unwrap();
assert_eq!(cl_ord_id.doc(), Some("Unique order id."));
assert_eq!(dictionary.field_by_id(55).unwrap().doc(), None);
let side = dictionary.field_by_id(54).unwrap();
assert_eq!(side.doc(), Some("Side of order."));
assert_eq!(side.variants()[0].doc(), Some("Buy order."));
assert_eq!(side.variants()[1].doc(), None);
let msg = dictionary
.message_by_name(fix_str!("NewOrderSingle"))
.unwrap();
assert_eq!(msg.doc(), Some("Submits a new order."));
let instrument = dictionary.component(fix_str!("Instrument")).unwrap();
assert_eq!(instrument.doc(), Some("Instrument identification."));
let allocs = dictionary.group(fix_str!("Allocs")).unwrap();
assert_eq!(allocs.doc(), Some("Allocations of the order."));
}
#[test]
fn test_group_doc_inherited_from_wrapping_component() {
let dictionary = doc_dictionary();
let parties = dictionary.group(fix_str!("Parties")).unwrap();
assert_eq!(parties.doc(), Some("Parties of the order."));
let undocumented = dictionary.group(fix_str!("Undocumented")).unwrap();
assert_eq!(undocumented.doc(), Some("Group own doc."));
}
#[test]
fn test_doc_survives_flatten() {
let dictionary = doc_dictionary().flatten().unwrap();
assert_eq!(
dictionary.field_by_id(11).unwrap().doc(),
Some("Unique order id.")
);
assert_eq!(
dictionary
.message_by_name(fix_str!("NewOrderSingle"))
.unwrap()
.doc(),
Some("Submits a new order.")
);
assert_eq!(
dictionary.group(fix_str!("Allocs")).unwrap().doc(),
Some("Allocations of the order.")
);
assert_eq!(
dictionary.group(fix_str!("Parties")).unwrap().doc(),
Some("Parties of the order.")
);
}