2 aggregates::structs::PostAggregates,
4 schema::post_aggregates,
5 utils::{get_conn, DbPool},
7 use diesel::{result::Error, ExpressionMethods, QueryDsl};
8 use diesel_async::RunQueryDsl;
11 pub async fn read(pool: &DbPool, post_id: PostId) -> Result<Self, Error> {
12 let conn = &mut get_conn(pool).await?;
13 post_aggregates::table
14 .filter(post_aggregates::post_id.eq(post_id))
23 aggregates::post_aggregates::PostAggregates,
25 comment::{Comment, CommentInsertForm},
26 community::{Community, CommunityInsertForm},
28 person::{Person, PersonInsertForm},
29 post::{Post, PostInsertForm, PostLike, PostLikeForm},
31 traits::{Crud, Likeable},
32 utils::build_db_pool_for_tests,
34 use serial_test::serial;
38 async fn test_crud() {
39 let pool = &build_db_pool_for_tests().await;
41 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
45 let new_person = PersonInsertForm::builder()
46 .name("thommy_community_agg".into())
47 .public_key("pubkey".to_string())
48 .instance_id(inserted_instance.id)
51 let inserted_person = Person::create(pool, &new_person).await.unwrap();
53 let another_person = PersonInsertForm::builder()
54 .name("jerry_community_agg".into())
55 .public_key("pubkey".to_string())
56 .instance_id(inserted_instance.id)
59 let another_inserted_person = Person::create(pool, &another_person).await.unwrap();
61 let new_community = CommunityInsertForm::builder()
62 .name("TIL_community_agg".into())
63 .title("nada".to_owned())
64 .public_key("pubkey".to_string())
65 .instance_id(inserted_instance.id)
68 let inserted_community = Community::create(pool, &new_community).await.unwrap();
70 let new_post = PostInsertForm::builder()
71 .name("A test post".into())
72 .creator_id(inserted_person.id)
73 .community_id(inserted_community.id)
76 let inserted_post = Post::create(pool, &new_post).await.unwrap();
78 let comment_form = CommentInsertForm::builder()
79 .content("A test comment".into())
80 .creator_id(inserted_person.id)
81 .post_id(inserted_post.id)
84 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
86 let child_comment_form = CommentInsertForm::builder()
87 .content("A test comment".into())
88 .creator_id(inserted_person.id)
89 .post_id(inserted_post.id)
92 let inserted_child_comment =
93 Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
97 let post_like = PostLikeForm {
98 post_id: inserted_post.id,
99 person_id: inserted_person.id,
103 PostLike::like(pool, &post_like).await.unwrap();
105 let post_aggs_before_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
107 assert_eq!(2, post_aggs_before_delete.comments);
108 assert_eq!(1, post_aggs_before_delete.score);
109 assert_eq!(1, post_aggs_before_delete.upvotes);
110 assert_eq!(0, post_aggs_before_delete.downvotes);
112 // Add a post dislike from the other person
113 let post_dislike = PostLikeForm {
114 post_id: inserted_post.id,
115 person_id: another_inserted_person.id,
119 PostLike::like(pool, &post_dislike).await.unwrap();
121 let post_aggs_after_dislike = PostAggregates::read(pool, inserted_post.id).await.unwrap();
123 assert_eq!(2, post_aggs_after_dislike.comments);
124 assert_eq!(0, post_aggs_after_dislike.score);
125 assert_eq!(1, post_aggs_after_dislike.upvotes);
126 assert_eq!(1, post_aggs_after_dislike.downvotes);
128 // Remove the comments
129 Comment::delete(pool, inserted_comment.id).await.unwrap();
130 Comment::delete(pool, inserted_child_comment.id)
133 let after_comment_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
134 assert_eq!(0, after_comment_delete.comments);
135 assert_eq!(0, after_comment_delete.score);
136 assert_eq!(1, after_comment_delete.upvotes);
137 assert_eq!(1, after_comment_delete.downvotes);
139 // Remove the first post like
140 PostLike::remove(pool, inserted_person.id, inserted_post.id)
143 let after_like_remove = PostAggregates::read(pool, inserted_post.id).await.unwrap();
144 assert_eq!(0, after_like_remove.comments);
145 assert_eq!(-1, after_like_remove.score);
146 assert_eq!(0, after_like_remove.upvotes);
147 assert_eq!(1, after_like_remove.downvotes);
149 // This should delete all the associated rows, and fire triggers
150 Person::delete(pool, another_inserted_person.id)
153 let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
154 assert_eq!(1, person_num_deleted);
156 // Delete the community
157 let community_num_deleted = Community::delete(pool, inserted_community.id)
160 assert_eq!(1, community_num_deleted);
162 // Should be none found, since the creator was deleted
163 let after_delete = PostAggregates::read(pool, inserted_post.id).await;
164 assert!(after_delete.is_err());
166 Instance::delete(pool, inserted_instance.id).await.unwrap();