Skip to content

Commit a7a28cc

Browse files
committed
Compare BeamlineConfiguration using derived Eq/PartialEq
Instead of comparing each field individually. Adds a bit of boilerplate in terms of derives etc, but reduces need for duplicated blocks of testing code when there are multiple tests comparing configurations.
1 parent 1b7af06 commit a7a28cc

File tree

2 files changed

+68
-119
lines changed

2 files changed

+68
-119
lines changed

src/db_service.rs

Lines changed: 65 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub struct NumtrackerConfig {
4141
pub extension: String,
4242
}
4343

44-
#[derive(Debug)]
44+
#[derive(Debug, PartialEq, Eq)]
4545
struct RawPathTemplate<F>(String, PhantomData<F>);
4646

4747
impl<Spec> RawPathTemplate<Spec>
@@ -59,8 +59,14 @@ impl<F> From<String> for RawPathTemplate<F> {
5959
}
6060
}
6161

62+
impl<F> From<&str> for RawPathTemplate<F> {
63+
fn from(value: &str) -> Self {
64+
value.to_string().into()
65+
}
66+
}
67+
6268
/// The current configuration for a beamline
63-
#[derive(Debug)]
69+
#[derive(Debug, PartialEq, Eq)]
6470
pub struct BeamlineConfiguration {
6571
name: String,
6672
scan_number: u32,
@@ -581,27 +587,17 @@ mod db_tests {
581587
.with_extension("ext")
582588
.insert_new(&db));
583589
let conf = ok!(db.current_configuration("i22"));
584-
assert_eq!(conf.name(), "i22");
585-
assert_eq!(conf.scan_number(), 122);
586-
assert_eq!(
587-
conf.visit().unwrap().to_string(),
588-
"/tmp/{instrument}/data/{year}/{visit}"
589-
);
590-
assert_eq!(
591-
conf.scan().unwrap().to_string(),
592-
"{subdirectory}/{instrument}-{scan_number}"
593-
);
594-
assert_eq!(
595-
conf.detector().unwrap().to_string(),
596-
"{subdirectory}/{instrument}-{scan_number}-{detector}"
597-
);
598-
let Some(ext) = conf.tracker_file_extension else {
599-
panic!("Missing extension");
590+
let expected = BeamlineConfiguration {
591+
name: "i22".into(),
592+
scan_number: 122,
593+
visit: "/tmp/{instrument}/data/{year}/{visit}".into(),
594+
scan: "{subdirectory}/{instrument}-{scan_number}".into(),
595+
detector: "{subdirectory}/{instrument}-{scan_number}-{detector}".into(),
596+
tracker_file_extension: Some("ext".into()),
600597
};
601-
assert_eq!(ext, "ext");
598+
assert_eq!(conf, expected);
602599
}
603600

604-
#[rstest]
605601
#[test]
606602
async fn configurations() {
607603
let db = SqliteScanPathService::memory().await;
@@ -614,62 +610,37 @@ mod db_tests {
614610
.with_extension("ext")
615611
.insert_new(&db));
616612

617-
let confs = ok!(db.configurations(vec![
613+
let mut confs = ok!(db.configurations(vec![
618614
"i22".to_string(),
619615
"i11".to_string(),
620616
"i03".to_string()
621617
]));
622-
// i03 has not been configured so it will not fetch it.
623-
assert_eq!(confs.len(), 2);
624618

625-
for conf in confs.iter() {
626-
match conf.name() {
627-
"i22" => {
628-
assert_eq!(conf.name(), "i22");
629-
assert_eq!(conf.scan_number(), 122);
630-
assert_eq!(
631-
conf.visit().unwrap().to_string(),
632-
"/tmp/{instrument}/data/{year}/{visit}"
633-
);
634-
assert_eq!(
635-
conf.scan().unwrap().to_string(),
636-
"{subdirectory}/{instrument}-{scan_number}"
637-
);
638-
assert_eq!(
639-
conf.detector().unwrap().to_string(),
640-
"{subdirectory}/{instrument}-{scan_number}-{detector}"
641-
);
642-
let Some(ext) = &conf.tracker_file_extension else {
643-
panic!("Missing extension");
644-
};
645-
assert_eq!(ext, "ext");
646-
}
647-
"i11" => {
648-
assert_eq!(conf.name(), "i11");
649-
assert_eq!(conf.scan_number(), 111);
650-
assert_eq!(
651-
conf.visit().unwrap().to_string(),
652-
"/tmp/{instrument}/data/{year}/{visit}"
653-
);
654-
assert_eq!(
655-
conf.scan().unwrap().to_string(),
656-
"{subdirectory}/{instrument}-{scan_number}"
657-
);
658-
assert_eq!(
659-
conf.detector().unwrap().to_string(),
660-
"{subdirectory}/{instrument}-{scan_number}-{detector}"
661-
);
662-
let Some(ext) = &conf.tracker_file_extension else {
663-
panic!("Missing extension");
664-
};
665-
assert_eq!(ext, "ext");
666-
}
667-
other => panic!("Unexpected beamline name: {other}"),
668-
}
669-
}
619+
// Sort returned list as DB order is not guaranteed
620+
confs.sort_unstable_by_key(BeamlineConfiguration::scan_number);
621+
622+
// i03 has not been configured so it will not fetch it.
623+
let expected = vec![
624+
BeamlineConfiguration {
625+
name: "i11".into(),
626+
scan_number: 111,
627+
visit: "/tmp/{instrument}/data/{year}/{visit}".into(),
628+
scan: "{subdirectory}/{instrument}-{scan_number}".into(),
629+
detector: "{subdirectory}/{instrument}-{scan_number}-{detector}".into(),
630+
tracker_file_extension: Some("ext".into()),
631+
},
632+
BeamlineConfiguration {
633+
name: "i22".into(),
634+
scan_number: 122,
635+
visit: "/tmp/{instrument}/data/{year}/{visit}".into(),
636+
scan: "{subdirectory}/{instrument}-{scan_number}".into(),
637+
detector: "{subdirectory}/{instrument}-{scan_number}-{detector}".into(),
638+
tracker_file_extension: Some("ext".into()),
639+
},
640+
];
641+
assert_eq!(expected, confs);
670642
}
671643

672-
#[rstest]
673644
#[test]
674645
async fn all_configurations() {
675646
let db = SqliteScanPathService::memory().await;
@@ -682,54 +653,32 @@ mod db_tests {
682653
.with_extension("ext")
683654
.insert_new(&db));
684655

685-
let confs = ok!(db.all_configurations());
686-
assert_eq!(confs.len(), 2);
656+
let mut confs = ok!(db.all_configurations());
687657

688-
for conf in confs.iter() {
689-
match conf.name() {
690-
"i22" => {
691-
assert_eq!(conf.name(), "i22");
692-
assert_eq!(conf.scan_number(), 122);
693-
assert_eq!(
694-
conf.visit().unwrap().to_string(),
695-
"/tmp/{instrument}/data/{year}/{visit}"
696-
);
697-
assert_eq!(
698-
conf.scan().unwrap().to_string(),
699-
"{subdirectory}/{instrument}-{scan_number}"
700-
);
701-
assert_eq!(
702-
conf.detector().unwrap().to_string(),
703-
"{subdirectory}/{instrument}-{scan_number}-{detector}"
704-
);
705-
let Some(ext) = &conf.tracker_file_extension else {
706-
panic!("Missing extension");
707-
};
708-
assert_eq!(ext, "ext");
709-
}
710-
"i11" => {
711-
assert_eq!(conf.name(), "i11");
712-
assert_eq!(conf.scan_number(), 111);
713-
assert_eq!(
714-
conf.visit().unwrap().to_string(),
715-
"/tmp/{instrument}/data/{year}/{visit}"
716-
);
717-
assert_eq!(
718-
conf.scan().unwrap().to_string(),
719-
"{subdirectory}/{instrument}-{scan_number}"
720-
);
721-
assert_eq!(
722-
conf.detector().unwrap().to_string(),
723-
"{subdirectory}/{instrument}-{scan_number}-{detector}"
724-
);
725-
let Some(ext) = &conf.tracker_file_extension else {
726-
panic!("Missing extension");
727-
};
728-
assert_eq!(ext, "ext");
729-
}
730-
other => panic!("Unexpected beamline name: {other}"),
731-
}
732-
}
658+
// Sort returned list as DB order is not guaranteed
659+
confs.sort_unstable_by_key(BeamlineConfiguration::scan_number);
660+
661+
// i03 has not been configured so it will not fetch it.
662+
assert_eq!(confs.len(), 2);
663+
let expected = vec![
664+
BeamlineConfiguration {
665+
name: "i11".into(),
666+
scan_number: 111,
667+
visit: "/tmp/{instrument}/data/{year}/{visit}".into(),
668+
scan: "{subdirectory}/{instrument}-{scan_number}".into(),
669+
detector: "{subdirectory}/{instrument}-{scan_number}-{detector}".into(),
670+
tracker_file_extension: Some("ext".into()),
671+
},
672+
BeamlineConfiguration {
673+
name: "i22".into(),
674+
scan_number: 122,
675+
visit: "/tmp/{instrument}/data/{year}/{visit}".into(),
676+
scan: "{subdirectory}/{instrument}-{scan_number}".into(),
677+
detector: "{subdirectory}/{instrument}-{scan_number}-{detector}".into(),
678+
tracker_file_extension: Some("ext".into()),
679+
},
680+
];
681+
assert_eq!(expected, confs);
733682
}
734683

735684
type Update = BeamlineConfigurationUpdate;

src/paths.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,11 @@ impl From<PathTemplateError> for InvalidPathTemplate {
179179
}
180180
}
181181

182-
#[derive(Debug, Clone, Copy)]
182+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
183183
pub struct VisitTemplate;
184-
#[derive(Debug, Clone, Copy)]
184+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
185185
pub struct ScanTemplate;
186-
#[derive(Debug, Clone, Copy)]
186+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
187187
pub struct DetectorTemplate;
188188

189189
impl PathSpec for VisitTemplate {

0 commit comments

Comments
 (0)