-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathend_to_end.rs
More file actions
377 lines (334 loc) · 12.4 KB
/
end_to_end.rs
File metadata and controls
377 lines (334 loc) · 12.4 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
use chrono::{DateTime, Duration, DurationRound, TimeDelta, TimeZone, Utc};
use rdkafka::producer::FutureProducer;
use lard_egress::{
LatestResp, TimeseriesResp, TimesliceResp, patchwork::PatchworkTables, timeseries::Timeseries,
};
use lard_ingestion::KldataResp;
use util::{
DbPools, PooledPgConn,
stinfofacade::{self, from_to_time::update_from_to},
};
pub mod common;
use common::{
Param, TestData, e2e_test_wrapper,
legacy::{IngestData, e2e_test_wrapper_legacy, ingest_raw},
mocks::MetadataMock,
};
async fn ingest_data(client: &reqwest::Client, obsinn_msg: String) -> KldataResp {
let resp = client
.post("http://localhost:3001/kldata")
.body(obsinn_msg)
.send()
.await
.unwrap();
resp.json().await.unwrap()
}
#[tokio::test]
async fn test_stations_endpoint_irregular() {
e2e_test_wrapper(&["TGM", "TGX"], async |_| {
let ts = TestData {
station_id: 20001,
params: vec![Param::new("TGM"), Param::new("TGX")],
start_time: Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap(),
period: Duration::hours(1),
type_id: 501,
len: 48,
};
let client = reqwest::Client::new();
let ingestor_resp = ingest_data(&client, ts.obsinn_zeros()).await;
assert_eq!(ingestor_resp.res, 0);
for param in ts.params {
let url = format!(
"http://localhost:3000/stations/{}/params/{}",
ts.station_id, param.id
);
let resp = reqwest::get(url).await.unwrap();
assert!(resp.status().is_success());
let json: TimeseriesResp = resp.json().await.unwrap();
assert_eq!(json.tseries.len(), 1);
let Timeseries::Irregular(series) = &json.tseries[0] else {
panic!("Expected irrregular timeseries")
};
assert_eq!(series.data.len(), ts.len);
}
})
.await
}
#[tokio::test]
async fn test_stations_endpoint_regular() {
let cases = vec![
// Scalar params
TestData {
station_id: 20001,
params: vec![Param::new("TA"), Param::new("TGX")],
start_time: Utc::now().duration_trunc(TimeDelta::hours(1)).unwrap()
- Duration::hours(11),
period: Duration::hours(1),
type_id: 501,
len: 12,
},
// TODO: probably write a separate test, so we can check actual sensor and level
// With sensor and level
TestData {
station_id: 20001,
params: vec![
Param::new("TA").with_sensor_level((1, 1)),
Param::new("TGX"),
],
start_time: Utc::now().duration_trunc(TimeDelta::hours(1)).unwrap()
- Duration::hours(11),
period: Duration::hours(1),
type_id: 501,
len: 12,
},
// Scalar and non-scalar
TestData {
station_id: 20001,
params: vec![Param::new("KLOBS"), Param::new("TA")],
start_time: Utc::now().duration_trunc(TimeDelta::hours(1)).unwrap()
- Duration::hours(11),
period: Duration::hours(1),
type_id: 501,
len: 12,
},
];
for ts in cases {
e2e_test_wrapper(&["TA", "TGX", "KLOBS"], async |_| {
let client = reqwest::Client::new();
let ingestor_resp = ingest_data(&client, ts.obsinn_zeros()).await;
assert_eq!(ingestor_resp.res, 0);
let resolution = "PT1H";
for param in ts.params {
let url = format!(
"http://localhost:3000/stations/{}/params/{}?time_resolution={}",
ts.station_id, param.id, resolution
);
let resp = reqwest::get(url).await.unwrap();
assert!(resp.status().is_success());
let json: TimeseriesResp = resp.json().await.unwrap();
assert_eq!(json.tseries.len(), 1);
let Timeseries::Regular(series) = &json.tseries[0] else {
panic!("Expected regular timeseries")
};
assert_eq!(series.data.len(), ts.len);
}
})
.await
}
}
// TODO: we should implement an availability endpoint?
async fn get_fromtotime(
conn: &PooledPgConn<'_>,
) -> Vec<(Option<DateTime<Utc>>, Option<DateTime<Utc>>)> {
conn.query(
"SELECT timeseries.fromtime, timeseries.totime FROM timeseries \
JOIN labels.met \
ON timeseries.id = met.timeseries \
ORDER BY station_id",
&[],
)
.await
.unwrap()
.iter()
.map(|row| (row.get(0), row.get(1)))
.collect()
}
#[tokio::test]
async fn test_fromtotime_update() {
e2e_test_wrapper_legacy(
&["KLOBS", "TA"],
async |producer: FutureProducer, db_pools: DbPools, patchwork_tables: PatchworkTables| {
let timeseries = IngestData::new(vec![
TestData {
station_id: 10001,
params: vec![Param::new("KLOBS")],
start_time: Utc.with_ymd_and_hms(1980, 12, 31, 12, 0, 0).unwrap(),
period: Duration::hours(1),
type_id: 503,
len: 14, // metadata should cut off the last part of this that goes into 1981
},
TestData {
station_id: 20001,
params: vec![Param::new("TA")],
start_time: Utc.with_ymd_and_hms(1950, 1, 1, 0, 0, 0).unwrap(),
period: Duration::hours(1),
type_id: 501,
len: 12,
},
]);
ingest_raw(×eries, producer, db_pools.clone(), patchwork_tables).await;
let fromtime = Utc.with_ymd_and_hms(1980, 12, 1, 0, 0, 0).unwrap();
let totime: DateTime<Utc> = Utc.with_ymd_and_hms(1981, 1, 1, 0, 0, 0).unwrap();
let metadata_mock = MetadataMock {
station: 10001,
fromtime,
totime,
};
let expected = vec![
// timeseries on station 10001 should be closed based on metadata
(
Some(Utc.with_ymd_and_hms(1980, 12, 31, 12, 0, 0).unwrap()),
Some(totime),
),
// timeseries on station 20001 is not, so it is left open
(
Some(Utc.with_ymd_and_hms(1950, 1, 1, 0, 0, 0).unwrap()),
None,
),
];
let mut conn = db_pools.open.get().await.unwrap();
// totimes should be empty
for fromtotimes in get_fromtotime(&conn).await {
assert_eq!(fromtotimes.1, None); // to time
}
let (obs_pgm_times_map, station_times_map) =
metadata_mock.cache_closed_stinfosys().await.unwrap();
let param_tables = stinfofacade::param::from_codes(&["TA", "KLOBS"]);
update_from_to(
&mut conn,
&obs_pgm_times_map,
&station_times_map,
param_tables,
tokio_util::sync::CancellationToken::new(),
)
.await
.unwrap();
let after = get_fromtotime(&conn).await;
// Now the totime for station 10001 should be set (and the to time for station 20001 should be its first observation time)
for (db, expect) in after.into_iter().zip(expected) {
assert_eq!(db.0, expect.0);
assert_eq!(db.1, expect.1);
}
},
)
.await
}
#[tokio::test]
async fn test_stations_endpoint_errors() {
let cases = vec![
//missing station
(99999, 211),
//missing param
(20001, 999),
];
for (station_id, param_id) in cases {
e2e_test_wrapper(&["TA"], async |_| {
let ts = TestData {
station_id: 20001,
params: vec![Param::new("TA")],
start_time: Utc.with_ymd_and_hms(2024, 1, 1, 00, 00, 00).unwrap(),
period: Duration::hours(1),
type_id: 501,
len: 48,
};
let client = reqwest::Client::new();
let ingestor_resp = ingest_data(&client, ts.obsinn_zeros()).await;
assert_eq!(ingestor_resp.res, 0);
for _ in ts.params {
let url = format!("http://localhost:3000/stations/{station_id}/params/{param_id}");
let resp = reqwest::get(url).await.unwrap();
// TODO: resp.status() returns 500, maybe it should return 404?
assert!(!resp.status().is_success());
}
})
.await
}
}
// We insert 4 timeseries, 2 with new data (UTC::now()) and 2 with old data (2020)
#[tokio::test]
async fn test_latest_endpoint() {
let cases = vec![
// without query
("", 2),
// latest max age 1
("?latest_max_age=2021-01-01T00:00:00Z", 2),
// latest max age 2
("?latest_max_age=2019-01-01T00:00:00Z", 4),
];
for (query, n_timeseries_found) in cases {
e2e_test_wrapper(&["TA", "TGX"], async |_| {
let test_data = [
TestData {
station_id: 20001,
params: vec![Param::new("TA"), Param::new("TGX")],
start_time: Utc::now().duration_trunc(TimeDelta::minutes(1)).unwrap()
- Duration::hours(3),
period: Duration::minutes(1),
type_id: 508,
len: 180,
},
TestData {
station_id: 20002,
params: vec![Param::new("TA"), Param::new("TGX")],
start_time: Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap(),
period: Duration::minutes(1),
type_id: 508,
len: 180,
},
];
let client = reqwest::Client::new();
for ts in test_data {
let ingestor_resp = ingest_data(&client, ts.obsinn_zeros()).await;
assert_eq!(ingestor_resp.res, 0);
}
let url = format!("http://localhost:3000/latest{query}");
let resp = reqwest::get(url).await.unwrap();
assert!(resp.status().is_success());
let json: LatestResp = resp.json().await.unwrap();
assert_eq!(json.data.len(), n_timeseries_found);
})
.await
}
}
#[tokio::test]
async fn test_timeslice_endpoint() {
e2e_test_wrapper(&["TA"], async |_| {
let timestamp = Utc.with_ymd_and_hms(2024, 1, 1, 1, 0, 0).unwrap();
let params = vec![Param::new("TA")];
let test_data = [
TestData {
station_id: 20001,
params: params.clone(),
start_time: timestamp - Duration::hours(1),
period: Duration::hours(1),
type_id: 501,
len: 2,
},
TestData {
station_id: 20002,
params: params.clone(),
start_time: timestamp - Duration::hours(1),
period: Duration::minutes(1),
type_id: 508,
len: 120,
},
];
let client = reqwest::Client::new();
for ts in &test_data {
let ingestor_resp = ingest_data(&client, ts.obsinn_zeros()).await;
assert_eq!(
ingestor_resp.res, 0,
"ingestor_resp.message: {}",
ingestor_resp.message
);
}
for param in ¶ms {
let url = format!(
"http://localhost:3000/timeslices/{}/params/{}",
timestamp, param.id
);
let resp = reqwest::get(url).await.unwrap();
assert!(resp.status().is_success());
let json: TimesliceResp = resp.json().await.unwrap();
assert!(json.tslices.len() == 1);
let slice = &json.tslices[0];
assert_eq!(slice.param_id, param.id);
assert_eq!(slice.timestamp, timestamp);
assert_eq!(slice.data.len(), test_data.len());
for (data, ts) in slice.data.iter().zip(&test_data) {
assert_eq!(data.station_id, ts.station_id);
}
}
})
.await
}