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 public_key: Some("pubkey".to_string()),
35 ..PersonForm::default()
38 let inserted_person = Person::create(&conn, &new_person).unwrap();
40 let another_person = PersonForm {
41 name: "jerry_community_agg".into(),
42 public_key: Some("pubkey".to_string()),
43 ..PersonForm::default()
46 let another_inserted_person = Person::create(&conn, &another_person).unwrap();
48 let new_community = CommunityForm {
49 name: "TIL_community_agg".into(),
50 title: "nada".to_owned(),
51 public_key: Some("pubkey".to_string()),
52 ..CommunityForm::default()
55 let inserted_community = Community::create(&conn, &new_community).unwrap();
57 let new_post = PostForm {
58 name: "A test post".into(),
59 creator_id: inserted_person.id,
60 community_id: inserted_community.id,
64 let inserted_post = Post::create(&conn, &new_post).unwrap();
66 let comment_form = CommentForm {
67 content: "A test comment".into(),
68 creator_id: inserted_person.id,
69 post_id: inserted_post.id,
70 ..CommentForm::default()
73 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
75 let child_comment_form = CommentForm {
76 content: "A test comment".into(),
77 creator_id: inserted_person.id,
78 post_id: inserted_post.id,
79 parent_id: Some(inserted_comment.id),
80 ..CommentForm::default()
83 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
85 let post_like = PostLikeForm {
86 post_id: inserted_post.id,
87 person_id: inserted_person.id,
91 PostLike::like(&conn, &post_like).unwrap();
93 let post_aggs_before_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
95 assert_eq!(2, post_aggs_before_delete.comments);
96 assert_eq!(1, post_aggs_before_delete.score);
97 assert_eq!(1, post_aggs_before_delete.upvotes);
98 assert_eq!(0, post_aggs_before_delete.downvotes);
100 // Add a post dislike from the other person
101 let post_dislike = PostLikeForm {
102 post_id: inserted_post.id,
103 person_id: another_inserted_person.id,
107 PostLike::like(&conn, &post_dislike).unwrap();
109 let post_aggs_after_dislike = PostAggregates::read(&conn, inserted_post.id).unwrap();
111 assert_eq!(2, post_aggs_after_dislike.comments);
112 assert_eq!(0, post_aggs_after_dislike.score);
113 assert_eq!(1, post_aggs_after_dislike.upvotes);
114 assert_eq!(1, post_aggs_after_dislike.downvotes);
116 // Remove the parent comment
117 Comment::delete(&conn, inserted_comment.id).unwrap();
118 let after_comment_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
119 assert_eq!(0, after_comment_delete.comments);
120 assert_eq!(0, after_comment_delete.score);
121 assert_eq!(1, after_comment_delete.upvotes);
122 assert_eq!(1, after_comment_delete.downvotes);
124 // Remove the first post like
125 PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
126 let after_like_remove = PostAggregates::read(&conn, inserted_post.id).unwrap();
127 assert_eq!(0, after_like_remove.comments);
128 assert_eq!(-1, after_like_remove.score);
129 assert_eq!(0, after_like_remove.upvotes);
130 assert_eq!(1, after_like_remove.downvotes);
132 // This should delete all the associated rows, and fire triggers
133 Person::delete(&conn, another_inserted_person.id).unwrap();
134 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
135 assert_eq!(1, person_num_deleted);
137 // Delete the community
138 let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
139 assert_eq!(1, community_num_deleted);
141 // Should be none found, since the creator was deleted
142 let after_delete = PostAggregates::read(&conn, inserted_post.id);
143 assert!(after_delete.is_err());