Files
tavernworks/tavern/tests/database.rs

140 lines
4.0 KiB
Rust
Raw Normal View History

#![cfg(feature = "tester")]
use chrono::NaiveDate;
use tavern::{Adventurer, Database, Legend, Lore, Story, Tale, Tavern};
fn generate_tavern() -> Tavern
{
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 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::Html("#Test Post\nThis is a test post.\n\n##Subsection\nMini post.\n* Test 1\n* Test 2\n* Test 3".to_string())
};
let lore2: Lore = Lore
{
title: String::from("Test Tale"),
slug: String::from("test_tale"),
author: author.handle.clone(),
summary: String::from("The Moon is made of rocks!"),
tags: vec![String::from("Space"), String::from("Rocks")],
publish_date:
NaiveDate::from_ymd_opt(2025, 12, 25).unwrap()
.and_hms_opt(13, 10, 41)
.unwrap()
};
let tale2: Tale = Tale
{
lore: lore2,
story: Story::Html("#Test Tale\nThis is a test tale.\n\n##Subsection\nMini tale.\n* Test 1\n* Test 2\n* Test 3".to_string())
};
Tavern { title: String::from("Runes & Ramblings"),
description: String::from("Join software engineer Jason Smith \
on his Rust programming journey. \
Explore program design, tech \
stacks, and more on this blog from \
CybeMages, LLC."),
tales: vec![tale, tale2],
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)
{
if path.exists()
{
let _ = std::fs::remove_file(path);
}
}
#[tokio::test]
async fn tavern_tale() -> Result<(), Box<dyn std::error::Error>>
{
let tavern = generate_tavern();
// Save the TOML to a temporary file.
let path = create_temp_file("tavern_test_out.db");
cleanup_temp_file(&path);
let db = Database::open(&path).await?;
db.insert_tavern(&tavern).await?;
// Write a Tale to the Tavern.
let lore: Lore = Lore
{
title: String::from("About Tavern"),
slug: String::from("about_tavern"),
author: String::from("myrddin"),
summary: String::from("Myrddin is awesome and wrote a blog system!"),
tags: vec![String::from("Blog"), String::from("Silly")],
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::Html("#Tavern Blog System\nThis is a great blogging system.\n\n##Subsection\nWritten by Myrddin himself!.\n* Test 1\n* Test 2\n* Test 3".to_string())
};
db.insert_tale(&tale).await?;
// Read the Tale from the Tavern.
let tale = db.get_tale_by_slug(&tale.lore.slug).await?;
match tale
{
None => { panic!("Do something to say this test failed.") }
Some(tale) =>
{
assert_eq!("Myrddin is awesome and wrote a blog system!", tale.lore.summary);
}
}
cleanup_temp_file(&path);
Ok(())
}