Skip to content

Fix the krate::downloads test #1545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/tests/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,14 +1185,19 @@ fn download() {
.expect_build(&conn);
});

let assert_dl_count = |name_and_version: &str, query: Option<&str>, count: usize| {
let assert_dl_count = |name_and_version: &str, query: Option<&str>, count: i32| {
let url = format!("/api/v1/crates/{}/downloads", name_and_version);
let downloads: Downloads = if let Some(query) = query {
anon.get_with_query(&url, query).good()
} else {
anon.get(&url).good()
};
assert_eq!(downloads.version_downloads.len(), count);
let total_downloads = downloads
.version_downloads
.iter()
.map(|vd| vd.downloads)
.sum::<i32>();
assert_eq!(total_downloads, count);
};

let download = |name_and_version: &str| {
Expand All @@ -1206,20 +1211,19 @@ fn download() {
assert_dl_count("foo_download", None, 1);

download("FOO_DOWNLOAD/1.0.0");
assert_dl_count("FOO_DOWNLOAD/1.0.0", None, 1);
assert_dl_count("FOO_DOWNLOAD", None, 1);
// TODO: Is the behavior above a bug?
assert_dl_count("FOO_DOWNLOAD/1.0.0", None, 2);
assert_dl_count("FOO_DOWNLOAD", None, 2);

let yesterday = (Utc::today() + Duration::days(-1)).format("%F");
let query = format!("before_date={}", yesterday);
assert_dl_count("FOO_DOWNLOAD/1.0.0", Some(&query), 0);
// crate/downloads always returns the last 90 days and ignores date params
assert_dl_count("FOO_DOWNLOAD", Some(&query), 1);
assert_dl_count("FOO_DOWNLOAD", Some(&query), 2);

let tomorrow = (Utc::today() + Duration::days(1)).format("%F");
let query = format!("before_date={}", tomorrow);
assert_dl_count("FOO_DOWNLOAD/1.0.0", Some(&query), 1);
assert_dl_count("FOO_DOWNLOAD", Some(&query), 1);
assert_dl_count("FOO_DOWNLOAD/1.0.0", Some(&query), 2);
assert_dl_count("FOO_DOWNLOAD", Some(&query), 2);
}

#[test]
Expand Down