2 aggregates::structs::PostAggregates,
4 schema::post_aggregates,
5 utils::{functions::hot_rank, get_conn, DbPool},
7 use diesel::{result::Error, ExpressionMethods, QueryDsl};
8 use diesel_async::RunQueryDsl;
11 pub async fn read(pool: &DbPool, post_id: PostId) -> Result<Self, Error> {
12 let conn = &mut get_conn(pool).await?;
13 post_aggregates::table
14 .filter(post_aggregates::post_id.eq(post_id))
19 pub async fn update_hot_rank(pool: &DbPool, post_id: PostId) -> Result<Self, Error> {
20 let conn = &mut get_conn(pool).await?;
22 diesel::update(post_aggregates::table)
23 .filter(post_aggregates::post_id.eq(post_id))
25 post_aggregates::hot_rank.eq(hot_rank(post_aggregates::score, post_aggregates::published)),
26 post_aggregates::hot_rank_active.eq(hot_rank(
27 post_aggregates::score,
28 post_aggregates::newest_comment_time_necro,
31 .get_result::<Self>(conn)
39 aggregates::post_aggregates::PostAggregates,
41 comment::{Comment, CommentInsertForm},
42 community::{Community, CommunityInsertForm},
44 person::{Person, PersonInsertForm},
45 post::{Post, PostInsertForm, PostLike, PostLikeForm},
47 traits::{Crud, Likeable},
48 utils::build_db_pool_for_tests,
50 use serial_test::serial;
54 async fn test_crud() {
55 let pool = &build_db_pool_for_tests().await;
57 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
61 let new_person = PersonInsertForm::builder()
62 .name("thommy_community_agg".into())
63 .public_key("pubkey".to_string())
64 .instance_id(inserted_instance.id)
67 let inserted_person = Person::create(pool, &new_person).await.unwrap();
69 let another_person = PersonInsertForm::builder()
70 .name("jerry_community_agg".into())
71 .public_key("pubkey".to_string())
72 .instance_id(inserted_instance.id)
75 let another_inserted_person = Person::create(pool, &another_person).await.unwrap();
77 let new_community = CommunityInsertForm::builder()
78 .name("TIL_community_agg".into())
79 .title("nada".to_owned())
80 .public_key("pubkey".to_string())
81 .instance_id(inserted_instance.id)
84 let inserted_community = Community::create(pool, &new_community).await.unwrap();
86 let new_post = PostInsertForm::builder()
87 .name("A test post".into())
88 .creator_id(inserted_person.id)
89 .community_id(inserted_community.id)
92 let inserted_post = Post::create(pool, &new_post).await.unwrap();
94 let comment_form = CommentInsertForm::builder()
95 .content("A test comment".into())
96 .creator_id(inserted_person.id)
97 .post_id(inserted_post.id)
100 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
102 let child_comment_form = CommentInsertForm::builder()
103 .content("A test comment".into())
104 .creator_id(inserted_person.id)
105 .post_id(inserted_post.id)
108 let inserted_child_comment =
109 Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
113 let post_like = PostLikeForm {
114 post_id: inserted_post.id,
115 person_id: inserted_person.id,
119 PostLike::like(pool, &post_like).await.unwrap();
121 let post_aggs_before_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
123 assert_eq!(2, post_aggs_before_delete.comments);
124 assert_eq!(1, post_aggs_before_delete.score);
125 assert_eq!(1, post_aggs_before_delete.upvotes);
126 assert_eq!(0, post_aggs_before_delete.downvotes);
128 // Add a post dislike from the other person
129 let post_dislike = PostLikeForm {
130 post_id: inserted_post.id,
131 person_id: another_inserted_person.id,
135 PostLike::like(pool, &post_dislike).await.unwrap();
137 let post_aggs_after_dislike = PostAggregates::read(pool, inserted_post.id).await.unwrap();
139 assert_eq!(2, post_aggs_after_dislike.comments);
140 assert_eq!(0, post_aggs_after_dislike.score);
141 assert_eq!(1, post_aggs_after_dislike.upvotes);
142 assert_eq!(1, post_aggs_after_dislike.downvotes);
144 // Remove the comments
145 Comment::delete(pool, inserted_comment.id).await.unwrap();
146 Comment::delete(pool, inserted_child_comment.id)
149 let after_comment_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
150 assert_eq!(0, after_comment_delete.comments);
151 assert_eq!(0, after_comment_delete.score);
152 assert_eq!(1, after_comment_delete.upvotes);
153 assert_eq!(1, after_comment_delete.downvotes);
155 // Remove the first post like
156 PostLike::remove(pool, inserted_person.id, inserted_post.id)
159 let after_like_remove = PostAggregates::read(pool, inserted_post.id).await.unwrap();
160 assert_eq!(0, after_like_remove.comments);
161 assert_eq!(-1, after_like_remove.score);
162 assert_eq!(0, after_like_remove.upvotes);
163 assert_eq!(1, after_like_remove.downvotes);
165 // This should delete all the associated rows, and fire triggers
166 Person::delete(pool, another_inserted_person.id)
169 let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
170 assert_eq!(1, person_num_deleted);
172 // Delete the community
173 let community_num_deleted = Community::delete(pool, inserted_community.id)
176 assert_eq!(1, community_num_deleted);
178 // Should be none found, since the creator was deleted
179 let after_delete = PostAggregates::read(pool, inserted_post.id).await;
180 assert!(after_delete.is_err());
182 Instance::delete(pool, inserted_instance.id).await.unwrap();