-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from gfbio/bms_provider_refactoring
ABCD Provider Refactoring
- Loading branch information
Showing
31 changed files
with
3,019 additions
and
1,173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
language: rust | ||
|
||
rust: | ||
- stable | ||
|
||
services: | ||
- postgresql | ||
|
||
addons: | ||
postgresql: "11.2" | ||
|
||
cache: cargo | ||
|
||
before_install: | ||
- sudo apt-get update | ||
- sudo apt-get --yes remove postgresql\* | ||
- sudo apt-get install -y postgresql-11 postgresql-client-11 | ||
- sudo cp /etc/postgresql/{9.6,11}/main/pg_hba.conf | ||
- sudo service postgresql restart 11 | ||
|
||
before_script: | ||
- psql -c 'create database travis_ci_test;' -U postgres | ||
- touch settings.toml | ||
- echo '[database]' >> settings.toml | ||
- echo 'database = "travis_ci_test"' >> settings.toml | ||
- echo 'tls = false' >> settings.toml | ||
- echo 'user = "postgres"' >> settings.toml | ||
- echo 'password = ""' >> settings.toml | ||
|
||
script: | ||
- cargo build --verbose --all | ||
- cargo test --verbose --all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
use std::collections::hash_map::Values; | ||
use std::collections::HashMap; | ||
use std::fs::File; | ||
use std::io::BufReader; | ||
use std::path::Path; | ||
|
||
use failure::Error; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// This struct reflect a field within the ABCD fields specification file. | ||
#[derive(Debug, Deserialize, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct AbcdField { | ||
pub name: String, | ||
pub numeric: bool, | ||
pub vat_mandatory: bool, | ||
pub gfbio_mandatory: bool, | ||
pub global_field: bool, | ||
pub unit: String, | ||
} | ||
|
||
type BinaryString = Vec<u8>; | ||
|
||
#[derive(Debug)] | ||
pub struct AbcdFields { | ||
fields: HashMap<BinaryString, AbcdField>, | ||
} | ||
|
||
impl AbcdFields { | ||
pub fn from_path(path: &Path) -> Result<Self, Error> { | ||
let file = File::open(path)?; | ||
let reader = BufReader::new(file); | ||
|
||
Ok(Self { | ||
fields: Self::fields_to_map(serde_json::from_reader(reader)?), | ||
}) | ||
} | ||
|
||
/// This function creates a map from binary field name to `AbcdField` from a list of `AbcdField`s. | ||
fn fields_to_map(fields: Vec<AbcdField>) -> HashMap<Vec<u8>, AbcdField> { | ||
let mut map = HashMap::with_capacity(fields.len()); | ||
for field in fields { | ||
map.insert(field.name.as_bytes().into(), field); | ||
} | ||
map | ||
} | ||
|
||
pub fn value_of(&self, field: &[u8]) -> Option<&AbcdField> { | ||
self.fields.get(field) | ||
} | ||
|
||
pub fn len(&self) -> usize { | ||
self.fields.len() | ||
} | ||
} | ||
|
||
impl<'a> IntoIterator for &'a AbcdFields { | ||
type Item = &'a AbcdField; | ||
type IntoIter = Values<'a, BinaryString, AbcdField>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
self.fields.values() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use tempfile::TempPath; | ||
|
||
use crate::test_utils; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn simple_file() { | ||
let path = create_test_file_path(); | ||
|
||
let abcd_fields = AbcdFields::from_path(&path).expect("Unable to deserialize input."); | ||
|
||
assert_eq!(abcd_fields.len(), 2); | ||
|
||
let field1 = abcd_fields | ||
.value_of(&b"/DataSets/DataSet/DatasetGUID".to_vec()) | ||
.expect("Field not found"); | ||
assert_eq!(field1.name, "/DataSets/DataSet/DatasetGUID"); | ||
assert_eq!(field1.numeric, false); | ||
assert_eq!(field1.vat_mandatory, false); | ||
assert_eq!(field1.gfbio_mandatory, false); | ||
assert_eq!(field1.global_field, true); | ||
assert!(field1.unit.is_empty()); | ||
|
||
let field2 = abcd_fields | ||
.value_of(&b"/DataSets/DataSet/Units/Unit/SourceInstitutionID".to_vec()) | ||
.expect("Field not found"); | ||
assert_eq!( | ||
field2.name, | ||
"/DataSets/DataSet/Units/Unit/SourceInstitutionID" | ||
); | ||
assert_eq!(field2.numeric, false); | ||
assert_eq!(field2.vat_mandatory, true); | ||
assert_eq!(field2.gfbio_mandatory, true); | ||
assert_eq!(field2.global_field, false); | ||
assert_eq!(field2.unit, "TEST"); | ||
} | ||
|
||
#[test] | ||
fn iterate_values() { | ||
let path = create_test_file_path(); | ||
|
||
let abcd_fields = AbcdFields::from_path(&path).expect("Unable to deserialize input."); | ||
|
||
let mut number_of_fields = 0; | ||
for _field in &abcd_fields { | ||
number_of_fields += 1; | ||
} | ||
|
||
assert_eq!(number_of_fields, 2); | ||
} | ||
|
||
fn create_test_file_path() -> TempPath { | ||
test_utils::create_temp_file( | ||
r#"[ | ||
{ | ||
"name": "/DataSets/DataSet/DatasetGUID", | ||
"numeric": false, | ||
"vatMandatory": false, | ||
"gfbioMandatory": false, | ||
"globalField": true, | ||
"unit": "" | ||
}, | ||
{ | ||
"name": "/DataSets/DataSet/Units/Unit/SourceInstitutionID", | ||
"numeric": false, | ||
"vatMandatory": true, | ||
"gfbioMandatory": true, | ||
"globalField": false, | ||
"unit": "TEST" | ||
} | ||
]"#, | ||
) | ||
} | ||
} |
Oops, something went wrong.