|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Integration tests for the `#[metric_doc]` macro. |
| 19 | +//! |
| 20 | +//! These tests verify: |
| 21 | +//! 1. Cross-file usage: metrics structs defined in one file, exec in another |
| 22 | +//! 2. Multiple metrics groups: one exec referencing multiple metrics structs |
| 23 | +
|
| 24 | +mod test_helpers; |
| 25 | + |
| 26 | +use datafusion_doc::metric_doc_sections::{DocumentedExec, DocumentedMetrics}; |
| 27 | +use test_helpers::separate_exec::UserDefinedExec; |
| 28 | +use test_helpers::separate_metrics::{MetricsGroupA, MetricsGroupB}; |
| 29 | + |
| 30 | +/// Test that metrics structs in a separate file correctly implement DocumentedMetrics |
| 31 | +#[test] |
| 32 | +fn test_cross_file_metrics_have_documented_metrics_trait() { |
| 33 | + // MetricsGroupA should implement DocumentedMetrics |
| 34 | + let doc_a = MetricsGroupA::metric_doc(); |
| 35 | + assert_eq!(doc_a.name, "MetricsGroupA"); |
| 36 | + assert!(doc_a.doc.contains("First group of metrics")); |
| 37 | + assert_eq!(doc_a.fields.len(), 2); |
| 38 | + |
| 39 | + // MetricsGroupB should implement DocumentedMetrics |
| 40 | + let doc_b = MetricsGroupB::metric_doc(); |
| 41 | + assert_eq!(doc_b.name, "MetricsGroupB"); |
| 42 | + assert!(doc_b.doc.contains("Second group of metrics")); |
| 43 | + assert_eq!(doc_b.fields.len(), 2); |
| 44 | +} |
| 45 | + |
| 46 | +/// Test that an exec with multiple metrics groups correctly implements DocumentedExec |
| 47 | +#[test] |
| 48 | +fn test_exec_with_multiple_metrics_groups() { |
| 49 | + let exec_doc = UserDefinedExec::exec_doc(); |
| 50 | + |
| 51 | + // Verify exec documentation |
| 52 | + assert_eq!(exec_doc.name, "UserDefinedExec"); |
| 53 | + assert!(exec_doc.doc.contains("user-defined execution plan")); |
| 54 | + |
| 55 | + // Verify that both metrics groups are linked |
| 56 | + assert_eq!( |
| 57 | + exec_doc.metrics.len(), |
| 58 | + 2, |
| 59 | + "Expected 2 metrics groups, got {}", |
| 60 | + exec_doc.metrics.len() |
| 61 | + ); |
| 62 | + |
| 63 | + // Verify the metrics are the correct ones (order should match declaration order) |
| 64 | + let metric_names: Vec<&str> = exec_doc.metrics.iter().map(|m| m.name).collect(); |
| 65 | + assert_eq!( |
| 66 | + metric_names[0], "MetricsGroupA", |
| 67 | + "Expected MetricsGroupA in metrics, got {}", |
| 68 | + metric_names[0] |
| 69 | + ); |
| 70 | + assert_eq!( |
| 71 | + metric_names[1], "MetricsGroupB", |
| 72 | + "Expected MetricsGroupB in metrics, got {}", |
| 73 | + metric_names[1] |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +/// Test that field documentation is correctly extracted from metrics structs |
| 78 | +#[test] |
| 79 | +fn test_metrics_field_documentation() { |
| 80 | + let doc_a = MetricsGroupA::metric_doc(); |
| 81 | + |
| 82 | + // Check that field docs are extracted |
| 83 | + let field_names: Vec<&str> = doc_a.fields.iter().map(|f| f.name).collect(); |
| 84 | + assert_eq!(field_names[0], "phase_a_time"); |
| 85 | + assert_eq!(field_names[1], "phase_a_rows"); |
| 86 | + |
| 87 | + // Check that field descriptions are captured |
| 88 | + let time_field = doc_a.fields.iter().find(|f| f.name == "phase_a_time"); |
| 89 | + assert_eq!( |
| 90 | + time_field.unwrap().doc.trim(), |
| 91 | + "Time spent executing phase A" |
| 92 | + ); |
| 93 | +} |
0 commit comments