From 336ecada6484569a8e31c63c09d5b1f8eefe0a97 Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Thu, 27 Oct 2022 19:33:05 +0800 Subject: [PATCH] test: correct test against new handler apis --- src/servers/src/http.rs | 14 ++++++ src/servers/src/http/handler.rs | 10 ++-- src/servers/tests/http/http_handler_test.rs | 56 +++++++-------------- 3 files changed, 37 insertions(+), 43 deletions(-) diff --git a/src/servers/src/http.rs b/src/servers/src/http.rs index 6268def631..69a6557889 100644 --- a/src/servers/src/http.rs +++ b/src/servers/src/http.rs @@ -70,6 +70,20 @@ pub struct HttpRecordsOutput { rows: Vec>, } +impl HttpRecordsOutput { + pub fn num_rows(&self) -> usize { + self.rows.len() + } + + pub fn num_cols(&self) -> usize { + if let Some(schema) = &self.schema { + schema.column_schemas.len() + } else { + 0 + } + } +} + impl TryFrom> for HttpRecordsOutput { type Error = String; diff --git a/src/servers/src/http/handler.rs b/src/servers/src/http/handler.rs index 4a4f4612ba..a254f3160e 100644 --- a/src/servers/src/http/handler.rs +++ b/src/servers/src/http/handler.rs @@ -1,8 +1,6 @@ use std::collections::HashMap; -use aide::axum::IntoApiResponse; use axum::extract::{Json, Query, State}; -use axum::response::IntoResponse; use common_telemetry::metric; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; @@ -15,7 +13,7 @@ use crate::query_handler::SqlQueryHandlerRef; pub async fn sql( State(sql_handler): State, Query(params): Query>, -) -> impl IntoApiResponse { +) -> Json { if let Some(sql) = params.get("sql") { Json(JsonResponse::from_output(sql_handler.do_query(sql).await).await) } else { @@ -27,7 +25,7 @@ pub async fn sql( /// Handler to export metrics #[axum_macros::debug_handler] -pub async fn metrics(Query(_params): Query>) -> impl IntoResponse { +pub async fn metrics(Query(_params): Query>) -> String { if let Some(handle) = metric::try_handle() { handle.render() } else { @@ -46,7 +44,7 @@ pub struct ScriptExecution { pub async fn scripts( State(query_handler): State, Json(payload): Json, -) -> impl IntoApiResponse { +) -> Json { if payload.name.is_empty() || payload.script.is_empty() { return Json(JsonResponse::with_error(Some( "Invalid name or script".to_string(), @@ -69,7 +67,7 @@ pub async fn scripts( pub async fn run_script( State(query_handler): State, Query(params): Query>, -) -> impl IntoApiResponse { +) -> Json { let name = params.get("name"); if name.is_none() || name.unwrap().is_empty() { diff --git a/src/servers/tests/http/http_handler_test.rs b/src/servers/tests/http/http_handler_test.rs index c2eb37b29b..551c90e5b0 100644 --- a/src/servers/tests/http/http_handler_test.rs +++ b/src/servers/tests/http/http_handler_test.rs @@ -5,7 +5,7 @@ use common_telemetry::metric; use metrics::counter; use servers::http::handler as http_handler; use servers::http::handler::ScriptExecution; -use servers::http::{HttpResponse, JsonOutput}; +use servers::http::JsonOutput; use table::test_util::MemTable; use crate::create_testing_sql_query_handler; @@ -13,18 +13,13 @@ use crate::create_testing_sql_query_handler; #[tokio::test] async fn test_sql_not_provided() { let query_handler = create_testing_sql_query_handler(MemTable::default_numbers_table()); - let json = http_handler::sql(State(query_handler), Query(HashMap::default())).await; - match json { - HttpResponse::Json(json) => { - assert!(!json.success()); - assert_eq!( - Some(&"sql parameter is required.".to_string()), - json.error() - ); - assert!(json.output().is_none()); - } - _ => unreachable!(), - } + let Json(json) = http_handler::sql(State(query_handler), Query(HashMap::default())).await; + assert!(!json.success()); + assert_eq!( + Some(&"sql parameter is required.".to_string()), + json.error() + ); + assert!(json.output().is_none()); } #[tokio::test] @@ -34,17 +29,12 @@ async fn test_sql_output_rows() { let query = create_query(); let query_handler = create_testing_sql_query_handler(MemTable::default_numbers_table()); - let json = http_handler::sql(State(query_handler), query).await; - match json { - HttpResponse::Json(json) => { - assert!(json.success(), "{:?}", json); - assert!(json.error().is_none()); - match json.output().expect("assertion failed") { - JsonOutput::Rows(rows) => { - assert_eq!(1, rows.len()); - } - _ => unreachable!(), - } + let Json(json) = http_handler::sql(State(query_handler), query).await; + assert!(json.success(), "{:?}", json); + assert!(json.error().is_none()); + match json.output().expect("assertion failed") { + JsonOutput::Rows(records) => { + assert_eq!(1, records.num_rows()); } _ => unreachable!(), } @@ -58,10 +48,7 @@ async fn test_metrics() { let query = create_query(); let text = http_handler::metrics(query).await; - match text { - HttpResponse::Text(s) => assert!(s.contains("test_metrics counter")), - _ => unreachable!(), - } + assert!(text.contains("test_metrics counter")); } #[tokio::test] @@ -71,15 +58,10 @@ async fn test_scripts() { let exec = create_script_payload(); let query_handler = create_testing_sql_query_handler(MemTable::default_numbers_table()); - let json = http_handler::scripts(State(query_handler), exec).await; - match json { - HttpResponse::Json(json) => { - assert!(json.success(), "{:?}", json); - assert!(json.error().is_none()); - assert!(json.output().is_none()); - } - _ => unreachable!(), - } + let Json(json) = http_handler::scripts(State(query_handler), exec).await; + assert!(json.success(), "{:?}", json); + assert!(json.error().is_none()); + assert!(json.output().is_none()); } fn create_script_payload() -> Json {