pub struct FakeResolver { /* private fields */ }Expand description
In-memory test double for Resolver.
Returns canned results without any network access. Gated behind
cfg(test) and the test-util feature so it is available to downstream
tests but never compiled into production builds.
Lookups are keyed by the normalized query (crate::normalize::normalize)
so callers can register a canned identity under any of its aliases.
The call_count atomic counter increments on every call to
Resolver::resolve; read it with FakeResolver::call_count to assert
the resolver was invoked an exact number of times (useful for verifying
cache-first behaviour).
Implementations§
Source§impl FakeResolver
impl FakeResolver
Sourcepub fn new() -> Self
pub fn new() -> Self
Construct an empty fake; unknown queries return ResolveError::NotFound.
use simbad_resolver::{FakeResolver, ResolveError, Resolver};
let err = FakeResolver::new().resolve("anything").await.unwrap_err();
assert!(matches!(err, ResolveError::NotFound(_)));Sourcepub fn with_response(self, query: &str, identity: ResolvedIdentity) -> Self
pub fn with_response(self, query: &str, identity: ResolvedIdentity) -> Self
Register a canned successful identity, keyed by the normalized query.
use simbad_resolver::{
AliasKind, FakeResolver, ObjectType, ResolvedAlias, ResolvedIdentity, Resolver,
TargetSource,
};
let m31 = ResolvedIdentity {
simbad_oid: Some(1_575_544),
primary_designation: "M 31".to_owned(),
common_name: None,
object_type: ObjectType::Galaxy,
otype_raw: "G".to_owned(),
ra_deg: 10.684_708,
dec_deg: 41.268_75,
v_mag: Some(3.44),
aliases: vec![ResolvedAlias::new("M 31", AliasKind::Designation)],
source: TargetSource::Seed,
};
let resolver = FakeResolver::new().with_response("M31", m31);
// Registered under normalized "M31"; any equivalent form matches.
let got = resolver.resolve("M 31").await.unwrap();
assert_eq!(got.primary_designation, "M 31");Sourcepub fn with_error(self, query: &str, error: ResolveError) -> Self
pub fn with_error(self, query: &str, error: ResolveError) -> Self
Register a canned error for query, keyed by its normalized form.
use simbad_resolver::{FakeResolver, ResolveError, Resolver};
let resolver = FakeResolver::new()
.with_error("M31", ResolveError::Ambiguous { query: "M31".to_owned(), count: 2 });
let err = resolver.resolve("M 31").await.unwrap_err();
assert!(matches!(err, ResolveError::Ambiguous { count: 2, .. }));Sourcepub fn with_default_error(self, error: ResolveError) -> Self
pub fn with_default_error(self, error: ResolveError) -> Self
Set the error returned for any unregistered query (default:
ResolveError::NotFound). Use ResolveError::Network to simulate
an offline resolver for degrade-to-cache tests.
use simbad_resolver::{FakeResolver, ResolveError, Resolver};
let resolver =
FakeResolver::new().with_default_error(ResolveError::Network("down".to_owned()));
let err = resolver.resolve("anything").await.unwrap_err();
assert!(matches!(err, ResolveError::Network(_)));Sourcepub fn call_count(&self) -> usize
pub fn call_count(&self) -> usize
Return the number of times Resolver::resolve has been called.
Uses Relaxed ordering; suitable for single-threaded test assertions
after all async work has completed.
use simbad_resolver::{FakeResolver, Resolver};
let resolver = FakeResolver::new();
assert_eq!(resolver.call_count(), 0);
let _ = resolver.resolve("M 31").await;
assert_eq!(resolver.call_count(), 1);Trait Implementations§
Source§impl Clone for FakeResolver
Available on crate features test-util only.
impl Clone for FakeResolver
test-util only.Source§impl Debug for FakeResolver
impl Debug for FakeResolver
Source§impl Default for FakeResolver
impl Default for FakeResolver
Source§impl Resolver for FakeResolver
impl Resolver for FakeResolver
Source§fn resolve<'life0, 'life1, 'async_trait>(
&'life0 self,
query: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<ResolvedIdentity, ResolveError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn resolve<'life0, 'life1, 'async_trait>(
&'life0 self,
query: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<ResolvedIdentity, ResolveError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn resolve_object<'life0, 'life1, 'async_trait>(
&'life0 self,
object_raw: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<ResolvedIdentity, ResolveError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn resolve_object<'life0, 'life1, 'async_trait>(
&'life0 self,
object_raw: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<ResolvedIdentity, ResolveError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
OBJECT header value to a canonical identity. Read more