use crate::prelude::*;
use hashbrown::HashSet;
#[driver_test(id(ID), scenario(crate::scenarios::has_many_same_target))]
pub async fn pair_hint_disambiguates_has_many(test: &mut Test) -> Result<()> {
let mut db = setup(test).await;
let users = toasty::create!(User::[
{ name: "Alice" },
{ name: "Bob" },
])
.exec(&mut db)
.await?;
let (alice, bob) = (&users[0], &users[1]);
let articles = toasty::create!(Article::[
{ title: "one", author: alice, reviewer: bob },
{ title: "two", author: alice, reviewer: bob },
{ title: "three", author: bob, reviewer: alice },
])
.exec(&mut db)
.await?;
let (a1, a2, a3) = (&articles[0], &articles[1], &articles[2]);
let alice_authored: HashSet<_> = alice
.authored_articles()
.exec(&mut db)
.await?
.into_iter()
.map(|a| a.id)
.collect();
assert_eq!(alice_authored, HashSet::from_iter([a1.id, a2.id]));
let alice_reviewed: HashSet<_> = alice
.reviewed_articles()
.exec(&mut db)
.await?
.into_iter()
.map(|a| a.id)
.collect();
assert_eq!(alice_reviewed, HashSet::from_iter([a3.id]));
let a1_author = a1.author().exec(&mut db).await?;
let a1_reviewer = a1.reviewer().exec(&mut db).await?;
assert_eq!(a1_author.id, alice.id);
assert_eq!(a1_reviewer.id, bob.id);
Ok(())
}
#[driver_test(id(ID), scenario(crate::scenarios::has_many_same_target))]
pub async fn pair_hint_create_via_has_many_accessor(test: &mut Test) -> Result<()> {
let mut db = setup(test).await;
let users = toasty::create!(User::[
{ name: "Alice" },
{ name: "Bob" },
])
.exec(&mut db)
.await?;
let (alice, bob) = (&users[0], &users[1]);
let article = toasty::create!(in alice.authored_articles() {
title: "draft",
reviewer: bob,
})
.exec(&mut db)
.await?;
assert_eq!(article.author_id, alice.id);
assert_eq!(article.reviewer_id, bob.id);
assert!(alice.reviewed_articles().exec(&mut db).await?.is_empty());
Ok(())
}