softloud / neet

Coding to code::proof: Boundary checks for test-driven development.
Other
2 stars 2 forks source link

Writing a test for build_folder_structure function #14

Open DrMattG opened 4 years ago

DrMattG commented 4 years ago

How would I write a test for a function like this? It builds a folder structure in to the working directory to set up the architecture for one of the #LivingNorway packages which is designed to help researchers to capture basic metadata for legacy data (data that has been kept in a dusty draw for many years). Would a test just ask the question that the folders with the specific names are created?

`# Build_folder_structure

This function builds the legacy data folder structure in the working directory

build_folder_structure<-function(project_name=project_name){

if(dir.exists(project_name)==TRUE) {

print("Directory already exists please check the name and retry")

} else{

dir.create(paste0(getwd(),"/", project_name))

dir.create(paste0(getwd(),"/", project_name,"/", "minimum_metadata"))

dir.create(paste0(getwd(),"/",project_name,"/", "data"))

dir.create(paste0(getwd(),"/", project_name,"/","data", "/", "raw_data"))

dir.create(paste0(getwd(),"/", project_name,"/","data", "/", "mapped_data"))

dir.create(paste0(getwd(),"/", project_name,"/","data", "/", "scan_data"))

dir.create(paste0(getwd(),"/", project_name,"/","scripts"))

dir.create(paste0(getwd(),"/", project_name,"/","meta_xml"))

dir.create(paste0(getwd(),"/", project_name,"/","dmp"))

rmarkdown::draft(paste0(getwd(),"/", project_name,"/", "minimum_metadata", "/","minimum_metadata.Rmd"),

                 template="minimum_metadata", package="TheDataPackage", edit=FALSE))

  }

}

build_folder_structure(project_name = "Test")`

softloud commented 4 years ago

Great question; great first step. I've spent all week working through bash tutorials precisely for this kind of thing. So, yeah, I think non-empy of expected type in this case would be to check that the file structures exist. Can be checked via R wrapper or bash.

But :thinking: to break it up into a neet flow, I think you might start with checking anything has been created. The and the rest tests will check that the file names are the exact names you want.

So, :thinking: start with one test for the function. When you call the list of filenames in that directory as a string in R, is the character string nonempty? I am taking a look at list.files and list.dirs and I don't fully understand what/how these work, will take some exploration. Another option is to write a bash check and wrap it; I am increasingly turning to bash for file handling and exploration, rather than R, which I use for data wrangling and analysis.

Tricky thing is, and this is where I get fuzzy and we move into stormy seas, is that tests are finnicky about where they put things and where they read things from. Would take a little fiddling. I suppose we could write the files to the testing directory, one level up from testthat. Will take some trial and error, but it's a good first testing project for us.

On Thu, Apr 16, 2020 at 12:34 AM Matthew Grainger notifications@github.com wrote:

How would I write a test for a function like this? It builds a folder structure in to the working directory to set up the architecture for one of the #LivingNorway packages which is designed to help researchers to capture basic metadata for legacy data (data that has been kept in a dusty draw for many years). Would a test just ask the question that the folders with the specific names are created?

`# Build_folder_structure This function builds the legacy data folder structure in the working directory

build_folder_structure<-function(project_name=project_name){

if(dir.exists(project_name)==TRUE) {

print("Directory already exists please check the name and retry")

} else{

dir.create(paste0(getwd(),"/", project_name))

dir.create(paste0(getwd(),"/", project_name,"/", "minimum_metadata"))

dir.create(paste0(getwd(),"/",project_name,"/", "data"))

dir.create(paste0(getwd(),"/", project_name,"/","data", "/", "raw_data"))

dir.create(paste0(getwd(),"/", project_name,"/","data", "/", "mapped_data"))

dir.create(paste0(getwd(),"/", project_name,"/","data", "/", "scan_data"))

dir.create(paste0(getwd(),"/", project_name,"/","scripts"))

dir.create(paste0(getwd(),"/", project_name,"/","meta_xml"))

dir.create(paste0(getwd(),"/", project_name,"/","dmp"))

rmarkdown::draft(paste0(getwd(),"/", project_name,"/", "minimum_metadata", "/","minimum_metadata.Rmd"),

             template="minimum_metadata", package="TheDataPackage", edit=FALSE))

}

}

build_folder_structure(project_name = "Test")`

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/softloud/neet/issues/14, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADGJTQQOEPCC3GXFQZCDIDDRMXAWBANCNFSM4MIT77YQ .