use serde_json::{json, Value};
use wiremock::matchers::query_param;
use wiremock::{Mock, MockServer, ResponseTemplate};
pub fn wikidata_siteinfo(server_uri: &str) -> Value {
let sparql_url = format!("{}/sparql", server_uri);
let conceptbaseuri = format!("{}/entity/", server_uri);
json!({
"batchcomplete": "",
"query": {
"general": {
"sitename": "Wikidata",
"dbname": "wikidatawiki",
"wikibase-conceptbaseuri": conceptbaseuri,
"wikibase-sparql": sparql_url
},
"namespaces": {
"0": {"id": 0, "case": "first-letter", "content": "", "*": ""},
"1": {"id": 1, "case": "first-letter", "*": "Talk", "canonical": "Talk"},
"-1": {"id": -1, "case": "first-letter", "*": "Special", "canonical": "Special"}
},
"namespacealiases": [],
"libraries": [],
"extensions": [],
"statistics": {}
}
})
}
pub async fn start_wikidata_mock() -> MockServer {
let server = MockServer::start().await;
let siteinfo = wikidata_siteinfo(&server.uri());
Mock::given(query_param("meta", "siteinfo"))
.respond_with(ResponseTemplate::new(200).set_body_json(siteinfo))
.mount(&server)
.await;
server
}
pub fn minimal_item(id: &str) -> Value {
json!({
"type": "item",
"id": id,
"labels": {},
"descriptions": {},
"aliases": {},
"sitelinks": {},
"claims": {}
})
}
pub fn missing_entity(id: &str) -> Value {
json!({"id": id, "missing": ""})
}
pub fn minimal_property(id: &str) -> Value {
json!({
"type": "property",
"id": id,
"labels": {},
"descriptions": {},
"aliases": {},
"claims": {},
"datatype": "string"
})
}
pub fn wbgetentities_response(entities: Vec<Value>) -> Value {
let mut map = serde_json::Map::new();
for entity in entities {
let id = entity["id"].as_str().unwrap().to_string();
map.insert(id, entity);
}
json!({"entities": Value::Object(map), "success": 1})
}
pub fn q12345_entity() -> Value {
json!({
"type": "item",
"id": "Q12345",
"labels": {"en": {"language": "en", "value": "Count von Count"}},
"descriptions": {"en": {"language": "en", "value": "character on Sesame Street"}},
"aliases": {"en": [{"language": "en", "value": "The Count"}]},
"sitelinks": {"enwiki": {"site": "enwiki", "title": "Count von Count", "badges": []}},
"claims": {
"P3478": [{
"mainsnak": {
"snaktype": "value",
"property": "P3478",
"datavalue": {"value": "850116", "type": "string"}
},
"type": "statement",
"rank": "normal"
}]
}
})
}
pub fn q42_entity() -> Value {
minimal_item("Q42")
}
pub fn l2_entity() -> Value {
json!({
"type": "lexeme",
"id": "L2",
"lemmas": {"en": {"language": "en", "value": "the"}},
"language": "Q1860",
"lexicalCategory": "Q1084",
"forms": [],
"senses": [],
"claims": {}
})
}
pub fn q1_to_q150_response() -> Value {
let missing_ids: std::collections::HashSet<usize> = (1..=14).map(|i| i * 10).collect();
let mut entities = serde_json::Map::new();
for n in 1..=150 {
let id = format!("Q{}", n);
let entity = if missing_ids.contains(&n) {
missing_entity(&id)
} else {
minimal_item(&id)
};
entities.insert(id, entity);
}
json!({"entities": Value::Object(entities), "success": 1})
}
pub fn q42_q12345_q50_response() -> Value {
wbgetentities_response(vec![
q42_entity(),
q12345_entity(),
missing_entity("Q50"),
])
}