use dialtone_common::rest::ap_objects::ap_object_reference::ApObjectReferenceType; use dialtone_sqlx::db::ap_object::page_by_reference::page_ap_objects_by_reference; use dialtone_sqlx::db::ap_object_reference::insert::insert_ap_object_reference; use dialtone_test_util::create_actor::create_actor_tst_utl; use dialtone_test_util::create_ap_object::create_article_tst_utl; use dialtone_test_util::create_site::create_site_tst_utl; use dialtone_test_util::{test_action, test_pg}; use sqlx::{Pool, Postgres}; #[tokio::test] async fn page_all_ap_objects_by_reference_test() { test_pg::test_pg(move |pool| async move { let host_name = "example.net"; create_site_tst_utl(&pool, host_name).await; let created_actor = create_actor_tst_utl(&pool, "testymctestfase", host_name).await; create_many_articles( &pool, host_name, created_actor.owned_actor.ap.preferred_user_name.as_str(), 20, "test_liked_article", &ApObjectReferenceType::Like, ) .await; let page1_result = page_ap_objects_by_reference( &pool, None, None, 5, created_actor.owned_actor.ap.id.as_str(), &ApObjectReferenceType::Like, ) .await; test_action!(page1_result); let page1_ap_objects = page1_result.unwrap().unwrap(); println!("{:?}", page1_ap_objects); assert_eq!(page1_ap_objects.page_values.len(), 5); let page2_result = page_ap_objects_by_reference( &pool, None, Some(&page1_ap_objects.max_modified_at), 10, created_actor.owned_actor.ap.id.as_str(), &ApObjectReferenceType::Like, ) .await; test_action!(page2_result); let page2_ap_objects = page2_result.unwrap().unwrap(); assert_eq!(page2_ap_objects.page_values.len(), 10); let all_page_result = page_ap_objects_by_reference( &pool, None, None, 20, created_actor.owned_actor.ap.id.as_str(), &ApObjectReferenceType::Like, ) .await; test_action!(all_page_result); let all_page_ap_objects = all_page_result.unwrap().unwrap(); assert_eq!(all_page_ap_objects.page_values.len(), 20); }) .await; } #[tokio::test] async fn page_ap_objects_of_reference_test() { test_pg::test_pg(move |pool| async move { let host_name = "example.net"; create_site_tst_utl(&pool, host_name).await; let actor1 = create_actor_tst_utl(&pool, "testymctestfase", host_name).await; create_many_articles( &pool, host_name, actor1.owned_actor.ap.preferred_user_name.as_str(), 20, "test_liked_article", &ApObjectReferenceType::Like, ) .await; create_many_articles( &pool, host_name, actor1.owned_actor.ap.preferred_user_name.as_str(), 30, "test_flagged_article", &ApObjectReferenceType::Flag, ) .await; let liked_all_result = page_ap_objects_by_reference( &pool, None, None, 20, actor1.owned_actor.ap.id.as_str(), &ApObjectReferenceType::Like, ) .await; test_action!(liked_all_result); let liked_all_ap_objects = liked_all_result.unwrap().unwrap(); assert_eq!(liked_all_ap_objects.page_values.len(), 20); let flagged_all_result = page_ap_objects_by_reference( &pool, None, None, 30, actor1.owned_actor.ap.id.as_str(), &ApObjectReferenceType::Flag, ) .await; test_action!(flagged_all_result); let flagged_all_ap_objects = flagged_all_result.unwrap().unwrap(); assert_eq!(flagged_all_ap_objects.page_values.len(), 30); }) .await; } async fn create_many_articles( pool: &Pool, host_name: &str, pun: &str, num_articles: i32, title_preamble: &str, reference_type: &ApObjectReferenceType, ) { for n in 1..=num_articles { let title = format!("{}_{}", title_preamble, n); let ap_object = create_article_tst_utl(pool, host_name, &title, Some(pun)).await; insert_ap_object_reference( pool, &ap_object.attributed_to.unwrap(), &ap_object.id.unwrap(), reference_type, ) .await .unwrap(); } }