Skip to content

Commit f59570b

Browse files
author
Andres Medina
committed
move tests for the api into their own file
an alternative would have been to make a lib.rs co-exist with main.rs and have an integration test there. Unsure which option I like the most right now
1 parent 0008f5b commit f59570b

File tree

3 files changed

+96
-93
lines changed

3 files changed

+96
-93
lines changed

src/api.rs

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -26,93 +26,3 @@ fn info(location: Location, client: State<Client>) -> Result<Json<String>, Strin
2626
fn bus_stops(area: Area, client: State<Client>) -> Result<Json<Vec<client::BusStop>>, String> {
2727
client.bus_stops(area).map(Json)
2828
}
29-
30-
#[cfg(all(test, not(feature = "contract")))]
31-
mod unit_test {
32-
use super::*;
33-
34-
use mocktopus::mocking::{MockResult, Mockable};
35-
36-
#[test]
37-
fn info_route() {
38-
let mut location = None;
39-
unsafe {
40-
Client::info.mock_raw(|_, loc| {
41-
location = Some(loc);
42-
MockResult::Return(Ok("Hello".to_string()))
43-
});
44-
}
45-
46-
test::crime();
47-
}
48-
49-
#[test]
50-
fn bus_stops() {
51-
let mut area = None;
52-
let expected = vec![client::BusStop {
53-
direction: String::from("S"),
54-
id: String::from("1_2345"),
55-
name: String::from("hello darkness"),
56-
lat: 1.23,
57-
lon: 123.23,
58-
}];
59-
unsafe {
60-
Client::bus_stops.mock_raw(|_, a| {
61-
area = Some(a);
62-
MockResult::Return(Ok(expected.clone()))
63-
});
64-
}
65-
66-
let actual = test::bus_stops();
67-
assert_eq!(actual, expected);
68-
69-
let area = area.expect("Client::bus_stops not called");
70-
assert_eq!(area.lat, 47.653435);
71-
assert_eq!(area.lon, -122.305641);
72-
assert_eq!(area.lat_span, 0.002);
73-
assert_eq!(area.lon_span, 0.003);
74-
}
75-
}
76-
77-
#[cfg(all(test, feature = "contract"))]
78-
mod contract_test {
79-
#[test]
80-
fn info_route() {
81-
super::test::crime();
82-
}
83-
84-
#[test]
85-
fn bus_route() {
86-
let stops = super::test::bus_stops();
87-
assert!(stops.len() > 0);
88-
}
89-
}
90-
91-
#[cfg(test)]
92-
mod test {
93-
use client;
94-
use rocket::http::{ContentType, Status};
95-
use serde_json;
96-
97-
pub fn bus_stops() -> Vec<client::BusStop> {
98-
let client = ::client();
99-
let mut response = client
100-
.get("/api/bus_stops?lat=47.653435&lon=-122.305641&lat_span=0.002&lon_span=0.003")
101-
.dispatch();
102-
assert_eq!(response.status(), Status::Ok);
103-
assert_eq!(response.content_type(), Some(ContentType::JSON));
104-
let body = response.body_string().expect("body was empty");
105-
serde_json::from_str(body.as_str())
106-
.expect("Could not parse api response into 'Vec<client::BusStop'")
107-
}
108-
109-
pub fn crime() -> String {
110-
let client = ::client();
111-
let mut response = client
112-
.get("/api/info?latitude=-122.33&longitude=47.59")
113-
.dispatch();
114-
assert_eq!(response.status(), Status::Ok);
115-
assert_eq!(response.content_type(), Some(ContentType::JSON));
116-
response.body_string().expect("body was empty")
117-
}
118-
}

src/api_test.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
use client;
2+
use rocket::{
3+
self,
4+
http::{ContentType, Status},
5+
};
6+
use serde_json;
7+
8+
fn client() -> rocket::local::Client {
9+
rocket::local::Client::new(::rocket()).unwrap()
10+
}
11+
12+
pub fn get_bus_stops() -> Vec<client::BusStop> {
13+
let client = client();
14+
let mut response = client
15+
.get("/api/bus_stops?lat=47.653435&lon=-122.305641&lat_span=0.002&lon_span=0.003")
16+
.dispatch();
17+
assert_eq!(response.status(), Status::Ok);
18+
assert_eq!(response.content_type(), Some(ContentType::JSON));
19+
let body = response.body_string().expect("body was empty");
20+
serde_json::from_str(body.as_str())
21+
.expect("Could not parse api response into 'Vec<client::BusStop>'")
22+
}
23+
24+
pub fn get_crime() -> String {
25+
let client = client();
26+
let mut response = client
27+
.get("/api/info?latitude=-122.33&longitude=47.59")
28+
.dispatch();
29+
assert_eq!(response.status(), Status::Ok);
30+
assert_eq!(response.content_type(), Some(ContentType::JSON));
31+
response.body_string().expect("body was empty")
32+
}
33+
34+
#[cfg(not(feature = "contract"))]
35+
mod unit {
36+
use super::*;
37+
38+
use mocktopus::mocking::{MockResult, Mockable};
39+
40+
#[test]
41+
fn crime() {
42+
let mut location = None;
43+
unsafe {
44+
client::Client::info.mock_raw(|_, loc| {
45+
location = Some(loc);
46+
MockResult::Return(Ok("Hello".to_string()))
47+
});
48+
}
49+
50+
get_crime();
51+
}
52+
53+
#[test]
54+
fn bus_stops() {
55+
let mut area = None;
56+
let expected = vec![client::BusStop {
57+
direction: String::from("S"),
58+
id: String::from("1_2345"),
59+
name: String::from("hello darkness"),
60+
lat: 1.23,
61+
lon: 123.23,
62+
}];
63+
unsafe {
64+
client::Client::bus_stops.mock_raw(|_, a| {
65+
area = Some(a);
66+
MockResult::Return(Ok(expected.clone()))
67+
});
68+
}
69+
70+
let actual = get_bus_stops();
71+
assert_eq!(actual, expected);
72+
73+
let area = area.expect("Client::bus_stops not called");
74+
assert_eq!(area.lat, 47.653435);
75+
assert_eq!(area.lon, -122.305641);
76+
assert_eq!(area.lat_span, 0.002);
77+
assert_eq!(area.lon_span, 0.003);
78+
}
79+
}
80+
81+
#[cfg(feature = "contract")]
82+
mod integration {
83+
use super::*;
84+
85+
#[test]
86+
fn crime() {
87+
get_crime();
88+
}
89+
90+
#[test]
91+
fn bus_stops() {
92+
let stops = get_bus_stops();
93+
assert!(stops.len() > 0);
94+
}
95+
}

src/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,4 @@ fn main() {
4747
}
4848

4949
#[cfg(test)]
50-
fn client() -> rocket::local::Client {
51-
rocket::local::Client::new(rocket()).unwrap()
52-
}
50+
mod api_test;

0 commit comments

Comments
 (0)