Skip to content

Commit

Permalink
Add initial unit tests
Browse files Browse the repository at this point in the history
Added initial set of unit tests to influx-client. Extended GitHub
Actions workflow to run tests.

Also fixed doc example.
  • Loading branch information
sophokles73 committed Oct 13, 2023
1 parent 90909b5 commit bfbf04d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/lint_source_code.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
62 changes: 60 additions & 2 deletions components/influx-client/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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::<String>(super::PARAM_INFLUXDB_URI).unwrap(), "http://influx.io");
assert_eq!(matches.get_one::<String>(super::PARAM_INFLUXDB_TOKEN).unwrap(), "the-token");
assert_eq!(matches.get_one::<String>(super::PARAM_INFLUXDB_ORG).unwrap(), "sdv");
assert_eq!(matches.get_one::<String>(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::<String>(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::<String>(super::PARAM_INFLUXDB_TOKEN_FILE).unwrap(), "/path/to/token-file");
}

}

0 comments on commit bfbf04d

Please sign in to comment.