Skip to content

Commit 0008f5b

Browse files
author
Andres Medina
committed
moved mod tests onto their own file; unsure if I like it but I'll give it a shot
1 parent bf2979c commit 0008f5b

File tree

6 files changed

+213
-208
lines changed

6 files changed

+213
-208
lines changed

src/client/bus/mod.rs

Lines changed: 17 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use reqwest;
22

3-
#[cfg(all(test, not(feature = "contract")))]
4-
use mocktopus::macros::mockable;
5-
63
#[derive(Debug, Clone, PartialEq, Serialize)]
74
#[serde(rename_all = "camelCase")]
85
pub struct StopsQuery {
@@ -13,16 +10,6 @@ pub struct StopsQuery {
1310
pub max_count: i8,
1411
}
1512

16-
#[derive(Debug, Deserialize)]
17-
struct StopsListResponse {
18-
data: StopsListData,
19-
}
20-
21-
#[derive(Debug, Deserialize)]
22-
struct StopsListData {
23-
list: Vec<Stop>,
24-
}
25-
2613
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
2714
pub struct Stop {
2815
pub id: String,
@@ -38,7 +25,6 @@ pub struct Client {
3825
http_client: reqwest::Client,
3926
}
4027

41-
#[cfg_attr(all(test, not(feature = "contract")), mockable)]
4228
impl Client {
4329
pub fn new(http_client: reqwest::Client, host: String, key: String) -> Self {
4430
Client {
@@ -47,7 +33,14 @@ impl Client {
4733
key,
4834
}
4935
}
36+
}
37+
38+
// allow users of Client to mock the requests in unit tests
39+
#[cfg(all(test, not(feature = "contract")))]
40+
use mocktopus::macros::mockable;
5041

42+
#[cfg_attr(all(test, not(feature = "contract")), mockable)]
43+
impl Client {
5144
pub fn stops(&self, query: &StopsQuery) -> Result<Vec<Stop>, String> {
5245
let url = format!("{}/{}", self.host, "api/where/stops-for-location.json");
5346
self.http_client
@@ -61,62 +54,15 @@ impl Client {
6154
}
6255
}
6356

64-
#[cfg(all(test, not(feature = "contract")))]
65-
mod test {
66-
use super::*;
67-
68-
use mockito::{mock, SERVER_URL};
69-
use serde_urlencoded;
70-
71-
#[test]
72-
fn stops() {
73-
let subject = Client::new(
74-
reqwest::Client::new(),
75-
String::from(SERVER_URL),
76-
String::from("SOME_KEY"),
77-
);
78-
let query = StopsQuery {
79-
lat: 32.3,
80-
lon: 23.1,
81-
lat_span: 0.01,
82-
lon_span: 0.0002,
83-
max_count: 9,
84-
};
85-
let query_path =
86-
serde_urlencoded::to_string(query.clone()).expect("could not encode 'StopsQuery'");
87-
let path = format!(
88-
"/api/where/stops-for-location.json?{}&key=SOME_KEY",
89-
query_path
90-
);
91-
92-
let mock = mock("GET", path.as_str())
93-
.with_status(200)
94-
.with_body(include_str!("../fixtures/stop_list.json"))
95-
.with_header("Content-Type", "application/json")
96-
.create();
57+
#[derive(Debug, Deserialize)]
58+
struct StopsListResponse {
59+
data: StopsListData,
60+
}
9761

98-
let actual = subject
99-
.stops(&query)
100-
.expect("expected a succesful stops response");
101-
mock.assert();
102-
assert_eq!(
103-
actual,
104-
vec![
105-
Stop {
106-
id: String::from("1_75403"),
107-
direction: String::from("S"),
108-
name: String::from("Stevens Way & Benton Ln"),
109-
lat: 47.654365,
110-
lon: -122.305214,
111-
},
112-
Stop {
113-
id: String::from("1_75414"),
114-
direction: String::from("N"),
115-
name: String::from("Stevens Way & Benton Ln"),
116-
lat: 47.653713,
117-
lon: -122.305023,
118-
},
119-
]
120-
)
121-
}
62+
#[derive(Debug, Deserialize)]
63+
struct StopsListData {
64+
list: Vec<Stop>,
12265
}
66+
67+
#[cfg(all(test, not(feature = "contract")))]
68+
mod test;

src/client/bus/test.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use super::*;
2+
3+
use mockito::{mock, SERVER_URL};
4+
use serde_urlencoded;
5+
6+
#[test]
7+
fn stops() {
8+
let subject = Client::new(
9+
reqwest::Client::new(),
10+
String::from(SERVER_URL),
11+
String::from("SOME_KEY"),
12+
);
13+
let query = StopsQuery {
14+
lat: 32.3,
15+
lon: 23.1,
16+
lat_span: 0.01,
17+
lon_span: 0.0002,
18+
max_count: 9,
19+
};
20+
let query_path =
21+
serde_urlencoded::to_string(query.clone()).expect("could not encode 'StopsQuery'");
22+
let path = format!(
23+
"/api/where/stops-for-location.json?{}&key=SOME_KEY",
24+
query_path
25+
);
26+
27+
let mock = mock("GET", path.as_str())
28+
.with_status(200)
29+
.with_body(include_str!("../fixtures/stop_list.json"))
30+
.with_header("Content-Type", "application/json")
31+
.create();
32+
33+
let actual = subject
34+
.stops(&query)
35+
.expect("expected a succesful stops response");
36+
mock.assert();
37+
assert_eq!(
38+
actual,
39+
vec![
40+
Stop {
41+
id: String::from("1_75403"),
42+
direction: String::from("S"),
43+
name: String::from("Stevens Way & Benton Ln"),
44+
lat: 47.654365,
45+
lon: -122.305214,
46+
},
47+
Stop {
48+
id: String::from("1_75414"),
49+
direction: String::from("N"),
50+
name: String::from("Stevens Way & Benton Ln"),
51+
lat: 47.653713,
52+
lon: -122.305023,
53+
},
54+
]
55+
)
56+
}

src/client/mod.rs

Lines changed: 8 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@ use api::{Area, Location};
77
use chrono::{Duration, Local};
88
use reqwest;
99

10-
#[cfg(all(test, not(feature = "contract")))]
11-
use mocktopus::macros::mockable;
12-
1310
pub struct Client {
1411
seattle_client: seattle_data::Client,
1512
bus_client: bus::Client,
1613
}
1714

15+
// in non-unit test get the secrets from the environment variables
16+
#[cfg(any(not(test), feature = "contract"))]
1817
impl Client {
19-
#[cfg(any(not(test), feature = "contract"))]
2018
pub fn new(http_client: reqwest::Client) -> Self {
2119
fn expect_env(name: &str) -> String {
2220
use std::env;
@@ -38,33 +36,18 @@ impl Client {
3836
bus_client,
3937
}
4038
}
41-
42-
#[cfg(all(test, not(feature = "contract")))]
43-
pub fn new(http_client: reqwest::Client) -> Self {
44-
let seattle_client = seattle_data::Client::new(
45-
http_client.clone(),
46-
String::from("http://localhost"),
47-
String::from("SEATTLE_TOKEN"),
48-
);
49-
let bus_client = bus::Client::new(
50-
http_client,
51-
String::from("http://localhost"),
52-
String::from("BUS_KEY"),
53-
);
54-
55-
Client {
56-
seattle_client,
57-
bus_client,
58-
}
59-
}
6039
}
6140

41+
// allow users of Client to mock the requests in unit tests
42+
#[cfg(all(test, not(feature = "contract")))]
43+
use mocktopus::macros::mockable;
44+
6245
#[cfg_attr(all(test, not(feature = "contract")), mockable)]
6346
impl Client {
6447
pub fn info(&self, location: Location) -> Result<String, String> {
6548
let start_date = Local::now() - Duration::days(180);
6649
let query = seattle_data::Query::new(location).and(start_date);
67-
self.seattle_client.request(&query)
50+
self.seattle_client.crime(&query)
6851
}
6952

7053
pub fn bus_stops(&self, area: Area) -> Result<Vec<bus::Stop>, String> {
@@ -79,78 +62,4 @@ impl Client {
7962
}
8063

8164
#[cfg(all(test, not(feature = "contract")))]
82-
mod test {
83-
use super::*;
84-
85-
use mocktopus::mocking::{MockResult, Mockable};
86-
87-
#[test]
88-
fn info() {
89-
let subject = Client::new(reqwest::Client::new());
90-
let expected = String::from("{}");
91-
92-
let mut query = None;
93-
unsafe {
94-
seattle_data::Client::request.mock_raw(|_, q| {
95-
query = Some(q.clone());
96-
MockResult::Return(Ok(expected.clone()))
97-
})
98-
}
99-
100-
let location = Location {
101-
latitude: 32.2,
102-
longitude: 67.23,
103-
};
104-
let actual = subject
105-
.info(location)
106-
.expect("expected a succesful crime response");
107-
108-
assert_eq!(actual, expected);
109-
let query = query.expect("seattle_data::Client::request not called");
110-
assert_eq!(
111-
query,
112-
seattle_data::Query::new(location).and(Local::now() - Duration::days(180))
113-
);
114-
}
115-
116-
#[test]
117-
fn bus_stops() {
118-
let subject = Client::new(reqwest::Client::new());
119-
let mut query = None;
120-
let expected_stops = vec![bus::Stop {
121-
id: String::from("1_1234"),
122-
direction: String::from("N"),
123-
name: String::from("some bus"),
124-
lat: 34.3199,
125-
lon: 23.12005,
126-
}];
127-
unsafe {
128-
bus::Client::stops.mock_raw(|_, q| {
129-
query = Some(q.clone());
130-
MockResult::Return(Ok(expected_stops.clone()))
131-
});
132-
}
133-
134-
let area = Area {
135-
lat: 34.32,
136-
lon: 23.12,
137-
lat_span: 0.002,
138-
lon_span: 0.0005,
139-
};
140-
let actual_stops = subject
141-
.bus_stops(area)
142-
.expect("expected a succesful bus stop response");
143-
assert_eq!(actual_stops, expected_stops);
144-
let query = query.expect("'bus::Client::stops' not called");
145-
assert_eq!(
146-
query,
147-
bus::StopsQuery {
148-
lat: 34.32,
149-
lon: 23.12,
150-
lat_span: 0.002,
151-
lon_span: 0.0005,
152-
max_count: 20,
153-
}
154-
)
155-
}
156-
}
65+
mod test;

src/client/seattle_data/mod.rs

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ pub use self::query::Query;
44

55
use reqwest;
66

7-
#[cfg(all(test, not(feature = "contract")))]
8-
use mocktopus::macros::mockable;
9-
107
header! { (XAppToken, "X-App-Token") => [String]}
118

129
pub struct Client {
@@ -15,7 +12,6 @@ pub struct Client {
1512
http_client: reqwest::Client,
1613
}
1714

18-
#[cfg_attr(all(test, not(feature = "contract")), mockable)]
1915
impl Client {
2016
pub fn new(http_client: reqwest::Client, host: String, token: String) -> Self {
2117
Client {
@@ -24,8 +20,15 @@ impl Client {
2420
token: XAppToken(token),
2521
}
2622
}
23+
}
24+
25+
// allow users of Client to mock the requests in unit tests
26+
#[cfg(all(test, not(feature = "contract")))]
27+
use mocktopus::macros::mockable;
2728

28-
pub fn request(&self, query: &Query) -> Result<String, String> {
29+
#[cfg_attr(all(test, not(feature = "contract")), mockable)]
30+
impl Client {
31+
pub fn crime(&self, query: &Query) -> Result<String, String> {
2932
let url = format!("{}/{}", self.host, "resource/policereport.json");
3033
self.http_client
3134
.get(&url)
@@ -38,36 +41,4 @@ impl Client {
3841
}
3942

4043
#[cfg(all(test, not(feature = "contract")))]
41-
mod test {
42-
use super::*;
43-
use api::Location;
44-
45-
use mockito::{mock, SERVER_URL};
46-
use serde_urlencoded;
47-
48-
#[test]
49-
fn request() {
50-
let subject = Client::new(
51-
reqwest::Client::new(),
52-
String::from(SERVER_URL),
53-
String::from("SOME_TOKEN"),
54-
);
55-
56-
let location = Location {
57-
latitude: 32.2,
58-
longitude: 67.23,
59-
};
60-
let query = Query::new(location);
61-
let query_path = serde_urlencoded::to_string(query.clone()).unwrap();
62-
let path = format!("/resource/policereport.json?{}", query_path);
63-
let mock = mock("GET", path.as_str())
64-
.with_status(200)
65-
.with_body("{}")
66-
.with_header("Content-Type", "application/json")
67-
.create();
68-
let actual = subject.request(&query);
69-
mock.assert();
70-
71-
assert_eq!(actual, Ok("{}".to_string()));
72-
}
73-
}
44+
mod test;

0 commit comments

Comments
 (0)