rformassspectrometry / Spectra

Low level infrastructure to handle MS spectra
https://rformassspectrometry.github.io/Spectra/
34 stars 24 forks source link

Add test suite framework and tests to check all spectra variables #227

Closed jorainer closed 2 years ago

jorainer commented 2 years ago

This PR bases heavily on the excellent work from @sneumann and @hechth (PR #224) and adds a test suite folder with unit tests checking all methods to access and set spectra variables in a MsBackend object.

Backend developers can call the test suite to check if their backend is compatible with the expectations. The example below (from the MsBackendMgf package) shows how these tests can be included by adding the code below to the package's "testthat.R" file:

## Run tests defined in test suites from the Spectra package.
fls <- dir(system.file("extdata", package = "MsBackendMgf"),
           full.names = TRUE, pattern = "mgf$")[1:2]
be <- MsBackendMgf()
be <- backendInitialize(be, fls)

## Run the MsBackend spectra variable test suite
test_suite <- system.file("test_backends", "test_MsBackend",
                          package = "Spectra")

## Run single test file.
res <- test_file(paste0(test_suite, "/test_spectra_variables.R"),
                 reporter = check_reporter(), stop_on_failure = TRUE)
## Run the whole suite.
## test_dir(test_suite, stop_on_failure = TRUE)

Thus, the backend to be tested needs to be created, initialized and assigned to a variable named be. Then, the respective test suite from the Spectra package is called. Importantly, it is required to set stop_on_failure = TRUE since otherwise the unit tests might silently fail and not be reported.

jorainer commented 2 years ago

If there are no major objections (@lgatto ) I would merge that PR. I think this testing framework might be really helpful for MsBackend developers to ensure the implemented backend is compliant with what we expect/require from a MsBackend.

jorainer commented 2 years ago

All changes from @sgibb applied - thanks for the review!

jorainer commented 2 years ago

I see it from a practical point of view. Spectra provides a infrastructure for MS data (similar to GRanges for genomics). I think we should allow users to change whatever variables they want to change. You have also to consider that Spectra are not always read from raw MS files - in many cases you want to manually create a Spectra object and fill/change its content.