Skip to content

Commit 87621e0

Browse files
LawnGnomeTurbo87
authored andcommitted
controllers/krate/delete: Reuse DOWNLOADS_PER_MONTH_LIMIT constant for assertions
Co-authored-by: Adam Harvey <[email protected]>
1 parent 6b966bd commit 87621e0

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/controllers/krate/delete.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use diesel_async::{AsyncConnection, RunQueryDsl};
1414
use http::request::Parts;
1515
use http::StatusCode;
1616

17+
const DOWNLOADS_PER_MONTH_LIMIT: u64 = 100;
18+
1719
pub async fn delete(Path(name): Path<String>, parts: Parts, app: AppState) -> AppResult<()> {
1820
let mut conn = app.db_write().await?;
1921

@@ -59,8 +61,6 @@ pub async fn delete(Path(name): Path<String>, parts: Parts, app: AppState) -> Ap
5961
return Err(custom(StatusCode::UNPROCESSABLE_ENTITY, msg));
6062
}
6163

62-
const DOWNLOADS_PER_MONTH_LIMIT: u64 = 100;
63-
6464
let age = Utc::now().signed_duration_since(created_at);
6565
let age_days = age.num_days().to_u64().unwrap_or(u64::MAX);
6666
let age_months = age_days.div_ceil(30);
@@ -146,7 +146,7 @@ mod tests {
146146
let crate_id = adjust_creation_date(&mut conn, "foo", 71).await?;
147147

148148
// Update downloads count so that it wouldn't be deletable if it wasn't new
149-
adjust_downloads(&mut conn, crate_id, 10_000).await?;
149+
adjust_downloads(&mut conn, crate_id, DOWNLOADS_PER_MONTH_LIMIT * 100).await?;
150150

151151
assert_crate_exists(&anon, "foo", true).await;
152152
assert!(upstream.crate_exists("foo")?);
@@ -181,7 +181,7 @@ mod tests {
181181

182182
publish_crate(&user, "foo").await;
183183
let crate_id = adjust_creation_date(&mut conn, "foo", 73).await?;
184-
adjust_downloads(&mut conn, crate_id, 100).await?;
184+
adjust_downloads(&mut conn, crate_id, DOWNLOADS_PER_MONTH_LIMIT).await?;
185185

186186
assert_crate_exists(&anon, "foo", true).await;
187187
assert!(upstream.crate_exists("foo")?);
@@ -322,7 +322,7 @@ mod tests {
322322

323323
publish_crate(&user, "foo").await;
324324
let crate_id = adjust_creation_date(&mut conn, "foo", 73).await?;
325-
adjust_downloads(&mut conn, crate_id, 101).await?;
325+
adjust_downloads(&mut conn, crate_id, DOWNLOADS_PER_MONTH_LIMIT + 1).await?;
326326

327327
let response = delete_crate(&user, "foo").await;
328328
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
@@ -384,8 +384,10 @@ mod tests {
384384
async fn adjust_downloads(
385385
conn: &mut AsyncPgConnection,
386386
crate_id: i32,
387-
downloads: i64,
387+
downloads: u64,
388388
) -> QueryResult<()> {
389+
let downloads = downloads.to_i64().unwrap_or(i64::MAX);
390+
389391
diesel::update(crate_downloads::table)
390392
.filter(crate_downloads::crate_id.eq(crate_id))
391393
.set(crate_downloads::downloads.eq(downloads))

0 commit comments

Comments
 (0)