Added a unit test in albums.rs
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
7f8312d0c2
commit
a790e2534d
@ -2,19 +2,12 @@ use hyper::{Body, Method, Request, header::{AUTHORIZATION, COOKIE}};
|
||||
use serde_json::{Map, Value, from_str};
|
||||
use serde::Deserialize;
|
||||
use tabled::Tabled;
|
||||
use crate::{LycheeClient, body_to_str};
|
||||
use crate::{LycheeClient, body_to_str, utils::display_option_string};
|
||||
|
||||
|
||||
fn display_option_string(o: &Option<String>) -> String {
|
||||
match o {
|
||||
Some(s) => format!("{}", s),
|
||||
None => format!("N/A")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct AllAlbums {
|
||||
pub smartalbums: Map<String, Value>,
|
||||
pub smartalbums: Option<Map<String, Value>>,
|
||||
pub albums: Vec<Album>,
|
||||
pub shared_albums: Vec<Album>
|
||||
}
|
||||
@ -60,3 +53,38 @@ pub async fn get_albums(c: &LycheeClient, lychee_session: &str) -> AllAlbums {
|
||||
let v: AllAlbums = from_str(body_to_str(res.into_body()).await.as_str()).unwrap();
|
||||
return v;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod albums_tests {
|
||||
use crate::LycheeClient;
|
||||
use hyper_tls::HttpsConnector;
|
||||
use hyper::client::Client;
|
||||
use mockito::mock;
|
||||
|
||||
use super::get_albums;
|
||||
|
||||
fn setup() -> LycheeClient {
|
||||
let https = HttpsConnector::new();
|
||||
return LycheeClient {
|
||||
client: Client::builder().build::<_, hyper::Body>(https),
|
||||
endpoint: mockito::server_url(),
|
||||
api_key: "value".to_string()
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_albums_empty_everything() {
|
||||
let client = setup();
|
||||
let m = mock("POST", "/api/Albums::get")
|
||||
.with_body("{\"smartalbums\": null,\"albums\": [],\"shared_albums\": []}")
|
||||
.with_header("content-type", "application/json")
|
||||
.match_header("cookie", "cookie value")
|
||||
.create();
|
||||
|
||||
let a = tokio_test::block_on(get_albums(&client, "cookie value"));
|
||||
assert!(a.smartalbums.is_none());
|
||||
assert!(a.albums.is_empty());
|
||||
assert!(a.shared_albums.is_empty());
|
||||
m.assert();
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,13 @@ pub fn read_from_file(p: &str) -> String {
|
||||
return d;
|
||||
}
|
||||
|
||||
pub fn display_option_string(o: &Option<String>) -> String {
|
||||
match o {
|
||||
Some(s) => format!("{}", s),
|
||||
None => format!("N/A")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod utils_tests {
|
||||
use hyper::Body;
|
||||
|
Loading…
Reference in New Issue
Block a user