Skip to content
Draft
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/graphql/resolvers/canteen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
graphql::types::Date,
utils::db_error,
};
use async_graphql::{ComplexObject, Context, Object, Result, SimpleObject};
use async_graphql::{ComplexObject, Context, Error, Object, Result, SimpleObject};
use prometheus::{IntCounterVec, labels};
use sea_orm::{
DatabaseTransaction, FromQueryResult, JoinType,
Expand Down Expand Up @@ -86,7 +86,7 @@ impl CanteenQuery {
select_columns!(ctx, query, canteen_data::Column);
select_columns!(ctx, query, "menus" => canteen_data::Column::Id);

let (start, end) = iso_week_range(year, week).unwrap();
let (start, end) = iso_week_range(year, week).ok_or_else(|| Error::new("invalid week"))?;

query
.filter(canteen_data::Column::Date.lte(end))
Expand Down
31 changes: 20 additions & 11 deletions tests/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,29 @@ async fn canteen_week_without_menus_is_empty() {
}

#[tokio::test]
#[ignore = "bug: a canteen week that does not exist panics and returns HTTP 500"]
async fn nonexistent_canteen_week_is_a_graphql_error() {
let app = TestApp::seeded().await;

let response = app
.post("/graphql")
.json(&json!({ "query": "{ canteen(year: 2026, week: 0) { date } }" }))
.send()
.await
.expect("request");

assert_eq!(response.status(), StatusCode::OK);
let body: Value = response.json().await.expect("JSON body");
assert!(!error_messages(&body).is_empty(), "{body:#}");
// 2026 has 53 ISO weeks (1 January is a Thursday); 2025 has only 52.
for (year, week) in [(2026, 0), (2026, -1), (2026, 54), (2025, 53)] {
let response = app
.post("/graphql")
.json(&json!({
"query": "query ($year: Int!, $week: Int!) { canteen(year: $year, week: $week) { date } }",
"variables": { "year": year, "week": week },
}))
.send()
.await
.expect("request");

assert_eq!(response.status(), StatusCode::OK, "{year}-W{week}");
let body: Value = response.json().await.expect("JSON body");
assert_eq!(
error_messages(&body),
["invalid week"],
"{year}-W{week}: {body:#}"
);
}
}

#[tokio::test]
Expand Down