-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathtest_server_submissions.rs
More file actions
152 lines (140 loc) · 4.69 KB
/
Copy pathtest_server_submissions.rs
File metadata and controls
152 lines (140 loc) · 4.69 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#![allow(clippy::unwrap_used, clippy::expect_used)]
mod common;
use axum::http::StatusCode;
use sea_orm::{ActiveValue, EntityTrait};
use serde_json::Value;
use sql_entities::submissions;
async fn seed(db: &sea_orm::DatabaseConnection) {
let rows = [
(1, 0, "p1", "c1", "u1", "WA"),
(2, 1, "p1", "c1", "u1", "AC"),
(3, 2, "p1", "c1", "u2", "AC"),
(4, 3, "p2", "c1", "u1", "AC"),
(5, 150, "p1", "c1", "u2", "AC"),
];
let ams: Vec<_> = rows
.iter()
.map(|(id, t, p, c, u, r)| submissions::ActiveModel {
id: ActiveValue::Set(*id),
epoch_second: ActiveValue::Set(*t),
problem_id: ActiveValue::Set((*p).into()),
contest_id: ActiveValue::Set((*c).into()),
user_id: ActiveValue::Set((*u).into()),
language: ActiveValue::Set("Rust".into()),
point: ActiveValue::Set(0.0),
length: ActiveValue::Set(0),
result: ActiveValue::Set((*r).into()),
execution_time: ActiveValue::Set(None),
})
.collect();
submissions::Entity::insert_many(ams)
.exec(db)
.await
.unwrap();
}
#[tokio::test]
async fn test_user_submissions_from_time() {
let db = common::setup_db().await;
seed(&db).await;
let app = common::build_app_no_auth(db);
let resp = common::get(
&app,
"/atcoder-api/v3/user/submissions?user=U1&from_second=3",
)
.await;
assert_eq!(resp.status(), StatusCode::OK);
let body: Value = common::read_json(resp).await;
let arr = body.as_array().unwrap();
assert_eq!(arr.len(), 1);
assert_eq!(arr[0]["id"], 4);
}
#[tokio::test]
async fn test_user_submissions_missing_from_second_is_400() {
let db = common::setup_db().await;
let app = common::build_app_no_auth(db);
let resp = common::get(&app, "/atcoder-api/v3/user/submissions?user=u1").await;
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}
#[tokio::test]
async fn test_from_time_negative_is_400() {
let db = common::setup_db().await;
let app = common::build_app_no_auth(db);
let resp = common::get(&app, "/atcoder-api/v3/from/-1").await;
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}
#[tokio::test]
async fn test_from_time() {
let db = common::setup_db().await;
seed(&db).await;
let app = common::build_app_no_auth(db);
let resp = common::get(&app, "/atcoder-api/v3/from/100").await;
let body: Value = common::read_json(resp).await;
let arr = body.as_array().unwrap();
assert_eq!(arr.len(), 1);
assert_eq!(arr[0]["id"], 5);
}
#[tokio::test]
async fn test_recent() {
let db = common::setup_db().await;
seed(&db).await;
let app = common::build_app_no_auth(db);
let resp = common::get(&app, "/atcoder-api/v3/recent").await;
let body: Value = common::read_json(resp).await;
let arr = body.as_array().unwrap();
assert_eq!(arr.len(), 5);
assert_eq!(arr[0]["id"], 5); // id descending
}
#[tokio::test]
async fn test_submission_count() {
let db = common::setup_db().await;
seed(&db).await;
let app = common::build_app_no_auth(db);
let resp = common::get(
&app,
"/atcoder-api/v3/user/submission_count?user=u1&from_second=1&to_second=4",
)
.await;
let body: Value = common::read_json(resp).await;
// from=1 (inclusive), to=4 (exclusive) -> u1 ids 2, 4 (epoch 1, 3) qualify.
assert_eq!(body["count"], 2);
}
#[tokio::test]
async fn test_users_and_time_empty_list_returns_empty() {
let db = common::setup_db().await;
seed(&db).await;
let app = common::build_app_no_auth(db);
let resp = common::get(
&app,
"/atcoder-api/v3/users_and_time?users=&problems=p1&from=0&to=100",
)
.await;
assert_eq!(resp.status(), StatusCode::OK);
let body: Value = common::read_json(resp).await;
assert_eq!(body.as_array().unwrap().len(), 0);
}
#[tokio::test]
async fn test_users_and_time_from_greater_than_to_is_400() {
let db = common::setup_db().await;
let app = common::build_app_no_auth(db);
let resp = common::get(
&app,
"/atcoder-api/v3/users_and_time?users=u1&problems=p1&from=100&to=0",
)
.await;
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}
#[tokio::test]
async fn test_users_and_time() {
let db = common::setup_db().await;
seed(&db).await;
let app = common::build_app_no_auth(db);
let resp = common::get(
&app,
"/atcoder-api/v3/users_and_time?users=u1,u2&problems=p1&from=0&to=200",
)
.await;
let body: Value = common::read_json(resp).await;
let arr = body.as_array().unwrap();
// p1 AND (u1, u2) AND 0 <= epoch <= 200: ids 1, 2, 3, 5 -> 4 rows.
assert_eq!(arr.len(), 4);
}