Skip to content

Commit

Permalink
feat: Review's importer test | NPG-8040 (#540)
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
Mr-Leshiy authored Aug 31, 2023
1 parent b3182ed commit d9a8e73
Show file tree
Hide file tree
Showing 29 changed files with 525 additions and 416 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ jobs:
run:
sudo apt install -y protobuf-compiler libssl-dev libpq-dev libsqlite3-dev pkg-config

# We are excluding cat-dat-service and event-db because we are already running it with Earthly
- name: Build and archive tests
run: |
cargo nextest archive \
Expand All @@ -137,6 +138,8 @@ jobs:
--exclude vit-servicing-station-server \
--exclude vit-servicing-station-tests \
--exclude vit-servicing-station-lib \
--exclude cat-data-service \
--exclude event-db \
--archive-file nextest-archive.tar.zst
- name: Save test archive
Expand Down Expand Up @@ -235,6 +238,7 @@ jobs:
PGPASSWORD: 123456
run: cargo make local-event-db-test -h 127.0.0.1 -U postgres

# We are excluding cat-dat-service and event-db because we are already running it with Earthly
- name: Build and archive tests
if: steps.archive-cache.outputs.cache-hit != 'true'
run: |
Expand All @@ -248,6 +252,8 @@ jobs:
--exclude vit-servicing-station-server \
--exclude vit-servicing-station-tests \
--exclude vit-servicing-station-lib \
--exclude cat-data-service \
--exclude event-db \
--archive-file nextest-archive.tar.zst
- name: Run Catalyst Core tests
Expand Down
19 changes: 16 additions & 3 deletions src/cat-data-service/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,23 @@ async fn track_metrics<T>(req: Request<T>, next: Next<T>) -> impl IntoResponse {

#[cfg(test)]
pub mod tests {
use axum::body::HttpBody;
use std::str::FromStr;

pub fn body_data_json_check(body_data: Vec<u8>, expected_json: serde_json::Value) -> bool {
serde_json::Value::from_str(String::from_utf8(body_data).unwrap().as_str()).unwrap()
== expected_json
pub async fn response_body_to_json<
T: HttpBody<Data = impl Into<Vec<u8>>, Error = axum::Error> + Unpin,
>(
response: axum::response::Response<T>,
) -> Result<serde_json::Value, String> {
let data: Vec<u8> = response
.into_body()
.data()
.await
.ok_or("response should have data in a body".to_string())?
.map_err(|e| e.to_string())?
.into();

serde_json::Value::from_str(String::from_utf8(data).map_err(|e| e.to_string())?.as_str())
.map_err(|e| e.to_string())
}
}
12 changes: 6 additions & 6 deletions src/cat-data-service/src/service/v0/fund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ async fn fund_exec(state: Arc<State>) -> Result<SerdeType<FundWithNext>, Error>
#[cfg(test)]
mod tests {
use super::*;
use crate::service::{app, tests::body_data_json_check};
use crate::service::{app, tests::response_body_to_json};
use axum::{
body::{Body, HttpBody},
body::Body,
http::{Request, StatusCode},
};
use tower::ServiceExt;
Expand All @@ -57,8 +57,8 @@ mod tests {
.unwrap();
let response = app.clone().oneshot(request).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
assert!(body_data_json_check(
response.into_body().data().await.unwrap().unwrap().to_vec(),
assert_eq!(
response_body_to_json(response).await.unwrap(),
serde_json::json!(
{
"id": 4,
Expand Down Expand Up @@ -178,7 +178,7 @@ mod tests {
"tallying_end": "1970-01-01T00:00:00+00:00",
}
}
)
));
),
);
}
}
12 changes: 6 additions & 6 deletions src/cat-data-service/src/service/v1/event/ballots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ async fn ballots_exec(
#[cfg(test)]
mod tests {
use super::*;
use crate::service::{app, tests::body_data_json_check};
use crate::service::{app, tests::response_body_to_json};
use axum::{
body::{Body, HttpBody},
body::Body,
http::{Request, StatusCode},
};
use tower::ServiceExt;
Expand All @@ -66,8 +66,8 @@ mod tests {
.unwrap();
let response = app.clone().oneshot(request).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
assert!(body_data_json_check(
response.into_body().data().await.unwrap().unwrap().to_vec(),
assert_eq!(
response_body_to_json(response).await.unwrap(),
serde_json::json!([
{
"objective_id": 1,
Expand Down Expand Up @@ -121,7 +121,7 @@ mod tests {
}
]
}
])
));
]),
);
}
}
95 changes: 54 additions & 41 deletions src/cat-data-service/src/service/v1/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ async fn events_exec(
#[cfg(test)]
mod tests {
use super::*;
use crate::service::{app, tests::body_data_json_check};
use crate::service::{app, tests::response_body_to_json};
use axum::{
body::{Body, HttpBody},
body::Body,
http::{Request, StatusCode},
};
use tower::ServiceExt;
Expand All @@ -105,8 +105,8 @@ mod tests {
.unwrap();
let response = app.clone().oneshot(request).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
assert!(body_data_json_check(
response.into_body().data().await.unwrap().unwrap().to_vec(),
assert_eq!(
response_body_to_json(response).await.unwrap(),
serde_json::json!(
{
"id": 1,
Expand Down Expand Up @@ -154,11 +154,11 @@ mod tests {
}
]
}
)
));
),
);

let request = Request::builder()
.uri(format!("/api/v1/event/{0}", 10))
.uri(format!("/api/v1/event/{0}", 100))
.body(Body::empty())
.unwrap();
let response = app.clone().oneshot(request).await.unwrap();
Expand All @@ -176,10 +176,17 @@ mod tests {
.unwrap();
let response = app.clone().oneshot(request).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
assert!(body_data_json_check(
response.into_body().data().await.unwrap().unwrap().to_vec(),
assert_eq!(
response_body_to_json(response).await.unwrap(),
serde_json::json!(
[
{
"id": 0,
"name": "Test Fund",
"starts": "1970-01-01T00:00:00+00:00",
"ends": "1970-01-01T00:00:00+00:00",
"final": true
},
{
"id": 1,
"name": "Test Fund 1",
Expand Down Expand Up @@ -215,21 +222,29 @@ mod tests {
"id": 5,
"name": "Test Fund 5",
"final": false
},
}
]
)
));
),
);

let request = Request::builder()
.uri(format!("/api/v1/events?offset={0}", 1))
.body(Body::empty())
.unwrap();
let response = app.clone().oneshot(request).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
assert!(body_data_json_check(
response.into_body().data().await.unwrap().unwrap().to_vec(),
assert_eq!(
response_body_to_json(response).await.unwrap(),
serde_json::json!(
[
{
"id": 1,
"name": "Test Fund 1",
"starts": "2020-05-01T12:00:00+00:00",
"ends": "2020-06-01T12:00:00+00:00",
"reg_checked": "2020-03-31T12:00:00+00:00",
"final": true,
},
{
"id": 2,
"name": "Test Fund 2",
Expand Down Expand Up @@ -257,64 +272,62 @@ mod tests {
"id": 5,
"name": "Test Fund 5",
"final": false
},
}
]
)
));
),
);

