forked from kenkoooo/AtCoderProblems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_problem_info.rs
More file actions
131 lines (117 loc) · 3.51 KB
/
test_problem_info.rs
File metadata and controls
131 lines (117 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use sql_client::models::{Contest, Submission};
use sql_client::problem_info::ProblemInfoUpdater;
use sql_client::simple_client::SimpleClient;
use sql_client::submission_client::SubmissionClient;
use sql_client::PgPool;
use sqlx::postgres::PgRow;
use sqlx::Row;
mod utils;
async fn get_solver(pool: &PgPool) -> Vec<(String, i32)> {
sqlx::query("SELECT problem_id, user_count FROM solver")
.map(|row: PgRow| {
let problem_id: String = row.get("problem_id");
let user_count: i32 = row.get("user_count");
(problem_id, user_count)
})
.fetch_all(pool)
.await
.unwrap()
}
async fn get_points(pool: &PgPool) -> Vec<(String, Option<f64>)> {
sqlx::query("SELECT problem_id, point FROM points")
.map(|row: PgRow| {
let problem_id: String = row.get("problem_id");
let point: Option<f64> = row.get("point");
(problem_id, point)
})
.fetch_all(pool)
.await
.unwrap()
}
#[sqlx::test]
async fn test_update_problem_solver_count(pool: PgPool) {
utils::initialize(&pool).await;
assert!(get_solver(&pool).await.is_empty());
pool.update_submissions(&[
Submission {
id: 0,
user_id: "user1".to_string(),
result: "AC".to_string(),
problem_id: "problem".to_string(),
..Default::default()
},
Submission {
id: 1,
user_id: "user2".to_string(),
result: "AC".to_string(),
problem_id: "problem".to_string(),
..Default::default()
},
Submission {
id: 2,
user_id: "user3".to_string(),
result: "WA".to_string(),
problem_id: "problem".to_string(),
..Default::default()
},
])
.await
.unwrap();
pool.update_solver_count().await.unwrap();
assert_eq!(get_solver(&pool).await, vec![("problem".to_string(), 2)]);
pool.update_submissions(&[Submission {
id: 3,
user_id: "user3".to_string(),
result: "AC".to_string(),
problem_id: "problem".to_string(),
..Default::default()
}])
.await
.unwrap();
pool.update_solver_count().await.unwrap();
assert_eq!(get_solver(&pool).await, vec![("problem".to_string(), 3)]);
}
#[sqlx::test]
async fn test_update_problem_points(pool: PgPool) {
let contest_id = "contest";
let problem_id = "problem";
utils::initialize(&pool).await;
pool.insert_contests(&[Contest {
id: contest_id.to_string(),
start_epoch_second: 1468670400,
rate_change: "All".to_string(),
duration_second: 0,
title: "".to_string(),
}])
.await
.unwrap();
assert!(get_points(&pool).await.is_empty());
pool.update_submissions(&[Submission {
id: 0,
point: 0.0,
problem_id: problem_id.to_string(),
contest_id: contest_id.to_string(),
..Default::default()
}])
.await
.unwrap();
pool.update_problem_points().await.unwrap();
assert_eq!(
get_points(&pool).await,
vec![("problem".to_string(), Some(0.0))]
);
pool.update_submissions(&[Submission {
id: 1,
point: 100.0,
problem_id: problem_id.to_string(),
contest_id: contest_id.to_string(),
..Default::default()
}])
.await
.unwrap();
pool.update_problem_points().await.unwrap();
assert_eq!(
get_points(&pool).await,
vec![("problem".to_string(), Some(100.0))]
);
}