Use insta for testing.

This commit is contained in:
2019-02-06 09:24:42 +01:00
parent 5a1d428c22
commit 8b1cc276d6
10 changed files with 714 additions and 702 deletions

617
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -10,6 +10,7 @@ chrono = { version = "0.4", features = ["serde"] }
chrono-tz = "0.5" chrono-tz = "0.5"
directories = "1.0" directories = "1.0"
fern = { version = "0.5", features = ["colored"] } fern = { version = "0.5", features = ["colored"] }
htmlescape = "0.3"
log = "0.4" log = "0.4"
regex = "1.1" regex = "1.1"
reqwest = "0.9" reqwest = "0.9"
@@ -17,9 +18,8 @@ scraper = "0.9"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
structopt = "0.2" structopt = "0.2"
unhtml = "0.4" unhtml = "0.5"
unhtml_derive = "0.4" unhtml_derive = "0.5"
htmlescape = "0.3"
[dev-dependencies] [dev-dependencies]
pretty_assertions = "0.5" insta = "0.6"

View File

@@ -18,9 +18,4 @@ Almost everything. I will add stuff when I need stuff. But hey, if you found thi
- [ ] List cache entries - [ ] List cache entries
- [ ] Clear cache entries - [ ] Clear cache entries
- [ ] Add some nice colors, so it's easier to read the output. - [ ] Add some nice colors, so it's easier to read the output.
- [ ] Add tests for probes. - [x] Add tests for probes.
## Unify
* Comments
* Number of searches the latest 24 hours

View File

@@ -78,8 +78,8 @@ fn main() {
let mut ctx = Context::new(); let mut ctx = Context::new();
for probe in &mut probes { for probe in &mut probes {
if probe.search(&mut ctx, &opt.number).is_ok() { if let Ok(entry) = probe.search(&mut ctx, &opt.number) {
println!(); print!("{}", entry);
} }
} }
} }

View File

