Rework repositories to simplify the structure
This commit is contained in:
parent
40331451ca
commit
e15c30fe06
1 changed files with 24 additions and 69 deletions
|
|
@ -4,12 +4,10 @@ use uuid::Uuid;
|
||||||
|
|
||||||
use crate::votes::model::BestSlugs;
|
use crate::votes::model::BestSlugs;
|
||||||
|
|
||||||
use super::model::Vote;
|
pub async fn insert_new_vote(slug: &str, voter_id: &Uuid, db: &PgPool) -> Result<()> {
|
||||||
|
|
||||||
pub async fn insert_new_vote(vote: &Vote, db: &PgPool) -> Result<()> {
|
|
||||||
query("insert into votes (slug, voter_id) values ($1, $2)")
|
query("insert into votes (slug, voter_id) values ($1, $2)")
|
||||||
.bind(vote.slug.clone())
|
.bind(slug)
|
||||||
.bind(vote.voter_id)
|
.bind(voter_id)
|
||||||
.execute(db)
|
.execute(db)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -58,66 +56,27 @@ mod postgres_tests {
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
test_helpers::db::test_pool,
|
test_helpers::db::test_pool,
|
||||||
votes::{
|
votes::repository::{delete_vote, get_top_n_slugs, get_vote_count_for_slug, insert_new_vote},
|
||||||
model::Vote,
|
|
||||||
repository::{delete_vote, get_top_n_slugs, get_vote_count_for_slug, insert_new_vote},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
async fn cleanup(db: &PgPool) {
|
fn test_votes() -> [(&'static str, Uuid); 9] {
|
||||||
for vote in test_votes() {
|
[
|
||||||
delete_vote(&vote.slug, &vote.voter_id, db).await.unwrap()
|
("blog_post1", Uuid::from_u128(0x1)),
|
||||||
}
|
("blog_post1", Uuid::from_u128(0x2)),
|
||||||
|
("blog_post2", Uuid::from_u128(0x3)),
|
||||||
|
("blog_post2", Uuid::from_u128(0x4)),
|
||||||
|
("blog_post3", Uuid::from_u128(0x5)),
|
||||||
|
("blog_post3", Uuid::from_u128(0x6)),
|
||||||
|
("blog_post1", Uuid::from_u128(0x7)),
|
||||||
|
("blog_post1", Uuid::from_u128(0x8)),
|
||||||
|
("blog_post3", Uuid::from_u128(0x9)),
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_votes() -> [Vote; 9] {
|
async fn cleanup(db: &PgPool) {
|
||||||
[
|
for (slug, voter_id) in test_votes() {
|
||||||
Vote {
|
delete_vote(slug, &voter_id, db).await.unwrap()
|
||||||
slug: "blog_post1".into(),
|
}
|
||||||
voter_id: Uuid::from_u128(0x1),
|
|
||||||
created_at: chrono::Utc::now(),
|
|
||||||
},
|
|
||||||
Vote {
|
|
||||||
slug: "blog_post1".into(),
|
|
||||||
voter_id: Uuid::from_u128(0x2),
|
|
||||||
created_at: chrono::Utc::now(),
|
|
||||||
},
|
|
||||||
Vote {
|
|
||||||
slug: "blog_post2".into(),
|
|
||||||
voter_id: Uuid::from_u128(0x3),
|
|
||||||
created_at: chrono::Utc::now(),
|
|
||||||
},
|
|
||||||
Vote {
|
|
||||||
slug: "blog_post2".into(),
|
|
||||||
voter_id: Uuid::from_u128(0x4),
|
|
||||||
created_at: chrono::Utc::now(),
|
|
||||||
},
|
|
||||||
Vote {
|
|
||||||
slug: "blog_post3".into(),
|
|
||||||
voter_id: Uuid::from_u128(0x5),
|
|
||||||
created_at: chrono::Utc::now(),
|
|
||||||
},
|
|
||||||
Vote {
|
|
||||||
slug: "blog_post3".into(),
|
|
||||||
voter_id: Uuid::from_u128(0x6),
|
|
||||||
created_at: chrono::Utc::now(),
|
|
||||||
},
|
|
||||||
Vote {
|
|
||||||
slug: "blog_post1".into(),
|
|
||||||
voter_id: Uuid::from_u128(0x7),
|
|
||||||
created_at: chrono::Utc::now(),
|
|
||||||
},
|
|
||||||
Vote {
|
|
||||||
slug: "blog_post1".into(),
|
|
||||||
voter_id: Uuid::from_u128(0x8),
|
|
||||||
created_at: chrono::Utc::now(),
|
|
||||||
},
|
|
||||||
Vote {
|
|
||||||
slug: "blog_post3".into(),
|
|
||||||
voter_id: Uuid::from_u128(0x9),
|
|
||||||
created_at: chrono::Utc::now(),
|
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|
@ -127,8 +86,8 @@ mod postgres_tests {
|
||||||
cleanup(&db).await;
|
cleanup(&db).await;
|
||||||
let votes = test_votes();
|
let votes = test_votes();
|
||||||
|
|
||||||
for vote in votes.iter() {
|
for (slug, voter_id) in votes.iter() {
|
||||||
insert_new_vote(vote, &db)
|
insert_new_vote(slug, voter_id, &db)
|
||||||
.await
|
.await
|
||||||
.expect("Insertions to db failed");
|
.expect("Insertions to db failed");
|
||||||
}
|
}
|
||||||
|
|
@ -141,12 +100,8 @@ mod postgres_tests {
|
||||||
assert_eq!(top_2[0].slug, "blog_post1");
|
assert_eq!(top_2[0].slug, "blog_post1");
|
||||||
assert_eq!(top_2[1].slug, "blog_post3");
|
assert_eq!(top_2[1].slug, "blog_post3");
|
||||||
|
|
||||||
delete_vote(&votes[4].slug, &votes[4].voter_id, &db)
|
delete_vote(votes[4].0, &votes[4].1, &db).await.unwrap();
|
||||||
.await
|
delete_vote(votes[5].0, &votes[5].1, &db).await.unwrap();
|
||||||
.unwrap();
|
|
||||||
delete_vote(&votes[5].slug, &votes[5].voter_id, &db)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(get_vote_count_for_slug("blog_post1", &db).await.unwrap(), 4);
|
assert_eq!(get_vote_count_for_slug("blog_post1", &db).await.unwrap(), 4);
|
||||||
assert_eq!(get_vote_count_for_slug("blog_post2", &db).await.unwrap(), 2);
|
assert_eq!(get_vote_count_for_slug("blog_post2", &db).await.unwrap(), 2);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue