Skip to content

Added tests for json serialization #743

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/docbuilder/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use postgres::Connection;
use std::collections::BTreeMap;
use std::time::Duration;

#[derive(Debug, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Limits {
memory: usize,
targets: usize,
Expand Down
213 changes: 213 additions & 0 deletions src/web/builds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,216 @@ pub fn build_list_handler(req: &mut Request) -> IronResult<Response> {
.to_resp("builds")
}
}

#[cfg(test)]
mod tests {
use super::*;
use rustc_serialize::json::Json;

#[test]
fn serialize_build() {
let time = time::get_time();
let mut build = Build {
id: 22,
rustc_version: "rustc 1.43.0 (4fb7144ed 2020-04-20)".to_string(),
cratesfyi_version: "docsrs 0.6.0 (3dd32ec 2020-05-01)".to_string(),
build_status: true,
build_time: time,
output: None,
};

let correct_json = format!(
r#"{{
"id": 22,
"rustc_version": "rustc 1.43.0 (4fb7144ed 2020-04-20)",
"cratesfyi_version": "docsrs 0.6.0 (3dd32ec 2020-05-01)",
"build_time": "{}",
"build_time_relative": "{}",
"output": null,
"build_status": true
}}"#,
time::at(time).rfc3339().to_string(),
duration_to_str(time),
);

// Have to call `.to_string()` here because for some reason rustc_serialize defaults to
// u64s for `Json::from_str`, which makes the `id`s unequal
assert_eq!(
Json::from_str(&correct_json).unwrap().to_string(),
build.to_json().to_string()
);

build.output = Some("some random stuff".to_string());
let correct_json = format!(
r#"{{
"id": 22,
"rustc_version": "rustc 1.43.0 (4fb7144ed 2020-04-20)",
"cratesfyi_version": "docsrs 0.6.0 (3dd32ec 2020-05-01)",
"build_time": "{}",
"build_time_relative": "{}",
"output": "some random stuff",
"build_status": true
}}"#,
time::at(time).rfc3339().to_string(),
duration_to_str(time),
);

// Have to call `.to_string()` here because for some reason rustc_serialize defaults to
// u64s for `Json::from_str`, which makes the `id`s unequal
assert_eq!(
Json::from_str(&correct_json).unwrap().to_string(),
build.to_json().to_string()
);
}

#[test]
fn serialize_build_page() {
let time = time::get_time();
let build = Build {
id: 22,
rustc_version: "rustc 1.43.0 (4fb7144ed 2020-04-20)".to_string(),
cratesfyi_version: "docsrs 0.6.0 (3dd32ec 2020-05-01)".to_string(),
build_status: true,
build_time: time,
output: None,
};
let limits = Limits::default();
let mut builds = BuildsPage {
metadata: Some(MetaData {
name: "serde".to_string(),
version: "1.0.0".to_string(),
description: Some("serde does stuff".to_string()),
target_name: None,
rustdoc_status: true,
default_target: "x86_64-unknown-linux-gnu".to_string(),
}),
builds: vec![build.clone()],
build_details: Some(build.clone()),
limits: limits.clone(),
};

let correct_json = format!(
r#"{{
"metadata": {{
"name": "serde",
"version": "1.0.0",
"description": "serde does stuff",
"target_name": null,
"rustdoc_status": true,
"default_target": "x86_64-unknown-linux-gnu"
}},
"builds": [{{
"id": 22,
"rustc_version": "rustc 1.43.0 (4fb7144ed 2020-04-20)",
"cratesfyi_version": "docsrs 0.6.0 (3dd32ec 2020-05-01)",
"build_time": "{time}",
"build_time_relative": "{time_rel}",
"output": null,
"build_status": true
}}],
"build_details": {{
"id": 22,
"rustc_version": "rustc 1.43.0 (4fb7144ed 2020-04-20)",
"cratesfyi_version": "docsrs 0.6.0 (3dd32ec 2020-05-01)",
"build_time": "{time}",
"build_time_relative": "{time_rel}",
"output": null,
"build_status": true
}},
"limits": {}
}}"#,
limits.for_website().to_json().to_string(),
time = time::at(time).rfc3339().to_string(),
time_rel = duration_to_str(time),
);

// Have to call `.to_string()` here because for some reason rustc_serialize defaults to
// u64s for `Json::from_str`, which makes the `id`s unequal
assert_eq!(
Json::from_str(&correct_json).unwrap().to_string(),
builds.to_json().to_string()
);

builds.metadata = None;
let correct_json = format!(
r#"{{
"metadata": null,
"builds": [{{
"id": 22,
"rustc_version": "rustc 1.43.0 (4fb7144ed 2020-04-20)",
"cratesfyi_version": "docsrs 0.6.0 (3dd32ec 2020-05-01)",
"build_time": "{time}",
"build_time_relative": "{time_rel}",
"output": null,
"build_status": true
}}],
"build_details": {{
"id": 22,
"rustc_version": "rustc 1.43.0 (4fb7144ed 2020-04-20)",
"cratesfyi_version": "docsrs 0.6.0 (3dd32ec 2020-05-01)",
"build_time": "{time}",
"build_time_relative": "{time_rel}",
"output": null,
"build_status": true
}},
"limits": {}
}}"#,
limits.for_website().to_json().to_string(),
time = time::at(time).rfc3339().to_string(),
time_rel = duration_to_str(time),
);

// Have to call `.to_string()` here because for some reason rustc_serialize defaults to
// u64s for `Json::from_str`, which makes the `id`s unequal
assert_eq!(
Json::from_str(&correct_json).unwrap().to_string(),
builds.to_json().to_string()
);

