use super::{
ElectionBackend, ElectionLabel, Field, FieldNumbers, IndexBackend, IndexGraphql, IndexModel,
IndexRename, Numbers,
answers::{fanned, fields, plain},
};
use crate::{model::GraphqlKind, prefix::EmissionPrefix};
fn ident(name: &str) -> syn::Ident {
syn::Ident::new(name, proc_macro2::Span::call_site())
}
fn word(spelled: &str) -> syn::LitStr {
syn::LitStr::new(spelled, proc_macro2::Span::call_site())
}
fn tight(model: &IndexModel) -> String {
model.emit().to_string().split_whitespace().collect()
}
fn dressed(model: IndexModel, skin: ElectionBackend) -> IndexModel {
IndexModel {
prefix: EmissionPrefix::default(),
field_numbers: vec![FieldNumbers {
field: ident("clip"),
numbers: Numbers::default(),
skins: vec![skin],
}],
..model
}
}
fn road(which: Field, spelled: &str) -> ElectionBackend {
let mut skin = ElectionBackend::of(GraphqlKind::AsyncGraphql);
let seat = match which {
Field::Keys => &mut skin.keys,
Field::Hydrated => &mut skin.hydrated,
Field::Fused => &mut skin.fused,
Field::Similar => &mut skin.similar,
Field::SimilarHydrated => &mut skin.similar_hydrated,
};
*seat = Some(word(spelled));
skin
}
#[test]
fn every_road_seat_reaches_the_field_it_names() {
for (which, base) in [
(Field::Keys, "shotsByClipKeys"),
(Field::Hydrated, "shotsByClip"),
(Field::Similar, "shotsSimilarByClipKeys"),
(Field::SimilarHydrated, "shotsSimilarByClip"),
] {
let published = fields(&dressed(plain(), road(which, "aWord")));
assert!(
published.contains(&"aWord".to_owned()),
"`{}` did not reach a field: {published:?}",
which.as_str(),
);
assert!(
!published.contains(&base.to_owned()),
"`{}` left its derived name behind as well: {published:?}",
which.as_str(),
);
}
assert_eq!(
fields(&dressed(fanned(), road(Field::Fused, "clipEverywhere"))),
vec!["clipEverywhere".to_owned()],
);
}
#[test]
fn a_road_resolves_member_then_base_then_default() {
let renamed = IndexModel {
prefix: EmissionPrefix::default(),
rename: Some(IndexRename::Base(ident("ShotSearch"))),
..dressed(plain(), road(Field::Keys, "clipNeighbours"))
};
let published = fields(&renamed);
assert!(
published.contains(&"clipNeighbours".to_owned()),
"the MEMBER lost to the base: {published:?}",
);
assert!(
published.contains(&"shotSearchByClip".to_owned()),
"the BASE did not reach the roads it was left to: {published:?}",
);
assert!(
!published.contains(&"shotsByClip".to_owned()),
"a road kept the DEFAULT although a base was written: {published:?}",
);
}
#[test]
fn a_declared_road_leaves_its_tier_below_on_the_derived_name() {
let published = fields(&dressed(plain(), road(Field::Keys, "clipNeighbours")));
assert!(published.contains(&"clipNeighbours".to_owned()));
assert!(
published.contains(&"shotsByClip".to_owned()),
"the keys word composed its way onto the hydrated twin: {published:?}",
);
}
#[test]
fn the_election_question_seat_moves_the_schema_name_and_no_rust_one() {
let mut skin = ElectionBackend::of(GraphqlKind::AsyncGraphql);
skin.question = Some(word("ClipAsk"));
let model = dressed(plain(), skin);
let emitted = tight(&model);
assert!(
emitted.contains(r#"Cow::Borrowed("ClipAsk")"#),
"the seat did not reach the published name: {emitted}",
);
assert!(
emitted.contains("enumShotsClipQuestion{"),
"the schema word moved the Rust declaration: {emitted}",
);
}
#[test]
fn the_election_engines_seat_moves_the_schema_name_of_the_product() {
let mut skin = ElectionBackend::of(GraphqlKind::AsyncGraphql);
skin.engines = Some(word("ClipLegs"));
let emitted = tight(&dressed(fanned(), skin));
assert!(
emitted.contains(r#""ClipLegs""#),
"the seat did not reach the published name: {emitted}",
);
assert!(
emitted.contains("structShotsClipEngines"),
"the schema word moved the Rust declaration: {emitted}",
);
}
#[test]
fn a_member_with_no_base_above_it_moves_its_own_face_and_nothing_else() {
let mut skin = ElectionBackend::of(GraphqlKind::AsyncGraphql);
skin.question = Some(word("ClipAsk"));
let emitted = tight(&dressed(plain(), skin));
assert!(emitted.contains(r#"Cow::Borrowed("ClipAsk")"#), "{emitted}");
assert!(
fields(&dressed(plain(), {
let mut skin = ElectionBackend::of(GraphqlKind::AsyncGraphql);
skin.question = Some(word("ClipAsk"));
skin
}))
.contains(&"shotsByClipKeys".to_owned()),
"a type seat with no base moved a field name as well",
);
}
#[test]
fn a_type_seat_wins_over_the_skin_base_and_the_base_takes_the_rest() {
let mut head = IndexBackend::of(GraphqlKind::AsyncGraphql);
head.prefix = Some(word("Reels"));
let mut skin = ElectionBackend::of(GraphqlKind::AsyncGraphql);
skin.question = Some(word("ClipAsk"));
let model = IndexModel {
prefix: EmissionPrefix::default(),
graphql: Some(IndexGraphql {
backends: vec![head],
}),
..dressed(plain(), skin)
};
let emitted = tight(&model);
assert!(
emitted.contains(r#"Cow::Borrowed("ClipAsk")"#),
"the base displaced the member: {emitted}",
);
assert!(
!emitted.contains(r#"Cow::Borrowed("ReelsClipQuestion")"#),
"both answers were published: {emitted}",
);
}
#[test]
fn no_seat_moves_an_argument_word_on_the_plain_road() {
let mut skin = ElectionBackend::of(GraphqlKind::AsyncGraphql);
skin.question = Some(word("ClipAsk"));
skin.engines = Some(word("ClipLegs"));
skin.keys = Some(word("aKeys"));
skin.hydrated = Some(word("aHydrated"));
skin.fused = Some(word("aFused"));
skin.similar = Some(word("aSimilar"));
skin.similar_hydrated = Some(word("aSimilarHydrated"));
let mut head = IndexBackend::of(GraphqlKind::AsyncGraphql);
head.prefix = Some(word("Reels"));
let model = IndexModel {
prefix: EmissionPrefix::default(),
graphql: Some(IndexGraphql {
backends: vec![head],
}),
rename: Some(IndexRename::Base(ident("ShotSearch"))),
..dressed(plain(), skin)
};
let emitted = tight(&model);
for floor in [
r#"name="question""#,
r#"name="engine""#,
r#"name="first""#,
r#"name="depth""#,
r#"name="key""#,
] {
assert!(emitted.contains(floor), "`{floor}` moved: {emitted}");
}
assert!(emitted.contains(r#"from("coordinates")"#), "{emitted}");
assert!(emitted.contains(r#"Cow::Borrowed("ClipAsk")"#), "{emitted}");
}
#[test]
fn no_seat_moves_an_answer_row_on_the_fused_road() {
let mut skin = ElectionBackend::of(GraphqlKind::AsyncGraphql);
skin.question = Some(word("ClipAsk"));
skin.engines = Some(word("ClipLegs"));
skin.fused = Some(word("clipEverywhere"));
let emitted = tight(&dressed(fanned(), skin));
for floor in [
r#"name="score""#,
r#"name="ordinal""#,
r#"name="contribution""#,
r#"name="evidence""#,
r#"name="node""#,
r#"name="engines""#,
r#"name="question""#,
] {
assert!(emitted.contains(floor), "`{floor}` moved: {emitted}");
}
assert_eq!(
fields(&dressed(fanned(), {
let mut skin = ElectionBackend::of(GraphqlKind::AsyncGraphql);
skin.fused = Some(word("clipEverywhere"));
skin
})),
vec!["clipEverywhere".to_owned()],
"the seats fired while every row above stayed put",
);
}
#[test]
fn the_silence_is_untouched_by_the_new_seats() {
assert_eq!(
fields(&plain()),
vec![
"shotsByClipKeys".to_owned(),
"shotsSimilarByClipKeys".to_owned(),
"shotsByClip".to_owned(),
"shotsSimilarByClip".to_owned(),
],
);
assert!(tight(&plain()).contains(r#"Cow::Borrowed("ShotsClipQuestion")"#));
assert!(matches!(
plain().embeddings[0].label,
ElectionLabel::Embedding
));
}