Skip to content

Commit

Permalink
Improve test coverage (#136)
Browse files Browse the repository at this point in the history
* Add secrets to codecov workflow
* Add more tests
  • Loading branch information
zsteinmetz authored Aug 31, 2023
1 parent ec4e5e2 commit 3c25673
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 40 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ jobs:
extra-packages: any::covr
needs: coverage

- name: Populate .Renviron with GitHub Secrets
run: |
echo AWS_ACCESS_KEY_ID="${{ secrets.AWS_ACCESS_KEY_ID }}" >> ~/.Renviron
echo AWS_SECRET_ACCESS_KEY="${{ secrets.AWS_SECRET_ACCESS_KEY }}" >> ~/.Renviron
shell: bash

- name: Test coverage
run: |
covr::codecov(
Expand Down
61 changes: 33 additions & 28 deletions tests/testthat/test-as_OpenSpecy.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
library(data.table)

data("raman_hdpe")
df <- read_extdata("raman_hdpe.csv") |> read.csv()

test_that("as_OpenSpecy() handles errors correctly", {
as_OpenSpecy(df$wavenumber, as.data.frame(df$intensity)) |> expect_silent()
as_OpenSpecy(df$wavenumber, df$intensity) |> expect_error()
as_OpenSpecy(df$wavenumber) |> expect_error()
as_OpenSpecy(df$wavenumber, as.data.frame(df$intensity[-1])) |> expect_error()
as_OpenSpecy(df$wavenumber, data.table(intensity = df$intensity,
intensity = df$intensity)) |>
expect_error()

as_OpenSpecy(data.frame(x = df$wavenumber, abs = df$intensity)) |>
expect_warning()
as_OpenSpecy(data.frame(wav = df$wavenumber, y = df$intensity)) |>
expect_warning()

amb <- df
names(amb) <- c("a", "b")
as_OpenSpecy(amb) |> expect_warning() |> expect_warning()

as_OpenSpecy(df$wavenumber, as.data.frame(df$intensity), coords = "") |>
expect_error()
as_OpenSpecy(df$wavenumber, as.data.frame(df$intensity), coords = df) |>
expect_error()
as_OpenSpecy(df$wavenumber, as.data.frame(df$intensity), metadata = "") |>
expect_error()
})

test_that("as_OpenSpecy() generates OpenSpecy objects", {
expect_silent(as_OpenSpecy(df))
as_OpenSpecy(df) |> expect_silent()
as_OpenSpecy(raman_hdpe, session_id = T) |> expect_silent()

expect_silent(osf <- as_OpenSpecy(df))
expect_silent(ost <- data.table(df) |> as_OpenSpecy())
expect_silent(osl <- list(df$wavenumber, df[2]) |> as_OpenSpecy())
expect_silent(OpenSpecy(osf))
osf <- as_OpenSpecy(df) |> expect_silent()
ost <- data.table(df) |> as_OpenSpecy() |> expect_silent()
osl <- list(df$wavenumber, df[2]) |> as_OpenSpecy() |> expect_silent()
OpenSpecy(osf) |> expect_silent()

expect_s3_class(osf, "OpenSpecy")
expect_s3_class(ost, "OpenSpecy")
Expand All @@ -29,32 +57,9 @@ test_that("as_OpenSpecy() generates OpenSpecy objects", {
expect_false(is_OpenSpecy(df))
})

test_that("as_OpenSpecy() handles errors correctly", {
expect_silent(as_OpenSpecy(df$wavenumber, as.data.frame(df$intensity)))
expect_error(as_OpenSpecy(df$wavenumber, df$intensity))
expect_error(as_OpenSpecy(df$wavenumber))
expect_error(as_OpenSpecy(df$wavenumber, as.data.frame(df$intensity[-1])))
expect_error(as_OpenSpecy(df$wavenumber,
data.table(intensity = df$intensity,
intensity = df$intensity)))

expect_warning(as_OpenSpecy(data.frame(x = df$wavenumber,
abs = df$intensity)))
expect_warning(as_OpenSpecy(data.frame(wav = df$wavenumber,
y = df$intensity)))

expect_error(as_OpenSpecy(df$wavenumber, as.data.frame(df$intensity),
coords = ""))
expect_error(as_OpenSpecy(df$wavenumber, as.data.frame(df$intensity),
coords = df))
expect_error(as_OpenSpecy(df$wavenumber, as.data.frame(df$intensity),
metadata = ""))
})

test_that("check_OpenSpecy() work as expected", {
os <- as_OpenSpecy(df)
check_OpenSpecy(os) |> expect_true()

check_OpenSpecy(df) |> expect_error()

osv <- osn <- oss <- ost <- osl <- os
Expand Down
22 changes: 16 additions & 6 deletions tests/testthat/test-manage_spec.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
library(data.table)
data("raman_hdpe")

test_that("merging identical files without range specification", {
specs <- lapply(c(read_extdata("raman_hdpe.yml"),
read_extdata("ftir_ldpe_soil.asp")), read_any)
no_overlap <- specs
no_overlap[[1]] <- restrict_range(no_overlap[[1]], 300, 500)
no_overlap[[2]] <- restrict_range(no_overlap[[2]], 700, 1000)

test_that("c_spec() handles input errors correctly", {
c_spec(1:1000) |> expect_error()
c_spec(list(1:1000, 1000:2000)) |> expect_error()
c_spec(raman_hdpe) |> expect_warning()
c_spec(no_overlap) |> expect_error()
})

test_that("c_spec() merges identical files without range specification", {
specs <- lapply(c(read_extdata("raman_hdpe.yml"),
read_extdata("raman_hdpe.yml")), read_spec)
same <- c_spec(specs) |> expect_silent()
Expand All @@ -10,10 +23,7 @@ test_that("merging identical files without range specification", {
expect_equal(same$spectra$intensity, raman_hdpe$spectra$intensity)
})

specs <- lapply(c(read_extdata("raman_hdpe.yml"),
read_extdata("ftir_ldpe_soil.asp")), read_any)

test_that("merging different files with common range", {
test_that("c_spec() merges different files with common range", {
diff <- c_spec(specs, range = "common", res = 5) |>
expect_silent()

Expand All @@ -22,7 +32,7 @@ test_that("merging different files with common range", {
diff$spectra$intensity.1[1:2] |> round(2) |> expect_equal(c(0.03, 0.03))
})

test_that("merging different files with specified range", {
test_that("c_spec() merges different files with specified range", {
spec <- c_spec(specs, range = c(1000, 2000), res = 5) |>
expect_silent()

Expand Down
17 changes: 13 additions & 4 deletions tests/testthat/test-process_spec.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
data("raman_hdpe")
tiny_map <- read_extdata("CA_tiny_map.zip") |> read_any()

test_that("process_spec() handles input errors corretly", {
process_spec(1:1000) |> expect_error()
})

test_that("process_spec() returns expected values", {
tiny_map <- read_extdata("CA_tiny_map.zip") |> read_any()
process_spec(raman_hdpe, adj_intens = T, conform_spec = T, restrict_range = T,
flatten_range = T, subtr_baseline = T, smooth_intens = T) |>
expect_silent()

conf <- process_spec(tiny_map) |> expect_silent()

expect_equal(conf, conform_spec(tiny_map, range = NULL, res = 5) |>
smooth_intens(polynomial = 3,
window = 11,
derivative = 1,
abs = T,
make_rel = T))


proc <- process_spec(raman_hdpe,
smooth_intens = TRUE,
smooth_intens_args = list(
polynomial = 3,
window = 11,
derivative = 1
)
) |> expect_silent()
)) |>
expect_silent()

expect_equal(proc, conform_spec(raman_hdpe) |>
smooth_intens(derivative = 1, make_rel = T))
Expand Down
9 changes: 7 additions & 2 deletions tests/testthat/test-sig_noise.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Load your OpenSpecy object
data("raman_hdpe")

test_that("sig_noise returns correct values", {
test_that("sig_noise() handles input errors corretly", {
sig_noise(1:1000) |> expect_error()
restrict_range(raman_hdpe, 300, 320) |> sig_noise() |>
expect_warning()
})

test_that("sig_noise() returns correct values", {
sig_noise(raman_hdpe, metric = "sig") |> round(2) |> unname() |>
expect_equal(101.17)

Expand Down

0 comments on commit 3c25673

Please sign in to comment.