test: correct test against new handler apis

This commit is contained in:
Ning Sun
2022-10-27 19:33:05 +08:00
parent 49263bcb2e
commit 336ecada64
3 changed files with 37 additions and 43 deletions

View File

@@ -70,6 +70,20 @@ pub struct HttpRecordsOutput {
rows: Vec<Vec<Value>>,
}
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<Vec<RecordBatch>> for HttpRecordsOutput {
type Error = String;

View File

@@ -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<SqlQueryHandlerRef>,
Query(params): Query<HashMap<String, String>>,
) -> impl IntoApiResponse {
) -> Json<JsonResponse> {
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<HashMap<String, String>>) -> impl IntoResponse {
pub async fn metrics(Query(_params): Query<HashMap<String, String>>) -> 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<SqlQueryHandlerRef>,
Json(payload): Json<ScriptExecution>,
) -> impl IntoApiResponse {
) -> Json<JsonResponse> {
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<SqlQueryHandlerRef>,
Query(params): Query<HashMap<String, String>>,
) -> impl IntoApiResponse {
) -> Json<JsonResponse> {
let name = params.get("name");
if name.is_none() || name.unwrap().is_empty() {

View File

@@ -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<ScriptExecution> {