Adjusting to make the database better tested.

This commit is contained in:
2025-09-07 17:18:50 -04:00
parent 2895897e2d
commit 6bf4aa4bdb
9 changed files with 330 additions and 147 deletions

View File

@ -1,37 +1,44 @@
#![cfg(feature = "publisher")]
use chrono::NaiveDate;
use tavern::{Adventurer, FrontMatter, Tale, Tavern};
use tavern::{Adventurer, Legend, Lore, Story, Tale, Tavern};
fn generate_tavern() -> Tavern
{
let author: Adventurer =
Adventurer { name: String::from("Jason Smith"),
handle: String::from("myrddin"),
profile:
String::from("https://cybermages.tech/about/myrddin"),
image:
String::from("https://cybermages.tech/about/myrddin/pic"),
blurb: String::from("I love code!") };
let fm: FrontMatter = FrontMatter {
title: String::from("Test post"),
slug: String::from("test_post"),
author: author.handle.clone(),
summary: String::from("The Moon is made of cheese!"),
tags: vec![String::from("Space"), String::from("Cheese")],
publish_date:
NaiveDate::from_ymd_opt(2025, 12, 25).unwrap()
.and_hms_opt(13, 10, 41)
.unwrap()
let legend: Legend = Legend
{
profile: String::from("https://cybermages.tech/about/myrddin"),
image: String::from("https://cybermages.tech/about/myrddin/pic"),
blurb: String::from("I love code!")
};
let tale: Tale = Tale {
front_matter: fm,
content: std::path::PathBuf::from("posts/test_post.md") };
let author: Adventurer = Adventurer
{
name: String::from("Jason Smith"),
handle: String::from("myrddin"),
legend: legend
};
let lore: Lore = Lore
{
title: String::from("Test post"),
slug: String::from("test_post"),
author: author.handle.clone(),
summary: String::from("The Moon is made of cheese!"),
tags: vec![String::from("Space"), String::from("Cheese")],
publish_date:
NaiveDate::from_ymd_opt(2025, 12, 25).unwrap()
.and_hms_opt(13, 10, 41)
.unwrap()
};
let tale: Tale = Tale
{
lore: lore,
story: Story::File(std::path::PathBuf::from("posts/test_post.md"))
};
Tavern { title: String::from("Runes & Ramblings"),
description: String::from("Join software engineer Jason Smith \
@ -43,6 +50,14 @@ fn generate_tavern() -> Tavern
authors: vec![author] }
}
fn create_temp_file<S>(filename: S) -> std::path::PathBuf
where S: AsRef<std::path::Path>
{
let mut path = std::env::temp_dir();
path.push(filename);
path
}
fn cleanup_temp_file(path: &std::path::PathBuf)
{
@ -63,8 +78,7 @@ fn to_file()
succeed");
// Save the TOML to a temporary file.
let mut path = std::env::temp_dir();
path.push("tavern_test_out.toml");
let path = create_temp_file("tavern_test_to.toml");
std::fs::write(&path, &toml_string).expect("Failed to write TOML to file");
cleanup_temp_file(&path);
@ -80,8 +94,7 @@ fn from_file()
succeed");
// Save the TOML to a temporary file.
let mut path = std::env::temp_dir();
path.push("tavern_test_in.toml");
let path = create_temp_file("tavern_test_from.toml");
std::fs::write(&path, &toml_string).expect("Failed to write TOML to file");
// Read the previously written TOML file
@ -94,9 +107,9 @@ fn from_file()
// Assert some known values to make this a real test
let tale = &tavern.tales[0];
assert_eq!(tale.front_matter.title, "Test post");
assert_eq!(tale.front_matter.slug, "test_post");
assert_eq!(tale.front_matter.author, "myrddin");
assert_eq!(tale.lore.title, "Test post");
assert_eq!(tale.lore.slug, "test_post");
assert_eq!(tale.lore.author, "myrddin");
cleanup_temp_file(&path);
}