Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: api test macro #220

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 59 additions & 85 deletions crates/api/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,102 +5,76 @@ use futures::FutureExt;
use poem::http::StatusCode;
use test_util::TestContext;

#[tokio::test]
async fn healthcheck_ok() {
TestContext::with(|ctx| {
async move {
let response = ctx.client().get("/api/v1/healthcheck").send().await;
response.assert_status_is_ok();
macro_rules! test {
($name:ident, $test:expr) => {
#[tokio::test]
async fn $name() {
TestContext::with(|ctx| async { $test(ctx).await }.boxed()).await;
}
.boxed()
})
.await;
};
}

#[tokio::test]
async fn healthcheck_not_ok() {
TestContext::with(|mut ctx| {
async move {
ctx.db_mut().kill_db().unwrap();
let response = ctx.client().get("/api/v1/healthcheck").send().await;
response.assert_status(StatusCode::SERVICE_UNAVAILABLE);
}
.boxed()
})
.await;
}
test![healthcheck_ok, |ctx: TestContext| async move {
let response = ctx.client().get("/api/v1/healthcheck").send().await;
response.assert_status_is_ok();
}];

#[tokio::test]
async fn pr_not_found() {
TestContext::with(|ctx| {
async move {
let response = ctx.client().get("/api/v1/2134").send().await;
response.assert_status(StatusCode::NOT_FOUND);
response.assert_text("Pull request not found.").await;
}
.boxed()
})
.await;
}
test![healthcheck_not_ok, |mut ctx: TestContext| async move {
ctx.db_mut().kill_db().unwrap();
let response = ctx.client().get("/api/v1/healthcheck").send().await;
response.assert_status(StatusCode::SERVICE_UNAVAILABLE);
}];

#[tokio::test]
async fn pr_not_landed() {
TestContext::with(|ctx| {
async move {
let mut connection = ctx.db().connection().await.unwrap();
test![pr_not_found, |ctx: TestContext| async move {
let response = ctx.client().get("/api/v1/2134").send().await;
response.assert_status(StatusCode::NOT_FOUND);
response.assert_text("Pull request not found.").await;
}];

pr_tracker_store::Pr {
number: 123.try_into().unwrap(),
commit: Some("deadbeef".into()),
}
.upsert(&mut connection)
.await
.unwrap();
test![pr_not_landed, |ctx: TestContext| async move {
let mut connection = ctx.db().connection().await.unwrap();

let response = ctx.client().get("/api/v1/123").send().await;
response.assert_status_is_ok();
response
.assert_json(pr_tracker_api::LandedIn { branches: vec![] })
.await;
}
.boxed()
})
.await;
}
pr_tracker_store::Pr {
number: 123.try_into().unwrap(),
commit: Some("deadbeef".into()),
}
.upsert(&mut connection)
.await
.unwrap();

#[tokio::test]
async fn pr_landed() {
TestContext::with(|ctx| {
async move {
let connection = &mut ctx.db().connection().await.unwrap();
let response = ctx.client().get("/api/v1/123").send().await;
response.assert_status_is_ok();
response
.assert_json(pr_tracker_api::LandedIn { branches: vec![] })
.await;
}];

let branch = pr_tracker_store::Branch::get_or_insert(connection, "nixos-unstable")
.await
.unwrap();
test![pr_landed, |ctx: TestContext| async move {
let connection = &mut ctx.db().connection().await.unwrap();

let github_pr = pr_tracker_store::Pr {
number: 2134.try_into().unwrap(),
commit: Some("deadbeef".into()),
};
github_pr.clone().upsert(connection).await.unwrap();
let branch = pr_tracker_store::Branch::get_or_insert(connection, "nixos-unstable")
.await
.unwrap();

let landing = pr_tracker_store::Landing {
github_pr: github_pr.number,
branch_id: branch.id(),
};
let github_pr = pr_tracker_store::Pr {
number: 2134.try_into().unwrap(),
commit: Some("deadbeef".into()),
};
github_pr.clone().upsert(connection).await.unwrap();

landing.upsert(connection).await.unwrap();
let landing = pr_tracker_store::Landing {
github_pr: github_pr.number,
branch_id: branch.id(),
};

let response = ctx.client().get("/api/v1/2134").send().await;
response.assert_status_is_ok();
landing.upsert(connection).await.unwrap();

response
.assert_json(pr_tracker_api::LandedIn {
branches: vec![pr_tracker_api::Branch("nixos-unstable".to_owned())],
})
.await;
}
.boxed()
})
.await;
}
let response = ctx.client().get("/api/v1/2134").send().await;
response.assert_status_is_ok();

response
.assert_json(pr_tracker_api::LandedIn {
branches: vec![pr_tracker_api::Branch("nixos-unstable".to_owned())],
})
.await;
}];