1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-12-23 23:31:23 +00:00

adding more helper functions for tests

This commit is contained in:
Felix Ableitner 2023-11-24 15:48:43 +01:00
parent 9a5a195bfd
commit d224016327
2 changed files with 48 additions and 91 deletions

View file

@ -1,4 +1,6 @@
use fediwiki::api::{EditArticleData, FollowInstance, GetArticleData, ResolveObject};
use fediwiki::api::{
CreateArticleData, EditArticleData, FollowInstance, GetArticleData, ResolveObject,
};
use fediwiki::error::MyResult;
use fediwiki::federation::objects::article::DbArticle;
use fediwiki::federation::objects::instance::DbInstance;
@ -64,6 +66,18 @@ impl TestData {
}
}
pub async fn create_article(hostname: &str, title: String) -> MyResult<DbArticle> {
let create_form = CreateArticleData { title };
post(hostname, "article", &create_form).await
}
pub async fn get_article(hostname: &str, title: &str) -> MyResult<DbArticle> {
let get_article = GetArticleData {
title: title.to_string(),
};
get_query::<DbArticle, _>(hostname, "article", Some(get_article.clone())).await
}
pub async fn edit_article(
hostname: &str,
title: &str,
@ -102,7 +116,7 @@ where
Ok(alpha_instance)
}
pub async fn post<T: Serialize, R>(hostname: &str, endpoint: &str, form: &T) -> MyResult<R>
async fn post<T: Serialize, R>(hostname: &str, endpoint: &str, form: &T) -> MyResult<R>
where
R: for<'de> Deserialize<'de>,
{

View file

@ -2,9 +2,11 @@ extern crate fediwiki;
mod common;
use crate::common::{edit_article, follow_instance, get_query, post, TestData};
use crate::common::{
create_article, edit_article, follow_instance, get_article, get_query, TestData,
};
use common::get;
use fediwiki::api::{CreateArticleData, EditArticleData, GetArticleData, ResolveObject};
use fediwiki::api::{EditArticleData, ResolveObject};
use fediwiki::error::MyResult;
use fediwiki::federation::objects::article::DbArticle;
use fediwiki::federation::objects::instance::DbInstance;
@ -17,33 +19,18 @@ async fn test_create_read_and_edit_article() -> MyResult<()> {
let data = TestData::start();
// error on nonexistent article
let get_article = GetArticleData {
title: "Manu_Chao".to_string(),
};
let not_found = get_query::<DbArticle, _>(
data.hostname_alpha,
&"article".to_string(),
Some(get_article.clone()),
)
.await;
let title = "Manu_Chao".to_string();
let not_found = get_article(data.hostname_alpha, &title).await;
assert!(not_found.is_err());
// create article
let create_article = CreateArticleData {
title: get_article.title.to_string(),
};
let create_res: DbArticle = post(data.hostname_alpha, "article", &create_article).await?;
assert_eq!(create_article.title, create_res.title);
let create_res = create_article(data.hostname_alpha, title.clone()).await?;
assert_eq!(title, create_res.title);
assert!(create_res.local);
// now article can be read
let get_res: DbArticle = get_query(
data.hostname_alpha,
&"article".to_string(),
Some(get_article.clone()),
)
.await?;
assert_eq!(create_article.title, get_res.title);
let get_res = get_article(data.hostname_alpha, &create_res.title).await?;
assert_eq!(title, get_res.title);
assert!(get_res.text.is_empty());
assert!(get_res.local);
@ -88,24 +75,14 @@ async fn test_synchronize_articles() -> MyResult<()> {
let data = TestData::start();
// create article on alpha
let create_article = CreateArticleData {
title: "Manu_Chao".to_string(),
};
let create_res: DbArticle = post(data.hostname_alpha, "article", &create_article).await?;
assert_eq!(create_article.title, create_res.title);
let title = "Manu_Chao".to_string();
let create_res = create_article(data.hostname_alpha, title.clone()).await?;
assert_eq!(title, create_res.title);
assert_eq!(0, create_res.edits.len());
assert!(create_res.local);
// article is not yet on beta
let get_article = GetArticleData {
title: "Manu_Chao".to_string(),
};
let get_res = get_query::<DbArticle, _>(
data.hostname_beta,
&"article".to_string(),
Some(get_article.clone()),
)
.await;
let get_res = get_article(data.hostname_beta, &create_res.title).await;
assert!(get_res.is_err());
// fetch alpha instance on beta, articles are also fetched automatically
@ -116,14 +93,9 @@ async fn test_synchronize_articles() -> MyResult<()> {
.await?;
// get the article and compare
let get_res: DbArticle = get_query(
data.hostname_beta,
&"article".to_string(),
Some(get_article.clone()),
)
.await?;
let get_res = get_article(data.hostname_beta, &create_res.title).await?;
assert_eq!(create_res.ap_id, get_res.ap_id);
assert_eq!(create_article.title, get_res.title);
assert_eq!(title, get_res.title);
assert_eq!(0, get_res.edits.len());
assert!(get_res.text.is_empty());
assert!(!get_res.local);
@ -139,20 +111,13 @@ async fn test_edit_local_article() -> MyResult<()> {
follow_instance(data.hostname_alpha, data.hostname_beta).await?;
// create new article
let create_form = CreateArticleData {
title: "Manu_Chao".to_string(),
};
let create_res: DbArticle = post(data.hostname_beta, "article", &create_form).await?;
assert_eq!(create_res.title, create_form.title);
let title = "Manu_Chao".to_string();
let create_res = create_article(data.hostname_beta, title.clone()).await?;
assert_eq!(title, create_res.title);
assert!(create_res.local);
// article should be federated to alpha
let get_article = GetArticleData {
title: create_res.title.clone(),
};
let get_res =
get_query::<DbArticle, _>(data.hostname_alpha, "article", Some(get_article.clone()))
.await?;
let get_res = get_article(data.hostname_alpha, &create_res.title).await?;
assert_eq!(create_res.title, get_res.title);
assert_eq!(0, get_res.edits.len());
assert!(!get_res.local);
@ -172,12 +137,7 @@ async fn test_edit_local_article() -> MyResult<()> {
.starts_with(&edit_res.ap_id.to_string()));
// edit should be federated to alpha
let get_article = GetArticleData {
title: edit_res.title.clone(),
};
let get_res =
get_query::<DbArticle, _>(data.hostname_alpha, "article", Some(get_article.clone()))
.await?;
let get_res = get_article(data.hostname_alpha, &edit_res.title).await?;
assert_eq!(edit_res.title, get_res.title);
assert_eq!(edit_res.edits.len(), 1);
assert_eq!(edit_res.text, get_res.text);
@ -194,27 +154,18 @@ async fn test_edit_remote_article() -> MyResult<()> {
follow_instance(data.hostname_gamma, data.hostname_beta).await?;
// create new article
let create_form = CreateArticleData {
title: "Manu_Chao".to_string(),
};
let create_res: DbArticle = post(data.hostname_beta, "article", &create_form).await?;
assert_eq!(create_res.title, create_form.title);
let title = "Manu_Chao".to_string();
let create_res = create_article(data.hostname_beta, title.clone()).await?;
assert_eq!(title, create_res.title);
assert!(create_res.local);
// article should be federated to alpha and gamma
let get_article = GetArticleData {
title: create_res.title.clone(),
};
let get_res =
get_query::<DbArticle, _>(data.hostname_alpha, "article", Some(get_article.clone()))
.await?;
let get_res = get_article(data.hostname_alpha, &title).await?;
assert_eq!(create_res.title, get_res.title);
assert_eq!(0, get_res.edits.len());
assert!(!get_res.local);
let get_res =
get_query::<DbArticle, _>(data.hostname_gamma, "article", Some(get_article.clone()))
.await?;
let get_res = get_article(data.hostname_gamma, &title).await?;
assert_eq!(create_res.title, get_res.title);
assert_eq!(create_res.text, get_res.text);
@ -222,7 +173,7 @@ async fn test_edit_remote_article() -> MyResult<()> {
ap_id: create_res.ap_id,
new_text: "Lorem Ipsum 2".to_string(),
};
let edit_res = edit_article(data.hostname_alpha, &create_res.title, &edit_form).await?;
let edit_res = edit_article(data.hostname_alpha, &title, &edit_form).await?;
assert_eq!(edit_res.text, edit_form.new_text);
assert_eq!(edit_res.edits.len(), 1);
assert!(!edit_res.local);
@ -232,18 +183,12 @@ async fn test_edit_remote_article() -> MyResult<()> {
.starts_with(&edit_res.ap_id.to_string()));
// edit should be federated to beta and gamma
let get_article = GetArticleData {
title: edit_res.title.clone(),
};
let get_res =
get_query::<DbArticle, _>(data.hostname_beta, "article", Some(get_article.clone())).await?;
let get_res = get_article(data.hostname_alpha, &title).await?;
assert_eq!(edit_res.title, get_res.title);
assert_eq!(edit_res.edits.len(), 1);
assert_eq!(edit_res.text, get_res.text);
let get_res =
get_query::<DbArticle, _>(data.hostname_gamma, "article", Some(get_article.clone()))
.await?;
let get_res = get_article(data.hostname_gamma, &title).await?;
assert_eq!(edit_res.title, get_res.title);
assert_eq!(edit_res.edits.len(), 1);
assert_eq!(edit_res.text, get_res.text);
@ -259,11 +204,9 @@ async fn test_edit_conflict() -> MyResult<()> {
follow_instance(data.hostname_alpha, data.hostname_beta).await?;
// create new article
let create_form = CreateArticleData {
title: "Manu_Chao".to_string(),
};
let create_res: DbArticle = post(data.hostname_beta, "article", &create_form).await?;
assert_eq!(create_res.title, create_form.title);
let title = "Manu_Chao".to_string();
let create_res = create_article(data.hostname_beta, title.clone()).await?;
assert_eq!(title, create_res.title);
assert!(create_res.local);
// fetch article to gamma