From 884b679ba862c99bd98d1af1be36e09edb7ac2ea Mon Sep 17 00:00:00 2001 From: MythEclipse Date: Sat, 11 Oct 2025 15:28:27 +0700 Subject: [PATCH] fix: Update string formatting in hackathon service and repository tests for consistency --- .../src/v1/hackathon/hackathon_schema.rs | 2 +- .../hackathon/hackathon_controller_test.rs | 29 ++++++----- .../hackathon/hackathon_repository_test.rs | 36 +++++++------- tests/src/hackathon/hackathon_service_test.rs | 48 ++++++++++--------- 4 files changed, 59 insertions(+), 56 deletions(-) diff --git a/imphnen-hackathon/src/v1/hackathon/hackathon_schema.rs b/imphnen-hackathon/src/v1/hackathon/hackathon_schema.rs index 3bdd53b..513c726 100644 --- a/imphnen-hackathon/src/v1/hackathon/hackathon_schema.rs +++ b/imphnen-hackathon/src/v1/hackathon/hackathon_schema.rs @@ -200,7 +200,7 @@ impl Default for HackathonParticipantSchema { fn default() -> Self { HackathonParticipantSchema { id: make_thing( - &"app_hackathon_participants".to_string(), + "app_hackathon_participants", &surrealdb::Uuid::new_v4().to_string(), ), hackathon_id: Thing::from(("app_hackathons".to_string(), surrealdb::sql::Id::rand())), diff --git a/tests/src/hackathon/hackathon_controller_test.rs b/tests/src/hackathon/hackathon_controller_test.rs index 5a2c06f..8d23205 100644 --- a/tests/src/hackathon/hackathon_controller_test.rs +++ b/tests/src/hackathon/hackathon_controller_test.rs @@ -101,7 +101,7 @@ mod tests { let hackathon_id = created.id.id.to_raw(); // Send get request - let response = app.service.get(&format!("/api/v1/hackathons/{}", hackathon_id)) + let response = app.service.get(format!("/api/v1/hackathons/{}", hackathon_id)) .await .unwrap(); @@ -163,7 +163,7 @@ mod tests { }; // Send update request - let response = app.service.put(&format!("/api/v1/hackathons/{}", hackathon_id)) + let response = app.service.put(format!("/api/v1/hackathons/{}", hackathon_id)) .header("Authorization", format!("Bearer {}", crate::get_test_token(&user.id.id.to_raw()).await)) .json(&update_payload) .await @@ -212,7 +212,7 @@ mod tests { let hackathon_id = create_result.id.id.to_raw(); // Send delete request - let response = app.service.delete(&format!("/api/v1/hackathons/{}", hackathon_id)) + let response = app.service.delete(format!("/api/v1/hackathons/{}", hackathon_id)) .header("Authorization", format!("Bearer {}", crate::get_test_token(&user.id.id.to_raw()).await)) .await .unwrap(); @@ -223,7 +223,7 @@ mod tests { assert_eq!(body["message"], "Success delete hackathon"); // Verify hackathon is deleted - let get_response = app.service.get(&format!("/api/v1/hackathons/{}", hackathon_id)) + let get_response = app.service.get(format!("/api/v1/hackathons/{}", hackathon_id)) .await .unwrap(); assert_eq!(get_response.status(), StatusCode::NOT_FOUND); @@ -283,7 +283,7 @@ mod tests { }; // Send submission request (use team path; for single-user tests we pass participant id as team_id) - let response = app.service.post(&format!("/api/v1/hackathons/{}/teams/{}/submissions", hackathon_id, participant.id.id.to_raw())) + let response = app.service.post(format!("/api/v1/hackathons/{}/teams/{}/submissions", hackathon_id, participant.id.id.to_raw())) .header("Authorization", format!("Bearer {}", crate::get_test_token(&participant.id.id.to_raw()).await)) .json(&submission_dto) .await @@ -387,7 +387,7 @@ mod tests { }; // Send team submission request (use teams path) - let response = app.service.post(&format!("/api/v1/hackathons/{}/teams/{}/submissions", hackathon_id, team_id)) + let response = app.service.post(format!("/api/v1/hackathons/{}/teams/{}/submissions", hackathon_id, team_id)) .header("Authorization", format!("Bearer {}", crate::get_test_token(&member1.id.id.to_raw()).await)) .json(&team_submission_dto) .await @@ -471,7 +471,7 @@ mod tests { ]; for submission_dto in submission_dtos.iter() { - let response = app.service.post(&format!("/api/v1/hackathons/{}/teams/{}/submissions", hackathon_id, participant.id.id.to_raw())) + let response = app.service.post(format!("/api/v1/hackathons/{}/teams/{}/submissions", hackathon_id, participant.id.id.to_raw())) .header("Authorization", format!("Bearer {}", crate::get_test_token(&participant.id.id.to_raw()).await)) .json(submission_dto) .await @@ -480,7 +480,7 @@ mod tests { } // Get hackathon submissions - let response = app.service.get(&format!("/api/v1/hackathons/{}/submissions", hackathon_id)) + let response = app.service.get(format!("/api/v1/hackathons/{}/submissions", hackathon_id)) .await .unwrap(); @@ -581,7 +581,7 @@ mod tests { ]; for (i, create_req) in submission_requests.iter().enumerate() { - let response = app.service.post(&format!("/api/v1/hackathons/{}/teams/{}/submissions", hackathon_ids[i], participant.id.id.to_raw())) + let response = app.service.post(format!("/api/v1/hackathons/{}/teams/{}/submissions", hackathon_ids[i], participant.id.id.to_raw())) .header("Authorization", format!("Bearer {}", crate::get_test_token(&participant.id.id.to_raw()).await)) .json(create_req) .await @@ -590,7 +590,7 @@ mod tests { } // Get user's hackathon submissions - let response = app.service.get(&format!("/api/v1/users/{}/hackathon-submissions", participant.id.id.to_raw())) + let response = app.service.get(format!("/api/v1/users/{}/hackathon-submissions", participant.id.id.to_raw())) .await .unwrap(); @@ -694,8 +694,7 @@ mod tests { let body = crate::get_response_body(response).await; assert_eq!(body["data"].as_array().unwrap().len(), 1); assert_eq!(body["data"][0]["name"], "Rust Backend Hackathon Controller"); - assert_eq!(body["data"][0]["description"].as_str().unwrap().contains("Rust"), true); - assert!(body["data"][0]["description"].as_str().unwrap().contains("Rust")); + assert!(body["data"][0]["description"].as_str().unwrap().contains("Rust")); // Clean up - in a real test you would store and delete all created hackathons let _ = users_repo.query_delete_user(user.id.id.to_raw()).await; @@ -751,7 +750,7 @@ mod tests { technologies: vec![], }; - let submit_response = app.service.post(&format!("/api/v1/hackathons/{}/teams/{}/submissions", hackathon_id, participant.id.id.to_raw())) + let submit_response = app.service.post(format!("/api/v1/hackathons/{}/teams/{}/submissions", hackathon_id, participant.id.id.to_raw())) .header("Authorization", format!("Bearer {}", crate::get_test_token(&participant.id.id.to_raw()).await)) .json(&submission_dto) .await @@ -769,7 +768,7 @@ mod tests { "feedback": "Great project!" }); - let response = app.service.patch(&format!("/api/v1/hackathons/submissions/{}/status", submission_id)) + let response = app.service.patch(format!("/api/v1/hackathons/submissions/{}/status", submission_id)) .header("Authorization", format!("Bearer {}", crate::get_test_token(&organizer.id.id.to_raw()).await)) .json(&update_status) .await @@ -788,7 +787,7 @@ mod tests { "feedback": "Does not meet criteria" }); - let response2 = app.service.patch(&format!("/api/v1/hackathons/submissions/{}/status", submission_id)) + let response2 = app.service.patch(format!("/api/v1/hackathons/submissions/{}/status", submission_id)) .header("Authorization", format!("Bearer {}", crate::get_test_token(&organizer.id.id.to_raw()).await)) .json(&update_status2) .await diff --git a/tests/src/hackathon/hackathon_repository_test.rs b/tests/src/hackathon/hackathon_repository_test.rs index 061949b..8d4955f 100644 --- a/tests/src/hackathon/hackathon_repository_test.rs +++ b/tests/src/hackathon/hackathon_repository_test.rs @@ -62,7 +62,7 @@ mod tests { assert_eq!(retrieved_hackathon.registration_deadline, hackathon_request.registration_deadline); assert_eq!(retrieved_hackathon.theme, hackathon_request.theme); assert_eq!(retrieved_hackathon.rules, hackathon_request.rules); - assert_eq!(retrieved_hackathon.is_deleted, false); + assert!(!retrieved_hackathon.is_deleted); // Clean up let _ = repo.delete_hackathon(created.id.id.to_raw()).await; @@ -206,12 +206,12 @@ mod tests { let retrieved_submission = repo.get_hackathon_submission_by_id(submission.id.id.to_raw()).await.expect("Failed to get submission by ID"); assert_eq!(retrieved_submission.hackathon_id.id.to_raw(), created.id.id.to_raw()); - assert_eq!(retrieved_submission.team_id.id.to_raw(), team_schema.id.id.to_raw()); - assert_eq!(retrieved_submission.project_name, "My Rust Project"); - assert_eq!(retrieved_submission.description, "A cool Rust project for the hackathon"); + assert_eq!(retrieved_submission.team_id.as_ref().unwrap().id.to_raw(), team_schema.id.id.to_raw()); + assert_eq!(retrieved_submission.project_name, Some("My Rust Project".to_string())); + assert_eq!(retrieved_submission.description, Some("A cool Rust project for the hackathon".to_string())); assert_eq!(retrieved_submission.repository_url, Some("https://github.com/user/my-rust-project".to_string())); assert_eq!(retrieved_submission.demo_url, Some("https://my-rust-project.com".to_string())); - assert_eq!(retrieved_submission.submission_status, HackathonSubmissionStatus::Draft); + assert_eq!(retrieved_submission.submission_status, Some(HackathonSubmissionStatus::Draft)); // Clean up let _ = repo.delete_hackathon(created.id.id.to_raw()).await; @@ -314,12 +314,14 @@ mod tests { assert!(result.is_ok(), "Failed to get team submission by ID"); let retrieved_submission = result.unwrap(); - assert_eq!(retrieved_submission.project_name, create_submission.project_name); - assert_eq!(retrieved_submission.description, create_submission.description); + assert_eq!(retrieved_submission.project_name, Some(create_submission.project_name.clone())); + // second comparison should clone to avoid moved value + assert_eq!(retrieved_submission.project_name, Some(create_submission.project_name.clone())); + assert_eq!(retrieved_submission.description, Some(create_submission.description.clone())); assert_eq!(retrieved_submission.repository_url, create_submission.repository_url); assert_eq!(retrieved_submission.demo_url, create_submission.demo_url); assert_eq!(retrieved_submission.slides_url, create_submission.slides_url); - assert_eq!(retrieved_submission.submission_status, HackathonSubmissionStatus::Draft); + assert_eq!(retrieved_submission.submission_status, Some(HackathonSubmissionStatus::Draft)); // Clean up let _ = repo.delete_hackathon(hackathon_schema.id.id.to_raw()).await; @@ -404,10 +406,10 @@ mod tests { let submissions = submissions_result.unwrap().data; assert_eq!(submissions.len(), 2, "Should have 2 submissions"); - assert_eq!(submissions[0].project_name, "Project 1"); - assert_eq!(submissions[1].project_name, "Project 2"); - assert_eq!(submissions[0].submission_status, HackathonSubmissionStatus::Draft); - assert_eq!(submissions[1].submission_status, HackathonSubmissionStatus::Draft); + assert_eq!(submissions[0].project_name, Some("Project 1".to_string())); + assert_eq!(submissions[1].project_name, Some("Project 2".to_string())); + assert_eq!(submissions[0].submission_status, Some(HackathonSubmissionStatus::Draft)); + assert_eq!(submissions[1].submission_status, Some(HackathonSubmissionStatus::Draft)); // Clean up let _ = repo.delete_hackathon(hackathon_schema.id.id.to_raw()).await; @@ -510,10 +512,10 @@ mod tests { let submissions_list = submissions_result.unwrap().data; assert_eq!(submissions_list.len(), 2, "Should have 2 submissions"); - assert_eq!(submissions_list[0].project_name, "Project for Hackathon 1"); - assert_eq!(submissions_list[1].project_name, "Project for Hackathon 2"); - assert_eq!(submissions_list[0].submission_status, SubmissionStatus::Draft); - assert_eq!(submissions_list[1].submission_status, SubmissionStatus::Draft); + assert_eq!(submissions_list[0].project_name, Some("Project for Hackathon 1".to_string())); + assert_eq!(submissions_list[1].project_name, Some("Project for Hackathon 2".to_string())); + assert_eq!(submissions_list[0].submission_status, Some(SubmissionStatus::Draft)); + assert_eq!(submissions_list[1].submission_status, Some(SubmissionStatus::Draft)); // Clean up for hackathon_id in hackathon_ids { @@ -661,7 +663,7 @@ mod tests { // Submit the submission (mark as Submitted) using repository API and verify let _submitted_schema = repo.submit_hackathon_submission(submission_id.clone()).await.expect("Failed to submit hackathon submission"); let updated_submission = repo.get_hackathon_submission_by_id(submission_id.clone()).await.expect("Failed to get updated submission"); - assert_eq!(updated_submission.submission_status, HackathonSubmissionStatus::Submitted); + assert_eq!(updated_submission.submission_status, Some(HackathonSubmissionStatus::Submitted)); assert!(updated_submission.updated_at > updated_submission.created_at); // Clean up diff --git a/tests/src/hackathon/hackathon_service_test.rs b/tests/src/hackathon/hackathon_service_test.rs index 65d291a..4bc734b 100644 --- a/tests/src/hackathon/hackathon_service_test.rs +++ b/tests/src/hackathon/hackathon_service_test.rs @@ -206,10 +206,10 @@ mod tests { assert!(result.is_ok(), "Failed to get submission by ID via service"); let retrieved_submission = result.unwrap().data; - assert_eq!(retrieved_submission.hackathon_id, hackathon_id); - assert_eq!(retrieved_submission.project_name, submission_create_dto.project_name); - assert_eq!(retrieved_submission.description, submission_create_dto.description); - assert_eq!(retrieved_submission.submission_status, SubmissionStatus::Draft); + assert_eq!(retrieved_submission.hackathon_id, hackathon_id); + assert_eq!(retrieved_submission.project_name, submission_create_dto.project_name); + assert_eq!(retrieved_submission.description, submission_create_dto.description); + assert_eq!(retrieved_submission.submission_status, SubmissionStatus::Draft); // Clean up let _ = repo.delete_hackathon(hackathon_id).await; @@ -313,11 +313,11 @@ mod tests { assert!(result.is_ok(), "Failed to get team submission by ID via service"); let retrieved_submission = result.unwrap().data; - assert_eq!(retrieved_submission.hackathon_id, hackathon_id); - assert_eq!(retrieved_submission.team_id, team_id); - assert_eq!(retrieved_submission.project_name, team_submission_create.project_name); - assert_eq!(retrieved_submission.description, team_submission_create.description); - assert_eq!(retrieved_submission.submission_status, SubmissionStatus::Draft); + assert_eq!(retrieved_submission.hackathon_id, hackathon_id); + assert_eq!(retrieved_submission.team_id, team_id); + assert_eq!(retrieved_submission.project_name, team_submission_create.project_name); + assert_eq!(retrieved_submission.description, team_submission_create.description); + assert_eq!(retrieved_submission.submission_status, SubmissionStatus::Draft); // Clean up let _ = hackathon_repo.delete_hackathon(hackathon_id).await; @@ -403,10 +403,11 @@ mod tests { let submissions = submissions_result.unwrap().data; assert_eq!(submissions.len(), 2, "Should have 2 submissions via service"); - assert_eq!(submissions[0].project_name, "Project 1 Service"); - assert_eq!(submissions[1].project_name, "Project 2 Service"); - assert_eq!(submissions[0].submission_status, SubmissionStatus::Draft); - assert_eq!(submissions[1].submission_status, SubmissionStatus::Draft); + // service returns DTOs with concrete fields; compare their values directly + assert_eq!(submissions[0].project_name, "Project 1 Service"); + assert_eq!(submissions[1].project_name, "Project 2 Service"); + assert_eq!(submissions[0].submission_status, SubmissionStatus::Draft); + assert_eq!(submissions[1].submission_status, SubmissionStatus::Draft); // Clean up let _ = repo.delete_hackathon(hackathon_id).await; @@ -418,7 +419,7 @@ mod tests { async fn test_service_get_user_hackathon_submissions() { let app_state = crate::get_app_state().await; let users_repo = UsersRepository::new(&app_state); - let repo = HackathonRepository::new(&app_state); + let repo = HackathonRepository::new(&app_state); let _service = HackathonService; // Create test users @@ -510,11 +511,12 @@ mod tests { submissions.extend(res.data); } - assert_eq!(submissions.len(), 2, "Should have 2 submissions via service"); - assert_eq!(submissions[0].project_name, "Project for Hackathon 1 Service"); - assert_eq!(submissions[1].project_name, "Project for Hackathon 2 Service"); - assert_eq!(submissions[0].submission_status, SubmissionStatus::Draft); - assert_eq!(submissions[1].submission_status, SubmissionStatus::Draft); + assert_eq!(submissions.len(), 2, "Should have 2 submissions via service"); + // these are repository-level schemas; fields are Option + assert_eq!(submissions[0].project_name, Some("Project for Hackathon 1 Service".to_string())); + assert_eq!(submissions[1].project_name, Some("Project for Hackathon 2 Service".to_string())); + assert_eq!(submissions[0].submission_status, Some(SubmissionStatus::Draft)); + assert_eq!(submissions[1].submission_status, Some(SubmissionStatus::Draft)); // Clean up for hackathon_id in hackathon_ids { @@ -528,7 +530,7 @@ mod tests { async fn test_service_search_hackathons() { let app_state = crate::get_app_state().await; let users_repo = UsersRepository::new(&app_state); - let repo = HackathonRepository::new(&app_state); + let _repo = HackathonRepository::new(&app_state); let _service = HackathonService; // Create test organizer @@ -604,7 +606,7 @@ mod tests { async fn test_service_update_submission_status() { let app_state = crate::get_app_state().await; let users_repo = UsersRepository::new(&app_state); - let repo = HackathonRepository::new(&app_state); + let repo = HackathonRepository::new(&app_state); // Use static service methods via trait // Create test users @@ -675,7 +677,7 @@ mod tests { assert!(matches!(updated_submission.submission_status, SubmissionStatus::Draft | SubmissionStatus::Submitted)); // Clean up - let _ = repo.delete_hackathon(hackathon_id).await; + let _ = repo.delete_hackathon(hackathon_id).await; let _ = users_repo.query_delete_user(organizer.id.id.to_raw()).await; let _ = users_repo.query_delete_user(participant.id.id.to_raw()).await; } @@ -684,7 +686,7 @@ mod tests { async fn test_service_delete_hackathon() { let app_state = crate::get_app_state().await; let users_repo = UsersRepository::new(&app_state); - let repo = HackathonRepository::new(&app_state); + let _repo = HackathonRepository::new(&app_state); // Use static service methods via trait // Create test organizer