Skip to content

Commit

Permalink
Create test for dowmloading baya character
Browse files Browse the repository at this point in the history
  • Loading branch information
Barafu committed Jul 15, 2024
1 parent fcae7c5 commit 6055556
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.vscode
*.log
src/example.rs
testing/*
61 changes: 61 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120"
serde_path_to_error = "0.1.16"
soup = "0.5.1"
test-context = "0.3.0"
49 changes: 48 additions & 1 deletion src/baya_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub fn download_card_from_baya_url(url: &str) -> Result<()> {
let tavern_card = TavernCardV2::from(&baya_character);
let tavern_image =
write_tavern_card(&tavern_card, &card_image).context("Could not write tavern card")?;
write_image_to_file(&tavern_image, &card_name)? ;
write_image_to_file(&tavern_image, &card_name)?;
println!("Done!");
print!("Fap away!");
flush();
Expand Down Expand Up @@ -245,4 +245,51 @@ impl From<&Lorebook> for CharacterBook {
}
}

mod tests {
use super::*;
use std::collections::HashMap;
use test_context::{test_context, TestContext};
use anyhow::Result;

const CACHE_PATH: &str = "testing/test_cache.txt"; // Cache for downloaded pages will be stored here.

#[derive(serde::Serialize, serde::Deserialize)]
struct TestCache {
page_cache: HashMap<String, String>,
}

impl TestContext for TestCache {
fn setup() -> Self {
let file_content =
std::fs::read_to_string(CACHE_PATH).unwrap_or_else(|_| String::new());
serde_json::from_str(&file_content).unwrap_or_else(|_| TestCache {
page_cache: HashMap::new(),
})
}

fn teardown(self) {
let serialized = serde_json::to_string(&self).expect("Failed to serialize cache");
std::fs::write(CACHE_PATH, serialized).expect("Failed to write cache to file");
}
}

fn download_testing_webpage<'a>(url: &str, cache: &'a mut TestCache) -> Result<&'a str> {
if cache.page_cache.contains_key(url) {
return Ok(cache.page_cache.get(url).unwrap());
}
let page_content = tools::download_page(url)?;
cache.page_cache.insert(url.to_string(), page_content);
return Ok(cache.page_cache.get(url).unwrap());
}

#[test_context(TestCache)]
#[test]
fn test_downloading_page(cache: &mut TestCache) -> Result<()> {
const TEST_URL: &str = "https://backyard.ai/hub/character/clmg7rj2e03j0mc0v69b1tai1";
let page = download_testing_webpage(TEST_URL, cache)?;
let baya_char = parse_page(page)?;
let baya_char_name = baya_char.aiDisplayName.unwrap();
assert_eq!(baya_char_name, "Character crafter Puppy");
Ok(())
}
}
23 changes: 5 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
#![allow(dead_code)]

use env_logger::Builder;
use std::{
env,
fs::File,
};
use std::{env, fs::File};

mod tavern_card_v2;
mod baya_download;
mod tavern_card_v2;
mod tools;
//mod example;


fn main() {
// Prepare debug logging.
#[cfg(debug_assertions)]
{
let target = Box::new(File::create("last_run.log").expect("Can't create file"));
let target = Box::new(File::create("testing/last_run.log")
.expect("Can't create file"));

Builder::new()
Builder::new()
.target(env_logger::Target::Pipe(target))
.filter(None, log::LevelFilter::Info)
.init();
Expand Down Expand Up @@ -57,13 +54,3 @@ fn wrong_usage() {
fn print_usage() {
println!("Usage: baya_get <url>");
}










0 comments on commit 6055556

Please sign in to comment.