2 aggregates::structs::CommentAggregates,
4 schema::comment_aggregates,
6 use diesel::{result::Error, *};
8 impl CommentAggregates {
9 pub fn read(conn: &mut PgConnection, comment_id: CommentId) -> Result<Self, Error> {
10 comment_aggregates::table
11 .filter(comment_aggregates::comment_id.eq(comment_id))
19 aggregates::comment_aggregates::CommentAggregates,
21 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
22 community::{Community, CommunityForm},
23 person::{Person, PersonForm},
24 post::{Post, PostForm},
26 traits::{Crud, Likeable},
27 utils::establish_unpooled_connection,
29 use serial_test::serial;
34 let conn = &mut establish_unpooled_connection();
36 let new_person = PersonForm {
37 name: "thommy_comment_agg".into(),
38 public_key: Some("pubkey".to_string()),
39 ..PersonForm::default()
42 let inserted_person = Person::create(conn, &new_person).unwrap();
44 let another_person = PersonForm {
45 name: "jerry_comment_agg".into(),
46 public_key: Some("pubkey".to_string()),
47 ..PersonForm::default()
50 let another_inserted_person = Person::create(conn, &another_person).unwrap();
52 let new_community = CommunityForm {
53 name: "TIL_comment_agg".into(),
54 title: "nada".to_owned(),
55 public_key: Some("pubkey".to_string()),
56 ..CommunityForm::default()
59 let inserted_community = Community::create(conn, &new_community).unwrap();
61 let new_post = PostForm {
62 name: "A test post".into(),
63 creator_id: inserted_person.id,
64 community_id: inserted_community.id,
68 let inserted_post = Post::create(conn, &new_post).unwrap();
70 let comment_form = CommentForm {
71 content: "A test comment".into(),
72 creator_id: inserted_person.id,
73 post_id: inserted_post.id,
74 ..CommentForm::default()
77 let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
79 let child_comment_form = CommentForm {
80 content: "A test comment".into(),
81 creator_id: inserted_person.id,
82 post_id: inserted_post.id,
83 ..CommentForm::default()
86 let _inserted_child_comment =
87 Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)).unwrap();
89 let comment_like = CommentLikeForm {
90 comment_id: inserted_comment.id,
91 post_id: inserted_post.id,
92 person_id: inserted_person.id,
96 CommentLike::like(conn, &comment_like).unwrap();
98 let comment_aggs_before_delete = CommentAggregates::read(conn, inserted_comment.id).unwrap();
100 assert_eq!(1, comment_aggs_before_delete.score);
101 assert_eq!(1, comment_aggs_before_delete.upvotes);
102 assert_eq!(0, comment_aggs_before_delete.downvotes);
104 // Add a post dislike from the other person
105 let comment_dislike = CommentLikeForm {
106 comment_id: inserted_comment.id,
107 post_id: inserted_post.id,
108 person_id: another_inserted_person.id,
112 CommentLike::like(conn, &comment_dislike).unwrap();
114 let comment_aggs_after_dislike = CommentAggregates::read(conn, inserted_comment.id).unwrap();
116 assert_eq!(0, comment_aggs_after_dislike.score);
117 assert_eq!(1, comment_aggs_after_dislike.upvotes);
118 assert_eq!(1, comment_aggs_after_dislike.downvotes);
120 // Remove the first comment like
121 CommentLike::remove(conn, inserted_person.id, inserted_comment.id).unwrap();
122 let after_like_remove = CommentAggregates::read(conn, inserted_comment.id).unwrap();
123 assert_eq!(-1, after_like_remove.score);
124 assert_eq!(0, after_like_remove.upvotes);
125 assert_eq!(1, after_like_remove.downvotes);
127 // Remove the parent post
128 Post::delete(conn, inserted_post.id).unwrap();
130 // Should be none found, since the post was deleted
131 let after_delete = CommentAggregates::read(conn, inserted_comment.id);
132 assert!(after_delete.is_err());
134 // This should delete all the associated rows, and fire triggers
135 Person::delete(conn, another_inserted_person.id).unwrap();
136 let person_num_deleted = Person::delete(conn, inserted_person.id).unwrap();
137 assert_eq!(1, person_num_deleted);
139 // Delete the community
140 let community_num_deleted = Community::delete(conn, inserted_community.id).unwrap();
141 assert_eq!(1, community_num_deleted);