1 use crate::{aggregates::structs::PostAggregates, newtypes::PostId, schema::post_aggregates};
2 use diesel::{result::Error, *};
5 pub fn read(conn: &PgConnection, post_id: PostId) -> Result<Self, Error> {
7 .filter(post_aggregates::post_id.eq(post_id))
15 aggregates::post_aggregates::PostAggregates,
17 comment::{Comment, CommentForm},
18 community::{Community, CommunityForm},
19 person::{Person, PersonForm},
20 post::{Post, PostForm, PostLike, PostLikeForm},
22 traits::{Crud, Likeable},
23 utils::establish_unpooled_connection,
25 use serial_test::serial;
30 let conn = establish_unpooled_connection();
32 let new_person = PersonForm {
33 name: "thommy_community_agg".into(),
34 ..PersonForm::default()
37 let inserted_person = Person::create(&conn, &new_person).unwrap();
39 let another_person = PersonForm {
40 name: "jerry_community_agg".into(),
41 ..PersonForm::default()
44 let another_inserted_person = Person::create(&conn, &another_person).unwrap();
46 let new_community = CommunityForm {
47 name: "TIL_community_agg".into(),
48 title: "nada".to_owned(),
49 ..CommunityForm::default()
52 let inserted_community = Community::create(&conn, &new_community).unwrap();
54 let new_post = PostForm {
55 name: "A test post".into(),
56 creator_id: inserted_person.id,
57 community_id: inserted_community.id,
61 let inserted_post = Post::create(&conn, &new_post).unwrap();
63 let comment_form = CommentForm {
64 content: "A test comment".into(),
65 creator_id: inserted_person.id,
66 post_id: inserted_post.id,
67 ..CommentForm::default()
70 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
72 let child_comment_form = CommentForm {
73 content: "A test comment".into(),
74 creator_id: inserted_person.id,
75 post_id: inserted_post.id,
76 parent_id: Some(inserted_comment.id),
77 ..CommentForm::default()
80 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
82 let post_like = PostLikeForm {
83 post_id: inserted_post.id,
84 person_id: inserted_person.id,
88 PostLike::like(&conn, &post_like).unwrap();
90 let post_aggs_before_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
92 assert_eq!(2, post_aggs_before_delete.comments);
93 assert_eq!(1, post_aggs_before_delete.score);
94 assert_eq!(1, post_aggs_before_delete.upvotes);
95 assert_eq!(0, post_aggs_before_delete.downvotes);
97 // Add a post dislike from the other person
98 let post_dislike = PostLikeForm {
99 post_id: inserted_post.id,
100 person_id: another_inserted_person.id,
104 PostLike::like(&conn, &post_dislike).unwrap();
106 let post_aggs_after_dislike = PostAggregates::read(&conn, inserted_post.id).unwrap();
108 assert_eq!(2, post_aggs_after_dislike.comments);
109 assert_eq!(0, post_aggs_after_dislike.score);
110 assert_eq!(1, post_aggs_after_dislike.upvotes);
111 assert_eq!(1, post_aggs_after_dislike.downvotes);
113 // Remove the parent comment
114 Comment::delete(&conn, inserted_comment.id).unwrap();
115 let after_comment_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
116 assert_eq!(0, after_comment_delete.comments);
117 assert_eq!(0, after_comment_delete.score);
118 assert_eq!(1, after_comment_delete.upvotes);
119 assert_eq!(1, after_comment_delete.downvotes);
121 // Remove the first post like
122 PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
123 let after_like_remove = PostAggregates::read(&conn, inserted_post.id).unwrap();
124 assert_eq!(0, after_like_remove.comments);
125 assert_eq!(-1, after_like_remove.score);
126 assert_eq!(0, after_like_remove.upvotes);
127 assert_eq!(1, after_like_remove.downvotes);
129 // This should delete all the associated rows, and fire triggers
130 Person::delete(&conn, another_inserted_person.id).unwrap();
131 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
132 assert_eq!(1, person_num_deleted);
134 // Delete the community
135 let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
136 assert_eq!(1, community_num_deleted);
138 // Should be none found, since the creator was deleted
139 let after_delete = PostAggregates::read(&conn, inserted_post.id);
140 assert!(after_delete.is_err());