Skip to content

Commit

Permalink
Switched to tokio::test for unit tests. Closing #3.
Browse files Browse the repository at this point in the history
  • Loading branch information
RRArny committed Nov 24, 2024
1 parent dcbf4f6 commit 5161bf5
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 172 deletions.
31 changes: 31 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ anyhow = "1.0.86"
chrono = "0.4.38"
clap = { version = "4.5.7", features = ["derive"] }
colored = "2"
env_logger = "0.11.5"
log = "0.4.22"
regex = "1.10.5"
reqwest = { version = "0.12.5", features = ["json"] }
serde = "1.0.203"
serde_json = "1.0.117"
strum = { version = "0.26.3", features = ["derive", "strum_macros"] }
strum_macros = "0.26.4"
tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread"] }
tokio = { version = "1.38.0", features = ["macros", "rt", "rt-multi-thread"] }
tokio-macros = "2.3.0"
toml = "0.8.14"
7 changes: 6 additions & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// limitations under the License.
// WxFetch - api.rs

use log::error;
use reqwest::{Client, Error, Response};
use serde_json::Value;

Expand All @@ -19,9 +20,13 @@ use crate::{Config, Secrets};
pub async fn request_wx(config: &Config, secrets: &Secrets) -> Option<Value> {
let position = config.position.get_location_str().await;
let resp = send_api_call(position, secrets).await.ok()?;
let status = resp.status().as_u16();

if resp.status().as_u16() == 200 {
if status == 200 {
resp.json().await.ok()
} else if status == 401 {
error!("Weather request failed. Provide a valid AvWx API key.");
None
} else if let Some(nearest_station_code) = get_nearest_station(config, secrets).await {
send_api_call(nearest_station_code, secrets)
.await
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ fn read_config_file(config_filepath: Option<String>) -> Config {
mod test {
use super::*;

#[test]
fn test_read_config_file() {
#[tokio::test]
async fn test_read_config_file() {
let expected = Config::default();
let actual = read_config_file(Some("./config.toml".to_string()));
assert_eq!(expected, actual);
Expand Down
9 changes: 3 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ struct Secrets {
}

fn get_secrets(param: Option<String>) -> Secrets {
let avwx_api_key = if let Some(key) = param {
key
} else {
std::env::var("AVWX_API_KEY").expect("Could not load secret keys.")
};
let avwx_api_key =
param.unwrap_or(std::env::var("AVWX_API_KEY").expect("Could not load secret key."));
Secrets { avwx_api_key }
}

Expand All @@ -82,7 +79,7 @@ async fn main() {
None => get_weather(&config, &secrets).await,
};
let wx_string = Metar::from_json(&json, &config)
.expect("Invalid weather data received...")
.expect("Invalid weather data received.")
.colorise(&config);

println!("{wx_string}");
Expand Down
Loading

0 comments on commit 5161bf5

Please sign in to comment.