Skip to content

Commit

Permalink
add tests, use use_grs param instead
Browse files Browse the repository at this point in the history
  • Loading branch information
rxu17 committed Dec 4, 2024
1 parent dd94688 commit 1caada4
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 11 deletions.
17 changes: 10 additions & 7 deletions scripts/uploads/merge_and_uncode_rca_uploads.R
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,17 @@ merge_mappings <- function(primarys, secondarys, debug = F) {
#' @param dd Matrix with two columns, first containing a label and
#' second columns a mapping string.
#' @param grs Another mapping matrix that is used secondarily
#' if the label is not found in the primary mapping matrix. Optional.
#' if the label is not found in the primary mapping matrix.
#' @param use_grs Whether we are using grs or not
#' @return Data frame of uncoded data.
#' @example
#' map_code_to_value(data = my_data, dd = dd, grs = grs)
uncode_data <- function(df_coded, dd, grs = NULL) {
uncode_data <- function(df_coded, dd, grs, use_grs) {

df_uncoded <- df_coded

# merge reference mappings
if(!is.null(grs)){
if(use_grs){
mappings_primary <- parse_mappings(strs = grs[,config$column_name$variable_mapping],
labels = grs[,config$column_name$variable_name])
mappings_secondary <- parse_mappings(strs = dd[[config$column_name$variable_mapping]],
Expand Down Expand Up @@ -597,9 +598,10 @@ get_data_dictionary <- function(cohort) {
return(dd)
}

#' Retrieves the Global Response Set (grs)
#' Retrieves the Global Response Set (grs) depending
#' on value of use_grs. If not using grs, returns NULL
#'
#' @param use_grs Whether to retrieve it or not
#' @param use_grs Whether to use grs or not
#' @return grs
get_global_response_set <- function(use_grs){
if(use_grs){
Expand Down Expand Up @@ -744,7 +746,7 @@ main <- function(){
make_option(c("--comment"), type = "character",
help="Comment for new table snapshot version. This must be unique and is tied to the cohort run."),
make_option(c("--use_grs"), type="logical", default = FALSE,
help="Whether to use GRS / DD or just DD for mapping")
help="Whether to use grs as primary mapping (dd as secondary) or not (using dd only).")
)
opt <- parse_args(OptionParser(option_list=option_list))

Expand Down Expand Up @@ -817,7 +819,8 @@ main <- function(){
# uncode
uncoded <- uncode_data(df_coded = coded,
dd = dd,
grs = grs)
grs = grs,
use_grs = opt$use_grs)

if (debug) {
print(glue("{now(timeOnly = T)}: Formatting uncoded data..."))
Expand Down
69 changes: 65 additions & 4 deletions scripts/uploads/tests/test_merge_and_uncode_rca_uploads.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
library(mockery)
library(testthat)

source(testthat::test_path("..", "merge_and_uncode_rca_uploads.R"))
Expand All @@ -8,18 +9,78 @@ setup({
if (!file.exists("config.yaml")) {
workdir <- "/usr/local/src/myscripts"
}
config <<- read_yaml(glue("{workdir}/config.yaml"))
config_path <<- read_yaml(glue("{workdir}/config.yaml"))

# Mock config
config <<- list(
synapse = list(
prissmm = list(id = "mock_prissmm_id"),
grs = list(id = "mock_grs_id")
),
upload = list(
cohort1 = list(
site1 = list(
data1 = "mock_data1_id",
data2 = "mock_data2_id"
),
site2 = list(
data1 = "mock_data3_id",
data2 = "mock_data4_id"
)
)
)
)

})


test_that("get_output_folder_id gets expected id when environment is production", {
folder_id <- get_output_folder_id(config, environment = "production")
folder_id <- get_output_folder_id(config_path, environment = "production")
expect_equal(folder_id, "syn23286928")
})


test_that("get_output_folder_id gets expected id when environment is staging", {
folder_id <- get_output_folder_id(config, environment = "staging")
folder_id <- get_output_folder_id(config_path, environment = "staging")
expect_equal(folder_id, "syn63887337")
})
})


test_that("get_global_response_set returns NULL when use_grs is FALSE", {
result <- get_global_response_set(use_grs = FALSE)
expect_null(result)
})


test_that("get_global_response_set calls read.csv and returns data when use_grs is TRUE", {
mock_read_csv <- mock(data.frame(a = 1:3, b = 4:6)) # Example mock data frame
stub(get_global_response_set, "read.csv", mock_read_csv)

mock_synGet <- mock(list(path = "mock_path"))
stub(get_global_response_set, "synGet", mock_synGet)

result <- get_global_response_set(use_grs = TRUE)

# Verify the function returns the mocked data frame
expect_equal(result, data.frame(a = 1:3, b = 4:6))
})


test_that("get_prov_used returns correct prov when use_grs is FALSE", {
# Mock the `get_bpc_synid_prissmm` function
mock_get_bpc_synid_prissmm <- mock("mock_synid_dd")
stub(get_prov_used, "get_bpc_synid_prissmm", mock_get_bpc_synid_prissmm)

result <- get_prov_used(cohort = "cohort1", use_grs = FALSE)
expect_equal(result, c("mock_data1_id", "mock_data2_id", "mock_data3_id", "mock_data4_id", "mock_synid_dd"))
})

test_that("get_prov_used returns correct prov when use_grs is TRUE", {
# Mock the `get_bpc_synid_prissmm` function
mock_get_bpc_synid_prissmm <- mock("mock_synid_dd")
stub(get_prov_used, "get_bpc_synid_prissmm", mock_get_bpc_synid_prissmm)

result <- get_prov_used(cohort = "cohort1", use_grs = TRUE)
print(result)
expect_equal(result, c("mock_data1_id", "mock_data2_id", "mock_data3_id", "mock_data4_id", "mock_synid_dd", "mock_grs_id"))
})

0 comments on commit 1caada4

Please sign in to comment.