@@ -1,6 +1,7 @@
use std::fmt; use std::fmt;
use chrono::{DateTime, Local, Utc}; use chrono::{DateTime, Local, Utc};
use serde::Serialize;
mod eniro; mod eniro;
mod hitta; mod hitta;
@@ -8,15 +9,15 @@ mod konsument_info;
mod telefonforsaljare; mod telefonforsaljare;
mod vem_ringde; mod vem_ringde;
use crate::context::Context;
pub use self::eniro::Eniro; pub use self::eniro::Eniro;
pub use self::hitta::Hitta; pub use self::hitta::Hitta;
pub use self::konsument_info::KonsumentInfo; pub use self::konsument_info::KonsumentInfo;
pub use self::telefonforsaljare::Telefonforsaljare; pub use self::telefonforsaljare::Telefonforsaljare;
pub use self::vem_ringde::VemRingde; pub use self::vem_ringde::VemRingde;
use crate::context::Context; #[derive(Debug, PartialEq, Serialize)]
#[derive(Debug, PartialEq)]
pub struct Entry { pub struct Entry {
pub messages: Vec<String>, pub messages: Vec<String>,
pub history: Vec<String>, pub history: Vec<String>,
@@ -47,7 +48,7 @@ impl fmt::Display for Entry {
} }
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Serialize)]
pub struct Comment { pub struct Comment {
pub datetime: DateTime<Utc>, pub datetime: DateTime<Utc>,
pub title: Option<String>, pub title: Option<String>,
@@ -79,5 +80,5 @@ impl fmt::Display for Comment {
pub trait Probe { pub trait Probe {
fn uri(&self, _: &str) -> String; fn uri(&self, _: &str) -> String;
fn search(&mut self, _: &mut Context, _: &str) -> Result<(), ()>; fn search(&mut self, _: &mut Context, _: &str) -> Result<Entry, ()>;
} }

View File

@@ -48,7 +48,7 @@ impl Probe for Eniro {
format!("https://gulasidorna.eniro.se/hitta:{}", number) format!("https://gulasidorna.eniro.se/hitta:{}", number)
} }
fn search(&mut self, ctx: &mut Context, number: &str) -> Result<(), ()> { fn search(&mut self, ctx: &mut Context, number: &str) -> Result<Entry, ()> {
let body = if let Some(cache) = ctx.cache_get("eniro", &number) { let body = if let Some(cache) = ctx.cache_get("eniro", &number) {
String::from_utf8(cache.data).unwrap() String::from_utf8(cache.data).unwrap()
} else { } else {
@@ -60,21 +60,13 @@ impl Probe for Eniro {
body body
}; };
match from_html(&body) { from_html(&body)
Ok(entry) => {
println!("eniro.se:");
print!("{}", entry);
Ok(())
}
Err(_) => Err(()),
}
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use pretty_assertions::assert_eq; use insta::assert_yaml_snapshot_matches;
use super::*; use super::*;
@@ -82,93 +74,76 @@ mod tests {
fn test_0104754350() { fn test_0104754350() {
let document = include_str!("../../fixtures/eniro/0104754350.html"); let document = include_str!("../../fixtures/eniro/0104754350.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![ messages:
"Företaget bedriver telefonförsäljning eller marknadsundersökningar" - Företaget bedriver telefonförsäljning eller marknadsundersökningar
.to_string(), history: []
], comments: []"###);
history: vec![],
comments: vec![],
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0313908905() { fn test_0313908905() {
let document = include_str!("../../fixtures/eniro/0313908905.html"); let document = include_str!("../../fixtures/eniro/0313908905.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec!["3464 denna vecka och 6637 totalt.".to_string()], history:
comments: vec![], - 3464 denna vecka och 6637 totalt.
}; comments: []"###);
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0702269893() { fn test_0702269893() {
let document = include_str!("../../fixtures/eniro/0702269893.html"); let document = include_str!("../../fixtures/eniro/0702269893.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec!["Anonym Kund För Refill".to_string()], messages:
history: vec![], - Anonym Kund För Refill
comments: vec![], history: []
}; comments: []"###);
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0726443387() { fn test_0726443387() {
let document = include_str!("../../fixtures/eniro/0726443387.html"); let document = include_str!("../../fixtures/eniro/0726443387.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec!["16 denna vecka och 98 totalt.".to_string()], history:
comments: vec![], - 16 denna vecka och 98 totalt.
}; comments: []"###);
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0751793426() { fn test_0751793426() {
let document = include_str!("../../fixtures/eniro/0751793426.html"); let document = include_str!("../../fixtures/eniro/0751793426.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec!["20 denna vecka och 602 totalt.".to_string()], history:
comments: vec![], - 20 denna vecka och 602 totalt.
}; comments: []"###);
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0751793483() { fn test_0751793483() {
let document = include_str!("../../fixtures/eniro/0751793483.html"); let document = include_str!("../../fixtures/eniro/0751793483.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec!["29 denna vecka och 900 totalt.".to_string()], history:
comments: vec![], - 29 denna vecka och 900 totalt.
}; comments: []"###);
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0751793499() { fn test_0751793499() {
let document = include_str!("../../fixtures/eniro/0751793499.html"); let document = include_str!("../../fixtures/eniro/0751793499.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec!["303 denna vecka och 304 totalt.".to_string()], history:
comments: vec![], - 303 denna vecka och 304 totalt.
}; comments: []"###);
assert_eq!(from_html(&document), Ok(expected));
} }
} }

View File

@@ -101,7 +101,7 @@ impl Probe for Hitta {
format!("https://www.hitta.se/vem-ringde/{}", number) format!("https://www.hitta.se/vem-ringde/{}", number)
} }
fn search(&mut self, ctx: &mut Context, number: &str) -> Result<(), ()> { fn search(&mut self, ctx: &mut Context, number: &str) -> Result<Entry, ()> {
let body = if let Some(cache) = ctx.cache_get("hitta", &number) { let body = if let Some(cache) = ctx.cache_get("hitta", &number) {
String::from_utf8(cache.data).unwrap() String::from_utf8(cache.data).unwrap()
} else { } else {
@@ -113,22 +113,13 @@ impl Probe for Hitta {
body body
}; };
match from_html(&body) { from_html(&body)
Ok(entry) => {
println!("hitta.se:");
print!("{}", entry);
Ok(())
}
Err(_) => Err(()),
}
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use chrono::DateTime; use insta::assert_yaml_snapshot_matches;
use pretty_assertions::assert_eq;
use super::*; use super::*;
@@ -136,236 +127,159 @@ mod tests {
fn test_0104754350() { fn test_0104754350() {
let document = include_str!("../../fixtures/hitta/0104754350.html"); let document = include_str!("../../fixtures/hitta/0104754350.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec!["42 andra har rapporterat detta nummer".to_string()], history:
comments: vec![ - 42 andra har rapporterat detta nummer
probe::Comment { comments:
datetime: "2019-01-17T17:29:22Z".parse::<DateTime<Utc>>().unwrap(), - datetime: "2019-01-17T17:29:22Z"
title: None, title: ~
message: "Varmsälj från Folksam".to_string(), message: Varmsälj från Folksam
}, - datetime: "2018-12-14T13:45:28Z"
probe::Comment { title: ~
datetime: "2018-12-14T13:45:28Z".parse::<DateTime<Utc>>().unwrap(), message: Folksam
title: None, - datetime: "2018-11-28T07:30:18Z"
message: "Folksam".to_string(), title: ~
}, message: Höglandschskt
probe::Comment { - datetime: "2018-11-20T19:18:09Z"
datetime: "2018-11-28T07:30:18Z".parse::<DateTime<Utc>>().unwrap(), title: ~
title: None, message: "Försäljare "
message: "Höglandschskt".to_string(), - datetime: "2018-11-19T17:38:34Z"
}, title: ~
probe::Comment { message: mögg från Folksam
datetime: "2018-11-20T19:18:09Z".parse::<DateTime<Utc>>().unwrap(), - datetime: "2018-11-12T16:00:41Z"
title: None, title: ~
message: "Försäljare ".to_string(), message: Folksam försäkringsförsäljare
}, - datetime: "2018-10-25T10:28:36Z"
probe::Comment { title: ~
datetime: "2018-11-19T17:38:34Z".parse::<DateTime<Utc>>().unwrap(), message: folksam
title: None, - datetime: "2018-10-10T07:30:40Z"
message: "mögg från Folksam".to_string(), title: ~
}, message: Telefonförsäljare
probe::Comment { - datetime: "2018-10-04T10:04:55Z"
datetime: "2018-11-12T16:00:41Z".parse::<DateTime<Utc>>().unwrap(), title: ~
title: None, message: Folksam säljare
message: "Folksam försäkringsförsäljare".to_string(), - datetime: "2018-10-03T13:55:19Z"
}, title: ~
probe::Comment { message: Sa inget.
datetime: "2018-10-25T10:28:36Z".parse::<DateTime<Utc>>().unwrap(), - datetime: "2018-08-24T16:56:46Z"
title: None, title: ~
message: "folksam".to_string(), message: Folksam
}, - datetime: "2018-08-24T09:42:43Z"
probe::Comment { title: ~
datetime: "2018-10-10T07:30:40Z".parse::<DateTime<Utc>>().unwrap(), message: Achmati azmut från folksam
title: None, - datetime: "2018-08-21T18:29:29Z"
message: "Telefonförsäljare".to_string(), title: ~
}, message: Folksam
probe::Comment { - datetime: "2018-08-16T18:56:56Z"
datetime: "2018-10-04T10:04:55Z".parse::<DateTime<Utc>>().unwrap(), title: ~
title: None, message: Säljare från Folksam.
message: "Folksam säljare".to_string(), - datetime: "2018-08-16T14:48:59Z"
}, title: ~
probe::Comment { message: "Folksam "
datetime: "2018-10-03T13:55:19Z".parse::<DateTime<Utc>>().unwrap(), - datetime: "2018-08-09T16:30:28Z"
title: None, title: ~
message: "Sa inget.".to_string(), message: Folksam
}, - datetime: "2018-08-02T16:29:32Z"
probe::Comment { title: ~
datetime: "2018-08-24T16:56:46Z".parse::<DateTime<Utc>>().unwrap(), message: "Folksam "
title: None, - datetime: "2018-08-02T15:33:38Z"
message: "Folksam".to_string(), title: ~
}, message: "Folksam "
probe::Comment { - datetime: "2018-07-25T08:28:27Z"
datetime: "2018-08-24T09:42:43Z".parse::<DateTime<Utc>>().unwrap(), title: ~
title: None, message: Säljare Folksam
message: "Achmati azmut från folksam".to_string(), - datetime: "2018-07-17T21:20:51Z"
}, title: ~
probe::Comment { message: "Inga Hansson "
datetime: "2018-08-21T18:29:29Z".parse::<DateTime<Utc>>().unwrap(), - datetime: "2018-07-16T18:11:46Z"
title: None, title: ~
message: "Folksam".to_string(), message: Folksam
}, - datetime: "2018-07-06T15:45:46Z"
probe::Comment { title: ~
datetime: "2018-08-16T18:56:56Z".parse::<DateTime<Utc>>().unwrap(), message: "Folksam "
title: None, - datetime: "2018-07-05T17:24:07Z"
message: "Säljare från Folksam.".to_string(), title: ~
}, message: folksam
probe::Comment { - datetime: "2018-07-05T11:15:02Z"
datetime: "2018-08-16T14:48:59Z".parse::<DateTime<Utc>>().unwrap(), title: ~
title: None, message: Vesran
message: "Folksam ".to_string(), - datetime: "2018-07-04T13:30:49Z"
}, title: ~
probe::Comment { message: Folksam
datetime: "2018-08-09T16:30:28Z".parse::<DateTime<Utc>>().unwrap(), - datetime: "2018-06-29T10:52:51Z"
title: None, title: ~
message: "Folksam".to_string(), message: folksam
}, - datetime: "2018-06-28T13:33:01Z"
probe::Comment { title: ~
datetime: "2018-08-02T16:29:32Z".parse::<DateTime<Utc>>().unwrap(), message: Säljare folksam
title: None, - datetime: "2018-06-28T07:42:42Z"
message: "Folksam ".to_string(), title: ~
}, message: Folksam försäkringar
probe::Comment { - datetime: "2018-06-26T12:59:33Z"
datetime: "2018-08-02T15:33:38Z".parse::<DateTime<Utc>>().unwrap(), title: ~
title: None, message: Säljare Folksam"###);
message: "Folksam ".to_string(),
},
probe::Comment {
datetime: "2018-07-25T08:28:27Z".parse::<DateTime<Utc>>().unwrap(),
title: None,
message: "Säljare Folksam".to_string(),
},
probe::Comment {
datetime: "2018-07-17T21:20:51Z".parse::<DateTime<Utc>>().unwrap(),
title: None,
message: "Inga Hansson ".to_string(),
},
probe::Comment {
datetime: "2018-07-16T18:11:46Z".parse::<DateTime<Utc>>().unwrap(),
title: None,
message: "Folksam".to_string(),
},
probe::Comment {
datetime: "2018-07-06T15:45:46Z".parse::<DateTime<Utc>>().unwrap(),
title: None,
message: "Folksam ".to_string(),
},
probe::Comment {
datetime: "2018-07-05T17:24:07Z".parse::<DateTime<Utc>>().unwrap(),
title: None,
message: "folksam".to_string(),
},
probe::Comment {
datetime: "2018-07-05T11:15:02Z".parse::<DateTime<Utc>>().unwrap(),
title: None,
message: "Vesran".to_string(),
},
probe::Comment {
datetime: "2018-07-04T13:30:49Z".parse::<DateTime<Utc>>().unwrap(),
title: None,
message: "Folksam".to_string(),
},
probe::Comment {
datetime: "2018-06-29T10:52:51Z".parse::<DateTime<Utc>>().unwrap(),
title: None,
message: "folksam".to_string(),
},
probe::Comment {
datetime: "2018-06-28T13:33:01Z".parse::<DateTime<Utc>>().unwrap(),
title: None,
message: "Säljare folksam".to_string(),
},
probe::Comment {
datetime: "2018-06-28T07:42:42Z".parse::<DateTime<Utc>>().unwrap(),
title: None,
message: "Folksam försäkringar".to_string(),
},
probe::Comment {
datetime: "2018-06-26T12:59:33Z".parse::<DateTime<Utc>>().unwrap(),
title: None,
message: "Säljare Folksam".to_string(),
},
],
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0313908905() { fn test_0313908905() {
let document = include_str!("../../fixtures/hitta/0313908905.html"); let document = include_str!("../../fixtures/hitta/0313908905.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec![], history: []
comments: vec![], comments: []"###);
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0702269893() { fn test_0702269893() {
let document = include_str!("../../fixtures/hitta/0702269893.html"); let document = include_str!("../../fixtures/hitta/0702269893.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec!["Tre andra har också sökt på detta nummer".to_string()], history:
comments: vec![], - Tre andra har också sökt på detta nummer
}; comments: []"###);
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0726443387() { fn test_0726443387() {
let document = include_str!("../../fixtures/hitta/0726443387.html"); let document = include_str!("../../fixtures/hitta/0726443387.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec!["1299 andra har också sökt på detta nummer".to_string()], history:
comments: vec![], - 1299 andra har också sökt på detta nummer
}; comments: []"###);
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0751793426() { fn test_0751793426() {
let document = include_str!("../../fixtures/hitta/0751793426.html"); let document = include_str!("../../fixtures/hitta/0751793426.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec![], history: []
comments: vec![], comments: []"###);
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0751793483() { fn test_0751793483() {
let document = include_str!("../../fixtures/hitta/0751793483.html"); let document = include_str!("../../fixtures/hitta/0751793483.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec![], history: []
comments: vec![], comments: []"###);
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0751793499() { fn test_0751793499() {
let document = include_str!("../../fixtures/hitta/0751793499.html"); let document = include_str!("../../fixtures/hitta/0751793499.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec![], history: []
comments: vec![], comments: []"###);
};
assert_eq!(from_html(&document), Ok(expected));
} }
} }

View File

@@ -37,7 +37,7 @@ impl Probe for KonsumentInfo {
format!("http://konsumentinfo.se/telefonnummer/sverige/{}", number) format!("http://konsumentinfo.se/telefonnummer/sverige/{}", number)
} }
fn search(&mut self, ctx: &mut Context, number: &str) -> Result<(), ()> { fn search(&mut self, ctx: &mut Context, number: &str) -> Result<Entry, ()> {
let body = if let Some(cache) = ctx.cache_get("konsument_info", &number) { let body = if let Some(cache) = ctx.cache_get("konsument_info", &number) {
String::from_utf8(cache.data).unwrap() String::from_utf8(cache.data).unwrap()
} else { } else {
@@ -49,21 +49,13 @@ impl Probe for KonsumentInfo {
body body
}; };
match from_html(&body) { from_html(&body)
Ok(entry) => {
println!("konsumentinfo.se:");
print!("{}", entry);
Ok(())
}
Err(_) => Err(()),
}
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use pretty_assertions::assert_eq; use insta::assert_yaml_snapshot_matches;
use super::*; use super::*;
@@ -71,90 +63,52 @@ mod tests {
fn test_0104754350() { fn test_0104754350() {
let document = include_str!("../../fixtures/konsumentinfo/0104754350.html"); let document = include_str!("../../fixtures/konsumentinfo/0104754350.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @"Err: ~");
messages: vec![],
history: vec![],
comments: vec![],
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0313908905() { fn test_0313908905() {
let document = include_str!("../../fixtures/konsumentinfo/0313908905.html"); let document = include_str!("../../fixtures/konsumentinfo/0313908905.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @"Err: ~");
messages: vec![],
history: vec![],
comments: vec![],
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0702269893() { fn test_0702269893() {
let document = include_str!("../../fixtures/konsumentinfo/0702269893.html"); let document = include_str!("../../fixtures/konsumentinfo/0702269893.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec!["Hydroscand AB".to_string()], messages:
history: vec![], - Hydroscand AB
comments: vec![], history: []
}; comments: []"###);
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0726443387() { fn test_0726443387() {
let document = include_str!("../../fixtures/konsumentinfo/0726443387.html"); let document = include_str!("../../fixtures/konsumentinfo/0726443387.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @"Err: ~");
messages: vec![],
history: vec![],
comments: vec![],
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0751793426() { fn test_0751793426() {
let document = include_str!("../../fixtures/konsumentinfo/0751793426.html"); let document = include_str!("../../fixtures/konsumentinfo/0751793426.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @"Err: ~");
messages: vec![],
history: vec![],
comments: vec![],
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0751793483() { fn test_0751793483() {
let document = include_str!("../../fixtures/konsumentinfo/0751793483.html"); let document = include_str!("../../fixtures/konsumentinfo/0751793483.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @"Err: ~");
messages: vec![],
history: vec![],
comments: vec![],
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0751793499() { fn test_0751793499() {
let document = include_str!("../../fixtures/konsumentinfo/0751793499.html"); let document = include_str!("../../fixtures/konsumentinfo/0751793499.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @"Err: ~");
messages: vec![],
history: vec![],
comments: vec![],
};
assert_eq!(from_html(&document), Ok(expected));
} }
} }

View File

@@ -1,7 +1,6 @@
use chrono::offset::LocalResult; use chrono::offset::LocalResult;
use chrono::{DateTime, NaiveDateTime, TimeZone, Utc}; use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};
use chrono_tz::Europe::Stockholm; use chrono_tz::Europe::Stockholm;
use log::debug;
use scraper::{Html, Selector}; use scraper::{Html, Selector};
use crate::context::Context; use crate::context::Context;
@@ -98,7 +97,7 @@ impl Probe for Telefonforsaljare {
format!("http://www.telefonforsaljare.nu/telefonnummer/{}/", number) format!("http://www.telefonforsaljare.nu/telefonnummer/{}/", number)
} }
fn search(&mut self, ctx: &mut Context, number: &str) -> Result<(), ()> { fn search(&mut self, ctx: &mut Context, number: &str) -> Result<Entry, ()> {
let body = if let Some(cache) = ctx.cache_get("telefonforsaljare", &number) { let body = if let Some(cache) = ctx.cache_get("telefonforsaljare", &number) {
String::from_utf8(cache.data).unwrap() String::from_utf8(cache.data).unwrap()
} else { } else {
@@ -110,26 +109,13 @@ impl Probe for Telefonforsaljare {
body body
}; };
println!("telefonforsaljare.nu:"); from_html(&body)
match from_html(&body) {
Ok(entry) => {
print!("{}", entry);
}
Err(_) => {
debug!("telefonforsaljare: failed to parse page");
println!(" Failed to find any data");
}
}
Ok(())
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use pretty_assertions::assert_eq; use insta::assert_yaml_snapshot_matches;
use super::*; use super::*;
@@ -137,133 +123,103 @@ mod tests {
fn test_0104754350() { fn test_0104754350() {
let document = include_str!("../../fixtures/telefonforsaljare/0104754350.html"); let document = include_str!("../../fixtures/telefonforsaljare/0104754350.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec!["Folksam".to_string()], messages:
history: vec!["De senaste 24 timmarna har 9 personer sökt efter numret 0104754350. Det kan tyda på att numret används av telefonförsäljare. Totalt har minst 4786 personer sökt efter numret.".to_string()], - Folksam
comments: vec![ history:
Comment { - De senaste 24 timmarna har 9 personer sökt efter numret 0104754350. Det kan tyda på att numret används av telefonförsäljare. Totalt har minst 4786 personer sökt efter numret.
datetime: "2018-05-09T12:31:39Z".parse::<DateTime<Utc>>().unwrap(), comments:
title: Some("Folksam".to_string()), - datetime: "2018-05-09T12:31:39Z"
message: "Svara inte på okända nummer. Blockerat!".to_string(), title: Folksam
}, message: Svara inte på okända nummer. Blockerat!
Comment { - datetime: "2017-12-05T16:33:10Z"
datetime: "2017-12-05T16:33:10Z".parse::<DateTime<Utc>>().unwrap(), title: Folksam
title: Some("Folksam".to_string()), message: Svarade aldrig men när jag ringde upp var det Folksam
message: "Svarade aldrig men när jag ringde upp var det Folksam".to_string(), - datetime: "2017-11-28T10:30:10Z"
}, title: ~
Comment { message: Ringde och la på
datetime: "2017-11-28T10:30:10Z".parse::<DateTime<Utc>>().unwrap(), - datetime: "2017-11-20T14:53:16Z"
title: None, title: Folksam
message: "Ringde och la på".to_string(), message: färsäljare
}, - datetime: "2017-11-16T12:38:07Z"
Comment { title: Folksam
datetime: "2017-11-20T14:53:16Z".parse::<DateTime<Utc>>().unwrap(), message: "missat samtal, ringde tillbaka och automatsvar sa att det var folksam som sökt mig för att presentera ett erbjudande."
title: Some("Folksam".to_string()), - datetime: "2017-10-25T05:59:26Z"
message: "färsäljare".to_string(), title: Folksam
}, message: Försäljare"###);
Comment {
datetime: "2017-11-16T12:38:07Z".parse::<DateTime<Utc>>().unwrap(),
title: Some("Folksam".to_string()),
message: "missat samtal, ringde tillbaka och automatsvar sa att det var folksam som sökt mig för att presentera ett erbjudande.".to_string(),
},
Comment {
datetime: "2017-10-25T05:59:26Z".parse::<DateTime<Utc>>().unwrap(),
title: Some("Folksam".to_string()),
message: "Försäljare".to_string(),
},
],
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0313908905() { fn test_0313908905() {
let document = include_str!("../../fixtures/telefonforsaljare/0313908905.html"); let document = include_str!("../../fixtures/telefonforsaljare/0313908905.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec!["Du är den första de senaste 24 timmarna som söker efter detta nummer. Det tyder på att numret inte används av telefonförsäljare. Totalt har minst 301 personer sökt efter numret.".to_string()], history:
comments: vec![], - Du är den första de senaste 24 timmarna som söker efter detta nummer. Det tyder på att numret inte används av telefonförsäljare. Totalt har minst 301 personer sökt efter numret.
}; comments: []"###);
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0702269893() { fn test_0702269893() {
let document = include_str!("../../fixtures/telefonforsaljare/0702269893.html"); let document = include_str!("../../fixtures/telefonforsaljare/0702269893.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec!["Alnö Design & Produktion AB".to_string()], messages:
history: vec!["De senaste 24 timmarna har 3 personer sökt efter numret 0702269893. Det kan tyda på att numret används av telefonförsäljare. Totalt har minst 4 personer sökt efter numret.".to_string()], - Alnö Design & Produktion AB
comments: vec![ history:
Comment { - De senaste 24 timmarna har 3 personer sökt efter numret 0702269893. Det kan tyda på att numret används av telefonförsäljare. Totalt har minst 4 personer sökt efter numret.
datetime: "2019-01-18T13:30:55Z".parse::<DateTime<Utc>>().unwrap(), comments:
title: Some("Alnö Design & Produktion AB".to_string()), - datetime: "2019-01-18T13:30:55Z"
message: "Renhållning, service, kemprodukter".to_string(), title: Alnö Design & Produktion AB
} message: "Renhållning, service, kemprodukter""###);
],
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0726443387() { fn test_0726443387() {
let document = include_str!("../../fixtures/telefonforsaljare/0726443387.html"); let document = include_str!("../../fixtures/telefonforsaljare/0726443387.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec!["Tele2".to_string()], messages:
history: vec!["De senaste 24 timmarna har 1 personer sökt efter numret 0726443387. Det kan tyda på att numret används av telefonförsäljare. Totalt har minst 231 personer sökt efter numret.".to_string()], - Tele2
comments: vec![ history:
Comment { - De senaste 24 timmarna har 1 personer sökt efter numret 0726443387. Det kan tyda på att numret används av telefonförsäljare. Totalt har minst 231 personer sökt efter numret.
datetime: "2018-10-31T17:48:27Z".parse::<DateTime<Utc>>().unwrap(), comments:
title: Some("Tele2".to_string()), - datetime: "2018-10-31T17:48:27Z"
message: "Bättre priser som inte finns online".to_string(), title: Tele2
}, message: Bättre priser som inte finns online"###);
],
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0751793426() { fn test_0751793426() {
let document = include_str!("../../fixtures/telefonforsaljare/0751793426.html"); let document = include_str!("../../fixtures/telefonforsaljare/0751793426.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec!["Du är den första de senaste 24 timmarna som söker efter detta nummer. Det tyder på att numret inte används av telefonförsäljare. Totalt har minst 38 personer sökt efter numret.".to_string()], history:
comments: vec![], - Du är den första de senaste 24 timmarna som söker efter detta nummer. Det tyder på att numret inte används av telefonförsäljare. Totalt har minst 38 personer sökt efter numret.
}; comments: []"###);
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0751793483() { fn test_0751793483() {
let document = include_str!("../../fixtures/telefonforsaljare/0751793483.html"); let document = include_str!("../../fixtures/telefonforsaljare/0751793483.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec!["Du är den första de senaste 24 timmarna som söker efter detta nummer. Det tyder på att numret inte används av telefonförsäljare. Totalt har minst 25 personer sökt efter numret.".to_string()], history:
comments: vec![], - Du är den första de senaste 24 timmarna som söker efter detta nummer. Det tyder på att numret inte används av telefonförsäljare. Totalt har minst 25 personer sökt efter numret.
}; comments: []"###);
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0751793499() { fn test_0751793499() {
let document = include_str!("../../fixtures/telefonforsaljare/0751793499.html"); let document = include_str!("../../fixtures/telefonforsaljare/0751793499.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec!["Du är den första de senaste 24 timmarna som söker efter detta nummer. Det tyder på att numret inte används av telefonförsäljare. Totalt har minst 22 personer sökt efter numret.".to_string()], history:
comments: vec![], - Du är den första de senaste 24 timmarna som söker efter detta nummer. Det tyder på att numret inte används av telefonförsäljare. Totalt har minst 22 personer sökt efter numret.
}; comments: []"###);
assert_eq!(from_html(&document), Ok(expected));
} }
} }

View File

@@ -40,7 +40,7 @@ impl Probe for VemRingde {
format!("http://vemringde.se/?q={}", number) format!("http://vemringde.se/?q={}", number)
} }
fn search(&mut self, ctx: &mut Context, number: &str) -> Result<(), ()> { fn search(&mut self, ctx: &mut Context, number: &str) -> Result<Entry, ()> {
let body = if let Some(cache) = ctx.cache_get("vem_ringde", &number) { let body = if let Some(cache) = ctx.cache_get("vem_ringde", &number) {
String::from_utf8(cache.data).unwrap() String::from_utf8(cache.data).unwrap()
} else { } else {
@@ -52,21 +52,13 @@ impl Probe for VemRingde {
body body
}; };
match from_html(&body) { from_html(&body)
Ok(entry) => {
println!("vemringde.se:");
print!("{}", entry);
Ok(())
}
Err(_) => Err(()),
}
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use pretty_assertions::assert_eq; use insta::assert_yaml_snapshot_matches;
use super::*; use super::*;
@@ -74,90 +66,70 @@ mod tests {
fn test_0104754350() { fn test_0104754350() {
let document = include_str!("../../fixtures/vemringde/0104754350.html"); let document = include_str!("../../fixtures/vemringde/0104754350.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec!["Folksam (5 samtal)".to_string()], messages:
history: vec![], - Folksam (5 samtal)
comments: vec![], history: []
}; comments: []"###);
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0313908905() { fn test_0313908905() {
let document = include_str!("../../fixtures/vemringde/0313908905.html"); let document = include_str!("../../fixtures/vemringde/0313908905.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec![], history: []
comments: vec![], comments: []"###);
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0702269893() { fn test_0702269893() {
let document = include_str!("../../fixtures/vemringde/0702269893.html"); let document = include_str!("../../fixtures/vemringde/0702269893.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec![], history: []
comments: vec![], comments: []"###);
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0726443387() { fn test_0726443387() {
let document = include_str!("../../fixtures/vemringde/0726443387.html"); let document = include_str!("../../fixtures/vemringde/0726443387.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec![], history: []
comments: vec![], comments: []"###);
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0751793426() { fn test_0751793426() {
let document = include_str!("../../fixtures/vemringde/0751793426.html"); let document = include_str!("../../fixtures/vemringde/0751793426.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec![], history: []
comments: vec![], comments: []"###);
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0751793483() { fn test_0751793483() {
let document = include_str!("../../fixtures/vemringde/0751793483.html"); let document = include_str!("../../fixtures/vemringde/0751793483.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec![], history: []
comments: vec![], comments: []"###);
};
assert_eq!(from_html(&document), Ok(expected));
} }
#[test] #[test]
fn test_0751793499() { fn test_0751793499() {
let document = include_str!("../../fixtures/vemringde/0751793499.html"); let document = include_str!("../../fixtures/vemringde/0751793499.html");
let expected = Entry { assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok:
messages: vec![], messages: []
history: vec![], history: []
comments: vec![], comments: []"###);
};
assert_eq!(from_html(&document), Ok(expected));
} }
} }