aboutsummaryrefslogtreecommitdiffstats
path: root/rust
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2018-11-14 18:52:48 -0800
committerBryan Newbold <bnewbold@robocracy.org>2018-11-14 18:52:50 -0800
commit54c4b6cd67c7f8190dc7dfb4da6ad99dc2101c82 (patch)
treef121a1deebef99a639ff82bc241eb44f2d5bc8ef /rust
parent610b0a6550c758529cf6c34587f45e483a240df4 (diff)
downloadfatcat-54c4b6cd67c7f8190dc7dfb4da6ad99dc2101c82.tar.gz
fatcat-54c4b6cd67c7f8190dc7dfb4da6ad99dc2101c82.zip
fix date/datetime confusion on rust/API side
Should have dug in to this earlier; python code was getting confused. This is a breaking API change, from a practical standpoint, as both python and rust code had been hacked to work around this.
Diffstat (limited to 'rust')
-rwxr-xr-xrust/codegen_openapi2.sh3
-rw-r--r--rust/fatcat-api-spec/src/models.rs2
-rw-r--r--rust/src/api_entity_crud.rs7
-rw-r--r--rust/tests/test_api_server_http.rs66
4 files changed, 72 insertions, 6 deletions
diff --git a/rust/codegen_openapi2.sh b/rust/codegen_openapi2.sh
index 05e75ff0..f7d1df53 100755
--- a/rust/codegen_openapi2.sh
+++ b/rust/codegen_openapi2.sh
@@ -8,4 +8,7 @@ git checkout fatcat-api-spec/Cargo.toml
sed -i 's/Object/serde_json::Value/g' fatcat-api-spec/src/models.rs
sed -i 's/extern crate uuid;/extern crate serde_json;\nextern crate uuid;/g' fatcat-api-spec/src/models.rs
+# Hack to fix "release_date" as Date, not DateTime
+sed -i 's/release_date: Option<chrono::DateTime<chrono::Utc>>/release_date: Option<chrono::NaiveDate>/g' fatcat-api-spec/src/models.rs
+
cargo fmt
diff --git a/rust/fatcat-api-spec/src/models.rs b/rust/fatcat-api-spec/src/models.rs
index ed37b58a..73418c45 100644
--- a/rust/fatcat-api-spec/src/models.rs
+++ b/rust/fatcat-api-spec/src/models.rs
@@ -536,7 +536,7 @@ pub struct ReleaseEntity {
#[serde(rename = "release_date")]
#[serde(skip_serializing_if = "Option::is_none")]
- pub release_date: Option<chrono::DateTime<chrono::Utc>>,
+ pub release_date: Option<chrono::NaiveDate>,
#[serde(rename = "release_status")]
#[serde(skip_serializing_if = "Option::is_none")]
diff --git a/rust/src/api_entity_crud.rs b/rust/src/api_entity_crud.rs
index 258355f8..f8e3a0ed 100644
--- a/rust/src/api_entity_crud.rs
+++ b/rust/src/api_entity_crud.rs
@@ -1,6 +1,5 @@
use api_helpers::*;
use api_server::get_release_files;
-use chrono;
use database_models::*;
use database_schema::*;
use diesel::prelude::*;
@@ -833,9 +832,7 @@ impl EntityCrud for ReleaseEntity {
title: rev_row.title,
release_type: rev_row.release_type,
release_status: rev_row.release_status,
- release_date: rev_row
- .release_date
- .map(|v| chrono::DateTime::from_utc(v.and_hms(0, 0, 0), chrono::Utc)),
+ release_date: rev_row.release_date,
doi: rev_row.doi,
pmid: rev_row.pmid,
pmcid: rev_row.pmcid,
@@ -891,7 +888,7 @@ impl EntityCrud for ReleaseEntity {
title: model.title.clone(),
release_type: model.release_type.clone(),
release_status: model.release_status.clone(),
- release_date: model.release_date.map(|v| v.naive_utc().date()),
+ release_date: model.release_date,
doi: model.doi.clone(),
pmid: model.pmid.clone(),
pmcid: model.pmcid.clone(),
diff --git a/rust/tests/test_api_server_http.rs b/rust/tests/test_api_server_http.rs
index 6266a747..6df616a6 100644
--- a/rust/tests/test_api_server_http.rs
+++ b/rust/tests/test_api_server_http.rs
@@ -416,6 +416,7 @@ fn test_post_release() {
// TODO: target_release_id
r#"{"title": "secret paper",
"release_type": "journal-article",
+ "release_date": "2000-01-02",
"doi": "10.1234/abcde.781231231239",
"pmid": "54321",
"pmcid": "PMC12345",
@@ -1024,3 +1025,68 @@ fn test_post_batch_autoaccept() {
None,
);
}
+
+#[test]
+fn test_release_dates() {
+ let (headers, router, _conn) = setup_http();
+
+ // Ok
+ check_http_response(
+ request::post(
+ "http://localhost:9411/v0/release",
+ headers.clone(),
+ r#"{"title": "secret minimal paper",
+ "release_type": "journal-article",
+ "release_date": "2000-01-02"
+ }"#,
+ &router,
+ ),
+ status::Created,
+ None,
+ );
+
+ // Bad: year/month only
+ check_http_response(
+ request::post(
+ "http://localhost:9411/v0/release",
+ headers.clone(),
+ r#"{"title": "secret minimal paper",
+ "release_type": "journal-article",
+ "release_date": "2000-01"
+ }"#,
+ &router,
+ ),
+ status::BadRequest,
+ None,
+ );
+
+ // Bad: full timestamp
+ check_http_response(
+ request::post(
+ "http://localhost:9411/v0/release",
+ headers.clone(),
+ r#"{"title": "secret minimal paper",
+ "release_type": "journal-article",
+ "release_date": "2005-08-30T00:00:00Z"
+ }"#,
+ &router,
+ ),
+ status::BadRequest,
+ None,
+ );
+
+ // Bad: bogus month/day
+ check_http_response(
+ request::post(
+ "http://localhost:9411/v0/release",
+ headers.clone(),
+ r#"{"title": "secret minimal paper",
+ "release_type": "journal-article",
+ "release_date": "2000-88-99"
+ }"#,
+ &router,
+ ),
+ status::BadRequest,
+ None,
+ );
+}