forked from PecanProject/pecan
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request PecanProject#3191 from meetagrawal09/test-qaqc
Improving test coverage for `PEcAn.qaqc` package
- Loading branch information
Showing
15 changed files
with
130 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,35 @@ | ||
##' get_table_column_names | ||
##' @author Tempest McCabe | ||
##' | ||
##' @param table a table that is output from one of the find_* functions, | ||
##' or a data.frame containing the output from multiple find_* functions. Could also be a vector of table names. | ||
##' @param con a connection to the bety database. | ||
##' | ||
##' | ||
##' @description This function will return a vector of the column names for a given table(s) in the bety database. | ||
##' Useful for choseing which columns to include in the written-out table. | ||
##' | ||
##' @param table a table that is output from one of the find_* functions, | ||
##' or a data.frame containing the output from multiple find_* functions. Could also be a vector of table names. | ||
##' @param con a connection to the bety database. | ||
##' | ||
##' | ||
##' @description This function will return a vector of the column names for a given table(s) in the bety database. | ||
##' Useful for choosing which columns to include in the written-out table. | ||
##' | ||
##' For more information on how to use this function see the "Pre-release-database-cleanup" script in the 'vignettes' folder | ||
##' or look at the README | ||
##' @export | ||
get_table_column_names<-function(table, con){ | ||
|
||
if(is.data.frame(table)){ | ||
if("table_name" %in% names(table)){ | ||
table_factor<-as.factor(table$table_name) | ||
table_name<-levels(table_factor) | ||
}else{ | ||
get_table_column_names <- function(table, con) { | ||
if (is.data.frame(table)) { | ||
if ("table_name" %in% names(table)) { | ||
table_factor <- as.factor(table$table_name) | ||
table_name <- levels(table_factor) | ||
} else { | ||
PEcAn.logger::logger.severe("Table needs either a 'table_names' column or be a character vector of table names") | ||
} | ||
|
||
}else if(is.vector(table)){ | ||
table_name<-table | ||
}else{ | ||
} else if (is.vector(table)) { | ||
table_name <- table | ||
} else { | ||
PEcAn.logger::logger.severe("table must either be a dataframe or a vector") | ||
} | ||
column_names<-list() | ||
for(i in seq_along(table_name)){ | ||
query <- PEcAn.DB::db.query(paste("SELECT * from", table_name, "LIMIT 1"), con=con) | ||
column_names[[i]]<-colnames(query) | ||
names(column_names)<-table_name | ||
|
||
column_names <- list() | ||
for (i in seq_along(table_name)) { | ||
query <- PEcAn.DB::db.query(paste("SELECT * from", table_name, "LIMIT 1"), con = con) | ||
column_names[[i]] <- colnames(query) | ||
names(column_names) <- table_name | ||
} | ||
return(column_names) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,29 @@ | ||
##' write_out_table | ||
##' @author Tempest McCabe | ||
##' | ||
##' @param table a table that is output from one of the find_* fucntions | ||
##' | ||
##' @param table a table that is output from one of the find_* functions | ||
##' @param table_name name of table | ||
##' @param outdir path to folder into which the editable table will be written | ||
##' @param relevant_table_columns a list of all columns to keep. ID and table name will be automatically included. | ||
##' | ||
##' | ||
##' @description This is a fucntion that returns a dataframe with all of the format entries that have no assosiated input records. | ||
##' @param relevant_table_columns a list of all columns to keep. ID and table name will be automatically included. | ||
##' | ||
##' | ||
##' @description This is a function that returns a dataframe with all of the format entries that have no associated input records. | ||
##' | ||
##' For more information on how to use this function see the "Pre-release-database-cleanup" script in the 'vignettes' folder | ||
##' or look at the README | ||
##' @export | ||
write_out_table<-function(table,table_name,outdir, relevant_table_columns){ | ||
|
||
if(!"id" %in% relevant_table_columns){ | ||
relevant_table_columns<-c(relevant_table_columns, "id") | ||
write_out_table <- function(table, table_name, outdir, relevant_table_columns) { | ||
if (!"id" %in% relevant_table_columns) { | ||
relevant_table_columns <- c(relevant_table_columns, "id") | ||
} | ||
if(!"table_name" %in% relevant_table_columns){ | ||
relevant_table_columns<-c(relevant_table_columns, "id", "table_name") | ||
if (!"table_name" %in% relevant_table_columns) { | ||
relevant_table_columns <- c(relevant_table_columns, "table_name") | ||
} | ||
if(!any(c("id", "table_name") %in% names(table))){ | ||
if (!any(c("id", "table_name") %in% names(table))) { | ||
PEcAn.logger::logger.severe("table provided doesn't have a table_name or id column or both. ") | ||
} | ||
table<-table[ , (relevant_table_columns)] | ||
utils::write.table(table, file=paste(outdir,"/query_of_",table_name ,sep=""),row.names = FALSE,sep="|") | ||
} | ||
|
||
|
||
table <- table[, (relevant_table_columns)] | ||
utils::write.table(table, file = paste(outdir, "/query_of_", table_name, sep = ""), row.names = FALSE, sep = "|") | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
test_that("`cull_database_entries()` gives errors for faulty inputs",{ | ||
expect_error( | ||
cull_database_entries(outdir = 'test'), | ||
"If a table object hasn't been provided, a file_name must be set." | ||
) | ||
expect_error( | ||
cull_database_entries(table = 'test_table', file_name = 'test_file', outdir = 'test'), | ||
"table and file_name cannot both be provided." | ||
) | ||
expect_error( | ||
cull_database_entries(table = 'test_table', outdir = 'test'), | ||
"Please provide a table_name" | ||
) | ||
}) | ||
|
||
test_that("`cull_database_entries()` able to correctly add logs to the output file", { | ||
withr::with_dir(tempdir(), { | ||
mockery::stub(cull_database_entries, 'PEcAn.DB::db.query', 'test_log') | ||
dir <- getwd() | ||
cull_database_entries(table = data.frame(id = 1), table_name = 'test', con = 1, outdir = dir) | ||
expect_true(file.exists(paste0(dir, "/deletion_log_of_test"))) | ||
file_data <- readLines(paste0(dir, "/deletion_log_of_test")) | ||
expect_equal(grepl("test_log", file_data), c(TRUE, TRUE)) | ||
}) | ||
}) |
14 changes: 14 additions & 0 deletions
14
base/qaqc/tests/testthat/test.find_formats_without_inputs.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
test_that("`find_formats_without_inputs()` able to find formats with no input record",{ | ||
format_command_mock <- data.frame(user_id = '2020', created_at = '2001-01-01', updated_at = '2010-01-01') | ||
input_command_mock <- data.frame(format_id = '2000', user_id = '2021', created_at = '2002-01-02', updated_at = '2012-01-02') | ||
mocked_res <- mockery::mock(input_command_mock, format_command_mock) | ||
mockery::stub(find_formats_without_inputs, 'dplyr::tbl', mocked_res) | ||
|
||
res <- find_formats_without_inputs( | ||
con = NULL, user_id_code = '2020', created_after = '2000-01-01', updated_after = '2009-01-01', created_before = '2002-01-01', updated_before = '2011-01-01' | ||
) | ||
expect_equal( | ||
res, | ||
data.frame(id = '2020', created_at = '2001-01-01', updated_at = '2010-01-01', table_name = "formats") | ||
) | ||
}) |
13 changes: 13 additions & 0 deletions
13
base/qaqc/tests/testthat/test.find_inputs_without_formats.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
test_that("`find_inputs_without_formats()` able to find inputs with no format records", { | ||
input_command_mock <- data.frame(format_id = '2020', user_id = '2020', created_at = '2001-01-01', updated_at = '2010-01-01') | ||
format_command_mock <- data.frame(user_id = '2021', created_at = '2002-01-02', updated_at = '2012-01-02') | ||
mocked_res <- mockery::mock(input_command_mock, format_command_mock) | ||
mockery::stub(find_inputs_without_formats, 'dplyr::tbl', mocked_res) | ||
res <- find_inputs_without_formats( | ||
con = NULL, user_id = '2020', created_after = '2000-01-01', updated_after = '2009-01-01', created_before = '2002-01-01', updated_before = '2011-01-01' | ||
) | ||
expect_equal( | ||
res, | ||
data.frame(id = '2020', user_id = '2020',created_at = '2001-01-01', updated_at = '2010-01-01', table_name = "inputs") | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
test_that("`get_table_column_names()` able to return the column names of a table as a list",{ | ||
mocked_res <- mockery::mock(data.frame(head1 = 1, head2 = 2)) | ||
mockery::stub(get_table_column_names, 'PEcAn.DB::db.query', mocked_res) | ||
res <- get_table_column_names(table = data.frame(table_name = 'test_table'), con = 1) | ||
args <- mockery::mock_args(mocked_res) | ||
expect_equal(args[[1]][[1]], "SELECT * from test_table LIMIT 1") | ||
expect_equal(res, list(test_table = c("head1", "head2"))) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
test_that("`write_out_table()` able to create and update output file with relevant data",{ | ||
withr::with_dir(tempdir(), { | ||
dir <- getwd() | ||
write_out_table( | ||
table = data.frame(id = 1, table_name = 'test'), table_name = 'test', relevant_table_columns = c(), outdir = dir | ||
) | ||
expect_true(file.exists(paste0(dir, "/query_of_test"))) | ||
file_data <- readLines(paste0(dir, "/query_of_test")) | ||
expect_equal(grepl("test", file_data), c(FALSE, TRUE)) | ||
}) | ||
}) |