From b5f9fe29004addab2815101987e70f9d0c39c5bf Mon Sep 17 00:00:00 2001 From: mimir-d Date: Sun, 6 Oct 2024 02:01:34 +0100 Subject: [PATCH] remove annoying nulls - now that the tests pass, remove the nulls from the json output - while the spec says this is ok, it just makes the tests unnecessarily verbose Signed-off-by: mimir-d --- src/output/config.rs | 5 - src/output/error.rs | 34 ++----- src/spec.rs | 48 ++++++++++ tests/output/runner.rs | 209 +++++++---------------------------------- 4 files changed, 91 insertions(+), 205 deletions(-) diff --git a/src/output/config.rs b/src/output/config.rs index bbefc12..0c98dfc 100644 --- a/src/output/config.rs +++ b/src/output/config.rs @@ -86,11 +86,6 @@ impl ConfigBuilder { pub trait TimestampProvider { fn now(&self) -> chrono::DateTime; - - fn to_string(&self) -> String { - self.now() - .to_rfc3339_opts(chrono::SecondsFormat::Millis, true) - } } struct ConfiguredTzProvider { diff --git a/src/output/error.rs b/src/output/error.rs index 22b1d17..1fbe845 100644 --- a/src/output/error.rs +++ b/src/output/error.rs @@ -82,8 +82,7 @@ impl ErrorBuilder { #[cfg(test)] mod tests { use anyhow::Result; - - use assert_json_diff::assert_json_include; + use assert_json_diff::assert_json_eq; use super::*; use crate::output as tv; @@ -140,45 +139,30 @@ mod tests { "message": "message", "softwareInfoIds": [ { - "computerSystem": null, "name": "name", - "revision": null, - "softwareInfoId": - "software_id", - "softwareType": null, - "version": null + "softwareInfoId": "software_id", }, { - "computerSystem": null, "name": "name", - "revision": null, - "softwareInfoId": - "software_id", - "softwareType": null, - "version": null + "softwareInfoId": "software_id", } ], - "sourceLocation": {"file": "file.rs", "line": 1}, + "sourceLocation": { + "file": "file.rs", + "line": 1 + }, "symptom": "symptom" }); let expected_step = serde_json::json!({ "message": "message", "softwareInfoIds": [ { - "computerSystem": null, "name": "name", - "revision": null, "softwareInfoId": "software_id", - "softwareType": null, - "version": null }, { - "computerSystem": null, "name": "name", - "revision": null, "softwareInfoId": "software_id", - "softwareType": null, - "version": null } ], "sourceLocation": {"file":"file.rs","line":1}, @@ -195,11 +179,11 @@ mod tests { let spec_error = error.to_artifact(); let actual = serde_json::json!(spec_error); - assert_json_include!(actual: actual, expected: &expected_run); + assert_json_eq!(actual, expected_run); let spec_error = error.to_artifact(); let actual = serde_json::json!(spec_error); - assert_json_include!(actual: actual, expected: &expected_step); + assert_json_eq!(actual, expected_step); Ok(()) } diff --git a/src/spec.rs b/src/spec.rs index 9a33681..b6ce19f 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -273,6 +273,7 @@ pub struct TestRunStart { #[serde(rename = "dutInfo")] pub dut_info: DutInfo, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "metadata")] pub metadata: Option>, } @@ -288,18 +289,23 @@ pub struct DutInfo { #[serde(rename = "dutInfoId")] pub id: String, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "name")] pub name: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "platformInfos")] pub platform_infos: Option>, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "softwareInfos")] pub software_infos: Option>, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "hardwareInfos")] pub hardware_infos: Option>, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "metadata")] pub metadata: Option>, } @@ -330,15 +336,19 @@ pub struct SoftwareInfo { #[serde(rename = "name")] pub name: String, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "version")] pub version: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "revision")] pub revision: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "softwareType")] pub software_type: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "computerSystem")] pub computer_system: Option, } @@ -357,33 +367,43 @@ pub struct HardwareInfo { #[serde(rename = "name")] pub name: String, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "version")] pub version: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "revision")] pub revision: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "location")] pub location: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "serialNumber")] pub serial_no: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "partNumber")] pub part_no: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "manufacturer")] pub manufacturer: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "manufacturerPartNumber")] pub manufacturer_part_no: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "odataId")] pub odata_id: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "computerSystem")] pub computer_system: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "manager")] pub manager: Option, } @@ -415,13 +435,16 @@ pub struct Error { #[serde(rename = "symptom")] pub symptom: String, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "message")] pub message: Option, // TODO: support this field during serialization to print only the id of SoftwareInfo struct + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "softwareInfoIds")] pub software_infos: Option>, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "sourceLocation")] pub source_location: Option, } @@ -440,6 +463,7 @@ pub struct Log { #[serde(rename = "message")] pub message: String, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "sourceLocation")] pub source_location: Option, } @@ -548,18 +572,23 @@ pub struct Measurement { #[serde(rename = "value")] pub value: Value, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "unit")] pub unit: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "validators")] pub validators: Option>, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "hardwareInfoId")] pub hardware_info_id: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "subcomponent")] pub subcomponent: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "metadata")] pub metadata: Option>, } @@ -572,6 +601,7 @@ pub struct Measurement { #[derive(Debug, Serialize, Clone, PartialEq)] #[serde(rename = "validator")] pub struct Validator { + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "name")] pub name: Option, @@ -581,6 +611,7 @@ pub struct Validator { #[serde(rename = "value")] pub value: Value, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "metadata")] pub metadata: Option>, } @@ -593,18 +624,22 @@ pub struct Validator { #[derive(Debug, Serialize, Clone, PartialEq)] #[serde(rename = "subcomponent")] pub struct Subcomponent { + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "type")] pub subcomponent_type: Option, #[serde(rename = "name")] pub name: String, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "location")] pub location: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "version")] pub version: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "revision")] pub revision: Option, } @@ -620,21 +655,26 @@ pub struct MeasurementSeriesStart { #[serde(rename = "name")] pub name: String, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "unit")] pub unit: Option, #[serde(rename = "measurementSeriesId")] pub series_id: String, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "validators")] pub validators: Option>, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "hardwareInfoId")] pub hardware_info: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "subcomponent")] pub subcomponent: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "metadata")] pub metadata: Option>, } @@ -674,6 +714,7 @@ pub struct MeasurementSeriesElement { #[serde(rename = "measurementSeriesId")] pub series_id: String, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "metadata")] pub metadata: Option>, } @@ -692,15 +733,19 @@ pub struct Diagnosis { #[serde(rename = "type")] pub diagnosis_type: DiagnosisType, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "message")] pub message: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "validators")] pub hardware_info: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "subComponent")] pub subcomponent: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "sourceLocation")] pub source_location: Option, } @@ -722,12 +767,15 @@ pub struct File { #[serde(rename = "isSnapshot")] pub is_snapshot: bool, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "description")] pub description: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "contentType")] pub content_type: Option, + #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "metadata")] pub metadata: Option>, } diff --git a/tests/output/runner.rs b/tests/output/runner.rs index 291227c..82c7b85 100644 --- a/tests/output/runner.rs +++ b/tests/output/runner.rs @@ -55,20 +55,12 @@ fn json_run_default_start() -> serde_json::Value { "testRunArtifact": { "testRunStart": { "dutInfo": { - "dutInfoId": "dut_id", - - "name": null, - "metadata": null, - "softwareInfos": null, - "hardwareInfos": null, - "platformInfos": null + "dutInfoId": "dut_id" }, "name": "run_name", "parameters": {}, "version": "1.0", - - "commandLine": "", - "metadata": null, + "commandLine": "" } }, "sequenceNumber": 1, @@ -195,9 +187,7 @@ async fn test_testrun_with_log() -> Result<()> { "testRunArtifact": { "log": { "message": "This is a log message with INFO severity", - "severity": "INFO", - - "sourceLocation": null, + "severity": "INFO" } }, "sequenceNumber": 2, @@ -264,11 +254,7 @@ async fn test_testrun_with_error() -> Result<()> { json!({ "testRunArtifact": { "error": { - "symptom": "symptom", - - "message": null, - "softwareInfoIds": null, - "sourceLocation": null, + "symptom": "symptom" } }, "sequenceNumber": 2, @@ -292,10 +278,7 @@ async fn test_testrun_with_error_with_message() -> Result<()> { "testRunArtifact": { "error": { "message": "Error message", - "symptom": "symptom", - - "sourceLocation": null, - "softwareInfoIds": null, + "symptom": "symptom" } }, "sequenceNumber": 2, @@ -321,12 +304,7 @@ async fn test_testrun_with_error_with_details() -> Result<()> { "message": "Error message", "softwareInfoIds": [{ "name": "name", - "softwareInfoId": "id", - - "softwareType": null, - "version": null, - "computerSystem": null, - "revision": null, + "softwareInfoId": "id" }], "sourceLocation": { "file": "file", @@ -415,9 +393,7 @@ async fn test_testrun_step_log() -> Result<()> { "testStepId": "step_0", "log": { "message": "This is a log message with INFO severity", - "severity": "INFO", - - "sourceLocation": null, + "severity": "INFO" } }, "sequenceNumber": 3, @@ -494,11 +470,7 @@ async fn test_testrun_step_error() -> Result<()> { "testStepArtifact": { "testStepId": "step_0", "error": { - "symptom": "symptom", - - "sourceLocation": null, - "softwareInfoIds": null, - "message": null, + "symptom": "symptom" } }, "sequenceNumber": 3, @@ -530,10 +502,7 @@ async fn test_testrun_step_error_with_message() -> Result<()> { "testStepId": "step_0", "error": { "message": "Error message", - "symptom": "symptom", - - "sourceLocation": null, - "softwareInfoIds": null, + "symptom": "symptom" } }, "sequenceNumber": 3, @@ -567,12 +536,7 @@ async fn test_testrun_step_error_with_details() -> Result<()> { "message": "Error message", "softwareInfoIds": [{ "name": "name", - "softwareInfoId": "id", - - "revision": null, - "computerSystem": null, - "version": null, - "softwareType": null, + "softwareInfoId": "id" }], "sourceLocation": { "file": "file", @@ -655,13 +619,7 @@ async fn test_step_with_measurement() -> Result<()> { "testStepId": "step_0", "measurement": { "name": "name", - "value": 50, - - "metadata": null, - "hardwareInfoId": null, - "subcomponent": null, - "unit": null, - "validators": null, + "value": 50 } }, "sequenceNumber": 3, @@ -698,23 +656,13 @@ async fn test_step_with_measurement_builder() -> Result<()> { }, "name": "name", "subcomponent": { - "name": "name", - - "type": null, - "revision": null, - "version": null, - "location": null, + "name": "name" }, "validators": [{ "type": "EQUAL", - "value": 30, - - "name": null, - "metadata": null, + "value": 30 }], - "value": 50, - - "unit": null, + "value": 50 } }, "sequenceNumber": 3, @@ -752,13 +700,7 @@ async fn test_step_with_measurement_series() -> Result<()> { "testStepId": "step_0", "measurementSeriesStart": { "measurementSeriesId": "series_0", - "name": "name", - - "unit": null, - "hardwareInfoId": null, - "metadata": null, - "subcomponent": null, - "validators": null, + "name": "name" } }, "sequenceNumber": 3, @@ -802,13 +744,7 @@ async fn test_step_with_multiple_measurement_series() -> Result<()> { "testStepId": "step_0", "measurementSeriesStart": { "measurementSeriesId": "series_0", - "name": "name", - - "unit": null, - "subcomponent": null, - "metadata": null, - "validators": null, - "hardwareInfoId": null, + "name": "name" } }, "sequenceNumber": 3, @@ -830,13 +766,7 @@ async fn test_step_with_multiple_measurement_series() -> Result<()> { "testStepId": "step_0", "measurementSeriesStart": { "measurementSeriesId": "series_1", - "name": "name", - - "unit": null, - "subcomponent": null, - "metadata": null, - "validators": null, - "hardwareInfoId": null, + "name": "name" } }, "sequenceNumber": 5, @@ -883,13 +813,7 @@ async fn test_step_with_measurement_series_with_details() -> Result<()> { "testStepId": "step_0", "measurementSeriesStart": { "measurementSeriesId": "series_id", - "name": "name", - - "unit": null, - "hardwareInfoId": null, - "metadata": null, - "subcomponent": null, - "validators": null, + "name": "name" } }, "sequenceNumber": 3, @@ -936,18 +860,7 @@ async fn test_step_with_measurement_series_with_details_and_start_builder() -> R "measurementSeriesStart": { "hardwareInfoId": { "hardwareInfoId": "id", - "name": "name", - - "serialNumber": null, - "revision": null, - "computerSystem": null, - "location": null, - "odataId": null, - "version": null, - "manufacturerPartNumber": null, - "manufacturer": null, - "manager": null, - "partNumber": null, + "name": "name" }, "measurementSeriesId": "series_id", "metadata": { @@ -955,22 +868,12 @@ async fn test_step_with_measurement_series_with_details_and_start_builder() -> R }, "name": "name", "subcomponent": { - "name": "name", - - "type": null, - "version": null, - "location": null, - "revision": null + "name": "name" }, - "validators":[{ + "validators": [{ "type": "EQUAL", - "value": 30, - - "metadata": null, - "name": null, - }], - - "unit": null, + "value": 30 + }] } }, "sequenceNumber": 3, @@ -1024,13 +927,7 @@ async fn test_step_with_measurement_series_element() -> Result<()> { "testStepId": "step_0", "measurementSeriesStart": { "measurementSeriesId": "series_0", - "name": "name", - - "unit": null, - "hardwareInfoId": null, - "metadata": null, - "subcomponent": null, - "validators": null, + "name": "name" } }, "sequenceNumber": 3, @@ -1043,9 +940,7 @@ async fn test_step_with_measurement_series_element() -> Result<()> { "index": 0, "measurementSeriesId": "series_0", "value": 60, - "timestamp": DATETIME_FORMATTED, - - "metadata": null, + "timestamp": DATETIME_FORMATTED } }, "sequenceNumber": 4, @@ -1090,13 +985,7 @@ async fn test_step_with_measurement_series_element_index_no() -> Result<()> { "testStepId": "step_0", "measurementSeriesStart": { "measurementSeriesId": "series_0", - "name": "name", - - "unit": null, - "hardwareInfoId": null, - "metadata": null, - "subcomponent": null, - "validators": null, + "name": "name" } }, "sequenceNumber": 3, @@ -1109,9 +998,7 @@ async fn test_step_with_measurement_series_element_index_no() -> Result<()> { "index": 0, "measurementSeriesId": "series_0", "value": 60, - "timestamp": DATETIME_FORMATTED, - - "metadata": null, + "timestamp": DATETIME_FORMATTED } }, "sequenceNumber": 4, @@ -1124,9 +1011,7 @@ async fn test_step_with_measurement_series_element_index_no() -> Result<()> { "index": 1, "measurementSeriesId": "series_0", "value": 70, - "timestamp": DATETIME_FORMATTED, - - "metadata": null, + "timestamp": DATETIME_FORMATTED } }, "sequenceNumber": 5, @@ -1139,9 +1024,7 @@ async fn test_step_with_measurement_series_element_index_no() -> Result<()> { "index": 2, "measurementSeriesId": "series_0", "value": 80, - "timestamp": DATETIME_FORMATTED, - - "metadata": null, + "timestamp": DATETIME_FORMATTED } }, "sequenceNumber": 6, @@ -1189,14 +1072,7 @@ async fn test_step_with_measurement_series_element_with_metadata() -> Result<()> "testStepId": "step_0", "measurementSeriesStart": { "measurementSeriesId": "series_0", - "name": "name", - - - "unit": null, - "hardwareInfoId": null, - "metadata": null, - "subcomponent": null, - "validators": null, + "name": "name" } }, "sequenceNumber": 3, @@ -1259,13 +1135,7 @@ async fn test_step_with_measurement_series_element_with_metadata_index_no() -> R "testStepId": "step_0", "measurementSeriesStart": { "measurementSeriesId": "series_0", - "name": "name", - - "unit": null, - "hardwareInfoId": null, - "metadata": null, - "subcomponent": null, - "validators": null, + "name": "name" } }, "sequenceNumber": 3, @@ -1459,6 +1329,7 @@ async fn test_config_builder_with_file() -> Result<()> { .config( Config::builder() .timezone(chrono_tz::Europe::Rome) + .with_timestamp_provider(Box::new(FixedTsProvider {})) .with_file_output(output_file.path()) .await? .build(), @@ -1510,13 +1381,7 @@ async fn test_testrun_metadata() -> Result<()> { "testRunArtifact": { "testRunStart": { "dutInfo": { - "dutInfoId": "dut_id", - - "name": null, - "metadata": null, - "softwareInfos": null, - "hardwareInfos": null, - "platformInfos": null + "dutInfoId": "dut_id" }, "metadata": {"key": "value"}, "name": "run_name", @@ -1554,13 +1419,7 @@ async fn test_testrun_builder() -> Result<()> { "testRunStart": { "commandLine": "cmd_line", "dutInfo": { - "dutInfoId": "dut_id", - - "name": null, - "metadata": null, - "softwareInfos": null, - "hardwareInfos": null, - "platformInfos": null + "dutInfoId": "dut_id" }, "metadata": { "key": "value",