add tests for rendering the index pages
This commit is contained in:
parent
ff8f38c563
commit
d585ba3f92
4 changed files with 48 additions and 2 deletions
|
@ -51,3 +51,13 @@ impl IntoResponse for WebsiteError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn render_index() {
|
||||||
|
let tera = tera::Tera::new("templates/**/*").unwrap();
|
||||||
|
let ctx = tera::Context::new();
|
||||||
|
let _res = tera.render("index.html", &ctx).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -74,3 +74,39 @@ pub async fn view(
|
||||||
.inc();
|
.inc();
|
||||||
Ok(Html(res))
|
Ok(Html(res))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use chrono::DateTime;
|
||||||
|
|
||||||
|
use crate::post::Post;
|
||||||
|
|
||||||
|
use super::IndexContext;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn render_index() {
|
||||||
|
let posts = vec![Post {
|
||||||
|
title: "test".into(),
|
||||||
|
slug: "test".into(),
|
||||||
|
date: Some(DateTime::parse_from_rfc3339("2023-03-26T13:04:01+02:00").unwrap()),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
Post {
|
||||||
|
title: "test2".into(),
|
||||||
|
slug: "test2".into(),
|
||||||
|
date: None,
|
||||||
|
..Default::default()
|
||||||
|
}];
|
||||||
|
let ctx = IndexContext {
|
||||||
|
title: "Posts",
|
||||||
|
posts: posts.iter().collect(),
|
||||||
|
};
|
||||||
|
let tera = tera::Tera::new("templates/**/*").unwrap();
|
||||||
|
let _res = tera
|
||||||
|
.render(
|
||||||
|
"posts_index.html",
|
||||||
|
&tera::Context::from_serialize(ctx).unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::{collections::HashMap, sync::Arc};
|
use std::{collections::HashMap, sync::Arc};
|
||||||
|
|
||||||
use axum::{body, response::Response, routing::get, Extension, Router};
|
use axum::{body, response::Response, routing::get, Extension, Router};
|
||||||
use color_eyre::eyre::{Result, Error};
|
use color_eyre::eyre::{Error, Result};
|
||||||
use hyper::header::CONTENT_TYPE;
|
use hyper::header::CONTENT_TYPE;
|
||||||
use post::Post;
|
use post::Post;
|
||||||
use prometheus::{Encoder, TextEncoder};
|
use prometheus::{Encoder, TextEncoder};
|
||||||
|
|
|
@ -22,7 +22,7 @@ pub struct TomlFrontMatter {
|
||||||
pub tags: Option<Vec<String>>,
|
pub tags: Option<Vec<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Clone, Debug)]
|
#[derive(Serialize, Clone, Debug, Default)]
|
||||||
pub struct Post {
|
pub struct Post {
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub date: Option<DateTime<FixedOffset>>,
|
pub date: Option<DateTime<FixedOffset>>,
|
||||||
|
|
Loading…
Reference in a new issue