Skip to content

Commit d9a8e73

Browse files
authored
feat: Review's importer test | NPG-8040 (#540)
# Description This is an initial review's importer test, right now it is not functional because we need to prepare data in ideascale with which this test will operate. But the general test case already defined. - Added a review's importer test scenario - Update dev stage data - Fixed cat-data-service and event-db tests with the enabled stage data
1 parent b3182ed commit d9a8e73

File tree

29 files changed

+525
-416
lines changed

29 files changed

+525
-416
lines changed

.github/workflows/rust.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ jobs:
125125
run:
126126
sudo apt install -y protobuf-compiler libssl-dev libpq-dev libsqlite3-dev pkg-config
127127

128+
# We are excluding cat-dat-service and event-db because we are already running it with Earthly
128129
- name: Build and archive tests
129130
run: |
130131
cargo nextest archive \
@@ -137,6 +138,8 @@ jobs:
137138
--exclude vit-servicing-station-server \
138139
--exclude vit-servicing-station-tests \
139140
--exclude vit-servicing-station-lib \
141+
--exclude cat-data-service \
142+
--exclude event-db \
140143
--archive-file nextest-archive.tar.zst
141144
142145
- name: Save test archive
@@ -235,6 +238,7 @@ jobs:
235238
PGPASSWORD: 123456
236239
run: cargo make local-event-db-test -h 127.0.0.1 -U postgres
237240

241+
# We are excluding cat-dat-service and event-db because we are already running it with Earthly
238242
- name: Build and archive tests
239243
if: steps.archive-cache.outputs.cache-hit != 'true'
240244
run: |
@@ -248,6 +252,8 @@ jobs:
248252
--exclude vit-servicing-station-server \
249253
--exclude vit-servicing-station-tests \
250254
--exclude vit-servicing-station-lib \
255+
--exclude cat-data-service \
256+
--exclude event-db \
251257
--archive-file nextest-archive.tar.zst
252258
253259
- name: Run Catalyst Core tests

src/cat-data-service/src/service/mod.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,23 @@ async fn track_metrics<T>(req: Request<T>, next: Next<T>) -> impl IntoResponse {
143143

144144
#[cfg(test)]
145145
pub mod tests {
146+
use axum::body::HttpBody;
146147
use std::str::FromStr;
147148

148-
pub fn body_data_json_check(body_data: Vec<u8>, expected_json: serde_json::Value) -> bool {
149-
serde_json::Value::from_str(String::from_utf8(body_data).unwrap().as_str()).unwrap()
150-
== expected_json
149+
pub async fn response_body_to_json<
150+
T: HttpBody<Data = impl Into<Vec<u8>>, Error = axum::Error> + Unpin,
151+
>(
152+
response: axum::response::Response<T>,
153+
) -> Result<serde_json::Value, String> {
154+
let data: Vec<u8> = response
155+
.into_body()
156+
.data()
157+
.await
158+
.ok_or("response should have data in a body".to_string())?
159+
.map_err(|e| e.to_string())?
160+
.into();
161+
162+
serde_json::Value::from_str(String::from_utf8(data).map_err(|e| e.to_string())?.as_str())
163+
.map_err(|e| e.to_string())
151164
}
152165
}

src/cat-data-service/src/service/v0/fund.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ async fn fund_exec(state: Arc<State>) -> Result<SerdeType<FundWithNext>, Error>
3939
#[cfg(test)]
4040
mod tests {
4141
use super::*;
42-
use crate::service::{app, tests::body_data_json_check};
42+
use crate::service::{app, tests::response_body_to_json};
4343
use axum::{
44-
body::{Body, HttpBody},
44+
body::Body,
4545
http::{Request, StatusCode},
4646
};
4747
use tower::ServiceExt;
@@ -57,8 +57,8 @@ mod tests {
5757
.unwrap();
5858
let response = app.clone().oneshot(request).await.unwrap();
5959
assert_eq!(response.status(), StatusCode::OK);
60-
assert!(body_data_json_check(
61-
response.into_body().data().await.unwrap().unwrap().to_vec(),
60+
assert_eq!(
61+
response_body_to_json(response).await.unwrap(),
6262
serde_json::json!(
6363
{
6464
"id": 4,
@@ -178,7 +178,7 @@ mod tests {
178178
"tallying_end": "1970-01-01T00:00:00+00:00",
179179
}
180180
}
181-
)
182-
));
181+
),
182+
);
183183
}
184184
}

src/cat-data-service/src/service/v1/event/ballots.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ async fn ballots_exec(
4848
#[cfg(test)]
4949
mod tests {
5050
use super::*;
51-
use crate::service::{app, tests::body_data_json_check};
51+
use crate::service::{app, tests::response_body_to_json};
5252
use axum::{
53-
body::{Body, HttpBody},
53+
body::Body,
5454
http::{Request, StatusCode},
5555
};
5656
use tower::ServiceExt;
@@ -66,8 +66,8 @@ mod tests {
6666
.unwrap();
6767
let response = app.clone().oneshot(request).await.unwrap();
6868
assert_eq!(response.status(), StatusCode::OK);
69-
assert!(body_data_json_check(
70-
response.into_body().data().await.unwrap().unwrap().to_vec(),
69+
assert_eq!(
70+
response_body_to_json(response).await.unwrap(),
7171
serde_json::json!([
7272
{
7373
"objective_id": 1,
@@ -121,7 +121,7 @@ mod tests {
121121
}
122122
]
123123
}
124-
])
125-
));
124+
]),
125+
);
126126
}
127127
}

