diff --git a/src/message.rs b/src/message.rs index 496c35b..77d9d70 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1604,6 +1604,7 @@ mod operands { #[derive(Clone, Debug)] enum CalcValue { + Calculate(Calculation), FType(FieldType), Value(Field), } @@ -1613,10 +1614,17 @@ impl CalcValue { match self { Self::FType(ftype) => ftype.get_default(), Self::Value(field) => field.clone(), + Self::Calculate(calc) => calc.calculate(), } } } +impl From for CalcValue { + fn from(value: Calculation) -> Self { + Self::Calculate(value) + } +} + impl From for CalcValue { fn from(value: Field) -> Self { Self::Value(value) @@ -1687,10 +1695,11 @@ mod calcvalues { let value = Uuid::new_v4(); let expected: Field = value.into(); let result: CalcValue = value.into(); - match result { - CalcValue::FType(_) => unreachable!("got {:?}, should have gotten a field", result), + match result.clone() { CalcValue::Value(data) => assert_eq!(data, expected), + _ => unreachable!("got {:?}, should have gotten a field", result), } + assert_eq!(result.get(), expected); } #[test] @@ -1698,10 +1707,11 @@ mod calcvalues { let value = "something"; let expected: Field = value.into(); let result: CalcValue = value.into(); - match result { - CalcValue::FType(_) => unreachable!("got {:?}, should have gotten a field", result), + match result.clone() { CalcValue::Value(data) => assert_eq!(data, expected), + _ => unreachable!("got {:?}, should have gotten a field", result), } + assert_eq!(result.get(), expected); } #[test] @@ -1709,10 +1719,11 @@ mod calcvalues { let value = "data".to_string(); let expected: Field = value.clone().into(); let result: CalcValue = value.into(); - match result { - CalcValue::FType(_) => unreachable!("got {:?}, should have gotten a field", result), + match result.clone() { CalcValue::Value(data) => assert_eq!(data, expected), + _ => unreachable!("got {:?}, should have gotten a field", result), } + assert_eq!(result.get(), expected); } #[test] @@ -1720,10 +1731,11 @@ mod calcvalues { let value = true; let expected: Field = value.clone().into(); let result: CalcValue = value.into(); - match result { - CalcValue::FType(_) => unreachable!("got {:?}, should have gotten a field", result), + match result.clone() { CalcValue::Value(data) => assert_eq!(data, expected), + _ => unreachable!("got {:?}, should have gotten a field", result), } + assert_eq!(result.get(), expected); } #[test] @@ -1731,10 +1743,11 @@ mod calcvalues { let value = Utc::now(); let expected: Field = value.clone().into(); let result: CalcValue = value.into(); - match result { - CalcValue::FType(_) => unreachable!("got {:?}, should have gotten a field", result), + match result.clone() { CalcValue::Value(data) => assert_eq!(data, expected), + _ => unreachable!("got {:?}, should have gotten a field", result), } + assert_eq!(result.get(), expected); } #[test] @@ -1742,10 +1755,11 @@ mod calcvalues { let value = Duration::from_secs(5); let expected: Field = value.clone().into(); let result: CalcValue = value.into(); - match result { - CalcValue::FType(_) => unreachable!("got {:?}, should have gotten a field", result), + match result.clone() { CalcValue::Value(data) => assert_eq!(data, expected), + _ => unreachable!("got {:?}, should have gotten a field", result), } + assert_eq!(result.get(), expected); } #[test] @@ -1753,10 +1767,30 @@ mod calcvalues { let value: i128 = 5; let expected: Field = value.clone().into(); let result: CalcValue = value.into(); - match result { - CalcValue::FType(_) => unreachable!("got {:?}, should have gotten a field", result), + match result.clone() { CalcValue::Value(data) => assert_eq!(data, expected), + _ => unreachable!("got {:?}, should have gotten a field", result), } + assert_eq!(result.get(), expected); + } + + #[test] + fn from_calculation() { + let duration = Duration::from_secs(300); + let start = Utc::now() + duration; + let mut calc = Calculation::new(Operand::Add); + calc.add_value(FieldType::DateTime); + calc.add_value(duration.clone()); + let result: CalcValue = calc.into(); + let data = match result.get() { + Field::DateTime(data) => data, + _ => unreachable!(), + }; + let stop = Utc::now() + duration; + assert!( + data > start && data < stop, + "should be about 5 minutes ahead" + ); } } @@ -3813,6 +3847,35 @@ mod document_files { _ => unreachable!("got {:?}: should have gotten reply", action), } } + + #[test] + fn can_calculate_field_values() { + let mut doc = TestDocument::new([FieldType::DateTime].to_vec()); + doc.start(); + let duration = Duration::from_secs(300); + let mut calc = Calculation::new(Operand::Add); + calc.add_value(FieldType::DateTime).unwrap(); + calc.add_value(duration.clone()).unwrap(); + let mut addition = Addition::new(); + addition.add_field("field0".to_string(), calc); + let start = Utc::now() + duration; + doc.send(addition).unwrap(); + let result = doc.get_receiver().recv_timeout(TIMEOUT).unwrap(); + let stop = Utc::now() + duration; + let action = result.get_action(); + match action { + MsgAction::Reply(data) => { + assert_eq!(data.len(), 1); + for doc in data.iter() { + match doc.get_field("field0").unwrap() { + Field::DateTime(datetime) => assert!(datetime > start && datetime < stop), + _ => unreachable!("did not get uuid"), + } + } + } + _ => unreachable!("got {:?}: should have gotten reply", action), + } + } } #[cfg(test)]