Skip to content

Commit

Permalink
test: snapshots tests for KafkaRecord::parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Mcdostone committed Jan 8, 2025
1 parent e7d43e2 commit 62d6f9a
Show file tree
Hide file tree
Showing 15 changed files with 191 additions and 18 deletions.
31 changes: 16 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions crates/lib/tests/inputs/raw-records/record-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"key": [65],
"value": [65]
}
4 changes: 4 additions & 0 deletions crates/lib/tests/inputs/raw-records/record-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"key": [0, 0, 0, 0, 1],
"value": [0, 0, 0, 0, 2]
}
4 changes: 4 additions & 0 deletions crates/lib/tests/inputs/raw-records/record-3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"key": [0, 0, 0, 0, 1, 123, 125],
"value": [0, 0, 0, 0, 2, 123, 125]
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
35 changes: 33 additions & 2 deletions crates/lib/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Vec<u8>>,
pub value: Option<Vec<u8>>,
}
25 changes: 25 additions & 0 deletions crates/lib/tests/snapshots/r#mod__parse_records.snap
Original file line number Diff line number Diff line change
@@ -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,
}
25 changes: 25 additions & 0 deletions crates/lib/tests/snapshots/r#[email protected]
Original file line number Diff line number Diff line change
@@ -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,
}
39 changes: 39 additions & 0 deletions crates/lib/tests/snapshots/r#[email protected]
Original file line number Diff line number Diff line change
@@ -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,
}
39 changes: 39 additions & 0 deletions crates/lib/tests/snapshots/r#[email protected]
Original file line number Diff line number Diff line change
@@ -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,
}
2 changes: 1 addition & 1 deletion crates/tui/src/component/schemas_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
]));
}
Expand Down

0 comments on commit 62d6f9a

Please sign in to comment.