Added a unit test in albums.rs

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-10-31 18:37:34 +01:00
parent 7f8312d0c2
commit a790e2534d
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283
2 changed files with 44 additions and 9 deletions

View File

@ -2,19 +2,12 @@ use hyper::{Body, Method, Request, header::{AUTHORIZATION, COOKIE}};
use serde_json::{Map, Value, from_str}; use serde_json::{Map, Value, from_str};
use serde::Deserialize; use serde::Deserialize;
use tabled::Tabled; 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)] #[derive(Deserialize, Debug)]
pub struct AllAlbums { pub struct AllAlbums {
pub smartalbums: Map<String, Value>, pub smartalbums: Option<Map<String, Value>>,
pub albums: Vec<Album>, pub albums: Vec<Album>,
pub shared_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(); let v: AllAlbums = from_str(body_to_str(res.into_body()).await.as_str()).unwrap();
return v; 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();
}
}

View File

@ -18,6 +18,13 @@ pub fn read_from_file(p: &str) -> String {
return d; return d;
} }
pub fn display_option_string(o: &Option<String>) -> String {
match o {
Some(s) => format!("{}", s),
None => format!("N/A")
}
}
#[cfg(test)] #[cfg(test)]
mod utils_tests { mod utils_tests {
use hyper::Body; use hyper::Body;