src/cat-data-service/src/service/v1/event/mod.rs

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ async fn events_exec(
8787
#[cfg(test)]
8888
mod tests {
8989
use super::*;
90-
use crate::service::{app, tests::body_data_json_check};
90+
use crate::service::{app, tests::response_body_to_json};
9191
use axum::{
92-
body::{Body, HttpBody},
92+
body::Body,
9393
http::{Request, StatusCode},
9494
};
9595
use tower::ServiceExt;
@@ -105,8 +105,8 @@ mod tests {
105105
.unwrap();
106106
let response = app.clone().oneshot(request).await.unwrap();
107107
assert_eq!(response.status(), StatusCode::OK);
108-
assert!(body_data_json_check(
109-
response.into_body().data().await.unwrap().unwrap().to_vec(),
108+
assert_eq!(
109+
response_body_to_json(response).await.unwrap(),
110110
serde_json::json!(
111111
{
112112
"id": 1,
@@ -154,11 +154,11 @@ mod tests {
154154
}
155155
]
156156
}
157-
)
158-
));
157+
),
158+
);
159159

160160
let request = Request::builder()
161-
.uri(format!("/api/v1/event/{0}", 10))
161+
.uri(format!("/api/v1/event/{0}", 100))
162162
.body(Body::empty())
163163
.unwrap();
164164
let response = app.clone().oneshot(request).await.unwrap();
@@ -176,10 +176,17 @@ mod tests {
176176
.unwrap();
177177
let response = app.clone().oneshot(request).await.unwrap();
178178
assert_eq!(response.status(), StatusCode::OK);
179-
assert!(body_data_json_check(
180-
response.into_body().data().await.unwrap().unwrap().to_vec(),
179+
assert_eq!(
180+
response_body_to_json(response).await.unwrap(),
181181
serde_json::json!(
182182
[
183+
{
184+
"id": 0,
185+
"name": "Test Fund",
186+
"starts": "1970-01-01T00:00:00+00:00",
187+
"ends": "1970-01-01T00:00:00+00:00",
188+
"final": true
189+
},
183190
{
184191
"id": 1,
185192
"name": "Test Fund 1",
@@ -215,21 +222,29 @@ mod tests {
215222
"id": 5,
216223
"name": "Test Fund 5",
217224
"final": false
218-
},
225+
}
219226
]
220-
)
221-
));
227+
),
228+
);
222229

223230
let request = Request::builder()
224231
.uri(format!("/api/v1/events?offset={0}", 1))
225232
.body(Body::empty())
226233
.unwrap();
227234
let response = app.clone().oneshot(request).await.unwrap();
228235
assert_eq!(response.status(), StatusCode::OK);
229-
assert!(body_data_json_check(
230-
response.into_body().data().await.unwrap().unwrap().to_vec(),
236+
assert_eq!(
237+
response_body_to_json(response).await.unwrap(),
231238
serde_json::json!(
232239
[
240+
{
241+
"id": 1,
242+
"name": "Test Fund 1",
243+
"starts": "2020-05-01T12:00:00+00:00",
244+
"ends": "2020-06-01T12:00:00+00:00",
245+
"reg_checked": "2020-03-31T12:00:00+00:00",
246+
"final": true,
247+
},
233248
{
234249
"id": 2,
235250
"name": "Test Fund 2",
@@ -257,64 +272,62 @@ mod tests {
257272
"id": 5,
258273
"name": "Test Fund 5",
259274
"final": false
260-
},
275+
}
261276
]
262-
)
263-
));
277+
),
278+
);
264279

