Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
magnusmanske committed Dec 6, 2024
1 parent 94fe94b commit 8fd7edf
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 54 deletions.
11 changes: 4 additions & 7 deletions src/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,13 +506,13 @@ mod tests {
assert_eq!(
state
.site_matrix
.get_wiki_for_server_url(&"https://am.wiktionary.org".to_string()),
.get_wiki_for_server_url("https://am.wiktionary.org"),
Some("amwiktionary".to_string())
);
assert_eq!(
state
.site_matrix
.get_wiki_for_server_url(&"https://outreach.wikimedia.org".to_string()),
.get_wiki_for_server_url("https://outreach.wikimedia.org"),
Some("outreachwiki".to_string())
);
}
Expand All @@ -522,15 +522,12 @@ mod tests {
let state = get_state().await;
assert_eq!(
"enwiki_p".to_string(),
state
.db_host_and_schema_for_wiki(&"enwiki".to_string())
.unwrap()
.1
state.db_host_and_schema_for_wiki("enwiki").unwrap().1
);
assert_eq!(
"be_x_oldwiki_p".to_string(),
state
.db_host_and_schema_for_wiki(&"be-taraskwiki".to_string())
.db_host_and_schema_for_wiki("be-taraskwiki")
.unwrap()
.1
);
Expand Down
2 changes: 1 addition & 1 deletion src/datasource_sparql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl DataSource for SourceSparql {
.to_string();
}
" }, {" | " } ]" => match mode {
0 => header += &line,
0 => header += line,
1 => {
binding = "{".to_string() + &binding + "}";
let j: Value = serde_json::from_str(&binding).unwrap_or_else(|_| json!({}));
Expand Down
20 changes: 10 additions & 10 deletions src/form_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ impl FormParameters {
}

fn has_param(&self, key: &str) -> bool {
self.params.contains_key(&key.to_string())
self.params.contains_key(key)
}

fn has_param_with_value(&self, key: &str) -> bool {
match self.params.get(&key.to_string()) {
match self.params.get(key) {
Some(s) => !s.trim().is_empty(),
None => false,
}
Expand Down Expand Up @@ -215,8 +215,8 @@ mod tests {
fn test_has_param_with_value() {
let mut form_params = FormParameters::new();
form_params.set_param("test", "value");
assert_eq!(form_params.has_param_with_value("test"), true);
assert_eq!(form_params.has_param_with_value("test2"), false);
assert!(form_params.has_param_with_value("test"));
assert!(!form_params.has_param_with_value("test2"));
}

#[test]
Expand Down Expand Up @@ -244,7 +244,7 @@ mod tests {
#[test]
fn test_outcome_from_query() {
let form_params = FormParameters::outcome_from_query("test=value&test2=value2");
assert_eq!(form_params.is_ok(), true);
assert!(form_params.is_ok());
let form_params = form_params.unwrap();
assert_eq!(form_params.params.get("test"), Some(&"value".to_string()));
assert_eq!(form_params.params.get("test2"), Some(&"value2".to_string()));
Expand All @@ -254,8 +254,8 @@ mod tests {
fn test_has_param() {
let mut form_params = FormParameters::new();
form_params.set_param("test", "value");
assert_eq!(form_params.has_param("test"), true);
assert_eq!(form_params.has_param("test2"), false);
assert!(form_params.has_param("test"));
assert!(!form_params.has_param("test2"));
}

#[test]
Expand All @@ -272,9 +272,9 @@ mod tests {
params.insert("ns[1]".to_string(), "1".to_string());
params.insert("ns[2]".to_string(), "1".to_string());
let form_params = FormParameters::new_from_pairs(params);
assert_eq!(form_params.ns.contains(&0), true);
assert_eq!(form_params.ns.contains(&1), true);
assert_eq!(form_params.ns.contains(&2), true);
assert!(form_params.ns.contains(&0));
assert!(form_params.ns.contains(&1));
assert!(form_params.ns.contains(&2));
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions src/pagelist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,15 +801,15 @@ mod tests {
#[test]
fn page_list_sort() {
assert_eq!(
PageListSort::new_from_params(&"incoming_links".to_string(), true),
PageListSort::new_from_params("incoming_links", true),
PageListSort::IncomingLinks(true)
);
assert_eq!(
PageListSort::new_from_params(&"ns_title".to_string(), false),
PageListSort::new_from_params("ns_title", false),
PageListSort::NsTitle(false)
);
assert_eq!(
PageListSort::new_from_params(&"this is not a sort parameter".to_string(), true),
PageListSort::new_from_params("this is not a sort parameter", true),
PageListSort::Default(true)
);
}
Expand Down
15 changes: 5 additions & 10 deletions src/pagelist_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,20 +551,18 @@ mod tests {
fn file_usage() {
// 3 instead of 4 parts
assert_eq!(
FileUsage::new_from_part(&"the_wiki:7:12345:the_namespace_name".to_string()),
FileUsage::new_from_part("the_wiki:7:12345:the_namespace_name"),
None
);
// String instead of namespace ID
assert_eq!(
FileUsage::new_from_part(
&"the_wiki:the_namespace_id:the_page_id:the_namespace_name:The:page".to_string()
"the_wiki:the_namespace_id:the_page_id:the_namespace_name:The:page"
),
None
);
// This should work
let fu =
FileUsage::new_from_part(&"the_wiki:7:12345:the_namespace_name:The:page".to_string())
.unwrap();
let fu = FileUsage::new_from_part("the_wiki:7:12345:the_namespace_name:The:page").unwrap();
assert_eq!(fu.wiki(), "the_wiki");
assert_eq!(fu.namespace_name(), "the_namespace_name");
assert_eq!(*fu.title(), Title::new("The:page", 7));
Expand All @@ -573,12 +571,9 @@ mod tests {

#[test]
fn file_info() {
let fu =
FileUsage::new_from_part(&"the_wiki:7:12345:the_namespace_name:The:page".to_string())
.unwrap();
let fu = FileUsage::new_from_part("the_wiki:7:12345:the_namespace_name:The:page").unwrap();
let fi = FileInfo::new_from_gil_group(
&"|somesuch|the_wiki:7:12345:the_namespace_name:The:page|the_wiki:7:the_namespace_name"
.to_string(),
"|somesuch|the_wiki:7:12345:the_namespace_name:The:page|the_wiki:7:the_namespace_name",
);
assert_eq!(fi.file_usage, vec![fu]);
}
Expand Down
35 changes: 16 additions & 19 deletions src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::render_tsv::RenderTSV;
use crate::render_wikitext::RenderWiki;
use crate::wdfist::*;
use futures::future::join_all;
use my::Value::Bytes;
use mysql_async as my;
use mysql_async::from_row;
use mysql_async::prelude::Queryable;
Expand Down Expand Up @@ -767,13 +768,13 @@ impl Platform {
parts.remove(0); // page_namespace
if add_image {
entry.set_page_image(match parts.remove(0) {
my::Value::Bytes(s) => String::from_utf8(s).ok(),
Bytes(s) => String::from_utf8(s).ok(),
_ => None,
});
}
if add_coordinates {
let coordinates = match parts.remove(0) {
my::Value::Bytes(s) => match String::from_utf8(s) {
Bytes(s) => match String::from_utf8(s) {
Ok(lat_lon) => wikimisc::lat_lon::LatLon::from_str(&lat_lon).ok(),
_ => None,
},
Expand All @@ -783,7 +784,7 @@ impl Platform {
}
if add_defaultsort {
entry.set_defaultsort(match parts.remove(0) {
my::Value::Bytes(s) => String::from_utf8(s).ok(),
Bytes(s) => String::from_utf8(s).ok(),
_ => None,
});
}
Expand Down Expand Up @@ -1046,7 +1047,7 @@ impl Platform {
let full_page_title = match row.get(0) {
/* trunk-ignore(clippy/collapsible_match) */
Some(title) => match title {
my::Value::Bytes(uv) => match String::from_utf8(uv) {
Bytes(uv) => match String::from_utf8(uv) {
Ok(s) => s,
Err(_) => return,
},
Expand Down Expand Up @@ -1583,13 +1584,10 @@ impl Platform {

pub async fn get_response(&self) -> Result<MyResponse, String> {
// Shortcut: WDFIST
match &self.wdfist_result {
Some(j) => {
return Ok(self
.state
.output_json(j, self.form_parameters.params.get("callback")));
}
None => {}
if let Some(j) = &self.wdfist_result {
return Ok(self
.state
.output_json(j, self.form_parameters.params.get("callback")));
}

let result = match &self.result {
Expand Down Expand Up @@ -1870,7 +1868,7 @@ impl Platform {

/// Checks is the parameter is set, and non-blank
pub fn has_param(&self, param: &str) -> bool {
match self.form_parameters().params.get(&param.to_string()) {
match self.form_parameters().params.get(param) {
Some(s) => !s.is_empty(),
None => false,
}
Expand All @@ -1880,7 +1878,7 @@ impl Platform {
if self.has_param(param) {
self.form_parameters()
.params
.get(&param.to_string())
.get(param)
.map(|s| s.to_string())
} else {
None
Expand Down Expand Up @@ -2106,8 +2104,7 @@ mod tests {

#[tokio::test]
async fn test_parse_combination_string() {
let res =
Platform::parse_combination_string(&"categories NOT (sparql OR pagepile)".to_string());
let res = Platform::parse_combination_string("categories NOT (sparql OR pagepile)");
let expected = Combination::Not((
Box::new(Combination::Source("categories".to_string())),
Box::new(Combination::Union((
Expand Down Expand Up @@ -2178,7 +2175,7 @@ mod tests {
.cloned()
.collect::<Vec<PageListEntry>>();
assert_eq!(entries.len(), 1);
let entry = entries.get(0).unwrap();
let entry = entries.first().unwrap();
assert_eq!(entry.page_id(), Some(1340715));
let fi = entry.get_file_info();
assert!(fi.is_some());
Expand Down Expand Up @@ -2207,7 +2204,7 @@ mod tests {
.cloned()
.collect::<Vec<PageListEntry>>();
assert_eq!(entries.len(), 1);
let entry = entries.get(0).unwrap();
let entry = entries.first().unwrap();
assert_eq!(entry.page_id(), Some(36995));
assert!(entry.page_bytes().is_some());
assert!(entry.get_page_timestamp().is_some());
Expand All @@ -2233,7 +2230,7 @@ mod tests {
.cloned()
.collect::<Vec<PageListEntry>>();
assert_eq!(entries.len(), 1);
let entry = entries.get(0).unwrap();
let entry = entries.first().unwrap();
assert_eq!(entry.page_id(), Some(239794));
assert_eq!(entry.get_wikidata_item(), Some("Q12345".to_string()));
}
Expand Down Expand Up @@ -2270,7 +2267,7 @@ mod tests {
.cloned()
.collect::<Vec<PageListEntry>>();
assert_eq!(entries.len(), 1);
let entry = entries.get(0).unwrap();
let entry = entries.first().unwrap();
assert_eq!(entry.page_id(), Some(13925));
assert_eq!(entry.get_wikidata_label(), Some("Graaf Tel".to_string()));
assert_eq!(
Expand Down
7 changes: 3 additions & 4 deletions src/render_html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,8 @@ impl RenderHTML {
// TODO properties?
if is_page_link && wiki == "wikidatawiki" && title.namespace_id() == 0 {
ret += &format!("&nbsp;<small><tt>[{}]</tt></small>", title.pretty());
match &wikidata_description {
Some(desc) => ret += &format!("<div class='smaller'>{}</div>", &desc),
None => {}
if let Some(desc) = &wikidata_description {
ret += &format!("<div class='smaller'>{desc}</div>")
}
}
ret
Expand All @@ -387,7 +386,7 @@ impl RenderHTML {
ret += class_name;
ret += "'>";
}
ret += &item;
ret += item;
ret += "</td>";
}
ret += "</tr>";
Expand Down

0 comments on commit 8fd7edf

Please sign in to comment.