From b9699e27936573271506dbf6a3af744f7b454c03 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Thu, 14 Feb 2019 16:24:16 -0800 Subject: verify release_status field --- python/tests/api_releases.py | 19 ++++++++++++++++++- rust/src/entity_crud.rs | 3 +++ rust/src/identifiers.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/python/tests/api_releases.py b/python/tests/api_releases.py index 247ad2c2..9a64e0f9 100644 --- a/python/tests/api_releases.py +++ b/python/tests/api_releases.py @@ -18,7 +18,7 @@ def test_release(api): title="some title", original_title="оригинальное название", release_type="post-weblog", - release_status="pre-print", + release_status="submitted", release_date=datetime.datetime.utcnow().date(), release_year=2015, doi="10.5555/12345678", @@ -114,3 +114,20 @@ def test_empty_fields(api): with pytest.raises(fatcat_client.rest.ApiException): r2 = ReleaseEntity(title="something", contribs=[ReleaseContrib(raw_name="")]) api.create_release(r2, editgroup_id=eg.editgroup_id) + +def test_controlled_vocab(api): + + eg = quick_eg(api) + + r1 = ReleaseEntity(title="something", release_type="journal-thingie") + with pytest.raises(fatcat_client.rest.ApiException): + api.create_release(r1, editgroup_id=eg.editgroup_id) + r1.release_type = "article" + api.create_release(r1, editgroup_id=eg.editgroup_id) + + r2 = ReleaseEntity(title="something elase", release_status="pre-print") + with pytest.raises(fatcat_client.rest.ApiException): + api.create_release(r2, editgroup_id=eg.editgroup_id) + r2.release_status = "published" + api.create_release(r2, editgroup_id=eg.editgroup_id) + diff --git a/rust/src/entity_crud.rs b/rust/src/entity_crud.rs index 743d451a..faf4b496 100644 --- a/rust/src/entity_crud.rs +++ b/rust/src/entity_crud.rs @@ -1964,6 +1964,9 @@ impl EntityCrud for ReleaseEntity { if let Some(ref release_type) = entity.release_type { check_release_type(release_type)?; } + if let Some(ref release_status) = entity.release_status { + check_release_status(release_status)?; + } if let Some(ref abstracts) = entity.abstracts { if abstracts.len() > 200 { return Err(FatcatError::BadRequest( diff --git a/rust/src/identifiers.rs b/rust/src/identifiers.rs index 1c859269..ed88939d 100644 --- a/rust/src/identifiers.rs +++ b/rust/src/identifiers.rs @@ -342,6 +342,38 @@ fn test_check_release_type() { assert!(check_release_type("book ").is_err()); } +pub fn check_release_status(raw: &str) -> Result<()> { + let valid_types = vec![ + // DRIVER types (minus "version" suffix) + "draft", + "submitted", + "accepted", + "published", + "updated", + // fatcat-specific extensions + "retraction", + ]; + for good in valid_types { + if raw == good { + return Ok(()); + } + } + Err(FatcatError::NotInControlledVocabulary( + "release_status".to_string(), + raw.to_string(), + ))? +} + +#[test] +fn test_check_release_status() { + assert!(check_release_status("draft").is_ok()); + assert!(check_release_status("retraction").is_ok()); + assert!(check_release_status("published").is_ok()); + assert!(check_release_status("pre-print").is_err()); + assert!(check_release_status("DRAFT").is_err()); + assert!(check_release_status("draft ").is_err()); +} + pub fn check_contrib_role(raw: &str) -> Result<()> { let valid_types = vec![ // Citation Style Language official role types -- cgit v1.2.3