builds.builds = Vec::new();
let correct_json = format!(
r#"{{
"metadata": null,
"builds": [],
"build_details": {{
"id": 22,
"rustc_version": "rustc 1.43.0 (4fb7144ed 2020-04-20)",
"cratesfyi_version": "docsrs 0.6.0 (3dd32ec 2020-05-01)",
"build_time": "{time}",
"build_time_relative": "{time_rel}",
"output": null,
"build_status": true
}},
"limits": {}
}}"#,
limits.for_website().to_json().to_string(),
time = time::at(time).rfc3339().to_string(),
time_rel = duration_to_str(time),
);

// Have to call `.to_string()` here because for some reason rustc_serialize defaults to
// u64s for `Json::from_str`, which makes the `id`s unequal
assert_eq!(
Json::from_str(&correct_json).unwrap().to_string(),
builds.to_json().to_string()
);

builds.build_details = None;
let correct_json = format!(
r#"{{
"metadata": null,
"builds": [],
"build_details": null,
"limits": {}
}}"#,
limits.for_website().to_json().to_string(),
);

// Have to call `.to_string()` here because for some reason rustc_serialize defaults to
// u64s for `Json::from_str`, which makes the `id`s unequal
assert_eq!(
Json::from_str(&correct_json).unwrap().to_string(),
builds.to_json().to_string()
);
}
}
157 changes: 157 additions & 0 deletions src/web/crate_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,47 @@ impl CrateDetails {
// releases will always contain at least one element
&self.releases[0].version
}

#[cfg(test)]
pub fn default_tester(release_time: time::Timespec) -> Self {
Self {
name: "rcc".to_string(),
version: "100.0.0".to_string(),
description: None,
authors: vec![],
owners: vec![],
authors_json: None,
dependencies: None,
readme: None,
rustdoc: None,
release_time,
build_status: true,
last_successful_build: None,
rustdoc_status: true,
repository_url: None,
homepage_url: None,
keywords: None,
have_examples: true,
target_name: "x86_64-unknown-linux-gnu".to_string(),
releases: vec![],
github: true,
github_stars: None,
github_forks: None,
github_issues: None,
metadata: MetaData {
name: "serde".to_string(),
version: "1.0.0".to_string(),
description: Some("serde does stuff".to_string()),
target_name: None,
rustdoc_status: true,
default_target: "x86_64-unknown-linux-gnu".to_string(),
},
is_library: true,
doc_targets: vec![],
license: None,
documentation_url: None,
}
}
}

fn map_to_release(conn: &Connection, crate_id: i32, version: String) -> Release {
Expand Down Expand Up @@ -531,4 +572,120 @@ mod tests {
Ok(())
})
}

#[test]
fn serialize_crate_details() {
let time = time::get_time();
let mut details = CrateDetails::default_tester(time);

let correct_json = Json::from_str(&format!(
r#"{{
"name": "rcc",
"version": "100.0.0",
"description": null,
"authors": [],
"owners": [],
"authors_json": null,
"dependencies": null,
"release_time": "{}",
"build_status": true,
"last_successful_build": null,
"rustdoc_status": true,
"repository_url": null,
"homepage_url": null,
"keywords": null,
"have_examples": true,
"target_name": "x86_64-unknown-linux-gnu",
"releases": [],
"github": true,
"github_stars": null,
"github_forks": null,
"github_issues": null,
"metadata": {{
"name": "serde",
"version": "1.0.0",
"description": "serde does stuff",
"target_name": null,
"rustdoc_status": true,
"default_target": "x86_64-unknown-linux-gnu"
}},
"is_library": true,
"doc_targets": [],
"license": null,
"documentation_url": null
}}"#,
super::super::duration_to_str(time),
))
.unwrap();

assert_eq!(correct_json, details.to_json());

details.description = Some("serde does stuff".to_string());
details.owners = vec![("Owner".to_string(), "owner@ownsstuff.com".to_string())];

let authors = vec![("Somebody".to_string(), "somebody@somebody.com".to_string())];
details.authors_json = Some(authors.to_json());
details.authors = authors;

let correct_json = Json::from_str(&format!(
r#"{{
"name": "rcc",
"version": "100.0.0",
"description": "serde does stuff",
"authors": [["Somebody", "somebody@somebody.com"]],
"owners": [["Owner", "owner@ownsstuff.com"]],
"authors_json": [["Somebody", "somebody@somebody.com"]],
"dependencies": null,
"release_time": "{}",
"build_status": true,
"last_successful_build": null,
"rustdoc_status": true,
"repository_url": null,
"homepage_url": null,
"keywords": null,
"have_examples": true,
"target_name": "x86_64-unknown-linux-gnu",
"releases": [],
"github": true,
"github_stars": null,
"github_forks": null,
"github_issues": null,
"metadata": {{
"name": "serde",
"version": "1.0.0",
"description": "serde does stuff",
"target_name": null,
"rustdoc_status": true,
"default_target": "x86_64-unknown-linux-gnu"
}},
"is_library": true,
"doc_targets": [],
"license": null,
"documentation_url": null
}}"#,
super::super::duration_to_str(time),
))
.unwrap();

assert_eq!(correct_json, details.to_json());
}

#[test]
fn serialize_releases() {
let release = Release {
version: "idkman".to_string(),
build_status: true,
yanked: true,
};

let correct_json = Json::from_str(
r#"{
"version": "idkman",
"build_status": true
}"#,
)
.unwrap();

assert_eq!(correct_json, release.to_json());
}
}
Loading