From 7ef1ec6c591d24dd44fc150bb9e1cecf92b912d1 Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 28 Mar 2022 20:53:04 +0200 Subject: [PATCH] Fix tests with testdata Signed-off-by: trivernis --- tests/client/test_adding_files.rs | 15 ++++++++++----- tests/client/test_adding_tags.rs | 3 ++- tests/client/test_adding_urls.rs | 31 ++++++++---------------------- tests/{common.rs => common/mod.rs} | 12 ++++++++++++ tests/common/test_data.rs | 17 ++++++++++++++++ 5 files changed, 49 insertions(+), 29 deletions(-) rename tests/{common.rs => common/mod.rs} (72%) create mode 100644 tests/common/test_data.rs diff --git a/tests/client/test_adding_files.rs b/tests/client/test_adding_files.rs index 37b1a7e..285c038 100644 --- a/tests/client/test_adding_files.rs +++ b/tests/client/test_adding_files.rs @@ -1,4 +1,6 @@ -use super::super::common; +use crate::common; +use crate::common::create_testdata; +use crate::common::test_data::get_test_hashes; #[tokio::test] async fn it_adds_files() { @@ -20,23 +22,26 @@ async fn it_adds_binary_files() { #[tokio::test] async fn it_deletes_files() { let client = common::get_client(); - client.delete_files(vec![]).await.unwrap(); + client.delete_files(get_test_hashes()).await.unwrap(); } #[tokio::test] async fn it_undeletes_files() { let client = common::get_client(); - client.undelete_files(vec![]).await.unwrap(); + create_testdata(&client).await; + client.undelete_files(get_test_hashes()).await.unwrap(); } #[tokio::test] async fn it_archives_files() { let client = common::get_client(); - client.archive_files(vec![]).await.unwrap(); + create_testdata(&client).await; + client.archive_files(get_test_hashes()).await.unwrap(); } #[tokio::test] async fn it_unarchives_files() { let client = common::get_client(); - client.unarchive_files(vec![]).await.unwrap(); + create_testdata(&client).await; + client.unarchive_files(get_test_hashes()).await.unwrap(); } diff --git a/tests/client/test_adding_tags.rs b/tests/client/test_adding_tags.rs index c2797f8..3b34952 100644 --- a/tests/client/test_adding_tags.rs +++ b/tests/client/test_adding_tags.rs @@ -1,4 +1,5 @@ use super::super::common; +use crate::common::test_data::EMPTY_HASH; use hydrus_api::api_core::adding_tags::{AddTagsRequestBuilder, TagAction}; use hydrus_api::api_core::common::ServiceIdentifier; @@ -22,7 +23,7 @@ async fn it_adds_tags() { #![allow(deprecated)] let client = common::get_client(); let request = AddTagsRequestBuilder::default() - .add_hash("0000000000000000000000000000000000000000000000000000000000000000") // valid hash, I hope no files are affected + .add_hash(EMPTY_HASH) // valid hash, I hope no files are affected .add_tags( ServiceIdentifier::name("my tags"), vec!["beach".into(), "summer".into()], diff --git a/tests/client/test_adding_urls.rs b/tests/client/test_adding_urls.rs index ef0939a..4e7a629 100644 --- a/tests/client/test_adding_urls.rs +++ b/tests/client/test_adding_urls.rs @@ -1,14 +1,12 @@ use super::super::common; +use crate::common::test_data::{get_test_hashes, get_test_urls, TEST_URL_1}; use hydrus_api::api_core::adding_urls::{AddUrlRequestBuilder, URL_TYPE_POST}; use hydrus_api::api_core::common::ServiceIdentifier; #[tokio::test] async fn it_returns_files_for_an_url() { let client = common::get_client(); - let response = client - .get_url_files("https://www.pixiv.net/member_illust.php?illust_id=83406361&mode=medium") - .await - .unwrap(); + let response = client.get_url_files(TEST_URL_1).await.unwrap(); assert!(response.normalised_url.len() > 0); } @@ -16,10 +14,7 @@ async fn it_returns_files_for_an_url() { #[tokio::test] async fn it_returns_url_information() { let client = common::get_client(); - let info = client - .get_url_info("https://www.pixiv.net/member_illust.php?illust_id=83406361&mode=medium") - .await - .unwrap(); + let info = client.get_url_info(TEST_URL_1).await.unwrap(); assert!(info.normalised_url.len() > 0); assert_eq!(info.url_type, URL_TYPE_POST); } @@ -29,7 +24,7 @@ async fn it_adds_urls() { #![allow(deprecated)] let client = common::get_client(); let request = AddUrlRequestBuilder::default() - .url("https://www.pixiv.net/member_illust.php?illust_id=83406361&mode=medium") + .url(TEST_URL_1) .add_tags( ServiceIdentifier::name("my tags"), vec!["ark mage".to_string(), "grinning".to_string()], @@ -44,14 +39,9 @@ async fn it_adds_urls() { #[tokio::test] async fn it_associates_urls() { let client = common::get_client(); + common::create_testdata(&client).await; client - .associate_urls( - vec![ - "https://www.pixiv.net/member_illust.php?illust_id=83406361&mode=medium" - .to_string(), - ], - vec!["0000000000000000000000000000000000000000000000000000000000000000".to_string()], - ) + .associate_urls(get_test_urls(), get_test_hashes()) .await .unwrap(); } @@ -59,14 +49,9 @@ async fn it_associates_urls() { #[tokio::test] async fn it_disassociates_urls() { let client = common::get_client(); + common::create_testdata(&client).await; client - .disassociate_urls( - vec![ - "https://www.pixiv.net/member_illust.php?illust_id=83406361&mode=medium" - .to_string(), - ], - vec!["0000000000000000000000000000000000000000000000000000000000000000".to_string()], - ) + .disassociate_urls(get_test_urls(), get_test_hashes()) .await .unwrap(); } diff --git a/tests/common.rs b/tests/common/mod.rs similarity index 72% rename from tests/common.rs rename to tests/common/mod.rs index 996e6b5..cac4110 100644 --- a/tests/common.rs +++ b/tests/common/mod.rs @@ -1,8 +1,11 @@ +use hydrus_api::api_core::adding_urls::AddUrlRequestBuilder; use hydrus_api::api_core::client::Client; use hydrus_api::Hydrus; use std::env; use std::sync::{Arc, Mutex, MutexGuard}; use std::time::Duration; +use test_data::TEST_URLS; +pub mod test_data; pub fn setup() { lazy_static::lazy_static! { static ref SETUP_DONE: Arc> = Arc::new(Mutex::new(false)); } @@ -30,3 +33,12 @@ pub fn get_hydrus() -> Hydrus { Hydrus::new(client) } + +pub async fn create_testdata(client: &Client) { + for url in TEST_URLS { + client + .add_url(AddUrlRequestBuilder::default().url(url).build()) + .await + .unwrap(); + } +} diff --git a/tests/common/test_data.rs b/tests/common/test_data.rs new file mode 100644 index 0000000..5e9745f --- /dev/null +++ b/tests/common/test_data.rs @@ -0,0 +1,17 @@ +pub const TEST_HASH_1: &str = "277a138cd1ee79fc1fdb2869c321b848d4861e45b82184487139ef66dd40b62d"; +pub const TEST_HASH_2: &str = "9641a590e66d9f2e5137b6bcba07fdf6cec3ffaa54de2565c3afcc2125ad1160"; +pub const EMPTY_HASH: &str = "0000000000000000000000000000000000000000000000000000000000000000"; +pub const TEST_HASHES: &[&str] = &[TEST_HASH_1, TEST_HASH_2]; + +pub const TEST_URL_1: &str = + "https://www.pixiv.net/member_illust.php?illust_id=83406361&mode=medium"; +pub const TEST_URL_2: &str = "https://yande.re/post/show/923576"; +pub const TEST_URLS: &[&str] = &[TEST_URL_1, TEST_URL_2]; + +pub fn get_test_hashes() -> Vec { + TEST_HASHES.iter().map(|h| String::from(*h)).collect() +} + +pub fn get_test_urls() -> Vec { + TEST_URLS.iter().map(|u| String::from(*u)).collect() +}