use super::Route;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
struct Knobs {
ceiling: Option<u32>,
}
#[test]
fn the_unconfigured_edge_is_the_two_constants_and_no_package() {
let route = Route::<Knobs>::new();
assert!((route.weight() - 1.0).abs() < f64::EPSILON);
assert!(route.enabled());
assert_eq!(route.options(), None);
}
#[test]
fn the_shipped_edge_constants_are_one_and_walked() {
assert!((Route::<Knobs>::DEFAULT_WEIGHT - 1.0).abs() < f64::EPSILON);
const { assert!(Route::<Knobs>::DEFAULT_ENABLED) }
}
#[test]
fn the_default_edge_is_the_unconfigured_one() {
assert_eq!(Route::<Knobs>::default(), Route::<Knobs>::new());
}
#[test]
fn the_builders_compose_without_disturbing_each_other() {
let route = Route::new()
.with_weight(2.5)
.with_enabled(false)
.with_options(Knobs { ceiling: Some(64) });
assert!((route.weight() - 2.5).abs() < f64::EPSILON);
assert!(!route.enabled());
assert_eq!(route.options(), Some(&Knobs { ceiling: Some(64) }));
}
#[test]
fn the_interior_comes_back_out_untouched() {
let knobs = Knobs { ceiling: Some(7) };
assert_eq!(Route::new().with_options(knobs).options(), Some(&knobs));
}
#[cfg(test)]
#[test]
fn a_route_of_routes_is_the_same_type_one_level_down() {
let nested: Route<Route<Knobs>> = Route::new().with_options(Route::new().with_weight(0.5));
let inner = nested.options().expect("the child tier's edge");
assert!((inner.weight() - 0.5).abs() < f64::EPSILON);
assert!(inner.enabled(), "the inner edge kept its own default");
}
#[cfg(feature = "async-graphql")]
mod wire {
use async_graphql::{EmptyMutation, EmptySubscription, InputObject, InputType, Object, Schema};
use super::super::Route;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, InputObject)]
struct TranscriptFulltextOptions {
ceiling: Option<u32>,
}
struct Query;
#[Object]
impl Query {
async fn fused(&self, route: Option<Route<TranscriptFulltextOptions>>) -> bool {
route.is_some_and(|route| route.enabled())
}
}
fn sdl() -> String {
Schema::build(Query, EmptyMutation, EmptySubscription)
.finish()
.sdl()
}
#[test]
fn the_wire_name_is_the_packages_own_under_a_route_suffix() {
assert_eq!(
<Route<TranscriptFulltextOptions> as InputType>::type_name(),
"TranscriptFulltextOptionsRoute",
);
}
#[test]
fn the_edge_publishes_its_constants_as_the_schemas_own_defaults() {
let sdl = sdl();
assert!(
sdl.contains("input TranscriptFulltextOptionsRoute {"),
"{sdl}",
);
assert!(sdl.contains("weight: Float = 1"), "{sdl}");
assert!(sdl.contains("enabled: Boolean = true"), "{sdl}");
assert!(
sdl.contains("options: TranscriptFulltextOptions"),
"the interior is nullable and wholly omissible:\n{sdl}",
);
}
#[test]
fn an_empty_object_parses_as_the_unconfigured_edge() {
let parsed = <Route<TranscriptFulltextOptions> as InputType>::parse(Some(
async_graphql::Value::Object(async_graphql::indexmap::IndexMap::new()),
))
.expect("an empty object is a route");
assert_eq!(parsed, Route::new());
}
#[test]
fn a_written_field_beats_the_constant_and_leaves_the_others() {
let mut object = async_graphql::indexmap::IndexMap::new();
object.insert(
async_graphql::Name::new("weight"),
async_graphql::Value::from(3.0_f64),
);
let parsed = <Route<TranscriptFulltextOptions> as InputType>::parse(Some(
async_graphql::Value::Object(object),
))
.expect("one field is a route");
assert!((parsed.weight() - 3.0).abs() < f64::EPSILON);
assert!(parsed.enabled(), "the field nobody wrote kept its constant");
assert_eq!(parsed.options(), None);
}
}