Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template for read_*() unit tests #55

Open
GegznaV opened this issue Aug 5, 2021 · 3 comments
Open

Template for read_*() unit tests #55

GegznaV opened this issue Aug 5, 2021 · 3 comments

Comments

@GegznaV
Copy link
Member

GegznaV commented Aug 5, 2021

I suggest this basic template for unit tests of read_*() functions. In each situation, function/file-format-specific unit tests should be added. Written as RStudio snippet:

snippet hut
	# Unit tests -----------------------------------------------------------------
	
	hySpc.testthat::test(${1:function_to_test}) <- function() {
	  local_edition(3)
	  
	  filename <- system.file(
	    "extdata",
	    "${2:path_to_file_to_read}",
	    package = "hySpc.read.txt"
	  )
	  
	  expect_silent(spc <- ${1:function_to_test}(filename))
	  
	  n_wl <- nwl(spc)
	  n_rows <- nrow(spc)
	  n_clos <- ncol(spc)
	  
	  test_that("${3:file format}: hyperSpec obj. dimensions are correct", {
	    expect_equal(n_wl, ___)
	    expect_equal(n_rows, ___)
	    expect_equal(n_clos, ___)
	    
	  })
	  
	  test_that("${3:file format}: extra data are correct", {
	    # @data colnames
	    expect_equal(colnames(spc), c("spc", "filename", ___))
	    
	    # @data values
	    # (Add tests, if relevant or remove this row)
	    
	  })
	
	  test_that("${3:file format}: labels are correct", {
	    expect_equal(spc@label\$.wavelength, NULL)
	    expect_equal(spc@label\$spc, NULL)
	    expect_equal(spc@label\$filename, "filename")
	  })
	
	  test_that("${3:file format}: spectra are correct", {
	    # Dimensions of spectra matrix (@data$spc)
	    expect_equal(dim(spc@data\$spc), c(___, ___))
	
	    # Column names of spectra matrix
	    expect_equal(colnames(spc@data\$spc)[1], "___")
	    expect_equal(colnames(spc@data\$spc)[10], "___")
	    expect_equal(colnames(spc@data\$spc)[n_wl], "___") # last name
	
	    # Values of spectra matrix
	    expect_equal(unname(spc@data\$spc[1, 1]), ___)
	    expect_equal(unname(spc@data\$spc[2, 10]), ___)
	    expect_equal(unname(spc@data\$spc[n_rows, n_wl]), ___) # last spc value
	
	  })
	
	  test_that("${3:file format}: wavelengths are correct", {
	    expect_equal(spc@wavelength[1], ___)
	    expect_equal(spc@wavelength[10], ___)
	    expect_equal(spc@wavelength[n_wl], ___)
	  })
	}
  1. I suggest checking just a few values and not a whole vector, matrix, etc. This would check the peace of functionality in a minimal way but make the test more readable.
  2. I think, values must not be rounded if possible.

This is how this template would look like for read_asc_Andor() function:

# Unit tests -----------------------------------------------------------------

hySpc.testthat::test(read_asc_Andor) <- function() {
  local_edition(3)
  
  filename <- system.file(
    "extdata",
    "asc.Andor/ASCII-Andor-Solis.asc",
    package = "hySpc.read.txt"
  )
  
  expect_silent(spc <- read_asc_Andor(filename))
  
  n_wl <- nwl(spc)
  n_rows <- nrow(spc)
  n_clos <- ncol(spc)
  
  test_that("Andor Solis .asc: hyperSpec obj. dimensions are correct", {
    expect_equal(n_wl, 63)
    expect_equal(n_rows, 5)
    expect_equal(n_clos, 2)
    
  })
  
  test_that("Andor Solis .asc: extra data are correct", {
    # @data colnames
    expect_equal(colnames(spc), c("spc", "filename"))
    
    # @data values
    # (Add tests, if relevant or remove this row)
    
  })

  test_that("Andor Solis .asc: labels are correct", {
    expect_equal(spc@label$.wavelength, NULL)
    expect_equal(spc@label$spc, NULL)
    expect_equal(spc@label$filename, "filename")
  })

  test_that("Andor Solis .asc: spectra are correct", {
    # Dimensions of spectra matrix (@data$spc)
    expect_equal(dim(spc@data$spc), c(5, 63))

    # Column names of spectra matrix
    expect_equal(colnames(spc@data$spc)[1], "161.408")
    expect_equal(colnames(spc@data$spc)[10], "200.184")
    expect_equal(colnames(spc@data$spc)[n_wl], "423.651") # last name

    # Values of spectra matrix
    expect_equal(unname(spc@data$spc[1, 1]), 3404)
    expect_equal(unname(spc@data$spc[2, 10]), 3405)
    expect_equal(unname(spc@data$spc[n_rows, n_wl]), 3415) # last spc value

  })

  test_that("Andor Solis .asc: wavelengths are correct", {
    expect_equal(spc@wavelength[1], 161.40845)
    expect_equal(spc@wavelength[10], 200.18387)
    expect_equal(spc@wavelength[n_wl], 423.65106)
  })
}

What is your opinion, @sangttruong, @bryanhanson, @cbeleites? Is this template OK for you?

@bryanhanson
Copy link
Collaborator

Is there a tool or function that automatically fills this template in with the values set off by, for example, ${3:file format}?

@GegznaV
Copy link
Member Author

GegznaV commented Aug 15, 2021

Do you mean code snippets in the RStudio IDE?
When you insert a code snippet by typing the snippet keyword and pressing Shift+Tab (not sure what's the correct hotkey combination on Mac), the fields that have the same number, e.g. 3, can be filled with the same text at the same time. You can navigate between the fields by pressing Tab (move to the next field) and Shift+Tab (move to the previous field).

@bryanhanson
Copy link
Collaborator

Thanks, I'll look at the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants