2 aggregates::structs::CommentAggregates,
4 schema::comment_aggregates,
5 utils::{functions::hot_rank, get_conn, DbPool},
7 use diesel::{result::Error, ExpressionMethods, QueryDsl};
8 use diesel_async::RunQueryDsl;
10 impl CommentAggregates {
11 pub async fn read(pool: &mut DbPool<'_>, comment_id: CommentId) -> Result<Self, Error> {
12 let conn = &mut get_conn(pool).await?;
13 comment_aggregates::table
14 .filter(comment_aggregates::comment_id.eq(comment_id))
19 pub async fn update_hot_rank(
20 pool: &mut DbPool<'_>,
21 comment_id: CommentId,
22 ) -> Result<Self, Error> {
23 let conn = &mut get_conn(pool).await?;
25 diesel::update(comment_aggregates::table)
26 .filter(comment_aggregates::comment_id.eq(comment_id))
27 .set(comment_aggregates::hot_rank.eq(hot_rank(
28 comment_aggregates::score,
29 comment_aggregates::published,
31 .get_result::<Self>(conn)
38 #![allow(clippy::unwrap_used)]
39 #![allow(clippy::indexing_slicing)]
42 aggregates::comment_aggregates::CommentAggregates,
44 comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm},
45 community::{Community, CommunityInsertForm},
47 person::{Person, PersonInsertForm},
48 post::{Post, PostInsertForm},
50 traits::{Crud, Likeable},
51 utils::build_db_pool_for_tests,
53 use serial_test::serial;
57 async fn test_crud() {
58 let pool = &build_db_pool_for_tests().await;
59 let pool = &mut pool.into();
61 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
65 let new_person = PersonInsertForm::builder()
66 .name("thommy_comment_agg".into())
67 .public_key("pubkey".into())
68 .instance_id(inserted_instance.id)
71 let inserted_person = Person::create(pool, &new_person).await.unwrap();
73 let another_person = PersonInsertForm::builder()
74 .name("jerry_comment_agg".into())
75 .public_key("pubkey".into())
76 .instance_id(inserted_instance.id)
79 let another_inserted_person = Person::create(pool, &another_person).await.unwrap();
81 let new_community = CommunityInsertForm::builder()
82 .name("TIL_comment_agg".into())
83 .title("nada".to_owned())
84 .public_key("pubkey".to_string())
85 .instance_id(inserted_instance.id)
88 let inserted_community = Community::create(pool, &new_community).await.unwrap();
90 let new_post = PostInsertForm::builder()
91 .name("A test post".into())
92 .creator_id(inserted_person.id)
93 .community_id(inserted_community.id)
96 let inserted_post = Post::create(pool, &new_post).await.unwrap();
98 let comment_form = CommentInsertForm::builder()
99 .content("A test comment".into())
100 .creator_id(inserted_person.id)
101 .post_id(inserted_post.id)
104 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
106 let child_comment_form = CommentInsertForm::builder()
107 .content("A test comment".into())
108 .creator_id(inserted_person.id)
109 .post_id(inserted_post.id)
112 let _inserted_child_comment =
113 Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
117 let comment_like = CommentLikeForm {
118 comment_id: inserted_comment.id,
119 post_id: inserted_post.id,
120 person_id: inserted_person.id,
124 CommentLike::like(pool, &comment_like).await.unwrap();
126 let comment_aggs_before_delete = CommentAggregates::read(pool, inserted_comment.id)
130 assert_eq!(1, comment_aggs_before_delete.score);
131 assert_eq!(1, comment_aggs_before_delete.upvotes);
132 assert_eq!(0, comment_aggs_before_delete.downvotes);
134 // Add a post dislike from the other person
135 let comment_dislike = CommentLikeForm {
136 comment_id: inserted_comment.id,
137 post_id: inserted_post.id,
138 person_id: another_inserted_person.id,
142 CommentLike::like(pool, &comment_dislike).await.unwrap();
144 let comment_aggs_after_dislike = CommentAggregates::read(pool, inserted_comment.id)
148 assert_eq!(0, comment_aggs_after_dislike.score);
149 assert_eq!(1, comment_aggs_after_dislike.upvotes);
150 assert_eq!(1, comment_aggs_after_dislike.downvotes);
152 // Remove the first comment like
153 CommentLike::remove(pool, inserted_person.id, inserted_comment.id)
156 let after_like_remove = CommentAggregates::read(pool, inserted_comment.id)
159 assert_eq!(-1, after_like_remove.score);
160 assert_eq!(0, after_like_remove.upvotes);
161 assert_eq!(1, after_like_remove.downvotes);
163 // Remove the parent post
164 Post::delete(pool, inserted_post.id).await.unwrap();
166 // Should be none found, since the post was deleted
167 let after_delete = CommentAggregates::read(pool, inserted_comment.id).await;
168 assert!(after_delete.is_err());
170 // This should delete all the associated rows, and fire triggers
171 Person::delete(pool, another_inserted_person.id)
174 let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
175 assert_eq!(1, person_num_deleted);
177 // Delete the community
178 let community_num_deleted = Community::delete(pool, inserted_community.id)
181 assert_eq!(1, community_num_deleted);
183 Instance::delete(pool, inserted_instance.id).await.unwrap();