-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_tests.rs
More file actions
221 lines (193 loc) · 7.05 KB
/
api_tests.rs
File metadata and controls
221 lines (193 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#[cfg(test)]
mod tests {
use actix_web::{
App,
dev::Service,
http::StatusCode, test, web,
};
use diesel::RunQueryDsl;
use futures::FutureExt;
use lazy_static::lazy_static;
use crate::addresses;
use crate::data::models::{Address, AddressRecord};
use crate::data::repo::addresses::create_or_update_addresses;
use crate::db::{init_test_connection_pool, Pool};
embed_migrations!("./migrations");
lazy_static! {
static ref POOL: Pool = {
let pool = init_test_connection_pool();
// Run migrations once for all tests
embedded_migrations::run(&pool.get().unwrap())
.expect("Error while running migrations");
pool
};
}
async fn run_test<F, R>(test: F) -> R
where
F: std::future::Future<Output = R>,
{
setup().await;
// Make the fut UnwindSafe in order to catch unwind it.
// That way we can run teardown even in case of failure.
let test_fut = std::panic::AssertUnwindSafe(test).catch_unwind();
let result = test_fut.await;
teardown().await;
assert!(result.is_ok());
result.unwrap()
}
async fn setup() {
use crate::data::schema::addresses;
// Clear data from previous tests
web::block(|| {
diesel::delete(addresses::table)
.execute(&POOL.get().unwrap())
})
.await
.expect("Couldn't delete addresses table");
}
async fn teardown () {}
#[actix_rt::test]
async fn test_get_addresses_missing_query_param() {
run_test(async {
let mut app = test::init_service(
App::new()
.data(POOL.clone())
.route("/addresses", web::get().to(addresses))
)
.await;
let req = test::TestRequest::get()
.uri("/addresses")
.to_request();
let resp = app.call(req).await.unwrap();
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
})
.await
}
#[actix_rt::test]
async fn test_get_no_addresses() {
run_test(async {
let mut app = test::init_service(
App::new()
.data(POOL.clone())
.route("/addresses", web::get().to(addresses))
)
.await;
let req = test::TestRequest::get()
.uri("/addresses?postcode=2222AA")
.to_request();
let resp: Vec<Address> = test::read_response_json(&mut app, req).await;
assert_eq!(resp.len(), 0);
})
.await
}
#[actix_rt::test]
async fn test_get_addresses_postcode_only() {
run_test(async {
let mut app = test::init_service(
App::new()
.data(POOL.clone())
.route("/addresses", web::get().to(addresses))
)
.await;
create_test_set().await;
let req = test::TestRequest::get()
.uri("/addresses?postcode=2222AA")
.to_request();
let resp: Vec<Address> = test::read_response_json(&mut app, req).await;
assert_eq!(resp.len(), 4);
})
.await
}
#[actix_rt::test]
async fn test_get_addresses_postcode_and_number() {
run_test(async {
let mut app = test::init_service(
App::new()
.data(POOL.clone())
.route("/addresses", web::get().to(addresses))
)
.await;
create_test_set().await;
let req = test::TestRequest::get()
.uri("/addresses?postcode=2222AA&number=1")
.to_request();
let resp: Vec<Address> = test::read_response_json(&mut app, req).await;
assert_eq!(resp.len(), 1);
assert_eq!(resp[0].postcode, "2222AA");
assert_eq!(resp[0].number, "1");
let req = test::TestRequest::get()
.uri("/addresses?postcode=2222AA&number=2")
.to_request();
let resp: Vec<Address> = test::read_response_json(&mut app, req).await;
assert_eq!(resp.len(), 3);
assert_eq!(resp[0].postcode, "2222AA");
assert_eq!(resp[0].number, "2");
assert_eq!(resp[1].postcode, "2222AA");
assert_eq!(resp[1].number, "2A");
assert_eq!(resp[2].postcode, "2222AA");
assert_eq!(resp[2].number, "2B");
let req = test::TestRequest::get()
.uri("/addresses?postcode=2222AA&number=2A")
.to_request();
let resp: Vec<Address> = test::read_response_json(&mut app, req).await;
assert_eq!(resp.len(), 1);
assert_eq!(resp[0].postcode, "2222AA");
assert_eq!(resp[0].number, "2A");
let req = test::TestRequest::get()
.uri("/addresses?postcode=2222AA&number=2B")
.to_request();
let resp: Vec<Address> = test::read_response_json(&mut app, req).await;
assert_eq!(resp.len(), 1);
assert_eq!(resp[0].postcode, "2222AA");
assert_eq!(resp[0].number, "2B");
})
.await
}
async fn create_test_set() {
web::block(|| {
create_or_update_addresses(
&POOL.get().unwrap(),
&[
AddressRecord {
lat: 2.0,
lon: 1.0,
number: "1".to_string(),
street: "Street".to_string(),
city: "City".to_string(),
region: "Region".to_string(),
postcode: "2222AA".to_string()
},
AddressRecord {
lat: 3.0,
lon: 2.0,
number: "2".to_string(),
street: "Street".to_string(),
city: "City".to_string(),
region: "Region".to_string(),
postcode: "2222AA".to_string()
},
AddressRecord {
lat: 4.0,
lon: 3.0,
number: "2A".to_string(),
street: "Street".to_string(),
city: "City".to_string(),
region: "Region".to_string(),
postcode: "2222AA".to_string()
},
AddressRecord {
lat: 5.0,
lon: 4.0,
number: "2B".to_string(),
street: "Street".to_string(),
city: "City".to_string(),
region: "Region".to_string(),
postcode: "2222AA".to_string()
},
]
)
})
.await
.expect("Error creating tests data");
}
}