Skip to content

Commit 658b62f

Browse files
committed
Move rfc3339 tests to the views module
1 parent e0cff8b commit 658b62f

File tree

4 files changed

+98
-106
lines changed

4 files changed

+98
-106
lines changed

src/models/category.rs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,10 @@ impl<'a> NewCategory<'a> {
158158
#[cfg(test)]
159159
mod tests {
160160
use super::*;
161-
use chrono::NaiveDate;
162161
use diesel::connection::SimpleConnection;
163162
use dotenv::dotenv;
164-
use serde_json;
165163
use std::env;
166164

167-
use views::EncodableCategoryWithSubcategories;
168-
169165
fn pg_connection() -> PgConnection {
170166
let _ = dotenv();
171167
let database_url =
@@ -294,42 +290,4 @@ mod tests {
294290
let expected = vec![("Cat 3".to_string(), 6), ("Cat 1".to_string(), 3)];
295291
assert_eq!(expected, categories);
296292
}
297-
298-
#[test]
299-
fn category_dates_serializes_to_rfc3339() {
300-
let cat = EncodableCategory {
301-
id: "".to_string(),
302-
category: "".to_string(),
303-
slug: "".to_string(),
304-
description: "".to_string(),
305-
crates_cnt: 1,
306-
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
307-
};
308-
let json = serde_json::to_string(&cat).unwrap();
309-
assert!(
310-
json.as_str()
311-
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
312-
.is_some()
313-
);
314-
}
315-
316-
#[test]
317-
fn category_with_sub_dates_serializes_to_rfc3339() {
318-
let cat = EncodableCategoryWithSubcategories {
319-
id: "".to_string(),
320-
category: "".to_string(),
321-
slug: "".to_string(),
322-
description: "".to_string(),
323-
crates_cnt: 1,
324-
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
325-
subcategories: vec![],
326-
};
327-
let json = serde_json::to_string(&cat).unwrap();
328-
assert!(
329-
json.as_str()
330-
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
331-
.is_some()
332-
);
333-
}
334-
335293
}

src/models/keyword.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,9 @@ impl Keyword {
9797
#[cfg(test)]
9898
mod tests {
9999
use super::*;
100-
use chrono::NaiveDate;
101100
use diesel;
102101
use diesel::connection::SimpleConnection;
103102
use dotenv::dotenv;
104-
use serde_json;
105103
use std::env;
106104

107105
fn pg_connection() -> PgConnection {
@@ -129,21 +127,4 @@ mod tests {
129127
assert_eq!(associated.len(), 1);
130128
assert_eq!(associated.first().unwrap().keyword, "no");
131129
}
132-
133-
#[test]
134-
fn keyword_serializes_to_rfc3339() {
135-
let key = EncodableKeyword {
136-
id: "".to_string(),
137-
keyword: "".to_string(),
138-
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
139-
crates_cnt: 0,
140-
};
141-
let json = serde_json::to_string(&key).unwrap();
142-
assert!(
143-
json.as_str()
144-
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
145-
.is_some()
146-
);
147-
}
148-
149130
}

src/version/mod.rs

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ pub struct EncodableVersion {
5858
pub features: HashMap<String, Vec<String>>,
5959
pub yanked: bool,
6060
pub license: Option<String>,
61-
pub links: VersionLinks,
61+
pub links: EncodableVersionLinks,
6262
}
6363

6464
#[derive(Serialize, Deserialize, Debug)]
65-
pub struct VersionLinks {
65+
pub struct EncodableVersionLinks {
6666
pub dependencies: String,
6767
pub version_downloads: String,
6868
pub authors: String,
@@ -94,7 +94,7 @@ impl Version {
9494
features: features,
9595
yanked: yanked,
9696
license: license,
97-
links: VersionLinks {
97+
links: EncodableVersionLinks {
9898
dependencies: format!("/api/v1/crates/{}/{}/dependencies", crate_name, num),
9999
version_downloads: format!("/api/v1/crates/{}/{}/downloads", crate_name, num),
100100
authors: format!("/api/v1/crates/{}/{}/authors", crate_name, num),
@@ -267,44 +267,3 @@ 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-
}

src/views/mod.rs

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,103 @@ pub use krate::EncodableCrate;
4242
pub use owner::{EncodableOwner, EncodableTeam};
4343
pub use token::EncodableApiTokenWithToken;
4444
pub use user::{EncodablePrivateUser, EncodablePublicUser};
45-
pub use version::EncodableVersion;
45+
pub use version::{EncodableVersion, EncodableVersionLinks};
4646

4747
// TODO: Prefix many of these with `Encodable` then clean up the reexports
4848
pub mod krate_publish;
4949
pub use self::krate_publish::CrateDependency as EncodableCrateDependency;
5050
pub use self::krate_publish::NewCrate as EncodableCrateUpload;
51+
52+
#[cfg(test)]
53+
mod tests {
54+
use std::collections::HashMap;
55+
use super::*;
56+
use chrono::NaiveDate;
57+
use serde_json;
58+
59+
#[test]
60+
fn category_dates_serializes_to_rfc3339() {
61+
let cat = EncodableCategory {
62+
id: "".to_string(),
63+
category: "".to_string(),
64+
slug: "".to_string(),
65+
description: "".to_string(),
66+
crates_cnt: 1,
67+
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
68+
};
69+
let json = serde_json::to_string(&cat).unwrap();
70+
assert!(
71+
json.as_str()
72+
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
73+
.is_some()
74+
);
75+
}
76+
77+
#[test]
78+
fn category_with_sub_dates_serializes_to_rfc3339() {
79+
let cat = EncodableCategoryWithSubcategories {
80+
id: "".to_string(),
81+
category: "".to_string(),
82+
slug: "".to_string(),
83+
description: "".to_string(),
84+
crates_cnt: 1,
85+
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
86+
subcategories: vec![],
87+
};
88+
let json = serde_json::to_string(&cat).unwrap();
89+
assert!(
90+
json.as_str()
91+
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
92+
.is_some()
93+
);
94+
}
95+
96+
#[test]
97+
fn keyword_serializes_to_rfc3339() {
98+
let key = EncodableKeyword {
99+
id: "".to_string(),
100+
keyword: "".to_string(),
101+
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
102+
crates_cnt: 0,
103+
};
104+
let json = serde_json::to_string(&key).unwrap();
105+
assert!(
106+
json.as_str()
107+
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
108+
.is_some()
109+
);
110+
}
111+
112+
#[test]
113+
fn version_serializes_to_rfc3339() {
114+
let ver = EncodableVersion {
115+
id: 1,
116+
krate: "".to_string(),
117+
num: "".to_string(),
118+
dl_path: "".to_string(),
119+
readme_path: "".to_string(),
120+
updated_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 11),
121+
created_at: NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 12),
122+
downloads: 0,
123+
features: HashMap::new(),
124+
yanked: false,
125+
license: None,
126+
links: EncodableVersionLinks {
127+
dependencies: "".to_string(),
128+
version_downloads: "".to_string(),
129+
authors: "".to_string(),
130+
},
131+
};
132+
let json = serde_json::to_string(&ver).unwrap();
133+
assert!(
134+
json.as_str()
135+
.find(r#""updated_at":"2017-01-06T14:23:11+00:00""#)
136+
.is_some()
137+
);
138+
assert!(
139+
json.as_str()
140+
.find(r#""created_at":"2017-01-06T14:23:12+00:00""#)
141+
.is_some()
142+
);
143+
}
144+
}

0 commit comments

Comments
 (0)