use crate::reddit::Redditor;
use crate::reddit::service::Service;
use hypertyper::prelude::*;
use std::fs;
pub fn do_logging() {
let _ = env_logger::builder().is_test(true).try_init();
}
pub fn load_data(file: &str) -> String {
fs::read_to_string(format!("tests/data/reddit/{file}.json")).expect("could not find test data")
}
pub fn load_output(filename: &str) -> String {
let filename = format!("tests/output/{filename}.txt");
String::from(
fs::read_to_string(&filename)
.expect(&format!("could not load test data from {filename}"))
.trim_end(),
)
}
pub struct TestService<'a> {
suffix: &'a str,
}
impl<'a> TestService<'a> {
pub fn new(suffix: &'a str) -> Self {
Self { suffix }
}
}
impl<'a> HttpGet for TestService<'a> {
async fn get<U>(&self, uri: U) -> HttpResult<String>
where
U: IntoUrl + Send,
{
Ok(fs::read_to_string(uri.as_str()).expect("could not find test data"))
}
}
impl<'a> Service for TestService<'a> {
async fn get_resource(&self, _username: &str, resource: &str) -> HttpResult<String> {
let filename = format!("tests/data/reddit/{resource}_{}.json", self.suffix);
self.get(&filename).await
}
}
impl Redditor {
pub async fn test() -> Redditor {
Redditor::with_service(String::from("mipadi"), TestService::new("mipadi"))
.await
.unwrap()
}
pub async fn test_empty() -> Redditor {
Redditor::with_service(
String::from("testuserpleaseignore"),
TestService::new("empty"),
)
.await
.unwrap()
}
pub async fn test_none() -> Option<Redditor> {
Redditor::with_service(String::from("doesnotexist"), TestService::new("404"))
.await
.ok()
}
}