let request = Request::builder()
.uri(format!("/api/v1/events?limit={0}", 1))
.body(Body::empty())
.unwrap();
let response = app.clone().oneshot(request).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
assert!(body_data_json_check(
response.into_body().data().await.unwrap().unwrap().to_vec(),
assert_eq!(
response_body_to_json(response).await.unwrap(),
serde_json::json!(
[
{
"id": 1,
"name": "Test Fund 1",
"starts": "2020-05-01T12:00:00+00:00",
"ends": "2020-06-01T12:00:00+00:00",
"reg_checked": "2020-03-31T12:00:00+00:00",
"final": true,
},
"id": 0,
"name": "Test Fund",
"starts": "1970-01-01T00:00:00+00:00",
"ends": "1970-01-01T00:00:00+00:00",
"final": true
}
]
)
));
),
);

let request = Request::builder()
.uri(format!("/api/v1/events?limit={0}&offset={1}", 1, 1))
.body(Body::empty())
.unwrap();
let response = app.clone().oneshot(request).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
assert!(body_data_json_check(
response.into_body().data().await.unwrap().unwrap().to_vec(),
assert_eq!(
response_body_to_json(response).await.unwrap(),
serde_json::json!(
[
{
"id": 2,
"name": "Test Fund 2",
"starts": "2021-05-01T12:00:00+00:00",
"ends": "2021-06-01T12:00:00+00:00",
"reg_checked": "2021-03-31T12:00:00+00:00",
"id": 1,
"name": "Test Fund 1",
"starts": "2020-05-01T12:00:00+00:00",
"ends": "2020-06-01T12:00:00+00:00",
"reg_checked": "2020-03-31T12:00:00+00:00",
"final": true,
},
}
]
)
));
),
);

let request = Request::builder()
.uri(format!("/api/v1/events?offset={0}", 10))
.body(Body::empty())
.unwrap();
let response = app.clone().oneshot(request).await.unwrap();
assert_eq!(
String::from_utf8(response.into_body().data().await.unwrap().unwrap().to_vec())
.unwrap(),
serde_json::to_string(&Vec::<()>::new()).unwrap()
response_body_to_json(response).await.unwrap(),
serde_json::json!([])
);
}
}
12 changes: 6 additions & 6 deletions src/cat-data-service/src/service/v1/event/objective/ballots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ async fn ballots_exec(
#[cfg(test)]
mod tests {
use super::*;
use crate::service::{app, tests::body_data_json_check};
use crate::service::{app, tests::response_body_to_json};
use axum::{
body::{Body, HttpBody},
body::Body,
http::{Request, StatusCode},
};
use tower::ServiceExt;
Expand All @@ -73,8 +73,8 @@ mod tests {
.unwrap();
let response = app.clone().oneshot(request).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
assert!(body_data_json_check(
response.into_body().data().await.unwrap().unwrap().to_vec(),
assert_eq!(
response_body_to_json(response).await.unwrap(),
serde_json::json!(
[
{
Expand Down Expand Up @@ -125,7 +125,7 @@ mod tests {
}
}
]
)
));
),
);
}
}
Loading

0 comments on commit d9a8e73

Please sign in to comment.