Skip to content

Commit

Permalink
adds unit tests to query_parser in fms-server
Browse files Browse the repository at this point in the history
  • Loading branch information
eriksven authored and sophokles73 committed Jan 26, 2024
1 parent b227f2f commit caf0dbf
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 10 deletions.
19 changes: 11 additions & 8 deletions components/fms-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use axum::http::StatusCode;
use axum::{routing::get, Json, Router};

use axum::extract::{Query, State};
use clap::{Command, ArgMatches};
use clap::{ArgMatches, Command};
use log::{error, info};

use serde_json::json;
Expand All @@ -37,9 +37,13 @@ mod influx_reader;
mod models;
mod query_parser;

use models::position::{
VehiclePositionResponseObject, VehiclePositionResponseObjectVehiclePositionResponse,
};
use models::status::{
VehicleStatusResponseObject, VehicleStatusResponseObjectVehicleStatusResponse,
};
use query_parser::parse_query_parameters;
use models::position::{VehiclePositionResponseObject, VehiclePositionResponseObjectVehiclePositionResponse};
use models::status::{VehicleStatusResponseObjectVehicleStatusResponse, VehicleStatusResponseObject};

pub fn app(args: ArgMatches) -> Router {
let influx_reader = InfluxReader::new(&args).map_or_else(
Expand Down Expand Up @@ -77,10 +81,9 @@ async fn get_vehicleposition(
.await
.map(|positions| {
let result = json!(VehiclePositionResponseObject {
vehicle_position_response:
VehiclePositionResponseObjectVehiclePositionResponse {
vehicle_positions: Some(positions)
},
vehicle_position_response: VehiclePositionResponseObjectVehiclePositionResponse {
vehicle_positions: Some(positions)
},
more_data_available: false,
more_data_available_link: None,
request_server_date_time: chrono::Utc::now()
Expand Down Expand Up @@ -146,4 +149,4 @@ async fn get_vehiclesstatuses(
error!("error retrieving vehicle statuses: {e}");
StatusCode::INTERNAL_SERVER_ERROR
})
}
}
4 changes: 3 additions & 1 deletion components/fms-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ async fn main() {
let args = parser.get_matches();
let router = fms_server::app(args);
let listener = tokio::net::TcpListener::bind("0.0.0.0:8081").await.unwrap();
axum::serve::serve(listener, router.into_make_service()).await.unwrap();
axum::serve::serve(listener, router.into_make_service())
.await
.unwrap();
}
133 changes: 132 additions & 1 deletion components/fms-server/src/query_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ const LATEST_ONLY_QUERY: &str = "latestOnly";
const START_TIME_QUERY: &str = "starttime";
const STOP_TIME_QUERY: &str = "stoptime";
const VIN_QUERY: &str = "vin";
const TRIGGER_FILTER_QUERY: &str = "triggerFiler";
const TRIGGER_FILTER_QUERY: &str = "triggerFilter";

#[derive(Debug)]
pub struct QueryParameters {
pub start_time: i64,
pub stop_time: i64,
Expand Down Expand Up @@ -98,3 +99,133 @@ fn parse_time(params: &HashMap<String, String>, key: &str) -> Result<Option<i64>
}
Ok(None)
}

#[cfg(test)]
mod tests {
use crate::query_parser::parse_query_parameters;
use axum::http::StatusCode;
use std::collections::HashMap;

#[test]
fn no_parameters_returns_400() {
let parameters: HashMap<String, String> = vec![].into_iter().collect();
let result = parse_query_parameters(&parameters);
assert!(result.is_err(), "Expected Err but got Ok");
assert_eq!(result.unwrap_err(), StatusCode::BAD_REQUEST);
}

#[test]
fn no_latestonly_or_time_returns_400() {
let parameters: HashMap<String, String> = vec![(String::from("vin"), String::from("1234"))]
.into_iter()
.collect();
let result = parse_query_parameters(&parameters);
assert!(result.is_err(), "Expected Err but got Ok");
assert_eq!(result.unwrap_err(), StatusCode::BAD_REQUEST);
}

#[test]
fn latestonly_and_starttime_returns_400() {
let parameters: HashMap<String, String> = vec![
(String::from("latestOnly"), String::from("true")),
(
String::from("starttime"),
String::from("2022-12-15T15:12:00Z"),
),
]
.into_iter()
.collect();
let result = parse_query_parameters(&parameters);
assert!(result.is_err(), "Expected Err but got Ok");
assert_eq!(result.unwrap_err(), StatusCode::BAD_REQUEST);
}

#[test]
fn latestonly_and_stoptime_returns_400() {
let parameters: HashMap<String, String> = vec![
(String::from("latestOnly"), String::from("false")),
(
String::from("stoptime"),
String::from("2022-12-15T15:12:00Z"),
),
]
.into_iter()
.collect();
let result = parse_query_parameters(&parameters);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), StatusCode::BAD_REQUEST);
}

#[test]
fn latestonly_but_not_bool_returns_400() {
let parameters: HashMap<String, String> =
vec![(String::from("latestOnly"), String::from("123"))]
.into_iter()
.collect();
let result = parse_query_parameters(&parameters);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), StatusCode::BAD_REQUEST);
}

#[test]
fn startime_but_not_timestamp_returns_400() {
let parameters: HashMap<String, String> =
vec![(String::from("starttime"), String::from("Random"))]
.into_iter()
.collect();
let result = parse_query_parameters(&parameters);
assert!(result.is_err(), "Expected Err but got Ok");
assert_eq!(result.unwrap_err(), StatusCode::BAD_REQUEST);
}

#[test]
fn with_latestonly_works() {
let parameters: HashMap<String, String> = vec![
(String::from("latestOnly"), String::from("false")),
(String::from("vin"), String::from("WDD169005-1J-236684")),
(String::from("triggerFilter"), String::from("TIMER")),
]
.into_iter()
.collect();
let result = parse_query_parameters(&parameters);
assert!(result.is_ok(), "Expected Ok but got Err");
let query_result = result.unwrap();
assert_eq!(
query_result
.latest_only
.unwrap_or_else(|| panic!("No latestOnly present")),
false
);
assert_eq!(
query_result.vin.unwrap_or_else(|| panic!("No vin present")),
"WDD169005-1J-236684"
);
assert_eq!(
query_result
.trigger_filter
.unwrap_or_else(|| panic!("No trigger filter present")),
"TIMER"
);
}

#[test]
fn with_starttime_works() {
let parameters: HashMap<String, String> = vec![
(
String::from("starttime"),
String::from("2022-12-15T15:12:00Z"),
),
(
String::from("stoptime"),
String::from("2022-12-15T15:13:00Z"),
),
]
.into_iter()
.collect();
let result = parse_query_parameters(&parameters);
assert!(result.is_ok(), "Expected Ok but got Err");
let query_result = result.unwrap();
assert_eq!(query_result.start_time, 1671117120);
assert_eq!(query_result.stop_time, 1671117180);
}
}

0 comments on commit caf0dbf

Please sign in to comment.