use super::ErrorCode;
const VOCABULARY: &[(ErrorCode, &str)] = &[
(ErrorCode::RepeatedOrderSubject, "REPEATED_ORDER_SUBJECT"),
(
ErrorCode::AmbiguousPageDirection,
"AMBIGUOUS_PAGE_DIRECTION",
),
(ErrorCode::UnusableCursor, "UNUSABLE_CURSOR"),
(ErrorCode::StoreRefused, "STORE_REFUSED"),
(ErrorCode::UnsupportedFilter, "UNSUPPORTED_FILTER"),
(ErrorCode::UnrenderableOrder, "UNRENDERABLE_ORDER"),
(ErrorCode::UnpageableOrder, "UNPAGEABLE_ORDER"),
(ErrorCode::UnreadableRow, "UNREADABLE_ROW"),
(ErrorCode::OversizedSkip, "OVERSIZED_SKIP"),
(ErrorCode::UnwrittenArtifact, "UNWRITTEN_ARTIFACT"),
(ErrorCode::UnusableAddress, "UNUSABLE_ADDRESS"),
(ErrorCode::UnreachableSource, "UNREACHABLE_SOURCE"),
(ErrorCode::UnreachableNode, "UNREACHABLE_NODE"),
(ErrorCode::RequestDropped, "REQUEST_DROPPED"),
(ErrorCode::RequestTimedOut, "REQUEST_TIMED_OUT"),
];
#[test]
fn each_code_renders_the_word_a_client_matches_on() {
for (code, word) in VOCABULARY {
assert_eq!(code.as_str(), *word);
assert_eq!(code.to_string(), *word, "the rendering is the word itself");
}
}
#[test]
fn no_two_codes_share_a_word() {
let mut seen: Vec<&str> = VOCABULARY.iter().map(|(_, word)| *word).collect();
let written = seen.len();
seen.sort_unstable();
seen.dedup();
assert_eq!(seen.len(), written, "each code has a word of its own");
}
#[test]
fn every_word_is_screaming_snake_case() {
for (_, word) in VOCABULARY {
assert!(
word
.bytes()
.all(|byte| byte.is_ascii_uppercase() || byte == b'_'),
"`{word}` is not SCREAMING_SNAKE_CASE"
);
assert!(!word.starts_with('_') && !word.ends_with('_'), "`{word}`");
}
}
#[test]
fn the_emitters_reserved_list_is_this_vocabulary() {
let mut words: Vec<&str> = VOCABULARY.iter().map(|(_, word)| *word).collect();
words.sort_unstable();
assert_eq!(
words,
ingraph_codegen::error::RESERVED,
"a word this crate publishes and the macro does not reserve is a word a declaration can steal"
);
}
#[test]
fn the_extension_key_is_the_one_the_ecosystem_reads() {
assert_eq!(ErrorCode::EXTENSION, "code");
}
#[test]
fn every_transport_refusal_answers_with_its_own_word() {
use crate::{QueryError, ToErrorCode as _};
for (refusal, word) in [
(QueryError::Unreachable, "UNREACHABLE_NODE"),
(QueryError::UnreachableSource, "UNREACHABLE_SOURCE"),
(QueryError::Dropped, "REQUEST_DROPPED"),
(QueryError::TimedOut, "REQUEST_TIMED_OUT"),
] {
assert_eq!(refusal.error_code().as_str(), word, "{refusal}");
}
}
#[test]
fn the_two_unreachables_name_different_far_ends() {
use crate::QueryError;
assert!(
QueryError::UnreachableSource
.to_string()
.contains("control line"),
"{}",
QueryError::UnreachableSource
);
assert!(
QueryError::Unreachable.to_string().contains("query node"),
"{}",
QueryError::Unreachable
);
assert_ne!(
QueryError::Unreachable.to_string(),
QueryError::UnreachableSource.to_string()
);
}
#[cfg(feature = "async-graphql")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, async_graphql::OneofObject)]
enum CueOrderBy {
Title(crate::OrderDirection),
}
#[cfg(feature = "async-graphql")]
impl crate::ToSubjectPath for CueOrderBy {
fn subject_path(&self) -> crate::SubjectPath {
const SUBJECT: crate::SubjectPath = crate::SubjectPath::new(&[crate::TermName::new("title")]);
SUBJECT
}
}
#[cfg(feature = "async-graphql")]
#[test]
fn the_repeated_subject_refusal_answers_with_its_own_word() {
use async_graphql::InputType;
use crate::{OrderDirection::Asc, Orderings};
let written = vec![CueOrderBy::Title(Asc), CueOrderBy::Title(Asc)];
let refusal = <Orderings<CueOrderBy> as InputType>::parse(Some(InputType::to_value(&written)))
.expect_err("the second term repeats the first");
let error = refusal.into_server_error(async_graphql::Pos::default());
assert_eq!(
word(error.extensions.as_ref()).as_deref(),
Some("REPEATED_ORDER_SUBJECT"),
"the code rides in `extensions`, beside a message nothing matches on"
);
}
#[cfg(feature = "async-graphql")]
fn word(extensions: Option<&async_graphql::ErrorExtensionValues>) -> Option<String> {
match extensions?.get(ErrorCode::EXTENSION)? {
async_graphql::Value::String(word) => Some(word.clone()),
_ => None,
}
}
#[cfg(feature = "async-graphql")]
#[test]
fn a_refusal_reaches_the_wire_as_a_sentence_and_a_word() {
struct Refused;
impl core::fmt::Display for Refused {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("a sentence for a person")
}
}
impl super::ToErrorCode for Refused {
fn error_code(&self) -> ErrorCode {
ErrorCode::RepeatedOrderSubject
}
}
let error = super::refused(&Refused);
assert_eq!(error.message, "a sentence for a person");
assert_eq!(
word(error.extensions.as_ref()).as_deref(),
Some("REPEATED_ORDER_SUBJECT"),
);
}