Skip to content

Commit 6cb501f

Browse files
Merge #1222
1222: Tests to ensure datetime JSON serialization stays the same r=carols10cents 99c459b fixed the serialization to be RFC3339 compliant with the timezone being represented as `+00:00`. This simply adds tests for the structs where we use `#[serde(with = "::util::rfc3339")]` to ensure the JSON representation of those dates stay the same. Fixes #1164
2 parents 8a72215 + 53d14d4 commit 6cb501f

File tree

6 files changed

+223
-3
lines changed

6 files changed

+223
-3
lines changed

src/category.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,10 @@ pub fn slugs(req: &mut Request) -> CargoResult<Response> {
266266
#[cfg(test)]
267267
mod tests {
268268
use super::*;
269+
use chrono::NaiveDate;
269270
use diesel::connection::SimpleConnection;
270271
use dotenv::dotenv;
272+
use serde_json;
271273
use std::env;
272274

273275
fn pg_connection() -> PgConnection {
@@ -398,4 +400,42 @@ mod tests {
398400
let expected = vec![("Cat 3".to_string(), 6), ("Cat 1".to_string(), 3)];
399401
assert_eq!(expected, categories);
400402
}
403+
404+
#[test]
405+
fn category_dates_serializes_to_rfc3339() {
406+
let cat = EncodableCategory {
407+
id: "".to_string(),
408+
category: "".to_string(),
409+
slug: "".to_string(),
410+
description: "".to_string(),
411+
crates_cnt: 1,
412+
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
413+
};
414+
let json = serde_json::to_string(&cat).unwrap();
415+
assert!(
416+
json.as_str()
417+
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
418+
.is_some()
419+
);
420+
}
421+
422+
#[test]
423+
fn category_with_sub_dates_serializes_to_rfc3339() {
424+
let cat = EncodableCategoryWithSubcategories {
425+
id: "".to_string(),
426+
category: "".to_string(),
427+
slug: "".to_string(),
428+
description: "".to_string(),
429+
crates_cnt: 1,
430+
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
431+
subcategories: vec![],
432+
};
433+
let json = serde_json::to_string(&cat).unwrap();
434+
assert!(
435+
json.as_str()
436+
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
437+
.is_some()
438+
);
439+
}
440+
401441
}

src/crate_owner_invitation.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,27 @@ fn decline_invite(
173173
crate_owner_invitation: crate_invite,
174174
}))
175175
}
176+
177+
#[cfg(test)]
178+
mod tests {
179+
use super::*;
180+
use chrono::NaiveDate;
181+
use serde_json;
182+
183+
#[test]
184+
fn crate_owner_invitation_serializes_to_rfc3339() {
185+
let inv = EncodableCrateOwnerInvitation {
186+
invited_by_username: "".to_string(),
187+
crate_name: "".to_string(),
188+
crate_id: 123,
189+
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
190+
};
191+
let json = serde_json::to_string(&inv).unwrap();
192+
assert!(
193+
json.as_str()
194+
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
195+
.is_some()
196+
);
197+
}
198+
199+
}

