2 aggregates::structs::CommentAggregates,
4 schema::comment_aggregates,
5 utils::{get_conn, DbPool},
7 use diesel::{result::Error, ExpressionMethods, QueryDsl};
8 use diesel_async::RunQueryDsl;
10 impl CommentAggregates {
11 pub async fn read(pool: &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))
23 aggregates::comment_aggregates::CommentAggregates,
25 comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm},
26 community::{Community, CommunityInsertForm},
28 person::{Person, PersonInsertForm},
29 post::{Post, PostInsertForm},
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::create(pool, "my_domain.tld").await.unwrap();
43 let new_person = PersonInsertForm::builder()
44 .name("thommy_comment_agg".into())
45 .public_key("pubkey".into())
46 .instance_id(inserted_instance.id)
49 let inserted_person = Person::create(pool, &new_person).await.unwrap();
51 let another_person = PersonInsertForm::builder()
52 .name("jerry_comment_agg".into())
53 .public_key("pubkey".into())
54 .instance_id(inserted_instance.id)
57 let another_inserted_person = Person::create(pool, &another_person).await.unwrap();
59 let new_community = CommunityInsertForm::builder()
60 .name("TIL_comment_agg".into())
61 .title("nada".to_owned())
62 .public_key("pubkey".to_string())
63 .instance_id(inserted_instance.id)
66 let inserted_community = Community::create(pool, &new_community).await.unwrap();
68 let new_post = PostInsertForm::builder()
69 .name("A test post".into())
70 .creator_id(inserted_person.id)
71 .community_id(inserted_community.id)
74 let inserted_post = Post::create(pool, &new_post).await.unwrap();
76 let comment_form = CommentInsertForm::builder()
77 .content("A test comment".into())
78 .creator_id(inserted_person.id)
79 .post_id(inserted_post.id)
82 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
84 let child_comment_form = CommentInsertForm::builder()
85 .content("A test comment".into())
86 .creator_id(inserted_person.id)
87 .post_id(inserted_post.id)
90 let _inserted_child_comment =
91 Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
95 let comment_like = CommentLikeForm {
96 comment_id: inserted_comment.id,
97 post_id: inserted_post.id,
98 person_id: inserted_person.id,
102 CommentLike::like(pool, &comment_like).await.unwrap();
104 let comment_aggs_before_delete = CommentAggregates::read(pool, inserted_comment.id)
108 assert_eq!(1, comment_aggs_before_delete.score);
109 assert_eq!(1, comment_aggs_before_delete.upvotes);
110 assert_eq!(0, comment_aggs_before_delete.downvotes);
112 // Add a post dislike from the other person
113 let comment_dislike = CommentLikeForm {
114 comment_id: inserted_comment.id,
115 post_id: inserted_post.id,
116 person_id: another_inserted_person.id,
120 CommentLike::like(pool, &comment_dislike).await.unwrap();
122 let comment_aggs_after_dislike = CommentAggregates::read(pool, inserted_comment.id)
126 assert_eq!(0, comment_aggs_after_dislike.score);
127 assert_eq!(1, comment_aggs_after_dislike.upvotes);
128 assert_eq!(1, comment_aggs_after_dislike.downvotes);
130 // Remove the first comment like
131 CommentLike::remove(pool, inserted_person.id, inserted_comment.id)
134 let after_like_remove = CommentAggregates::read(pool, inserted_comment.id)
137 assert_eq!(-1, after_like_remove.score);
138 assert_eq!(0, after_like_remove.upvotes);
139 assert_eq!(1, after_like_remove.downvotes);
141 // Remove the parent post
142 Post::delete(pool, inserted_post.id).await.unwrap();
144 // Should be none found, since the post was deleted
145 let after_delete = CommentAggregates::read(pool, inserted_comment.id).await;
146 assert!(after_delete.is_err());
148 // This should delete all the associated rows, and fire triggers
149 Person::delete(pool, another_inserted_person.id)
152 let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
153 assert_eq!(1, person_num_deleted);
155 // Delete the community
156 let community_num_deleted = Community::delete(pool, inserted_community.id)
159 assert_eq!(1, community_num_deleted);
161 Instance::delete(pool, inserted_instance.id).await.unwrap();