Skip to content

Commit

Permalink
feat: setup test for api docs and get_timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
batleforc committed Aug 10, 2024
1 parent 7264869 commit e74b2d9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
18 changes: 18 additions & 0 deletions apps/back/src/api/apidocs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,21 @@ use utoipa::OpenApi;
)
)]
pub struct ApiDocs;

#[cfg(test)]
mod tests {
use utoipa::OpenApi;

use super::*;

#[test]
fn test_api_docs_openapi() {
let openapi = ApiDocs::openapi();
assert_eq!(openapi.info.version, "0.1.0");
assert_eq!(openapi.info.title, "MonoFolio");
assert_eq!(
openapi.info.description,
Some("API documentation for MonoFolio".to_string())
);
}
}
61 changes: 61 additions & 0 deletions apps/back/src/api/blog/get_timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use actix_web::{get, web, HttpResponse, Responder};
use markdown_struct::blog_timeline::BlogTimeline;
use tracing::instrument;

/// Get the blog timeline
///
/// Get blog timeline with minimal description of each article
#[utoipa::path(
tag = "Blog",
operation_id = "get_timeline",
Expand All @@ -16,3 +19,61 @@ use tracing::instrument;
pub async fn get_timeline(blog_timeline: web::Data<BlogTimeline>) -> impl Responder {
HttpResponse::Ok().json(blog_timeline)
}

#[cfg(test)]
mod tests {
use actix_web::{App, Scope};
use utoipa::{openapi::PathItemType, OpenApi};

use super::*;

#[actix_web::test]
async fn test_get_timeline() {
let app = actix_web::test::init_service(
App::new()
.app_data(web::Data::new(BlogTimeline::default()))
.service(Scope::new("/api/blog").service(get_timeline)),
)
.await;

let req = actix_web::test::TestRequest::get()
.uri("/api/blog")
.to_request();
let resp = actix_web::test::call_service(&app, req).await;
assert!(resp.status().is_success());
let resp_body = actix_web::test::read_body(resp).await;
assert_eq!(resp_body, "{\"pages\":{}}");
}

#[test]
fn test_get_timeline_openapi() {
#[derive(utoipa::OpenApi)]
#[openapi(
info(
title = "MonoFolio",
version = "0.1.0",
description = "API documentation for MonoFolio"
),
tags(
(name = "Blog", description = "Blog related endpoints"),
(name = "Doc", description = "Doc related endpoints"),
(name = "Media", description = "Media related endpoints")
),
components(
schemas(
BlogTimeline,
)
),
paths(
get_timeline,
)
)]
struct ApiDocs;

let openapi = ApiDocs::openapi();
let api = openapi.paths.paths.get("/api/blog").unwrap();
let ope = api.operations.first_key_value().unwrap();
assert!(ope.0.eq(&PathItemType::Get));
assert!(ope.1.operation_id.eq(&Some("get_timeline".to_string())));
}
}
4 changes: 0 additions & 4 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,3 @@ services:
- '4317:4317'
environment:
- LOG_LEVEL=debug
pyroscope:
image: 'grafana/pyroscope:latest'
ports:
- 4040:4040

0 comments on commit e74b2d9

Please sign in to comment.