From bfbf04d9b0e652a2926456521f905aff2e7f399e Mon Sep 17 00:00:00 2001 From: Kai Hudalla Date: Thu, 12 Oct 2023 18:23:25 +0200 Subject: [PATCH] Add initial unit tests Added initial set of unit tests to influx-client. Extended GitHub Actions workflow to run tests. Also fixed doc example. --- .github/workflows/lint_source_code.yaml | 4 +- components/influx-client/src/connection.rs | 62 +++++++++++++++++++++- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/.github/workflows/lint_source_code.yaml b/.github/workflows/lint_source_code.yaml index 3764885..294193d 100644 --- a/.github/workflows/lint_source_code.yaml +++ b/.github/workflows/lint_source_code.yaml @@ -38,4 +38,6 @@ jobs: - uses: dtolnay/rust-toolchain@stable with: components: clippy - - run: cargo clippy --manifest-path components/Cargo.toml --workspace --all-features -- -Dwarnings + - run: |- + cargo clippy --manifest-path components/Cargo.toml --workspace --all-features -- -Dwarnings + cargo test --manifest-path components/Cargo.toml --workspace --all-features diff --git a/components/influx-client/src/connection.rs b/components/influx-client/src/connection.rs index ecc115e..7da163e 100644 --- a/components/influx-client/src/connection.rs +++ b/components/influx-client/src/connection.rs @@ -123,10 +123,10 @@ impl InfluxConnection { /// # Examples /// /// ``` - /// use clap::{ArgMatches, Arg, ArgGroup, Command}; + /// use clap::Command; /// use influx_client::connection::InfluxConnection; /// - /// let mut command = influx_client::add_command_line_args(Command::new("influx_client")); + /// let command = influx_client::connection::add_command_line_args(Command::new("influx_client")); /// let matches = command.get_matches_from(vec![ /// "influx_client", /// "--influxdb-uri", "http://my-influx.io", @@ -163,3 +163,61 @@ impl InfluxConnection { }) } } + +#[cfg(test)] +mod tests { + + #[test] + fn test_command_line_uses_defaults() { + + let command = crate::connection::add_command_line_args(clap::Command::new("influx_client")); + let matches = command.get_matches_from(vec![ + "influx_client", + "--influxdb-uri", "http://influx.io", + "--influxdb-token", "the-token", + ]); + assert_eq!(matches.get_one::(super::PARAM_INFLUXDB_URI).unwrap(), "http://influx.io"); + assert_eq!(matches.get_one::(super::PARAM_INFLUXDB_TOKEN).unwrap(), "the-token"); + assert_eq!(matches.get_one::(super::PARAM_INFLUXDB_ORG).unwrap(), "sdv"); + assert_eq!(matches.get_one::(super::PARAM_INFLUXDB_BUCKET).unwrap(), "demo"); + } + + #[test] + fn test_command_line_requires_uri() { + + let command = crate::connection::add_command_line_args(clap::Command::new("influx_client")); + let matches = command.try_get_matches_from(vec![ + "influx_client", + "--influxdb-token", "the-token", + ]); + assert!(matches.is_err_and(|e| e.kind() == clap::error::ErrorKind::MissingRequiredArgument)); + } + + #[test] + fn test_command_line_requires_token_or_token_file() { + + let command = crate::connection::add_command_line_args(clap::Command::new("influx_client")); + let no_token_matches = command.try_get_matches_from(vec![ + "influx_client", + "--influxdb-uri", "http://influx.io", + ]); + assert!(no_token_matches.is_err_and(|e| e.kind() == clap::error::ErrorKind::MissingRequiredArgument)); + + let command = crate::connection::add_command_line_args(clap::Command::new("influx_client")); + let with_token_matches = command.get_matches_from(vec![ + "influx_client", + "--influxdb-uri", "http://influx.io", + "--influxdb-token", "the-token", + ]); + assert_eq!(with_token_matches.get_one::(super::PARAM_INFLUXDB_TOKEN).unwrap(), "the-token"); + + let command = crate::connection::add_command_line_args(clap::Command::new("influx_client")); + let with_token_file_matches = command.get_matches_from(vec![ + "influx_client", + "--influxdb-uri", "http://influx.io", + "--influxdb-token-file", "/path/to/token-file", + ]); + assert_eq!(with_token_file_matches.get_one::(super::PARAM_INFLUXDB_TOKEN_FILE).unwrap(), "/path/to/token-file"); + } + +}