src/keyword.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,12 @@ pub fn show(req: &mut Request) -> CargoResult<Response> {
169169
#[cfg(test)]
170170
mod tests {
171171
use super::*;
172-
use dotenv::dotenv;
173-
use std::env;
172+
use chrono::NaiveDate;
174173
use diesel;
175174
use diesel::connection::SimpleConnection;
175+
use dotenv::dotenv;
176+
use serde_json;
177+
use std::env;
176178

177179
fn pg_connection() -> PgConnection {
178180
let _ = dotenv();
@@ -199,4 +201,21 @@ mod tests {
199201
assert_eq!(associated.len(), 1);
200202
assert_eq!(associated.first().unwrap().keyword, "no");
201203
}
204+
205+
#[test]
206+
fn keyword_serializes_to_rfc3339() {
207+
let key = EncodableKeyword {
208+
id: "".to_string(),
209+
keyword: "".to_string(),
210+
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
211+
crates_cnt: 0,
212+
};
213+
let json = serde_json::to_string(&key).unwrap();
214+
assert!(
215+
json.as_str()
216+
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
217+
.is_some()
218+
);
219+
}
220+
202221
}

src/krate/mod.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,9 @@ sql_function!(to_char, to_char_t, (a: Date, b: Text) -> Text);
557557

558558
#[cfg(test)]
559559
mod tests {
560-
use super::Crate;
560+
use super::*;
561+
use chrono::NaiveDate;
562+
use serde_json;
561563

562564
#[test]
563565
fn documentation_blacklist_no_url_provided() {
@@ -591,4 +593,45 @@ mod tests {
591593
None
592594
);
593595
}
596+
597+
#[test]
598+
fn crate_serializes_to_rfc3399() {
599+
let crt = EncodableCrate {
600+
id: "".to_string(),
601+
name: "".to_string(),
602+
updated_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
603+
versions: None,
604+
keywords: None,
605+
categories: None,
606+
badges: None,
607+
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 12),
608+
downloads: 0,
609+
recent_downloads: None,
610+
max_version: "".to_string(),
611+
description: None,
612+
homepage: None,
613+
documentation: None,
614+
repository: None,
615+
links: CrateLinks {
616+
version_downloads: "".to_string(),
617+
versions: None,
618+
owners: None,
619+
owner_team: None,
620+
owner_user: None,
621+
reverse_dependencies: "".to_string(),
622+
},
623+
exact_match: false,
624+
};
625+
let json = serde_json::to_string(&crt).unwrap();
626+
assert!(
627+
json.as_str()
628+
.find(r#""updated_at":"2017-01-06T14:23:11+00:00""#)
629+
.is_some()
630+
);
631+
assert!(
632+
json.as_str()
633+
.find(r#""created_at":"2017-01-06T14:23:12+00:00""#)
634+
.is_some()
635+
);
636+
}
594637
}

src/token.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,56 @@ pub fn revoke(req: &mut Request) -> CargoResult<Response> {
146146
struct R {}
147147
Ok(req.json(&R {}))
148148
}
149+
150+
#[cfg(test)]
151+
mod tests {
152+
use super::*;
153+
use chrono::NaiveDate;
154+
use serde_json;
155+
156+
#[test]
157+
fn api_token_serializes_to_rfc3339() {
158+
let tok = ApiToken {
159+
id: 12345,
160+
user_id: 23456,
161+
token: "".to_string(),
162+
name: "".to_string(),
163+
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
164+
last_used_at: Some(NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 12)),
165+
};
166+
let json = serde_json::to_string(&tok).unwrap();
167+
assert!(
168+
json.as_str()
169+
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
170+
.is_some()
171+
);
172+
assert!(
173+
json.as_str()
174+
.find(r#""last_used_at":"2017-01-06T14:23:12+00:00""#)
175+
.is_some()
176+
);
177+
}
178+
179+
#[test]
180+
fn encodeable_api_token_with_token_serializes_to_rfc3339() {
181+
let tok = EncodableApiTokenWithToken {
182+
id: 12345,
183+
name: "".to_string(),
184+
token: "".to_string(),
185+
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
186+
last_used_at: Some(NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 12)),
187+
};
188+
let json = serde_json::to_string(&tok).unwrap();
189+
assert!(
190+
json.as_str()
191+
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
192+
.is_some()
193+
);
194+
assert!(
195+
json.as_str()
196+
.find(r#""last_used_at":"2017-01-06T14:23:12+00:00""#)
197+
.is_some()
198+
);
199+
}
200+
201+
}

src/version/mod.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,44 @@ fn version_and_crate(req: &mut Request) -> CargoResult<(Version, Crate)> {
267267
})?;
268268
Ok((version, krate))
269269
}
270+
271+
#[cfg(test)]
272+
mod tests {
273+
use super::*;
274+
use chrono::NaiveDate;
275+
use serde_json;
276+
277+
#[test]
278+
fn version_serializes_to_rfc3339() {
279+
let ver = EncodableVersion {
280+
id: 1,
281+
krate: "".to_string(),
282+
num: "".to_string(),
283+
dl_path: "".to_string(),
284+
readme_path: "".to_string(),
285+
updated_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
286+
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 12),
287+
downloads: 0,
288+
features: HashMap::new(),
289+
yanked: false,
290+
license: None,
291+
links: VersionLinks {
292+
dependencies: "".to_string(),
293+
version_downloads: "".to_string(),
294+
authors: "".to_string(),
295+
},
296+
};
297+
let json = serde_json::to_string(&ver).unwrap();
298+
assert!(
299+
json.as_str()
300+
.find(r#""updated_at":"2017-01-06T14:23:11+00:00""#)
301+
.is_some()
302+
);
303+
assert!(
304+
json.as_str()
305+
.find(r#""created_at":"2017-01-06T14:23:12+00:00""#)
306+
.is_some()
307+
);
308+
}
309+
310+
}

0 commit comments

Comments
 (0)