Skip to content

Commit

Permalink
feat: add test judge
Browse files Browse the repository at this point in the history
  • Loading branch information
zrll12 committed Aug 18, 2024
1 parent 7be07fb commit 3f29895
Showing 1 changed file with 102 additions and 9 deletions.
111 changes: 102 additions & 9 deletions src/service/judge.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::collections::HashMap;
use std::str::FromStr;
use futures::executor::block_on;
use tracing::debug;
use crate::model::question::{Question, QuestionType};
use uuid::Uuid;
use crate::model::question::{Answer, Question, QuestionType};
use crate::model::ValueWithTitle;

pub async fn judge_subjectives(questions: Vec<Question>, answer: HashMap<String, String>) -> i32 {
let mut score = 0;
pub async fn judge_subjectives(questions: Vec<Question>, answer: HashMap<String, String>) -> (i32, HashMap<Uuid, i32>) {
let mut score = HashMap::new();
let mut full_score = 0;

for question in questions {
Expand All @@ -15,31 +19,36 @@ pub async fn judge_subjectives(questions: Vec<Question>, answer: HashMap<String,
let point = judge_multiple_choice(&ans.answer, answer.get(&question.id.to_string()).unwrap());
match point {
PointType::All => {
score += ans.all_points;
score.insert(question.id, ans.all_points);
}
PointType::Sub => {
if let Some(sub) = ans.sub_points {
score += sub;
score.insert(question.id, sub);
}
}
PointType::None => {}
PointType::None => {
score.insert(question.id, 0);

}
}
}
QuestionType::SingleChoice => {
let point = judge_single_choice(&ans.answer, answer.get(&question.id.to_string()).unwrap());
match point {
PointType::All => {
score += ans.all_points;
score.insert(question.id, ans.all_points);
}
PointType::None => {
score.insert(question.id, 0);
}
PointType::None => {}
_ => {}
}
}
_ => {}
}
}
}
score
(full_score, score)
}

pub fn judge_multiple_choice(answer: &str, user: &str) -> PointType {
Expand Down Expand Up @@ -84,4 +93,88 @@ pub enum PointType {
All,
Sub,
None,
}

#[test]
fn test_judge() {
let question1 = Question {
id: Uuid::from_str("d4135fda-00f3-4dd0-a19b-52150322912f").unwrap(),
content: ValueWithTitle {
title: "What is the capital of China?".to_string(),
content: "What is the capital of China?".to_string(),
},
r#type: QuestionType::SingleChoice,
values: Some(vec![
ValueWithTitle {
title: "Beijing".to_string(),
content: "Beijing".to_string(),
},
ValueWithTitle {
title: "Shanghai".to_string(),
content: "Shanghai".to_string(),
},
ValueWithTitle {
title: "Guangzhou".to_string(),
content: "Guangzhou".to_string(),
},
ValueWithTitle {
title: "Shenzhen".to_string(),
content: "Shenzhen".to_string(),
},
]),
condition: None,
required: true,
answer: Some(Answer {
answer: "0".to_string(),
all_points: 10,
sub_points: None,
}),
};

let question2 = Question {
id: Uuid::from_str("4135f52b-39a1-4aca-a19c-498ccb879725").unwrap(),
content: ValueWithTitle {
title: "Select A and B".to_string(),
content: "Hello!".to_string(),
},
r#type: QuestionType::MultipleChoice,
values: Some(vec![
ValueWithTitle {
title: "A".to_string(),
content: "A".to_string(),
},
ValueWithTitle {
title: "B".to_string(),
content: "B".to_string(),
},
ValueWithTitle {
title: "C".to_string(),
content: "C".to_string(),
},
ValueWithTitle {
title: "D".to_string(),
content: "D".to_string(),
},
]),
condition: None,
required: false,
answer: Some(Answer {
answer: "[\"0\",\"1\"]".to_string(),
all_points: 5,
sub_points: Some(3),
}),
};

let questions = vec![question1, question2];
let mut answer = HashMap::new();
answer.insert("d4135fda-00f3-4dd0-a19b-52150322912f".to_string(), "0".to_string());
answer.insert("4135f52b-39a1-4aca-a19c-498ccb879725".to_string(), "[\"0\"]".to_string());

let (full, scores) = block_on(judge_subjectives(questions, answer));

println!("{} {:?}", full, scores);

assert_eq!(full, 15);
assert_eq!(scores.get(&Uuid::from_str("d4135fda-00f3-4dd0-a19b-52150322912f").unwrap()), Some(&10));
assert_eq!(scores.get(&Uuid::from_str("4135f52b-39a1-4aca-a19c-498ccb879725").unwrap()), Some(&3));
}

0 comments on commit 3f29895

Please sign in to comment.