From d585ba3f921e6cf9512d0d32e2e0ab70d766c1da Mon Sep 17 00:00:00 2001 From: Adrian Hedqvist Date: Sun, 26 Mar 2023 13:05:39 +0200 Subject: [PATCH] add tests for rendering the index pages --- src/handlers/mod.rs | 10 ++++++++++ src/handlers/posts.rs | 36 ++++++++++++++++++++++++++++++++++++ src/main.rs | 2 +- src/post.rs | 2 +- 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 77e0610..4123959 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -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(); + } +} diff --git a/src/handlers/posts.rs b/src/handlers/posts.rs index 0938fa6..c3b7de8 100644 --- a/src/handlers/posts.rs +++ b/src/handlers/posts.rs @@ -74,3 +74,39 @@ pub async fn view( .inc(); 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(); + } +} diff --git a/src/main.rs b/src/main.rs index 8561edb..eb22655 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use std::{collections::HashMap, sync::Arc}; 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 post::Post; use prometheus::{Encoder, TextEncoder}; diff --git a/src/post.rs b/src/post.rs index 6a5aea5..9fe9113 100644 --- a/src/post.rs +++ b/src/post.rs @@ -22,7 +22,7 @@ pub struct TomlFrontMatter { pub tags: Option>, } -#[derive(Serialize, Clone, Debug)] +#[derive(Serialize, Clone, Debug, Default)] pub struct Post { pub title: String, pub date: Option>,