Skip to content
This repository has been archived by the owner on Jun 3, 2023. It is now read-only.

Commit

Permalink
Simplify test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
mlafeldt committed Aug 23, 2021
1 parent 512ea51 commit b56537a
Showing 1 changed file with 24 additions and 39 deletions.
63 changes: 24 additions & 39 deletions get-strip/dilbert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,37 +80,30 @@ mod tests {
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

struct Test {
date: &'static str,
title: &'static str,
image_url: &'static str,
strip_url: String,
}

fn tests(base_url: &str) -> Vec<Test> {
fn testdata(base_url: &str) -> Vec<Comic> {
vec![
Test {
date: "2000-01-01",
title: "Dilbert Comic for 2000-01-01",
image_url: "https://assets.amuniversal.com/bdc8a4d06d6401301d80001dd8b71c47",
Comic {
date: "2000-01-01".to_string(),
title: "Dilbert Comic for 2000-01-01".to_string(),
image_url: "https://assets.amuniversal.com/bdc8a4d06d6401301d80001dd8b71c47".to_string(),
strip_url: format!("{}/strip/2000-01-01", base_url),
},
Test {
date: "2018-10-30",
title: "Intentionally Underbidding",
image_url: "https://assets.amuniversal.com/cda546d0a88c01365b26005056a9545d",
Comic {
date: "2018-10-30".to_string(),
title: "Intentionally Underbidding".to_string(),
image_url: "https://assets.amuniversal.com/cda546d0a88c01365b26005056a9545d".to_string(),
strip_url: format!("{}/strip/2018-10-30", base_url),
},
Test {
date: "2019-11-02",
title: "Multiple Choice",
image_url: "https://assets.amuniversal.com/ce7ec130d6480137c832005056a9545d",
Comic {
date: "2019-11-02".to_string(),
title: "Multiple Choice".to_string(),
image_url: "https://assets.amuniversal.com/ce7ec130d6480137c832005056a9545d".to_string(),
strip_url: format!("{}/strip/2019-11-02", base_url),
},
Test {
date: "2020-11-11",
title: "Elbonian Words",
image_url: "https://assets.amuniversal.com/f25312c0fb5b01382ef9005056a9545d",
Comic {
date: "2020-11-11".to_string(),
title: "Elbonian Words".to_string(),
image_url: "https://assets.amuniversal.com/f25312c0fb5b01382ef9005056a9545d".to_string(),
strip_url: format!("{}/strip/2020-11-11", base_url),
},
]
Expand All @@ -120,31 +113,23 @@ mod tests {
async fn test_scrape_comic() {
let server = MockServer::start().await;

for t in tests(&server.uri()).iter() {
let body = fs::read_to_string(format!("dilbert/testdata/strip/{}", t.date)).unwrap();
let template = ResponseTemplate::new(200).set_body_raw(body, "text/html");
for td in testdata(&server.uri()).iter() {
let body = fs::read_to_string(format!("dilbert/testdata/strip/{}", td.date)).unwrap();
let resp = ResponseTemplate::new(200).set_body_raw(body, "text/html");

Mock::given(method("GET"))
.and(path(format!("/strip/{}", t.date)))
.respond_with(template)
.and(path(format!("/strip/{}", td.date)))
.respond_with(resp)
.expect(1)
.mount(&server)
.await;

let comic = Dilbert::new(&server.uri())
.scrape_comic(Some(t.date.to_string()))
.scrape_comic(Some(td.date.to_owned()))
.await
.unwrap();

assert_eq!(
comic,
Comic {
date: t.date.to_string(),
title: t.title.to_string(),
image_url: t.image_url.to_string(),
strip_url: t.strip_url.to_owned(),
},
);
assert_eq!(&comic, td);

server.reset().await;
}
Expand Down

0 comments on commit b56537a

Please sign in to comment.