//! Cotton's published `places` database as a test hears it: one synthetic //! place (an invented-for-tests row, Hyderabad) served by a fake reader //! installed for the process (`served`). A place column or a location pin //! reads it the way the app reads the server. use std::sync::Arc; use cotton_store::published::{self, Answer, Ask, Reader, Unread}; use serde_json::{json, Value}; /// The version every synthetic answer is read at. pub const VERSION: &str = "synthetic-1"; struct Synthetic; fn hyderabad() -> Value { json!({"id": "geonames:1269843", "name": "Hyderabad", "admin1": "40", "country": "IN", "country_name": "India", "zone": "Asia/Kolkata", "population": 6809970, "lat": 17.38, "lng": 78.48}) } impl Reader for Synthetic { fn read(&self, publisher: &str, name: &str, table: &str, ask: &Ask) -> Result { let rows = match (publisher, name, table, ask) { ("cotton", "places", "places", Ask::Ids { ids, .. }) => ids.iter().filter(|id| *id == "geonames:1269843").map(|_| hyderabad()).collect(), ("cotton", "places", "places", Ask::Near { .. }) => vec![hyderabad()], ("cotton", "places", "places", Ask::Search { q, .. }) => { if "hyderabad".starts_with(&q.to_lowercase()) { vec![hyderabad()] } else { vec![] } } _ => vec![], }; Ok(Answer { version: VERSION.into(), rows }) } } /// Installed once per test process and never removed, as the app's reader is /// while signed in: every harness reads the same synthetic server. pub fn served() { published::install(Some(Arc::new(Synthetic))); }