From 62d6f9a6f5dd02fedd307562686108f9c9495b68 Mon Sep 17 00:00:00 2001 From: Yann Prono Date: Wed, 8 Jan 2025 22:57:53 +0100 Subject: [PATCH] test: snapshots tests for `KafkaRecord::parse` --- Cargo.lock | 31 ++++++++------- crates/lib/Cargo.toml | 1 + .../inputs/{ => parsed-records}/record-1.json | 0 .../tests/inputs/raw-records/record-1.json | 4 ++ .../tests/inputs/raw-records/record-2.json | 4 ++ .../tests/inputs/raw-records/record-3.json | 4 ++ .../tests/inputs/{ => search-queries}/1.sql | 0 .../tests/inputs/{ => search-queries}/2.sql | 0 .../tests/inputs/{ => search-queries}/3.sql | 0 crates/lib/tests/mod.rs | 35 ++++++++++++++++- .../tests/snapshots/r#mod__parse_records.snap | 25 ++++++++++++ .../r#mod__parse_records@record-1.json.snap | 25 ++++++++++++ .../r#mod__parse_records@record-2.json.snap | 39 +++++++++++++++++++ .../r#mod__parse_records@record-3.json.snap | 39 +++++++++++++++++++ crates/tui/src/component/schemas_component.rs | 2 +- 15 files changed, 191 insertions(+), 18 deletions(-) rename crates/lib/tests/inputs/{ => parsed-records}/record-1.json (100%) create mode 100644 crates/lib/tests/inputs/raw-records/record-1.json create mode 100644 crates/lib/tests/inputs/raw-records/record-2.json create mode 100644 crates/lib/tests/inputs/raw-records/record-3.json rename crates/lib/tests/inputs/{ => search-queries}/1.sql (100%) rename crates/lib/tests/inputs/{ => search-queries}/2.sql (100%) rename crates/lib/tests/inputs/{ => search-queries}/3.sql (100%) create mode 100644 crates/lib/tests/snapshots/r#mod__parse_records.snap create mode 100644 crates/lib/tests/snapshots/r#mod__parse_records@record-1.json.snap create mode 100644 crates/lib/tests/snapshots/r#mod__parse_records@record-2.json.snap create mode 100644 crates/lib/tests/snapshots/r#mod__parse_records@record-3.json.snap diff --git a/Cargo.lock b/Cargo.lock index b8b95c9..5f87be8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3465,9 +3465,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.42.0" +version = "1.43.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551" +checksum = "3d61fa4ffa3de412bfea335c6ecff681de2b609ba3c77ef3e00e521813a9ed9e" dependencies = [ "backtrace", "bytes", @@ -3484,9 +3484,9 @@ dependencies = [ [[package]] name = "tokio-macros" -version = "2.4.0" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8" dependencies = [ "proc-macro2", "quote", @@ -3941,12 +3941,12 @@ dependencies = [ [[package]] name = "wasm-encoder" -version = "0.222.0" +version = "0.223.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3432682105d7e994565ef928ccf5856cf6af4ba3dddebedb737f61caed70f956" +checksum = "7e636076193fa68103e937ac951b5f2f587624097017d764b8984d9c0f149464" dependencies = [ "leb128", - "wasmparser 0.222.0", + "wasmparser 0.223.0", ] [[package]] @@ -3965,9 +3965,9 @@ dependencies = [ [[package]] name = "wasmparser" -version = "0.222.0" +version = "0.223.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4adf50fde1b1a49c1add6a80d47aea500c88db70551805853aa8b88f3ea27ab5" +checksum = "d5a99faceb1a5a84dd6084ec4bfa4b2ab153b5793b43fd8f58b89232634afc35" dependencies = [ "bitflags", "indexmap", @@ -4240,24 +4240,24 @@ dependencies = [ [[package]] name = "wast" -version = "222.0.0" +version = "223.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ce7191f4b7da0dd300cc32476abae6457154e4625d9b1bc26890828a9a26f6e" +checksum = "d59b2ba8a2ff9f06194b7be9524f92e45e70149f4dacc0d0c7ad92b59ac875e4" dependencies = [ "bumpalo", "leb128", "memchr", "unicode-width 0.2.0", - "wasm-encoder 0.222.0", + "wasm-encoder 0.223.0", ] [[package]] name = "wat" -version = "1.222.0" +version = "1.223.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fde61b4b52f9a84ae31b5e8902a2cd3162ea45d8bf564c729c3288fe52f4334" +checksum = "662786915c427e4918ff01eabb3c4756d4d947cd8f635761526b4cc9da2eaaad" dependencies = [ - "wast 222.0.0", + "wast 223.0.0", ] [[package]] @@ -4855,6 +4855,7 @@ dependencies = [ "serde", "serde_json", "strum", + "tokio", "url", ] diff --git a/crates/lib/Cargo.toml b/crates/lib/Cargo.toml index 11610d6..ade3f00 100644 --- a/crates/lib/Cargo.toml +++ b/crates/lib/Cargo.toml @@ -28,6 +28,7 @@ reqwest = { version = "0.12.12", features = ["json"] } [dev-dependencies] insta = { version = "1.42.0", features = ["filters", "glob"] } protobuf = "3.7.1" +tokio = { version = "1.43.0", features = ["rt"] } [features] native = [ diff --git a/crates/lib/tests/inputs/record-1.json b/crates/lib/tests/inputs/parsed-records/record-1.json similarity index 100% rename from crates/lib/tests/inputs/record-1.json rename to crates/lib/tests/inputs/parsed-records/record-1.json diff --git a/crates/lib/tests/inputs/raw-records/record-1.json b/crates/lib/tests/inputs/raw-records/record-1.json new file mode 100644 index 0000000..f3cb578 --- /dev/null +++ b/crates/lib/tests/inputs/raw-records/record-1.json @@ -0,0 +1,4 @@ +{ + "key": [65], + "value": [65] +} \ No newline at end of file diff --git a/crates/lib/tests/inputs/raw-records/record-2.json b/crates/lib/tests/inputs/raw-records/record-2.json new file mode 100644 index 0000000..60e6561 --- /dev/null +++ b/crates/lib/tests/inputs/raw-records/record-2.json @@ -0,0 +1,4 @@ +{ + "key": [0, 0, 0, 0, 1], + "value": [0, 0, 0, 0, 2] +} \ No newline at end of file diff --git a/crates/lib/tests/inputs/raw-records/record-3.json b/crates/lib/tests/inputs/raw-records/record-3.json new file mode 100644 index 0000000..c7a596b --- /dev/null +++ b/crates/lib/tests/inputs/raw-records/record-3.json @@ -0,0 +1,4 @@ +{ + "key": [0, 0, 0, 0, 1, 123, 125], + "value": [0, 0, 0, 0, 2, 123, 125] +} \ No newline at end of file diff --git a/crates/lib/tests/inputs/1.sql b/crates/lib/tests/inputs/search-queries/1.sql similarity index 100% rename from crates/lib/tests/inputs/1.sql rename to crates/lib/tests/inputs/search-queries/1.sql diff --git a/crates/lib/tests/inputs/2.sql b/crates/lib/tests/inputs/search-queries/2.sql similarity index 100% rename from crates/lib/tests/inputs/2.sql rename to crates/lib/tests/inputs/search-queries/2.sql diff --git a/crates/lib/tests/inputs/3.sql b/crates/lib/tests/inputs/search-queries/3.sql similarity index 100% rename from crates/lib/tests/inputs/3.sql rename to crates/lib/tests/inputs/search-queries/3.sql diff --git a/crates/lib/tests/mod.rs b/crates/lib/tests/mod.rs index e5febbc..19ee93f 100644 --- a/crates/lib/tests/mod.rs +++ b/crates/lib/tests/mod.rs @@ -1,10 +1,13 @@ use insta::{assert_debug_snapshot, glob}; +use rdkafka::message::OwnedMessage; +use serde::Deserialize; use std::fs; +use tokio::runtime::Runtime; use yozefu_lib::{parse_search_query, ExportedKafkaRecord, KafkaRecord}; #[test] fn test_inputs() { - glob!("inputs/*.sql", |path| { + glob!("inputs/search-queries/*.sql", |path| { unsafe { use std::env; // Set the timezone to Paris to have a fixed timezone for the tests @@ -25,9 +28,37 @@ fn test_inputs() { #[test] fn test_exported_record() { - glob!("inputs/record*.json", |path| { + glob!("inputs/parsed-records/record*.json", |path| { let input = fs::read_to_string(path).unwrap(); let record: KafkaRecord = serde_json::from_str(&input).unwrap(); assert_debug_snapshot!(ExportedKafkaRecord::from(&record)); }); } + +#[test] +fn test_parse_records() { + let rt = Runtime::new().unwrap(); + glob!("inputs/raw-records/record*.json", |path| { + let input = fs::read_to_string(path).unwrap(); + let key_value: KeyValue = serde_json::from_str(&input).unwrap(); + let owned_message = OwnedMessage::new( + key_value.value, + key_value.key, + "my-topic".to_string(), + rdkafka::Timestamp::CreateTime(0), + 0, + 0, + None, + ); + rt.block_on(async { + assert_debug_snapshot!(KafkaRecord::parse(owned_message, &mut None).await); + }); + }); +} + +#[derive(Clone, Debug, Deserialize, Hash, PartialEq, Eq, Default)] +#[serde(rename_all = "lowercase")] +struct KeyValue { + pub key: Option>, + pub value: Option>, +} diff --git a/crates/lib/tests/snapshots/r#mod__parse_records.snap b/crates/lib/tests/snapshots/r#mod__parse_records.snap new file mode 100644 index 0000000..c4776a8 --- /dev/null +++ b/crates/lib/tests/snapshots/r#mod__parse_records.snap @@ -0,0 +1,25 @@ +--- +source: crates/lib/tests/mod.rs +expression: "KafkaRecord::parse(owned_message, &mut None).await" +input_file: crates/lib/tests/inputs/raw-records/record-1.json +--- +KafkaRecord { + value: String( + "A", + ), + value_as_string: "A", + key: String( + "A", + ), + key_as_string: "A", + topic: "my-topic", + timestamp: Some( + 0, + ), + partition: 0, + offset: 0, + headers: {}, + key_schema: None, + value_schema: None, + size: 2, +} diff --git a/crates/lib/tests/snapshots/r#mod__parse_records@record-1.json.snap b/crates/lib/tests/snapshots/r#mod__parse_records@record-1.json.snap new file mode 100644 index 0000000..c4776a8 --- /dev/null +++ b/crates/lib/tests/snapshots/r#mod__parse_records@record-1.json.snap @@ -0,0 +1,25 @@ +--- +source: crates/lib/tests/mod.rs +expression: "KafkaRecord::parse(owned_message, &mut None).await" +input_file: crates/lib/tests/inputs/raw-records/record-1.json +--- +KafkaRecord { + value: String( + "A", + ), + value_as_string: "A", + key: String( + "A", + ), + key_as_string: "A", + topic: "my-topic", + timestamp: Some( + 0, + ), + partition: 0, + offset: 0, + headers: {}, + key_schema: None, + value_schema: None, + size: 2, +} diff --git a/crates/lib/tests/snapshots/r#mod__parse_records@record-2.json.snap b/crates/lib/tests/snapshots/r#mod__parse_records@record-2.json.snap new file mode 100644 index 0000000..b8b1790 --- /dev/null +++ b/crates/lib/tests/snapshots/r#mod__parse_records@record-2.json.snap @@ -0,0 +1,39 @@ +--- +source: crates/lib/tests/mod.rs +expression: "KafkaRecord::parse(owned_message, &mut None).await" +input_file: crates/lib/tests/inputs/raw-records/record-2.json +--- +KafkaRecord { + value: String( + "Yozefu was not able to retrieve the schema 2 because there is no schema registry configured. Please visit https://github.com/MAIF/yozefu/blob/main/docs/schema-registry/README.md for more details.\nPayload: [0, 0, 0, 0, 2]\n String: \0\0\0\0\u{2}", + ), + value_as_string: "Yozefu was not able to retrieve the schema 2 because there is no schema registry configured. Please visit https://github.com/MAIF/yozefu/blob/main/docs/schema-registry/README.md for more details.\nPayload: [0, 0, 0, 0, 2]\n String: \0\0\0\0\u{2}", + key: String( + "Yozefu was not able to retrieve the schema 1 because there is no schema registry configured. Please visit https://github.com/MAIF/yozefu/blob/main/docs/schema-registry/README.md for more details.\nPayload: [0, 0, 0, 0, 1]\n String: \0\0\0\0\u{1}", + ), + key_as_string: "Yozefu was not able to retrieve the schema 1 because there is no schema registry configured. Please visit https://github.com/MAIF/yozefu/blob/main/docs/schema-registry/README.md for more details.\nPayload: [0, 0, 0, 0, 1]\n String: \0\0\0\0\u{1}", + topic: "my-topic", + timestamp: Some( + 0, + ), + partition: 0, + offset: 0, + headers: {}, + key_schema: Some( + Schema { + id: SchemaId( + 1, + ), + schema_type: None, + }, + ), + value_schema: Some( + Schema { + id: SchemaId( + 2, + ), + schema_type: None, + }, + ), + size: 10, +} diff --git a/crates/lib/tests/snapshots/r#mod__parse_records@record-3.json.snap b/crates/lib/tests/snapshots/r#mod__parse_records@record-3.json.snap new file mode 100644 index 0000000..76035fc --- /dev/null +++ b/crates/lib/tests/snapshots/r#mod__parse_records@record-3.json.snap @@ -0,0 +1,39 @@ +--- +source: crates/lib/tests/mod.rs +expression: "KafkaRecord::parse(owned_message, &mut None).await" +input_file: crates/lib/tests/inputs/raw-records/record-3.json +--- +KafkaRecord { + value: Json( + Object {}, + ), + value_as_string: "{}", + key: Json( + Object {}, + ), + key_as_string: "{}", + topic: "my-topic", + timestamp: Some( + 0, + ), + partition: 0, + offset: 0, + headers: {}, + key_schema: Some( + Schema { + id: SchemaId( + 1, + ), + schema_type: None, + }, + ), + value_schema: Some( + Schema { + id: SchemaId( + 2, + ), + schema_type: None, + }, + ), + size: 14, +} diff --git a/crates/tui/src/component/schemas_component.rs b/crates/tui/src/component/schemas_component.rs index aa8e5e9..1cac9f6 100644 --- a/crates/tui/src/component/schemas_component.rs +++ b/crates/tui/src/component/schemas_component.rs @@ -40,7 +40,7 @@ impl SchemasComponent<'_> { if let Some(s) = &self.key { to_render.push(Line::from(vec![ - Span::styled("Key schema URL: ", Style::default().bold()), + Span::styled("Key schema URL : ", Style::default().bold()), Span::styled(s.url.to_string(), Style::default()), ])); }