Skip to content

Commit

Permalink
feat: add basic mock api for aggregate
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Oct 30, 2022
1 parent 89d7a0c commit fd9b4b8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
12 changes: 12 additions & 0 deletions fkl_cli/src/mock_server/aggregate_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use rocket::{get, State};
use rocket::response::status::NotFound;
use rocket::serde::json::Json;
use crate::mock_server::{ApiError, MockServerConfig};

#[get("/<entry_type>/<id>")]
pub async fn get_aggregate_by_id(
entry_type: &str,
id: usize,
config: &State<MockServerConfig>, ) -> Result<Json<String>, NotFound<Json<ApiError>>> {
return Ok(Json("".to_string()));
}
42 changes: 41 additions & 1 deletion fkl_cli/src/mock_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ use rocket::figment::Figment;
use rocket::figment::providers::Serialized;
use rocket::get;
use rocket::serde::{Deserialize, Serialize};
use fkl_parser::mir::ContextMap;
use rocket::serde::json::Json;

use fkl_parser::mir::ContextMap;

pub mod aggregate_api;

#[get("/")]
pub(crate) async fn index(conf: &State<MockServerConfig>) -> Json<ContextMap> {
Json(conf.context_map.clone())
Expand All @@ -19,6 +22,12 @@ pub struct MockServerConfig {
pub context_map: ContextMap,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ApiError {
pub msg: String,
}


pub fn feakin_rocket(context_map: &ContextMap) -> Rocket<Build> {
let server_config = MockServerConfig {
port: 8080,
Expand All @@ -36,5 +45,36 @@ pub fn feakin_rocket(context_map: &ContextMap) -> Rocket<Build> {
.mount("/", routes![
index
])
.mount("/api", routes![
aggregate_api::get_aggregate_by_id,
])
.attach(AdHoc::config::<MockServerConfig>())
}

#[cfg(test)]
#[allow(unused_imports)]
mod test {
use rocket::http::Status;
use rocket::local::blocking::Client;
use fkl_parser::mir::ContextMap;

use crate::mock_server::feakin_rocket;

#[test]
fn hello_world() {
let context_map = ContextMap::default();
let client = Client::tracked(feakin_rocket(&context_map)).expect("valid rocket instance");
let response = client.get("/").dispatch();

assert_eq!(response.status(), Status::Ok);
}

#[test]
fn movie_api() {
let context_map = ContextMap::default();
let client = Client::tracked(feakin_rocket(&context_map)).expect("valid rocket instance");
let response = client.get("/api/movie/1").dispatch();

assert_eq!(response.status(), Status::Ok);
}
}

0 comments on commit fd9b4b8

Please sign in to comment.