1 use crate::{aggregates::structs::PostAggregates, newtypes::PostId, schema::post_aggregates};
2 use diesel::{result::Error, *};
5 pub fn read(conn: &mut PgConnection, post_id: PostId) -> Result<Self, Error> {
7 .filter(post_aggregates::post_id.eq(post_id))
15 aggregates::post_aggregates::PostAggregates,
17 comment::{Comment, CommentInsertForm},
18 community::{Community, CommunityInsertForm},
20 person::{Person, PersonInsertForm},
21 post::{Post, PostInsertForm, PostLike, PostLikeForm},
23 traits::{Crud, Likeable},
24 utils::establish_unpooled_connection,
26 use serial_test::serial;
31 let conn = &mut establish_unpooled_connection();
33 let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
35 let new_person = PersonInsertForm::builder()
36 .name("thommy_community_agg".into())
37 .public_key("pubkey".to_string())
38 .instance_id(inserted_instance.id)
41 let inserted_person = Person::create(conn, &new_person).unwrap();
43 let another_person = PersonInsertForm::builder()
44 .name("jerry_community_agg".into())
45 .public_key("pubkey".to_string())
46 .instance_id(inserted_instance.id)
49 let another_inserted_person = Person::create(conn, &another_person).unwrap();
51 let new_community = CommunityInsertForm::builder()
52 .name("TIL_community_agg".into())
53 .title("nada".to_owned())
54 .public_key("pubkey".to_string())
55 .instance_id(inserted_instance.id)
58 let inserted_community = Community::create(conn, &new_community).unwrap();
60 let new_post = PostInsertForm::builder()
61 .name("A test post".into())
62 .creator_id(inserted_person.id)
63 .community_id(inserted_community.id)
66 let inserted_post = Post::create(conn, &new_post).unwrap();
68 let comment_form = CommentInsertForm::builder()
69 .content("A test comment".into())
70 .creator_id(inserted_person.id)
71 .post_id(inserted_post.id)
74 let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
76 let child_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_child_comment =
83 Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)).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 comments
117 Comment::delete(conn, inserted_comment.id).unwrap();
118 Comment::delete(conn, inserted_child_comment.id).unwrap();
119 let after_comment_delete = PostAggregates::read(conn, inserted_post.id).unwrap();
120 assert_eq!(0, after_comment_delete.comments);
121 assert_eq!(0, after_comment_delete.score);
122 assert_eq!(1, after_comment_delete.upvotes);
123 assert_eq!(1, after_comment_delete.downvotes);
125 // Remove the first post like
126 PostLike::remove(conn, inserted_person.id, inserted_post.id).unwrap();
127 let after_like_remove = PostAggregates::read(conn, inserted_post.id).unwrap();
128 assert_eq!(0, after_like_remove.comments);
129 assert_eq!(-1, after_like_remove.score);
130 assert_eq!(0, after_like_remove.upvotes);
131 assert_eq!(1, after_like_remove.downvotes);
133 // This should delete all the associated rows, and fire triggers
134 Person::delete(conn, another_inserted_person.id).unwrap();
135 let person_num_deleted = Person::delete(conn, inserted_person.id).unwrap();
136 assert_eq!(1, person_num_deleted);
138 // Delete the community
139 let community_num_deleted = Community::delete(conn, inserted_community.id).unwrap();
140 assert_eq!(1, community_num_deleted);
142 // Should be none found, since the creator was deleted
143 let after_delete = PostAggregates::read(conn, inserted_post.id);
144 assert!(after_delete.is_err());
146 Instance::delete(conn, inserted_instance.id).unwrap();