Skip to content

Tests to ensure datetime JSON serialization stays the same #1222

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 3 commits into from
Jan 25, 2018
Merged
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
40 changes: 40 additions & 0 deletions src/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,10 @@ pub fn slugs(req: &mut Request) -> CargoResult<Response> {
#[cfg(test)]
mod tests {
use super::*;
use chrono::NaiveDate;
use diesel::connection::SimpleConnection;
use dotenv::dotenv;
use serde_json;
use std::env;

fn pg_connection() -> PgConnection {
Expand Down Expand Up @@ -398,4 +400,42 @@ mod tests {
let expected = vec![("Cat 3".to_string(), 6), ("Cat 1".to_string(), 3)];
assert_eq!(expected, categories);
}

#[test]
fn category_dates_serializes_to_rfc3339() {
let cat = EncodableCategory {
id: "".to_string(),
category: "".to_string(),
slug: "".to_string(),
description: "".to_string(),
crates_cnt: 1,
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
};
let json = serde_json::to_string(&cat).unwrap();
assert!(
json.as_str()
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
.is_some()
);
}

#[test]
fn category_with_sub_dates_serializes_to_rfc3339() {
let cat = EncodableCategoryWithSubcategories {
id: "".to_string(),
category: "".to_string(),
slug: "".to_string(),
description: "".to_string(),
crates_cnt: 1,
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
subcategories: vec![],
};
let json = serde_json::to_string(&cat).unwrap();
assert!(
json.as_str()
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
.is_some()
);
}

}
24 changes: 24 additions & 0 deletions src/crate_owner_invitation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,27 @@ fn decline_invite(
crate_owner_invitation: crate_invite,
}))
}

#[cfg(test)]
mod tests {
use super::*;
use chrono::NaiveDate;
use serde_json;

#[test]
fn crate_owner_invitation_serializes_to_rfc3339() {
let inv = EncodableCrateOwnerInvitation {
invited_by_username: "".to_string(),
crate_name: "".to_string(),
crate_id: 123,
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
};
let json = serde_json::to_string(&inv).unwrap();
assert!(
json.as_str()
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
.is_some()
);
}

}
23 changes: 21 additions & 2 deletions src/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,12 @@ pub fn show(req: &mut Request) -> CargoResult<Response> {
#[cfg(test)]
mod tests {
use super::*;
use dotenv::dotenv;
use std::env;
use chrono::NaiveDate;
use diesel;
use diesel::connection::SimpleConnection;
use dotenv::dotenv;
use serde_json;
use std::env;

fn pg_connection() -> PgConnection {
let _ = dotenv();
Expand All @@ -199,4 +201,21 @@ mod tests {
assert_eq!(associated.len(), 1);
assert_eq!(associated.first().unwrap().keyword, "no");
}

#[test]
fn keyword_serializes_to_rfc3339() {
let key = EncodableKeyword {
id: "".to_string(),
keyword: "".to_string(),
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
crates_cnt: 0,
};
let json = serde_json::to_string(&key).unwrap();
assert!(
json.as_str()
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
.is_some()
);
}

}
45 changes: 44 additions & 1 deletion src/krate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,9 @@ sql_function!(to_char, to_char_t, (a: Date, b: Text) -> Text);

#[cfg(test)]
mod tests {
use super::Crate;
use super::*;
use chrono::NaiveDate;
use serde_json;

#[test]
fn documentation_blacklist_no_url_provided() {
Expand Down Expand Up @@ -591,4 +593,45 @@ mod tests {
None
);
}

#[test]
fn crate_serializes_to_rfc3399() {
let crt = EncodableCrate {
id: "".to_string(),
name: "".to_string(),
updated_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
versions: None,
keywords: None,
categories: None,
badges: None,
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 12),
downloads: 0,
recent_downloads: None,
max_version: "".to_string(),
description: None,
homepage: None,
documentation: None,
repository: None,
links: CrateLinks {
version_downloads: "".to_string(),
versions: None,
owners: None,
owner_team: None,
owner_user: None,
reverse_dependencies: "".to_string(),
},
exact_match: false,
};
let json = serde_json::to_string(&crt).unwrap();
assert!(
json.as_str()
.find(r#""updated_at":"2017-01-06T14:23:11+00:00""#)
.is_some()
);
assert!(
json.as_str()
.find(r#""created_at":"2017-01-06T14:23:12+00:00""#)
.is_some()
);
}
}
53 changes: 53 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,56 @@ pub fn revoke(req: &mut Request) -> CargoResult<Response> {
struct R {}
Ok(req.json(&R {}))
}

#[cfg(test)]
mod tests {
use super::*;
use chrono::NaiveDate;
use serde_json;

#[test]
fn api_token_serializes_to_rfc3339() {
let tok = ApiToken {
id: 12345,
user_id: 23456,
token: "".to_string(),
name: "".to_string(),
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
last_used_at: Some(NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 12)),
};
let json = serde_json::to_string(&tok).unwrap();
assert!(
json.as_str()
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
.is_some()
);
assert!(
json.as_str()
.find(r#""last_used_at":"2017-01-06T14:23:12+00:00""#)
.is_some()
);
}

#[test]
fn encodeable_api_token_with_token_serializes_to_rfc3339() {
let tok = EncodableApiTokenWithToken {
id: 12345,
name: "".to_string(),
token: "".to_string(),
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
last_used_at: Some(NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 12)),
};
let json = serde_json::to_string(&tok).unwrap();
assert!(
json.as_str()
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
.is_some()
);
assert!(
json.as_str()
.find(r#""last_used_at":"2017-01-06T14:23:12+00:00""#)
.is_some()
);
}

}
41 changes: 41 additions & 0 deletions src/version/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,44 @@ fn version_and_crate(req: &mut Request) -> CargoResult<(Version, Crate)> {
})?;
Ok((version, krate))
}

#[cfg(test)]
mod tests {
use super::*;
use chrono::NaiveDate;
use serde_json;

#[test]
fn version_serializes_to_rfc3339() {
let ver = EncodableVersion {
id: 1,
krate: "".to_string(),
num: "".to_string(),
dl_path: "".to_string(),
readme_path: "".to_string(),
updated_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 12),
downloads: 0,
features: HashMap::new(),
yanked: false,
license: None,
links: VersionLinks {
dependencies: "".to_string(),
version_downloads: "".to_string(),
authors: "".to_string(),
},
};
let json = serde_json::to_string(&ver).unwrap();
assert!(
json.as_str()
.find(r#""updated_at":"2017-01-06T14:23:11+00:00""#)
.is_some()
);
assert!(
json.as_str()
.find(r#""created_at":"2017-01-06T14:23:12+00:00""#)
.is_some()
);
}

}