265280
let request = Request::builder()
266281
.uri(format!("/api/v1/events?limit={0}", 1))
267282
.body(Body::empty())
268283
.unwrap();
269284
let response = app.clone().oneshot(request).await.unwrap();
270285
assert_eq!(response.status(), StatusCode::OK);
271-
assert!(body_data_json_check(
272-
response.into_body().data().await.unwrap().unwrap().to_vec(),
286+
assert_eq!(
287+
response_body_to_json(response).await.unwrap(),
273288
serde_json::json!(
274289
[
275290
{
276-
"id": 1,
277-
"name": "Test Fund 1",
278-
"starts": "2020-05-01T12:00:00+00:00",
279-
"ends": "2020-06-01T12:00:00+00:00",
280-
"reg_checked": "2020-03-31T12:00:00+00:00",
281-
"final": true,
282-
},
291+
"id": 0,
292+
"name": "Test Fund",
293+
"starts": "1970-01-01T00:00:00+00:00",
294+
"ends": "1970-01-01T00:00:00+00:00",
295+
"final": true
296+
}
283297
]
284-
)
285-
));
298+
),
299+
);
286300

287301
let request = Request::builder()
288302
.uri(format!("/api/v1/events?limit={0}&offset={1}", 1, 1))
289303
.body(Body::empty())
290304
.unwrap();
291305
let response = app.clone().oneshot(request).await.unwrap();
292306
assert_eq!(response.status(), StatusCode::OK);
293-
assert!(body_data_json_check(
294-
response.into_body().data().await.unwrap().unwrap().to_vec(),
307+
assert_eq!(
308+
response_body_to_json(response).await.unwrap(),
295309
serde_json::json!(
296310
[
297311
{
298-
"id": 2,
299-
"name": "Test Fund 2",
300-
"starts": "2021-05-01T12:00:00+00:00",
301-
"ends": "2021-06-01T12:00:00+00:00",
302-
"reg_checked": "2021-03-31T12:00:00+00:00",
312+
"id": 1,
313+
"name": "Test Fund 1",
314+
"starts": "2020-05-01T12:00:00+00:00",
315+
"ends": "2020-06-01T12:00:00+00:00",
316+
"reg_checked": "2020-03-31T12:00:00+00:00",
303317
"final": true,
304-
},
318+
}
305319
]
306-
)
307-
));
320+
),
321+
);
308322

309323
let request = Request::builder()
310324
.uri(format!("/api/v1/events?offset={0}", 10))
311325
.body(Body::empty())
312326
.unwrap();
313327
let response = app.clone().oneshot(request).await.unwrap();
314328
assert_eq!(
315-
String::from_utf8(response.into_body().data().await.unwrap().unwrap().to_vec())
316-
.unwrap(),
317-
serde_json::to_string(&Vec::<()>::new()).unwrap()
329+
response_body_to_json(response).await.unwrap(),
330+
serde_json::json!([])
318331
);
319332
}
320333
}

src/cat-data-service/src/service/v1/event/objective/ballots.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ async fn ballots_exec(
5555
#[cfg(test)]
5656
mod tests {
5757
use super::*;
58-
use crate::service::{app, tests::body_data_json_check};
58+
use crate::service::{app, tests::response_body_to_json};
5959
use axum::{
60-
body::{Body, HttpBody},
60+
body::Body,
6161
http::{Request, StatusCode},
6262
};
6363
use tower::ServiceExt;
@@ -73,8 +73,8 @@ mod tests {
7373
.unwrap();
7474
let response = app.clone().oneshot(request).await.unwrap();
7575
assert_eq!(response.status(), StatusCode::OK);
76-
assert!(body_data_json_check(
77-
response.into_body().data().await.unwrap().unwrap().to_vec(),
76+
assert_eq!(
77+
response_body_to_json(response).await.unwrap(),
7878
serde_json::json!(
7979
[
8080
{
@@ -125,7 +125,7 @@ mod tests {
125125
}
126126
}
127127
]
128-
)
129-
));
128+
),
129+
);
130130
}
131131
}

0 commit comments

Comments
 (0)