use crate::{
interface::{
default_filter_name, default_input_name, default_order_by_name, default_output_name,
tests::{media, rendered},
},
model::{DatabaseKind, default_queries_name},
};
fn stored(kinds: &[DatabaseKind]) -> crate::interface::InterfaceModel {
let mut model = unstored();
model.persist = kinds
.iter()
.map(|kind| crate::interface::InterfaceDatabase {
kind: *kind,
aliases: crate::model::AliasOptions::default(),
})
.collect();
model
}
fn unstored() -> crate::interface::InterfaceModel {
let mut model = media();
model.persist = Vec::new();
for field in &mut model.fields {
field.pins = Vec::new();
}
model
}
#[test]
fn the_interface_emits_a_query_unit_of_the_records_own_shape() {
let model = stored(&[DatabaseKind::Sqlite]);
let emitted = rendered(&model);
let name = default_queries_name(&model.name);
assert!(
emitted.contains(&format!("pub struct {name};")),
"{emitted}"
);
assert!(emitted.contains("Default"), "{emitted}");
assert_eq!(name.to_string(), "MediaQueries");
}
#[test]
fn the_binding_seats_the_unit_at_graphql_query() {
let model = stored(&[DatabaseKind::Sqlite]);
let emitted = rendered(&model);
assert!(
emitted.contains("impl ::ingraph::__private::transform::Graphql for Media"),
"{emitted}"
);
assert!(emitted.contains("type Query = MediaQueries;"), "{emitted}");
assert!(
!emitted.contains("transform::Transformer for Media"),
"an interface implements no `Transformer`:\n{emitted}"
);
assert!(
!emitted.contains("transform::Identifiable for Media"),
"nor the supertrait whose `id_ref` it could not write:\n{emitted}"
);
}
#[test]
fn the_face_halves_name_the_argument_object_and_the_output_sum() {
let model = stored(&[DatabaseKind::Sqlite]);
let emitted = rendered(&model);
let (input, output) = (
default_input_name(&model.name),
default_output_name(&model.name),
);
assert!(
emitted.contains(&format!(
"impl ::ingraph::__private::transform::GraphqlInput for {input}"
)),
"{emitted}"
);
assert!(
emitted.contains(&format!(
"type Filter = {};",
default_filter_name(&model.name)
)),
"{emitted}"
);
assert!(
emitted.contains(&format!(
"type OrderBy = {};",
default_order_by_name(&model.name)
)),
"{emitted}"
);
assert!(
emitted.contains(&format!(
"impl ::ingraph::__private::transform::GraphqlOutput for {output}"
)),
"{emitted}"
);
assert!(
emitted.contains("type Key = ();"),
"the wire ADDRESS is the fusion seam's, and the empty product is the framework's own absent \
form:\n{emitted}"
);
assert!(
emitted.contains(&format!("type Input = {input};")),
"{emitted}"
);
assert!(
emitted.contains(&format!("type Output = {output};")),
"{emitted}"
);
}
#[test]
fn an_interface_about_no_sql_database_has_no_binding_and_no_unit() {
let emitted = rendered(&unstored());
assert!(
!emitted.contains("transform::Graphql for Media"),
"{emitted}"
);
assert!(!emitted.contains("struct MediaQueries"), "{emitted}");
}
#[test]
fn the_root_fields_are_served_from_the_first_declared_database() {
let model = stored(&[DatabaseKind::Postgres, DatabaseKind::Sqlite]);
assert_eq!(
model.wire_query_database(),
Some(DatabaseKind::Sqlite),
"`databases()` walks `DatabaseKind::ALL`, so 'first' is that order and not the block's",
);
}
#[test]
fn the_unit_is_declared_into_the_scope_only_where_it_is_written() {
let named = |model: &crate::interface::InterfaceModel| {
model
.emitted_types()
.into_iter()
.any(|declared| declared.name == "MediaQueries")
};
assert!(named(&stored(&[DatabaseKind::Sqlite])));
assert!(!named(&unstored()));
}