Skip to content

Commit f513768

Browse files
test: use rstest for table testing sdk resource (#2407)
Co-authored-by: Cijo Thomas <[email protected]>
1 parent 540cdb3 commit f513768

File tree

2 files changed

+51
-40
lines changed

2 files changed

+51
-40
lines changed

opentelemetry-sdk/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ rustdoc-args = ["--cfg", "docsrs"]
3535

3636
[dev-dependencies]
3737
criterion = { workspace = true, features = ["html_reports"] }
38+
rstest = "0.23.0"
3839
temp-env = { workspace = true }
3940

4041
[target.'cfg(not(target_os = "windows"))'.dev-dependencies]

opentelemetry-sdk/src/resource/mod.rs

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ pub trait ResourceDetector {
263263

264264
#[cfg(test)]
265265
mod tests {
266+
use rstest::rstest;
267+
266268
use super::*;
267269

268270
#[test]
@@ -308,47 +310,55 @@ mod tests {
308310
assert_eq!(resource_a.merge(&resource_b), expected_resource);
309311
}
310312

311-
#[test]
312-
fn merge_resource_schema_url() {
313-
// if both resources contains key value pairs
314-
let test_cases = vec![
315-
(Some("http://schema/a"), None, Some("http://schema/a")),
316-
(Some("http://schema/a"), Some("http://schema/b"), None),
317-
(None, Some("http://schema/b"), Some("http://schema/b")),
318-
(
319-
Some("http://schema/a"),
320-
Some("http://schema/a"),
321-
Some("http://schema/a"),
322-
),
323-
(None, None, None),
324-
];
325-
326-
for (schema_url_a, schema_url_b, expected_schema_url) in test_cases.into_iter() {
327-
let resource_a = Resource::from_schema_url(
328-
vec![KeyValue::new("key", "")],
329-
schema_url_a.unwrap_or(""),
330-
);
331-
let resource_b = Resource::from_schema_url(
332-
vec![KeyValue::new("key", "")],
333-
schema_url_b.unwrap_or(""),
334-
);
335-
336-
let merged_resource = resource_a.merge(&resource_b);
337-
let result_schema_url = merged_resource.schema_url();
338-
339-
assert_eq!(
340-
result_schema_url.map(|s| s as &str),
341-
expected_schema_url,
342-
"Merging schema_url_a {:?} with schema_url_b {:?} did not yield expected result {:?}",
343-
schema_url_a, schema_url_b, expected_schema_url
344-
);
345-
}
346-
347-
// if only one resource contains key value pairs
348-
let resource = Resource::from_schema_url(vec![], "http://schema/a");
349-
let other_resource = Resource::new(vec![KeyValue::new("key", "")]);
313+
#[rstest]
314+
#[case(Some("http://schema/a"), None, Some("http://schema/a"))]
315+
#[case(Some("http://schema/a"), Some("http://schema/b"), None)]
316+
#[case(None, Some("http://schema/b"), Some("http://schema/b"))]
317+
#[case(
318+
Some("http://schema/a"),
319+
Some("http://schema/a"),
320+
Some("http://schema/a")
321+
)]
322+
#[case(None, None, None)]
323+
fn merge_resource_schema_url(
324+
#[case] schema_url_a: Option<&'static str>,
325+
#[case] schema_url_b: Option<&'static str>,
326+
#[case] expected_schema_url: Option<&'static str>,
327+
) {
328+
let resource_a =
329+
Resource::from_schema_url(vec![KeyValue::new("key", "")], schema_url_a.unwrap_or(""));
330+
let resource_b =
331+
Resource::from_schema_url(vec![KeyValue::new("key", "")], schema_url_b.unwrap_or(""));
332+
333+
let merged_resource = resource_a.merge(&resource_b);
334+
let result_schema_url = merged_resource.schema_url();
335+
336+
assert_eq!(
337+
result_schema_url.map(|s| s as &str),
338+
expected_schema_url,
339+
"Merging schema_url_a {:?} with schema_url_b {:?} did not yield expected result {:?}",
340+
schema_url_a,
341+
schema_url_b,
342+
expected_schema_url
343+
);
344+
}
350345

351-
assert_eq!(resource.merge(&other_resource).schema_url(), None);
346+
#[rstest]
347+
#[case(vec![], vec![KeyValue::new("key", "b")], "http://schema/a", None)]
348+
#[case(vec![KeyValue::new("key", "a")], vec![KeyValue::new("key", "b")], "http://schema/a", Some("http://schema/a"))]
349+
fn merge_resource_with_missing_attribtes(
350+
#[case] key_values_a: Vec<KeyValue>,
351+
#[case] key_values_b: Vec<KeyValue>,
352+
#[case] schema_url: &'static str,
353+
#[case] expected_schema_url: Option<&'static str>,
354+
) {
355+
let resource = Resource::from_schema_url(key_values_a, schema_url);
356+
let other_resource = Resource::new(key_values_b);
357+
358+
assert_eq!(
359+
resource.merge(&other_resource).schema_url(),
360+
expected_schema_url
361+
);
352362
}
353363

354364
#[test]

0 commit comments

Comments
 (0)