Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 65 additions & 116 deletions src/db_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct NumtrackerConfig {
pub extension: String,
}

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
struct RawPathTemplate<F>(String, PhantomData<F>);

impl<Spec> RawPathTemplate<Spec>
Expand All @@ -59,8 +59,14 @@ impl<F> From<String> for RawPathTemplate<F> {
}
}

impl<F> From<&str> for RawPathTemplate<F> {
fn from(value: &str) -> Self {
value.to_string().into()
}
}

/// The current configuration for a beamline
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub struct BeamlineConfiguration {
name: String,
scan_number: u32,
Expand Down Expand Up @@ -581,27 +587,17 @@ mod db_tests {
.with_extension("ext")
.insert_new(&db));
let conf = ok!(db.current_configuration("i22"));
assert_eq!(conf.name(), "i22");
assert_eq!(conf.scan_number(), 122);
assert_eq!(
conf.visit().unwrap().to_string(),
"/tmp/{instrument}/data/{year}/{visit}"
);
assert_eq!(
conf.scan().unwrap().to_string(),
"{subdirectory}/{instrument}-{scan_number}"
);
assert_eq!(
conf.detector().unwrap().to_string(),
"{subdirectory}/{instrument}-{scan_number}-{detector}"
);
let Some(ext) = conf.tracker_file_extension else {
panic!("Missing extension");
let expected = BeamlineConfiguration {
name: "i22".into(),
scan_number: 122,
visit: "/tmp/{instrument}/data/{year}/{visit}".into(),
scan: "{subdirectory}/{instrument}-{scan_number}".into(),
detector: "{subdirectory}/{instrument}-{scan_number}-{detector}".into(),
tracker_file_extension: Some("ext".into()),
};
assert_eq!(ext, "ext");
assert_eq!(conf, expected);
}

#[rstest]
#[test]
async fn configurations() {
let db = SqliteScanPathService::memory().await;
Expand All @@ -614,62 +610,37 @@ mod db_tests {
.with_extension("ext")
.insert_new(&db));

let confs = ok!(db.configurations(vec![
let mut confs = ok!(db.configurations(vec![
"i22".to_string(),
"i11".to_string(),
"i03".to_string()
]));
// i03 has not been configured so it will not fetch it.
assert_eq!(confs.len(), 2);

for conf in confs.iter() {
match conf.name() {
"i22" => {
assert_eq!(conf.name(), "i22");
assert_eq!(conf.scan_number(), 122);
assert_eq!(
conf.visit().unwrap().to_string(),
"/tmp/{instrument}/data/{year}/{visit}"
);
assert_eq!(
conf.scan().unwrap().to_string(),
"{subdirectory}/{instrument}-{scan_number}"
);
assert_eq!(
conf.detector().unwrap().to_string(),
"{subdirectory}/{instrument}-{scan_number}-{detector}"
);
let Some(ext) = &conf.tracker_file_extension else {
panic!("Missing extension");
};
assert_eq!(ext, "ext");
}
"i11" => {
assert_eq!(conf.name(), "i11");
assert_eq!(conf.scan_number(), 111);
assert_eq!(
conf.visit().unwrap().to_string(),
"/tmp/{instrument}/data/{year}/{visit}"
);
assert_eq!(
conf.scan().unwrap().to_string(),
"{subdirectory}/{instrument}-{scan_number}"
);
assert_eq!(
conf.detector().unwrap().to_string(),
"{subdirectory}/{instrument}-{scan_number}-{detector}"
);
let Some(ext) = &conf.tracker_file_extension else {
panic!("Missing extension");
};
assert_eq!(ext, "ext");
}
other => panic!("Unexpected beamline name: {other}"),
}
}
// Sort returned list as DB order is not guaranteed
confs.sort_unstable_by_key(BeamlineConfiguration::scan_number);

// i03 has not been configured so it will not fetch it.
let expected = vec![
BeamlineConfiguration {
name: "i11".into(),
scan_number: 111,
visit: "/tmp/{instrument}/data/{year}/{visit}".into(),
scan: "{subdirectory}/{instrument}-{scan_number}".into(),
detector: "{subdirectory}/{instrument}-{scan_number}-{detector}".into(),
tracker_file_extension: Some("ext".into()),
},
BeamlineConfiguration {
name: "i22".into(),
scan_number: 122,
visit: "/tmp/{instrument}/data/{year}/{visit}".into(),
scan: "{subdirectory}/{instrument}-{scan_number}".into(),
detector: "{subdirectory}/{instrument}-{scan_number}-{detector}".into(),
tracker_file_extension: Some("ext".into()),
},
];
assert_eq!(expected, confs);
}

#[rstest]
#[test]
async fn all_configurations() {
let db = SqliteScanPathService::memory().await;
Expand All @@ -682,54 +653,32 @@ mod db_tests {
.with_extension("ext")
.insert_new(&db));

let confs = ok!(db.all_configurations());
assert_eq!(confs.len(), 2);
let mut confs = ok!(db.all_configurations());

for conf in confs.iter() {
match conf.name() {
"i22" => {
assert_eq!(conf.name(), "i22");
assert_eq!(conf.scan_number(), 122);
assert_eq!(
conf.visit().unwrap().to_string(),
"/tmp/{instrument}/data/{year}/{visit}"
);
assert_eq!(
conf.scan().unwrap().to_string(),
"{subdirectory}/{instrument}-{scan_number}"
);
assert_eq!(
conf.detector().unwrap().to_string(),
"{subdirectory}/{instrument}-{scan_number}-{detector}"
);
let Some(ext) = &conf.tracker_file_extension else {
panic!("Missing extension");
};
assert_eq!(ext, "ext");
}
"i11" => {
assert_eq!(conf.name(), "i11");
assert_eq!(conf.scan_number(), 111);
assert_eq!(
conf.visit().unwrap().to_string(),
"/tmp/{instrument}/data/{year}/{visit}"
);
assert_eq!(
conf.scan().unwrap().to_string(),
"{subdirectory}/{instrument}-{scan_number}"
);
assert_eq!(
conf.detector().unwrap().to_string(),
"{subdirectory}/{instrument}-{scan_number}-{detector}"
);
let Some(ext) = &conf.tracker_file_extension else {
panic!("Missing extension");
};
assert_eq!(ext, "ext");
}
other => panic!("Unexpected beamline name: {other}"),
}
}
// Sort returned list as DB order is not guaranteed
confs.sort_unstable_by_key(BeamlineConfiguration::scan_number);

// i03 has not been configured so it will not fetch it.
assert_eq!(confs.len(), 2);
let expected = vec![
BeamlineConfiguration {
name: "i11".into(),
scan_number: 111,
visit: "/tmp/{instrument}/data/{year}/{visit}".into(),
scan: "{subdirectory}/{instrument}-{scan_number}".into(),
detector: "{subdirectory}/{instrument}-{scan_number}-{detector}".into(),
tracker_file_extension: Some("ext".into()),
},
BeamlineConfiguration {
name: "i22".into(),
scan_number: 122,
visit: "/tmp/{instrument}/data/{year}/{visit}".into(),
scan: "{subdirectory}/{instrument}-{scan_number}".into(),
detector: "{subdirectory}/{instrument}-{scan_number}-{detector}".into(),
tracker_file_extension: Some("ext".into()),
},
];
assert_eq!(expected, confs);
}

type Update = BeamlineConfigurationUpdate;
Expand Down
6 changes: 3 additions & 3 deletions src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ impl From<PathTemplateError> for InvalidPathTemplate {
}
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct VisitTemplate;
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ScanTemplate;
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DetectorTemplate;

impl PathSpec for VisitTemplate {
Expand Down