From 8462a13101219a127d2f8dcb921754b364d381f0 Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 23 Jun 2024 12:37:29 +0200 Subject: [PATCH 01/30] Moved to version 2.1.0; Prepared testing for getStudyData --- DESCRIPTION | 5 ++- .../files_output/CastorAdverseEvent.csv | 3 ++ .../files_output/CastorComorbidities.csv | 2 + .../files_output/CastorMedication.csv | 3 ++ .../files_output/CastorQOLSurvey.csv | 3 ++ tests/test_files/files_output/CastorStudy.csv | 3 ++ .../files_output/CastorUnscheduledVisit.csv | 2 + tests/testthat/test_getStudyData.R | 45 +++++++++++++++++-- 8 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 tests/test_files/files_output/CastorAdverseEvent.csv create mode 100644 tests/test_files/files_output/CastorComorbidities.csv create mode 100644 tests/test_files/files_output/CastorMedication.csv create mode 100644 tests/test_files/files_output/CastorQOLSurvey.csv create mode 100644 tests/test_files/files_output/CastorStudy.csv create mode 100644 tests/test_files/files_output/CastorUnscheduledVisit.csv diff --git a/DESCRIPTION b/DESCRIPTION index 809e30f..f797ca1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: castoRedc Title: Wraps Castor API for easy data access -Version: 2.0.0 +Version: 2.1.0 Authors@R: c( person("Thomas", "Johnson", role = "aut"), person("Reinier", "van Linschoten", @@ -26,10 +26,11 @@ Imports: glue Suggests: knitr, - testthat + testthat (>= 3.0.0) RoxygenNote: 7.3.0 Collate: 'imports.R' 'utils.R' 'CastorData.R' 'CastorAPIWrapper.R' +Config/testthat/edition: 3 diff --git a/tests/test_files/files_output/CastorAdverseEvent.csv b/tests/test_files/files_output/CastorAdverseEvent.csv new file mode 100644 index 0000000..8ba49b6 --- /dev/null +++ b/tests/test_files/files_output/CastorAdverseEvent.csv @@ -0,0 +1,3 @@ +record_id,institute,created_on,custom_name,parent,archived,AE_type,AE_startdate,AE_stopdate,AE_severity,AE_relation,AE_action,AE_outcome,AE_expected,AE_SAE +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:58:13,Adverse event - 22-06-2021 09:58:12,No parent,FALSE,,,,,,,,, +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:55:13,Adverse event - 22-06-2021 09:55:12,Follow-up Visit,FALSE,A bug in the API,23-06-2021,24-06-2021,Life-threatening,Definitely related,Medical intervention,Resolved,Yes,Yes diff --git a/tests/test_files/files_output/CastorComorbidities.csv b/tests/test_files/files_output/CastorComorbidities.csv new file mode 100644 index 0000000..db92eed --- /dev/null +++ b/tests/test_files/files_output/CastorComorbidities.csv @@ -0,0 +1,2 @@ +record_id,institute,created_on,custom_name,parent,archived,comorbidities +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:56:07,Comorbidities - 22-06-2021 09:56:06,No parent,FALSE,-- Uploaded File: comorbidities.txt -- diff --git a/tests/test_files/files_output/CastorMedication.csv b/tests/test_files/files_output/CastorMedication.csv new file mode 100644 index 0000000..f775820 --- /dev/null +++ b/tests/test_files/files_output/CastorMedication.csv @@ -0,0 +1,3 @@ +record_id,institute,created_on,custom_name,parent,archived,med_name,med_start,med_stop,med_dose,med_units,med_other_unit +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:54:28,Medication - 22-06-2021 09:54:28,Unscheduled visits and Medication,FALSE,Diclofenac,25-06-2021,28-06-2021,150,mg/day, +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:58:27,Medication - 22-06-2021 09:58:26,No parent,FALSE,,,,,, diff --git a/tests/test_files/files_output/CastorQOLSurvey.csv b/tests/test_files/files_output/CastorQOLSurvey.csv new file mode 100644 index 0000000..baac99c --- /dev/null +++ b/tests/test_files/files_output/CastorQOLSurvey.csv @@ -0,0 +1,3 @@ +record_id,institute,survey_name,survey_instance_id,created_on,sent_on,progress,completed_on,package_id,package_name,archived,SF12_1,SF12_2,SF12_3,SF12_12,VAS +110001,Franciscus Gasthuis & Vlietland,QOL Survey,4FF130AD-274C-4C8F-A4A0-A7816A5A88E9,22-06-2021 10:12:19,22-06-2021 10:12:19,100,22-06-2021 10:12:44,0761A9BA-9802-483D-8EB3-D07233A56F2B,My first survey package,FALSE,Excellent,Mostly,Mostly,Rarely,85 +110001,Franciscus Gasthuis & Vlietland,QOL Survey,B99F0082-1B20-456B-8D4D-47D16867B211,22-06-2021 10:13:01,22-06-2021 10:13:01,0,,5B3FE56F-89FA-4C6D-A638-E8FA46113EB0,My first survey package,FALSE,,,,, diff --git a/tests/test_files/files_output/CastorStudy.csv b/tests/test_files/files_output/CastorStudy.csv new file mode 100644 index 0000000..7e3e8b4 --- /dev/null +++ b/tests/test_files/files_output/CastorStudy.csv @@ -0,0 +1,3 @@ +record_id,archived,institute,randomisation_group,randomisation_datetime,ic_date,ic_versions#Main study CF,ic_versions#Other,ic_main_version,ic_other_version,ic_language#English,ic_language#Dutch,ic_language#French,ic_language#Other,inc_ic,inc_trials,inc_age,inc_dx,inc_criteria,pat_birth_year,pat_sex,pat_race,pat_height,his_cvd,his_diabetes,his_smoke,his_smoke_start,his_smoke_dose,his_family#None,his_family#Unknown,his_family#Deafness,his_family#(Cardio)myopathy,his_family#Encephalopathy,his_family#Diabetes Mellitus,his_family#Hypertension/Cardiovascular disease,his_family#Thrombosis,his_family#Malignancy,randalloc,randdisp,disp_datetime,base_date,onset_stroke,onset_trombectomy,base_weight,base_bmi,base_sbp,base_dbp,base_hr,base_bl_date,base_hb,base_ht,base_leucoc,base_tromboc,base_urea,base_creat,base_CRP,fac_V_leiden_number,fac_V_leiden_date,base_gluc,fu_date,fu_weight,fu_bmi,fu_sbp,fu_dbp,fu_hr,fu_bl_date,fu_hb,fu_ht,fu_leucoc,fu_tromboc,fu_urea,fu_creat,fu_CRP,fu_gluc,unscheduled,conc_med +110001,FALSE,Franciscus Gasthuis & Vlietland,,,22-06-2021,TRUE,FALSE,2.5,,FALSE,FALSE,TRUE,FALSE,Yes,No,Yes,No,Yes,1996,Female,African/black,1.75,Yes,No,No,,,FALSE,FALSE,FALSE,TRUE,FALSE,FALSE,FALSE,FALSE,FALSE,,,,22-06-2021,21-06-2021 01:15,01:30,88,28.7,120,65,66,22-06-2021,7.4,0.23,4.1,252,4.6,88,2.1,55,22-06-2021,5.4,29-06-2021,66,21.6,132,72,69,29-06-2021,8.1,0.36,9.5,366,8.2,99,4.4,3.1,Yes,Yes +110002,FALSE,Franciscus Gasthuis & Vlietland,,,,FALSE,FALSE,,,-99,-99,-99,-99,,,,,,,,,-95,,,,,,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,,,,31-12-2999,,,,,,,,,,,,,,,,-96,01-01-2996,,,,,,,,,,,,,,,,,, diff --git a/tests/test_files/files_output/CastorUnscheduledVisit.csv b/tests/test_files/files_output/CastorUnscheduledVisit.csv new file mode 100644 index 0000000..5f0690e --- /dev/null +++ b/tests/test_files/files_output/CastorUnscheduledVisit.csv @@ -0,0 +1,2 @@ +record_id,institute,created_on,custom_name,parent,archived,unsvis_type,unsvis_date,unsvis_weight,unsvis_sbp,unsvis_dbp,unsvis_hr,unslab_date,unslab_s_hb,unslab_ht,unslab_leucoc,unslab_tromboc,unslab_urea,unslab_creat,unslab_CRP,unslab_gluc +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:57:38,Unscheduled visit - 22-06-2021 09:57:36,Unscheduled visits and Medication,FALSE,admission to day-care,24-06-2021,98.3,233,156,112,,,,,,,,, diff --git a/tests/testthat/test_getStudyData.R b/tests/testthat/test_getStudyData.R index bd37d70..8cb4980 100644 --- a/tests/testthat/test_getStudyData.R +++ b/tests/testthat/test_getStudyData.R @@ -1,10 +1,47 @@ -context("Test getStudyData in all its glory.") +context("Test if the output of getStudyData matches the expected output.") creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) +complete_study_output <- castor_api$getStudyData(creds$output_study) -test_that("getStudyData returns an appropriate object.", { - sdf <- castor_api$getStudyData(creds$example_study) +test_that("getStudyData returns the expected study data.", { + actual <- complete_study_output$Study + expected <- read.csv("tests/test_files/files_output/CastorStudy.csv", check.names=F) - expect_equal(creds$study_hash, digest::digest(sdf)) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Unscheduled Visit", { + actual <- complete_study_output$`Repeating data`$`Unscheduled visit` + expected <- read.csv("tests/test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Medication", { + actual <- complete_study_output$`Repeating data`$Medication + expected <- read.csv("tests/test_files/files_output/CastorMedication.csv", check.names=F) + + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Comorbidities", { + actual <- complete_study_output$`Repeating data`$Comorbidities + expected <- read.csv("tests/test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Adverse Events", { + actual <- complete_study_output$`Repeating data`$`Adverse event` + expected <- read.csv("tests/test_files/files_output/CastorAdverseEvent.csv", check.names=F) + + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the survey Quality of Life", { + actual <- complete_study_output$Surveys$`QOL Survey` + expected <- read.csv("tests/test_files/files_output/CastorQOLSurvey.csv", check.names=F) + + expect_identical(actual, expected) }) From 494efe5cec7d61ba19801e54ed2570069e241eac Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 23 Jun 2024 12:44:41 +0200 Subject: [PATCH 02/30] Testing of getStudyData runs --- tests/testthat/test_getStudyData.R | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tests/testthat/test_getStudyData.R b/tests/testthat/test_getStudyData.R index 8cb4980..593c52e 100644 --- a/tests/testthat/test_getStudyData.R +++ b/tests/testthat/test_getStudyData.R @@ -1,47 +1,45 @@ -context("Test if the output of getStudyData matches the expected output.") - creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) complete_study_output <- castor_api$getStudyData(creds$output_study) test_that("getStudyData returns the expected study data.", { actual <- complete_study_output$Study - expected <- read.csv("tests/test_files/files_output/CastorStudy.csv", check.names=F) + expected <- read.csv("../test_files/files_output/CastorStudy.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the repeated measure Unscheduled Visit", { actual <- complete_study_output$`Repeating data`$`Unscheduled visit` - expected <- read.csv("tests/test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + expected <- read.csv("../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the repeated measure Medication", { actual <- complete_study_output$`Repeating data`$Medication - expected <- read.csv("tests/test_files/files_output/CastorMedication.csv", check.names=F) + expected <- read.csv("../test_files/files_output/CastorMedication.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the repeated measure Comorbidities", { actual <- complete_study_output$`Repeating data`$Comorbidities - expected <- read.csv("tests/test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + expected <- read.csv("../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the repeated measure Adverse Events", { actual <- complete_study_output$`Repeating data`$`Adverse event` - expected <- read.csv("tests/test_files/files_output/CastorAdverseEvent.csv", check.names=F) + expected <- read.csv("../test_files/files_output/CastorAdverseEvent.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the survey Quality of Life", { actual <- complete_study_output$Surveys$`QOL Survey` - expected <- read.csv("tests/test_files/files_output/CastorQOLSurvey.csv", check.names=F) + expected <- read.csv("../test_files/files_output/CastorQOLSurvey.csv", check.names=F) expect_identical(actual, expected) }) From a4b2bead94cbe63f4536ac37b16c7ed3bd2d15b8 Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 23 Jun 2024 13:04:32 +0200 Subject: [PATCH 03/30] Changed testing folder structure; added tests for parametrised getStudyData --- .../files_output/CastorAdverseEvent.csv | 2 +- .../files_output/CastorComorbidities.csv | 2 +- .../files_output/CastorMedication.csv | 2 +- .../files_output/CastorQOLSurvey.csv | 2 +- tests/test_files/files_output/CastorStudy.csv | 2 +- .../files_output/CastorUnscheduledVisit.csv | 2 +- tests/testthat/test_fields.R | 1 + tests/testthat/test_forms.R | 1 + .../test_getStudyData.R | 20 +-- .../test_getStudyData_Parts.R | 124 ++++++++++++++++++ .../test_getStudyData_noBulk.R | 39 ++++++ tests/testthat/test_instantiation.R | 1 + tests/testthat/test_participants.R | 1 + tests/testthat/test_repeating_data.R | 1 + tests/testthat/test_studies.R | 1 + tests/testthat/test_studydatapoints.R | 1 + tests/testthat/test_surveys.R | 1 + tests/testthat/test_visits.R | 1 + 18 files changed, 185 insertions(+), 19 deletions(-) rename tests/testthat/{ => test_getStudyData}/test_getStudyData.R (68%) create mode 100644 tests/testthat/test_getStudyData/test_getStudyData_Parts.R create mode 100644 tests/testthat/test_getStudyData/test_getStudyData_noBulk.R diff --git a/tests/test_files/files_output/CastorAdverseEvent.csv b/tests/test_files/files_output/CastorAdverseEvent.csv index 8ba49b6..6674029 100644 --- a/tests/test_files/files_output/CastorAdverseEvent.csv +++ b/tests/test_files/files_output/CastorAdverseEvent.csv @@ -1,3 +1,3 @@ -record_id,institute,created_on,custom_name,parent,archived,AE_type,AE_startdate,AE_stopdate,AE_severity,AE_relation,AE_action,AE_outcome,AE_expected,AE_SAE +participant_id,institute,created_on,custom_name,parent,archived,AE_type,AE_startdate,AE_stopdate,AE_severity,AE_relation,AE_action,AE_outcome,AE_expected,AE_SAE 110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:58:13,Adverse event - 22-06-2021 09:58:12,No parent,FALSE,,,,,,,,, 110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:55:13,Adverse event - 22-06-2021 09:55:12,Follow-up Visit,FALSE,A bug in the API,23-06-2021,24-06-2021,Life-threatening,Definitely related,Medical intervention,Resolved,Yes,Yes diff --git a/tests/test_files/files_output/CastorComorbidities.csv b/tests/test_files/files_output/CastorComorbidities.csv index db92eed..1962c39 100644 --- a/tests/test_files/files_output/CastorComorbidities.csv +++ b/tests/test_files/files_output/CastorComorbidities.csv @@ -1,2 +1,2 @@ -record_id,institute,created_on,custom_name,parent,archived,comorbidities +participant_id,institute,created_on,custom_name,parent,archived,comorbidities 110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:56:07,Comorbidities - 22-06-2021 09:56:06,No parent,FALSE,-- Uploaded File: comorbidities.txt -- diff --git a/tests/test_files/files_output/CastorMedication.csv b/tests/test_files/files_output/CastorMedication.csv index f775820..34e5075 100644 --- a/tests/test_files/files_output/CastorMedication.csv +++ b/tests/test_files/files_output/CastorMedication.csv @@ -1,3 +1,3 @@ -record_id,institute,created_on,custom_name,parent,archived,med_name,med_start,med_stop,med_dose,med_units,med_other_unit +participant_id,institute,created_on,custom_name,parent,archived,med_name,med_start,med_stop,med_dose,med_units,med_other_unit 110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:54:28,Medication - 22-06-2021 09:54:28,Unscheduled visits and Medication,FALSE,Diclofenac,25-06-2021,28-06-2021,150,mg/day, 110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:58:27,Medication - 22-06-2021 09:58:26,No parent,FALSE,,,,,, diff --git a/tests/test_files/files_output/CastorQOLSurvey.csv b/tests/test_files/files_output/CastorQOLSurvey.csv index baac99c..2685903 100644 --- a/tests/test_files/files_output/CastorQOLSurvey.csv +++ b/tests/test_files/files_output/CastorQOLSurvey.csv @@ -1,3 +1,3 @@ -record_id,institute,survey_name,survey_instance_id,created_on,sent_on,progress,completed_on,package_id,package_name,archived,SF12_1,SF12_2,SF12_3,SF12_12,VAS +participant_id,institute,survey_name,survey_instance_id,created_on,sent_on,progress,completed_on,package_id,package_name,archived,SF12_1,SF12_2,SF12_3,SF12_12,VAS 110001,Franciscus Gasthuis & Vlietland,QOL Survey,4FF130AD-274C-4C8F-A4A0-A7816A5A88E9,22-06-2021 10:12:19,22-06-2021 10:12:19,100,22-06-2021 10:12:44,0761A9BA-9802-483D-8EB3-D07233A56F2B,My first survey package,FALSE,Excellent,Mostly,Mostly,Rarely,85 110001,Franciscus Gasthuis & Vlietland,QOL Survey,B99F0082-1B20-456B-8D4D-47D16867B211,22-06-2021 10:13:01,22-06-2021 10:13:01,0,,5B3FE56F-89FA-4C6D-A638-E8FA46113EB0,My first survey package,FALSE,,,,, diff --git a/tests/test_files/files_output/CastorStudy.csv b/tests/test_files/files_output/CastorStudy.csv index 7e3e8b4..808f809 100644 --- a/tests/test_files/files_output/CastorStudy.csv +++ b/tests/test_files/files_output/CastorStudy.csv @@ -1,3 +1,3 @@ -record_id,archived,institute,randomisation_group,randomisation_datetime,ic_date,ic_versions#Main study CF,ic_versions#Other,ic_main_version,ic_other_version,ic_language#English,ic_language#Dutch,ic_language#French,ic_language#Other,inc_ic,inc_trials,inc_age,inc_dx,inc_criteria,pat_birth_year,pat_sex,pat_race,pat_height,his_cvd,his_diabetes,his_smoke,his_smoke_start,his_smoke_dose,his_family#None,his_family#Unknown,his_family#Deafness,his_family#(Cardio)myopathy,his_family#Encephalopathy,his_family#Diabetes Mellitus,his_family#Hypertension/Cardiovascular disease,his_family#Thrombosis,his_family#Malignancy,randalloc,randdisp,disp_datetime,base_date,onset_stroke,onset_trombectomy,base_weight,base_bmi,base_sbp,base_dbp,base_hr,base_bl_date,base_hb,base_ht,base_leucoc,base_tromboc,base_urea,base_creat,base_CRP,fac_V_leiden_number,fac_V_leiden_date,base_gluc,fu_date,fu_weight,fu_bmi,fu_sbp,fu_dbp,fu_hr,fu_bl_date,fu_hb,fu_ht,fu_leucoc,fu_tromboc,fu_urea,fu_creat,fu_CRP,fu_gluc,unscheduled,conc_med +participant_id,archived,institute,randomisation_group,randomisation_datetime,ic_date,ic_versions#Main study CF,ic_versions#Other,ic_main_version,ic_other_version,ic_language#English,ic_language#Dutch,ic_language#French,ic_language#Other,inc_ic,inc_trials,inc_age,inc_dx,inc_criteria,pat_birth_year,pat_sex,pat_race,pat_height,his_cvd,his_diabetes,his_smoke,his_smoke_start,his_smoke_dose,his_family#None,his_family#Unknown,his_family#Deafness,his_family#(Cardio)myopathy,his_family#Encephalopathy,his_family#Diabetes Mellitus,his_family#Hypertension/Cardiovascular disease,his_family#Thrombosis,his_family#Malignancy,randalloc,randdisp,disp_datetime,base_date,onset_stroke,onset_trombectomy,base_weight,base_bmi,base_sbp,base_dbp,base_hr,base_bl_date,base_hb,base_ht,base_leucoc,base_tromboc,base_urea,base_creat,base_CRP,fac_V_leiden_number,fac_V_leiden_date,base_gluc,fu_date,fu_weight,fu_bmi,fu_sbp,fu_dbp,fu_hr,fu_bl_date,fu_hb,fu_ht,fu_leucoc,fu_tromboc,fu_urea,fu_creat,fu_CRP,fu_gluc,unscheduled,conc_med 110001,FALSE,Franciscus Gasthuis & Vlietland,,,22-06-2021,TRUE,FALSE,2.5,,FALSE,FALSE,TRUE,FALSE,Yes,No,Yes,No,Yes,1996,Female,African/black,1.75,Yes,No,No,,,FALSE,FALSE,FALSE,TRUE,FALSE,FALSE,FALSE,FALSE,FALSE,,,,22-06-2021,21-06-2021 01:15,01:30,88,28.7,120,65,66,22-06-2021,7.4,0.23,4.1,252,4.6,88,2.1,55,22-06-2021,5.4,29-06-2021,66,21.6,132,72,69,29-06-2021,8.1,0.36,9.5,366,8.2,99,4.4,3.1,Yes,Yes 110002,FALSE,Franciscus Gasthuis & Vlietland,,,,FALSE,FALSE,,,-99,-99,-99,-99,,,,,,,,,-95,,,,,,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,,,,31-12-2999,,,,,,,,,,,,,,,,-96,01-01-2996,,,,,,,,,,,,,,,,,, diff --git a/tests/test_files/files_output/CastorUnscheduledVisit.csv b/tests/test_files/files_output/CastorUnscheduledVisit.csv index 5f0690e..0ad24e3 100644 --- a/tests/test_files/files_output/CastorUnscheduledVisit.csv +++ b/tests/test_files/files_output/CastorUnscheduledVisit.csv @@ -1,2 +1,2 @@ -record_id,institute,created_on,custom_name,parent,archived,unsvis_type,unsvis_date,unsvis_weight,unsvis_sbp,unsvis_dbp,unsvis_hr,unslab_date,unslab_s_hb,unslab_ht,unslab_leucoc,unslab_tromboc,unslab_urea,unslab_creat,unslab_CRP,unslab_gluc +participant_id,institute,created_on,custom_name,parent,archived,unsvis_type,unsvis_date,unsvis_weight,unsvis_sbp,unsvis_dbp,unsvis_hr,unslab_date,unslab_s_hb,unslab_ht,unslab_leucoc,unslab_tromboc,unslab_urea,unslab_creat,unslab_CRP,unslab_gluc 110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:57:38,Unscheduled visit - 22-06-2021 09:57:36,Unscheduled visits and Medication,FALSE,admission to day-care,24-06-2021,98.3,233,156,112,,,,,,,,, diff --git a/tests/testthat/test_fields.R b/tests/testthat/test_fields.R index 2680273..db0167a 100644 --- a/tests/testthat/test_fields.R +++ b/tests/testthat/test_fields.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test Field related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_forms.R b/tests/testthat/test_forms.R index 0d50dc3..99a5237 100644 --- a/tests/testthat/test_forms.R +++ b/tests/testthat/test_forms.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test Form related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_getStudyData.R b/tests/testthat/test_getStudyData/test_getStudyData.R similarity index 68% rename from tests/testthat/test_getStudyData.R rename to tests/testthat/test_getStudyData/test_getStudyData.R index 593c52e..3bc3db9 100644 --- a/tests/testthat/test_getStudyData.R +++ b/tests/testthat/test_getStudyData/test_getStudyData.R @@ -1,45 +1,39 @@ -creds <- readRDS("testing_credentials.Rds") +creds <- readRDS("../testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) complete_study_output <- castor_api$getStudyData(creds$output_study) test_that("getStudyData returns the expected study data.", { actual <- complete_study_output$Study - expected <- read.csv("../test_files/files_output/CastorStudy.csv", check.names=F) - + expected <- read.csv("../../test_files/files_output/CastorStudy.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the repeated measure Unscheduled Visit", { actual <- complete_study_output$`Repeating data`$`Unscheduled visit` - expected <- read.csv("../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) - + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the repeated measure Medication", { actual <- complete_study_output$`Repeating data`$Medication - expected <- read.csv("../test_files/files_output/CastorMedication.csv", check.names=F) - + expected <- read.csv("../../test_files/files_output/CastorMedication.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the repeated measure Comorbidities", { actual <- complete_study_output$`Repeating data`$Comorbidities - expected <- read.csv("../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) - + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the repeated measure Adverse Events", { actual <- complete_study_output$`Repeating data`$`Adverse event` - expected <- read.csv("../test_files/files_output/CastorAdverseEvent.csv", check.names=F) - + expected <- read.csv("../../test_files/files_output/CastorAdverseEvent.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the survey Quality of Life", { actual <- complete_study_output$Surveys$`QOL Survey` - expected <- read.csv("../test_files/files_output/CastorQOLSurvey.csv", check.names=F) - + expected <- read.csv("../../test_files/files_output/CastorQOLSurvey.csv", check.names=F) expect_identical(actual, expected) }) diff --git a/tests/testthat/test_getStudyData/test_getStudyData_Parts.R b/tests/testthat/test_getStudyData/test_getStudyData_Parts.R new file mode 100644 index 0000000..e683da3 --- /dev/null +++ b/tests/testthat/test_getStudyData/test_getStudyData_Parts.R @@ -0,0 +1,124 @@ +creds <- readRDS("../testing_credentials.Rds") +castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) +study_output_nostudy <- castor_api$getStudyData(creds$output_study, load_study_data=FALSE) +study_output_nosurveys <- castor_api$getStudyData(creds$output_study, survey_instances=FALSE) +study_output_norepeatingdata <- castor_api$getStudyData(creds$output_study, repeating_data_instances=FALSE) + +test_that("getStudyData returns the expected study data when omitting study data.", { + actual <- study_output_nostudy$Study + # if study data is omitted, we should only retrieve participant metadata + expected <- read.csv("../../test_files/files_output/CastorStudy.csv", check.names=F)[c("participant_id", "archived", "institute", "randomisation_group", "randomisation_datetime")] + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected study data when omitting surveys.", { + actual <- study_output_nosurveys$Study + expected <- read.csv("../../test_files/files_output/CastorStudy.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected study data when omitting repeating data.", { + actual <- study_output_norepeatingdata$Study + expected <- read.csv("../../test_files/files_output/CastorStudy.csv", check.names=F) + expect_identical(actual, expected) +}) + + + +test_that("getStudyData returns the expected data for the repeated measure Unscheduled Visit when omitting study data", { + actual <- study_output_nostudy$`Repeating data`$`Unscheduled visit` + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Unscheduled Visit when omitting surveys", { + actual <- study_output_nosurveys$`Repeating data`$`Unscheduled visit` + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Unscheduled Visit when omitting repeated data", { + actual <- study_output_norepeatingdata$`Repeating data`$`Unscheduled visit` + expected <- NULL + expect_identical(actual, expected) +}) + + + +test_that("getStudyData returns the expected data for the repeated measure Medication when omitting study data", { + actual <- study_output_nostudy$`Repeating data`$Medication + expected <- read.csv("../../test_files/files_output/CastorMedication.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Medication when omitting surveys", { + actual <- study_output_nosurveys$`Repeating data`$Medication + expected <- read.csv("../../test_files/files_output/CastorMedication.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Medication when omitting repeated data", { + actual <- study_output_norepeatingdata$`Repeating data`$Medication + expected <- NULL + expect_identical(actual, expected) +}) + + + +test_that("getStudyData returns the expected data for the repeated measure Comorbidities when omitting study data", { + actual <- study_output_nostudy$`Repeating data`$Comorbidities + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Comorbidities when omitting surveys", { + actual <- study_output_nosurveys$`Repeating data`$Comorbidities + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Comorbidities when omitting repeated data", { + actual <- study_output_norepeatingdata$`Repeating data`$Comorbidities + expected <- NULL + expect_identical(actual, expected) +}) + + + +test_that("getStudyData returns the expected data for the repeated measure Adverse Events when omitting study data", { + actual <- study_output_nostudy$`Repeating data`$`Adverse event` + expected <- read.csv("../../test_files/files_output/CastorAdverseEvent.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Adverse Events when omitting surveys", { + actual <- study_output_nosurveys$`Repeating data`$`Adverse event` + expected <- read.csv("../../test_files/files_output/CastorAdverseEvent.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Adverse Events when omitting repeated data", { + actual <- study_output_norepeatingdata$`Repeating data`$`Adverse event` + expected <- NULL + expect_identical(actual, expected) +}) + + + +test_that("getStudyData returns the expected data for the survey Quality of Life when omitting study data", { + actual <- study_output_nostudy$Surveys$`QOL Survey` + expected <- read.csv("../../test_files/files_output/CastorQOLSurvey.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the survey Quality of Life when omitting surveys", { + actual <- study_output_nosurveys$Surveys$`QOL Survey` + expected <- NULL + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the survey Quality of Life when omitting repeated data", { + actual <- study_output_norepeatingdata$Surveys$`QOL Survey` + expected <- read.csv("../../test_files/files_output/CastorQOLSurvey.csv", check.names=F) + expect_identical(actual, expected) +}) diff --git a/tests/testthat/test_getStudyData/test_getStudyData_noBulk.R b/tests/testthat/test_getStudyData/test_getStudyData_noBulk.R new file mode 100644 index 0000000..4551902 --- /dev/null +++ b/tests/testthat/test_getStudyData/test_getStudyData_noBulk.R @@ -0,0 +1,39 @@ +creds <- readRDS("../testing_credentials.Rds") +castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) +complete_study_output <- castor_api$getStudyData(creds$output_study, bulk=FALSE) + +test_that("getStudyData returns the expected study data when not using bulk.", { + actual <- complete_study_output$Study + expected <- read.csv("../../test_files/files_output/CastorStudy.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Unscheduled Visit when not using bulk", { + actual <- complete_study_output$`Repeating data`$`Unscheduled visit` + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Medication when not using bulk", { + actual <- complete_study_output$`Repeating data`$Medication + expected <- read.csv("../../test_files/files_output/CastorMedication.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Comorbidities when not using bulk", { + actual <- complete_study_output$`Repeating data`$Comorbidities + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Adverse Events when not using bulk", { + actual <- complete_study_output$`Repeating data`$`Adverse event` + expected <- read.csv("../../test_files/files_output/CastorAdverseEvent.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the survey Quality of Life when not using bulk", { + actual <- complete_study_output$Surveys$`QOL Survey` + expected <- read.csv("../../test_files/files_output/CastorQOLSurvey.csv", check.names=F) + expect_identical(actual, expected) +}) diff --git a/tests/testthat/test_instantiation.R b/tests/testthat/test_instantiation.R index a154f49..d00e909 100644 --- a/tests/testthat/test_instantiation.R +++ b/tests/testthat/test_instantiation.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test instantiation and authentication.") test_that("Castor object instantiation produces valid R6 class object.", { diff --git a/tests/testthat/test_participants.R b/tests/testthat/test_participants.R index 134a935..01e6529 100644 --- a/tests/testthat/test_participants.R +++ b/tests/testthat/test_participants.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test Participant related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_repeating_data.R b/tests/testthat/test_repeating_data.R index 2950880..ed7652b 100644 --- a/tests/testthat/test_repeating_data.R +++ b/tests/testthat/test_repeating_data.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test RepeatingData related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_studies.R b/tests/testthat/test_studies.R index 83fe633..b97246d 100644 --- a/tests/testthat/test_studies.R +++ b/tests/testthat/test_studies.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test Study related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_studydatapoints.R b/tests/testthat/test_studydatapoints.R index ef6bd2b..ae4c80c 100644 --- a/tests/testthat/test_studydatapoints.R +++ b/tests/testthat/test_studydatapoints.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test StudyDataPoint related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_surveys.R b/tests/testthat/test_surveys.R index 97c2fdc..e2eb83b 100644 --- a/tests/testthat/test_surveys.R +++ b/tests/testthat/test_surveys.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test Survey related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_visits.R b/tests/testthat/test_visits.R index 0aaec24..c4155d3 100644 --- a/tests/testthat/test_visits.R +++ b/tests/testthat/test_visits.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test Visit related methods.") creds <- readRDS("testing_credentials.Rds") From c375fb3b33cfa6af4692b7118187e5ea02e9adea Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 23 Jun 2024 13:14:01 +0200 Subject: [PATCH 04/30] Added getStudyData tests for not translating checkbox values --- .../CastorAdverseEvent (noLabel).csv | 3 ++ .../CastorComorbidities (noLabel).csv | 2 + .../CastorMedication (noLabel).csv | 3 ++ .../CastorQOLSurvey (noLabel).csv | 3 ++ .../files_output/CastorStudy (noLabel).csv | 3 ++ .../CastorUnscheduledVisit (noLabel).csv | 2 + .../test_getStudyData_noLabels.R | 39 +++++++++++++++++++ 7 files changed, 55 insertions(+) create mode 100644 tests/test_files/files_output/CastorAdverseEvent (noLabel).csv create mode 100644 tests/test_files/files_output/CastorComorbidities (noLabel).csv create mode 100644 tests/test_files/files_output/CastorMedication (noLabel).csv create mode 100644 tests/test_files/files_output/CastorQOLSurvey (noLabel).csv create mode 100644 tests/test_files/files_output/CastorStudy (noLabel).csv create mode 100644 tests/test_files/files_output/CastorUnscheduledVisit (noLabel).csv create mode 100644 tests/testthat/test_getStudyData/test_getStudyData_noLabels.R diff --git a/tests/test_files/files_output/CastorAdverseEvent (noLabel).csv b/tests/test_files/files_output/CastorAdverseEvent (noLabel).csv new file mode 100644 index 0000000..053c14d --- /dev/null +++ b/tests/test_files/files_output/CastorAdverseEvent (noLabel).csv @@ -0,0 +1,3 @@ +participant_id,institute,created_on,custom_name,parent,archived,AE_type,AE_startdate,AE_stopdate,AE_severity,AE_relation,AE_action,AE_outcome,AE_expected,AE_SAE +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:58:13,Adverse event - 22-06-2021 09:58:12,No parent,FALSE,,,,,,,,, +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:55:13,Adverse event - 22-06-2021 09:55:12,Follow-up Visit,FALSE,A bug in the API,23-06-2021,24-06-2021,4,4,2,1,1,1 diff --git a/tests/test_files/files_output/CastorComorbidities (noLabel).csv b/tests/test_files/files_output/CastorComorbidities (noLabel).csv new file mode 100644 index 0000000..1962c39 --- /dev/null +++ b/tests/test_files/files_output/CastorComorbidities (noLabel).csv @@ -0,0 +1,2 @@ +participant_id,institute,created_on,custom_name,parent,archived,comorbidities +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:56:07,Comorbidities - 22-06-2021 09:56:06,No parent,FALSE,-- Uploaded File: comorbidities.txt -- diff --git a/tests/test_files/files_output/CastorMedication (noLabel).csv b/tests/test_files/files_output/CastorMedication (noLabel).csv new file mode 100644 index 0000000..46c64e3 --- /dev/null +++ b/tests/test_files/files_output/CastorMedication (noLabel).csv @@ -0,0 +1,3 @@ +participant_id,institute,created_on,custom_name,parent,archived,med_name,med_start,med_stop,med_dose,med_units,med_other_unit +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:54:28,Medication - 22-06-2021 09:54:28,Unscheduled visits and Medication,FALSE,Diclofenac,25-06-2021,28-06-2021,150,2, +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:58:27,Medication - 22-06-2021 09:58:26,No parent,FALSE,,,,,, diff --git a/tests/test_files/files_output/CastorQOLSurvey (noLabel).csv b/tests/test_files/files_output/CastorQOLSurvey (noLabel).csv new file mode 100644 index 0000000..c157315 --- /dev/null +++ b/tests/test_files/files_output/CastorQOLSurvey (noLabel).csv @@ -0,0 +1,3 @@ +participant_id,institute,survey_name,survey_instance_id,created_on,sent_on,progress,completed_on,package_id,package_name,archived,SF12_1,SF12_2,SF12_3,SF12_12,VAS +110001,Franciscus Gasthuis & Vlietland,QOL Survey,4FF130AD-274C-4C8F-A4A0-A7816A5A88E9,22-06-2021 10:12:19,22-06-2021 10:12:19,100,22-06-2021 10:12:44,0761A9BA-9802-483D-8EB3-D07233A56F2B,My first survey package,FALSE,5,2,2,5,85 +110001,Franciscus Gasthuis & Vlietland,QOL Survey,B99F0082-1B20-456B-8D4D-47D16867B211,22-06-2021 10:13:01,22-06-2021 10:13:01,0,,5B3FE56F-89FA-4C6D-A638-E8FA46113EB0,My first survey package,FALSE,,,,, diff --git a/tests/test_files/files_output/CastorStudy (noLabel).csv b/tests/test_files/files_output/CastorStudy (noLabel).csv new file mode 100644 index 0000000..370aa86 --- /dev/null +++ b/tests/test_files/files_output/CastorStudy (noLabel).csv @@ -0,0 +1,3 @@ +participant_id,archived,institute,randomisation_group,randomisation_datetime,ic_date,ic_versions#Main study CF,ic_versions#Other,ic_main_version,ic_other_version,ic_language#English,ic_language#Dutch,ic_language#French,ic_language#Other,inc_ic,inc_trials,inc_age,inc_dx,inc_criteria,pat_birth_year,pat_sex,pat_race,pat_height,his_cvd,his_diabetes,his_smoke,his_smoke_start,his_smoke_dose,his_family#None,his_family#Unknown,his_family#Deafness,his_family#(Cardio)myopathy,his_family#Encephalopathy,his_family#Diabetes Mellitus,his_family#Hypertension/Cardiovascular disease,his_family#Thrombosis,his_family#Malignancy,randalloc,randdisp,disp_datetime,base_date,onset_stroke,onset_trombectomy,base_weight,base_bmi,base_sbp,base_dbp,base_hr,base_bl_date,base_hb,base_ht,base_leucoc,base_tromboc,base_urea,base_creat,base_CRP,fac_V_leiden_number,fac_V_leiden_date,base_gluc,fu_date,fu_weight,fu_bmi,fu_sbp,fu_dbp,fu_hr,fu_bl_date,fu_hb,fu_ht,fu_leucoc,fu_tromboc,fu_urea,fu_creat,fu_CRP,fu_gluc,unscheduled,conc_med +110001,FALSE,Franciscus Gasthuis & Vlietland,,,22-06-2021,TRUE,FALSE,2.5,,FALSE,FALSE,TRUE,FALSE,1,0,1,0,Yes,1996,0,2,1.75,1,0,0,,,FALSE,FALSE,FALSE,TRUE,FALSE,FALSE,FALSE,FALSE,FALSE,,,,22-06-2021,21-06-2021 01:15,01:30,88,28.7,120,65,66,22-06-2021,7.4,0.23,4.1,252,4.6,88,2.1,55,22-06-2021,5.4,29-06-2021,66,21.6,132,72,69,29-06-2021,8.1,0.36,9.5,366,8.2,99,4.4,3.1,1,1 +110002,FALSE,Franciscus Gasthuis & Vlietland,,,,FALSE,FALSE,,,-99,-99,-99,-99,,,,,,,,,-95,,,,,,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,,,,31-12-2999,,,,,,,,,,,,,,,,-96,01-01-2996,,,,,,,,,,,,,,,,,, diff --git a/tests/test_files/files_output/CastorUnscheduledVisit (noLabel).csv b/tests/test_files/files_output/CastorUnscheduledVisit (noLabel).csv new file mode 100644 index 0000000..9e8ca98 --- /dev/null +++ b/tests/test_files/files_output/CastorUnscheduledVisit (noLabel).csv @@ -0,0 +1,2 @@ +participant_id,institute,created_on,custom_name,parent,archived,unsvis_type,unsvis_date,unsvis_weight,unsvis_sbp,unsvis_dbp,unsvis_hr,unslab_date,unslab_s_hb,unslab_ht,unslab_leucoc,unslab_tromboc,unslab_urea,unslab_creat,unslab_CRP,unslab_gluc +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:57:38,Unscheduled visit - 22-06-2021 09:57:36,Unscheduled visits and Medication,FALSE,3,24-06-2021,98.3,233,156,112,,,,,,,,, diff --git a/tests/testthat/test_getStudyData/test_getStudyData_noLabels.R b/tests/testthat/test_getStudyData/test_getStudyData_noLabels.R new file mode 100644 index 0000000..6f06126 --- /dev/null +++ b/tests/testthat/test_getStudyData/test_getStudyData_noLabels.R @@ -0,0 +1,39 @@ +creds <- readRDS("../testing_credentials.Rds") +castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) +complete_study_output <- castor_api$getStudyData(creds$output_study, translate_option_values = FALSE) + +test_that("getStudyData returns the expected study data when not translating checkboxes.", { + actual <- complete_study_output$Study + expected <- read.csv("../../test_files/files_output/CastorStudy (noLabel).csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Unscheduled Visit when not translating checkboxes", { + actual <- complete_study_output$`Repeating data`$`Unscheduled visit` + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit (noLabel).csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Medication when not translating checkboxes", { + actual <- complete_study_output$`Repeating data`$Medication + expected <- read.csv("../../test_files/files_output/CastorMedication (noLabel).csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Comorbidities when not translating checkboxes", { + actual <- complete_study_output$`Repeating data`$Comorbidities + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit (noLabel).csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Adverse Events when not translating checkboxes", { + actual <- complete_study_output$`Repeating data`$`Adverse event` + expected <- read.csv("../../test_files/files_output/CastorAdverseEvent (noLabel).csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the survey Quality of Life when not translating checkboxes", { + actual <- complete_study_output$Surveys$`QOL Survey` + expected <- read.csv("../../test_files/files_output/CastorQOLSurvey (noLabel).csv", check.names=F) + expect_identical(actual, expected) +}) From a3c1adf8d3c0feba0fb46d9573e92e6999167953 Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 23 Jun 2024 13:37:39 +0200 Subject: [PATCH 05/30] Added tests for getParticipants --- tests/testthat/test_fields.R | 2 +- tests/testthat/test_forms.R | 2 +- tests/testthat/test_instantiation.R | 2 +- tests/testthat/test_participants.R | 60 +++++++++++++++++++-------- tests/testthat/test_repeating_data.R | 2 +- tests/testthat/test_studies.R | 2 +- tests/testthat/test_studydatapoints.R | 2 +- tests/testthat/test_surveys.R | 2 +- tests/testthat/test_visits.R | 2 +- 9 files changed, 50 insertions(+), 26 deletions(-) diff --git a/tests/testthat/test_fields.R b/tests/testthat/test_fields.R index db0167a..8800271 100644 --- a/tests/testthat/test_fields.R +++ b/tests/testthat/test_fields.R @@ -1,4 +1,4 @@ -# Needs updates to tests +# TODO: Needs updates to tests context("Test Field related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_forms.R b/tests/testthat/test_forms.R index 99a5237..47ffe4f 100644 --- a/tests/testthat/test_forms.R +++ b/tests/testthat/test_forms.R @@ -1,4 +1,4 @@ -# Needs updates to tests +# TODO: Needs updates to tests context("Test Form related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_instantiation.R b/tests/testthat/test_instantiation.R index d00e909..bb3da20 100644 --- a/tests/testthat/test_instantiation.R +++ b/tests/testthat/test_instantiation.R @@ -1,4 +1,4 @@ -# Needs updates to tests +# TODO: Needs updates to tests context("Test instantiation and authentication.") test_that("Castor object instantiation produces valid R6 class object.", { diff --git a/tests/testthat/test_participants.R b/tests/testthat/test_participants.R index 01e6529..fcb10d3 100644 --- a/tests/testthat/test_participants.R +++ b/tests/testthat/test_participants.R @@ -1,34 +1,58 @@ -# Needs updates to tests -context("Test Participant related methods.") - creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) test_that("getParticipant returns an appropriate object.", { - participant <- castor_api$getParticipant(creds$example_study, creds$example_participant) + participant <- castor_api$getParticipant(creds$output_study, creds$example_participant) - expect_is(participant, "list") + expect_type(participant, "list") expect_equal(participant$participant_id, creds$example_participant) expect_gt(length(participant), 0) }) +test_that("getParticipant fails appropriately", { + error <- expect_error(castor_api$getParticipant(creds$output_study, creds$fail_participant)) + expect_match(error$message, "Error code: 404") +}) + test_that("getParticipantsPages returns an appropriate object.", { - participants <- castor_api$getParticipantsPages(creds$example_study) - one_participant <- castor_api$getParticipantsPages(creds$example_study, page = 1) + participants <- castor_api$getParticipantsPages(creds$output_study) + expect_type(participants, "list") +}) - expect_is(participants, "list") - expect_is(one_participant, "list") - expect_true(length(one_participant) == 1) - expect_error(castor_api$getParticipantsPages(creds$example_study, page = -1)) - expect_error(castor_api$getParticipantsPages(creds$example_study, page = 100000000)) - expect_error(castor_api$getParticipantsPages(creds$example_study, page = pi)) +test_that("getParticipantsPages returns an appropriate object when retrieving a single page.", { + one_page <- castor_api$getParticipantsPages(creds$output_study, page = 1) + expect_type(one_page, "list") + expect_true(length(one_page) == 1) +}) + +test_that("getParticipantsPages fails appropriately", { + error1 <- expect_error(castor_api$getParticipantsPages(creds$example_study, page = -1)) + expect_match(error1$message, "Error code: 400") + + error2 <- expect_error(castor_api$getParticipantsPages(creds$example_study, page = 100000000)) + expect_match(error2$message, "Error code: 409") + + error3 <- expect_error(castor_api$getParticipantsPages(creds$example_study, page = pi)) + expect_match(error3$message, "page must be an integer") }) test_that("getParticipants returns an appropriate object.", { - participant_data <- castor_api$getParticipants(creds$example_study) + participant_data <- castor_api$getParticipants(creds$output_study) + + expect_s3_class(participant_data, "data.frame") + expect_equal(nrow(participant_data), 2) + expect_equal(ncol(participant_data), 34) +}) + +test_that("getParticipants returns an appropriate object when allowing archived.", { + participant_data <- castor_api$getParticipants(creds$output_study, filter_archived = FALSE) + + expect_s3_class(participant_data, "data.frame") + expect_equal(nrow(participant_data), 3) + expect_equal(ncol(participant_data), 34) +}) - expect_is(participant_data, "data.frame") - expect_gt(nrow(participant_data), 0) - expect_gt(ncol(participant_data), 0) - expect_error(castor_api$getParticipants("this is not a stuy id")) +test_that("getParticipants fails appropriately", { + error <- expect_error(castor_api$getParticipants("THISISNOTASTUDYID")) + expect_match(error$message, "Error code: 404") }) diff --git a/tests/testthat/test_repeating_data.R b/tests/testthat/test_repeating_data.R index ed7652b..68a8a39 100644 --- a/tests/testthat/test_repeating_data.R +++ b/tests/testthat/test_repeating_data.R @@ -1,4 +1,4 @@ -# Needs updates to tests +# TODO: Needs updates to tests context("Test RepeatingData related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_studies.R b/tests/testthat/test_studies.R index b97246d..758d54d 100644 --- a/tests/testthat/test_studies.R +++ b/tests/testthat/test_studies.R @@ -1,4 +1,4 @@ -# Needs updates to tests +# TODO: Needs updates to tests context("Test Study related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_studydatapoints.R b/tests/testthat/test_studydatapoints.R index ae4c80c..1d08fc5 100644 --- a/tests/testthat/test_studydatapoints.R +++ b/tests/testthat/test_studydatapoints.R @@ -1,4 +1,4 @@ -# Needs updates to tests +# TODO: Needs updates to tests context("Test StudyDataPoint related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_surveys.R b/tests/testthat/test_surveys.R index e2eb83b..03b71a5 100644 --- a/tests/testthat/test_surveys.R +++ b/tests/testthat/test_surveys.R @@ -1,4 +1,4 @@ -# Needs updates to tests +# TODO: Needs updates to tests context("Test Survey related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_visits.R b/tests/testthat/test_visits.R index c4155d3..5d2b0bc 100644 --- a/tests/testthat/test_visits.R +++ b/tests/testthat/test_visits.R @@ -1,4 +1,4 @@ -# Needs updates to tests +# TODO: Needs updates to tests context("Test Visit related methods.") creds <- readRDS("testing_credentials.Rds") From 51cf99683ea88cb20aa54d7d768b762133bd2c5d Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 30 Jun 2024 12:24:37 +0200 Subject: [PATCH 06/30] Added field tests --- tests/testthat/test_fields.R | 56 ++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/tests/testthat/test_fields.R b/tests/testthat/test_fields.R index 8800271..d78d09b 100644 --- a/tests/testthat/test_fields.R +++ b/tests/testthat/test_fields.R @@ -1,34 +1,54 @@ -# TODO: Needs updates to tests -context("Test Field related methods.") - creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) test_that("getField returns an appropriate object.", { - field <- castor_api$getField(creds$example_study, creds$example_field) + field <- castor_api$getField(creds$output_study, creds$example_field) expect_is(field, "list") expect_equal(field$field_id, creds$example_field) expect_gt(length(field), 0) }) +test_that("getField fails appropriately", { + error <- expect_error(castor_api$getField(creds$output_study, creds$fail_field)) + expect_match(error$message, "Error code: 404") +}) + + test_that("getFieldsPages returns an appropriate object.", { - fields <- castor_api$getFieldsPages(creds$example_study) - one_field <- castor_api$getFieldsPages(creds$example_study, page = 1) - - expect_is(fields, "list") - expect_is(one_field, "list") - expect_true(length(one_field) == 1) - expect_error(castor_api$getFieldsPages(creds$example_study, page = -1)) - expect_error(castor_api$getFieldsPages(creds$example_study, page = 100000000)) - expect_error(castor_api$getFieldsPages(creds$example_study, page = pi)) + fields <- castor_api$getFieldsPages(creds$output_study) + expect_type(fields, "list") }) +test_that("getFieldPages returns an appropriate object when retrieving a single page.", { + one_page <- castor_api$getFieldsPages(creds$output_study, page = 1) + expect_type(one_page, "list") + expect_true(length(one_page) == 1) +}) + +test_that("getFieldPages fails appropriately", { + error1 <- expect_error(castor_api$getFieldsPages(creds$output_study, page = -1)) + expect_match(error1$message, "Error code: 400") + + error2 <- expect_error(castor_api$getFieldsPages(creds$output_study, page = 100000000)) + expect_match(error2$message, "Error code: 409") + + error3 <- expect_error(castor_api$getFieldsPages(creds$output_study, page = pi)) + expect_match(error3$message, "page must be an integer") +}) + + + test_that("getFields returns an appropriate object.", { - field_data <- castor_api$getFields(creds$example_study) + field_data <- castor_api$getFields(creds$output_study) - expect_is(field_data, "data.frame") - expect_gt(nrow(field_data), 0) - expect_gt(ncol(field_data), 0) - expect_error(castor_api$getFields("this is not a study id")) + expect_s3_class(field_data, "data.frame") + expect_equal(nrow(field_data), 115) + expect_equal(ncol(field_data), 37) }) + +test_that("getFields fails appropriately", { + error <- expect_error(castor_api$getFields("THISISNOTASTUDYID")) + expect_match(error$message, "Error code: 404") +}) + From e1d17b1d654dc7b276a39f8eec04b7c12969c447 Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 30 Jun 2024 12:25:08 +0200 Subject: [PATCH 07/30] Switched to 1 study for all tests --- tests/testthat/test_participants.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test_participants.R b/tests/testthat/test_participants.R index fcb10d3..c2b5831 100644 --- a/tests/testthat/test_participants.R +++ b/tests/testthat/test_participants.R @@ -26,13 +26,13 @@ test_that("getParticipantsPages returns an appropriate object when retrieving a }) test_that("getParticipantsPages fails appropriately", { - error1 <- expect_error(castor_api$getParticipantsPages(creds$example_study, page = -1)) + error1 <- expect_error(castor_api$getParticipantsPages(creds$output_study, page = -1)) expect_match(error1$message, "Error code: 400") - error2 <- expect_error(castor_api$getParticipantsPages(creds$example_study, page = 100000000)) + error2 <- expect_error(castor_api$getParticipantsPages(creds$output_study, page = 100000000)) expect_match(error2$message, "Error code: 409") - error3 <- expect_error(castor_api$getParticipantsPages(creds$example_study, page = pi)) + error3 <- expect_error(castor_api$getParticipantsPages(creds$output_study, page = pi)) expect_match(error3$message, "page must be an integer") }) From 7df1e0a41283a87ef4e67cf264e4a6414ae7f438 Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 30 Jun 2024 12:25:50 +0200 Subject: [PATCH 08/30] Switched to 1 study for all tests --- create_testing_credentials.R | 4 ++-- tests/testthat/test_forms.R | 14 +++++++------- tests/testthat/test_repeating_data.R | 28 +++++++++++++-------------- tests/testthat/test_studies.R | 4 ++-- tests/testthat/test_studydatapoints.R | 14 +++++++------- tests/testthat/test_surveys.R | 14 +++++++------- tests/testthat/test_visits.R | 14 +++++++------- 7 files changed, 46 insertions(+), 46 deletions(-) diff --git a/create_testing_credentials.R b/create_testing_credentials.R index 22a96f7..4a8e961 100644 --- a/create_testing_credentials.R +++ b/create_testing_credentials.R @@ -1,7 +1,7 @@ creds = list( client_id = "", client_secret = "", - example_study = "", + output_study = "", base_url = "https://data.castoredc.com", example_field = "", example_visit = "", @@ -14,7 +14,7 @@ creds = list( ) castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) -entire_study <- castor_api$getStudyData(creds$example_study) +entire_study <- castor_api$getStudyData(creds$output_study) study_hash <- digest::digest(entire_study) creds$study_hash <- study_hash diff --git a/tests/testthat/test_forms.R b/tests/testthat/test_forms.R index 47ffe4f..95806c6 100644 --- a/tests/testthat/test_forms.R +++ b/tests/testthat/test_forms.R @@ -5,7 +5,7 @@ creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) test_that("getForm returns an appropriate object.", { - form <- castor_api$getForm(creds$example_study, creds$example_form) + form <- castor_api$getForm(creds$output_study, creds$example_form) expect_is(form, "list") expect_equal(form$form_id, creds$example_form) @@ -13,19 +13,19 @@ test_that("getForm returns an appropriate object.", { }) test_that("getFormsPages returns an appropriate object.", { - forms <- castor_api$getFormsPages(creds$example_study) - one_form <- castor_api$getFormsPages(creds$example_study, page = 1) + forms <- castor_api$getFormsPages(creds$output_study) + one_form <- castor_api$getFormsPages(creds$output_study, page = 1) expect_is(forms, "list") expect_is(one_form, "list") expect_true(length(one_form) == 1) - expect_error(castor_api$getFormsPages(creds$example_study, page = -1)) - expect_error(castor_api$getFormsPages(creds$example_study, page = 100000000)) - expect_error(castor_api$getFormsPages(creds$example_study, page = pi)) + expect_error(castor_api$getFormsPages(creds$output_study, page = -1)) + expect_error(castor_api$getFormsPages(creds$output_study, page = 100000000)) + expect_error(castor_api$getFormsPages(creds$output_study, page = pi)) }) test_that("getForms returns an appropriate object.", { - form_data <- castor_api$getForms(creds$example_study) + form_data <- castor_api$getForms(creds$output_study) expect_is(form_data, "data.frame") expect_gt(nrow(form_data), 0) diff --git a/tests/testthat/test_repeating_data.R b/tests/testthat/test_repeating_data.R index 68a8a39..02c4826 100644 --- a/tests/testthat/test_repeating_data.R +++ b/tests/testthat/test_repeating_data.R @@ -5,7 +5,7 @@ creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) test_that("getRepeatingData returns an appropriate object.", { - repeating_data <- castor_api$getRepeatingData(creds$example_study, creds$example_repeating_data) + repeating_data <- castor_api$getRepeatingData(creds$output_study, creds$example_repeating_data) expect_is(repeating_data, "list") expect_equal(repeating_data$repeating_data_id, creds$example_repeating_data) @@ -13,20 +13,20 @@ test_that("getRepeatingData returns an appropriate object.", { }) test_that("getRepeatingDatasPages returns an appropriate object.", { - repeating_datas <- castor_api$getRepeatingDatasPages(creds$example_study) - one_repeating_data <- castor_api$getRepeatingDatasPages(creds$example_study, page = 1) + repeating_datas <- castor_api$getRepeatingDatasPages(creds$output_study) + one_repeating_data <- castor_api$getRepeatingDatasPages(creds$output_study, page = 1) expect_is(repeating_datas, "list") expect_is(one_repeating_data, "list") expect_true(length(one_repeating_data) == 1) - expect_error(castor_api$getRepeatingDatasPages(creds$example_study, page = -1)) - expect_error(castor_api$getRepeatingDatasPages(creds$example_study, + expect_error(castor_api$getRepeatingDatasPages(creds$output_study, page = -1)) + expect_error(castor_api$getRepeatingDatasPages(creds$output_study, page = 100000000)) - expect_error(castor_api$getRepeatingDatasPages(creds$example_study, page = pi)) + expect_error(castor_api$getRepeatingDatasPages(creds$output_study, page = pi)) }) test_that("getRepeatingDatas returns an appropriate object.", { - repeating_data_data <- castor_api$getRepeatingDatas(creds$example_study) + repeating_data_data <- castor_api$getRepeatingDatas(creds$output_study) expect_is(repeating_data_data, "data.frame") expect_gt(nrow(repeating_data_data), 0) @@ -36,7 +36,7 @@ test_that("getRepeatingDatas returns an appropriate object.", { test_that("getRepeatingDataForm returns an appropriate object.", { - repeating_data_form <- castor_api$getRepeatingDataForm(creds$example_study, + repeating_data_form <- castor_api$getRepeatingDataForm(creds$output_study, creds$example_repeating_data, creds$example_repeating_data_form) @@ -46,28 +46,28 @@ test_that("getRepeatingDataForm returns an appropriate object.", { }) test_that("getRepeatingDataFormsPages returns an appropriate object.", { - repeating_data_forms <- castor_api$getRepeatingDataFormsPages(creds$example_study, + repeating_data_forms <- castor_api$getRepeatingDataFormsPages(creds$output_study, creds$example_repeating_data) - one_repeating_data_form <- castor_api$getRepeatingDataFormsPages(creds$example_study, + one_repeating_data_form <- castor_api$getRepeatingDataFormsPages(creds$output_study, creds$example_repeating_data, page = 1) expect_is(repeating_data_forms, "list") expect_is(one_repeating_data_form, "list") expect_true(length(one_repeating_data_form) == 1) - expect_error(castor_api$getRepeatingDataFormsPages(creds$example_study, + expect_error(castor_api$getRepeatingDataFormsPages(creds$output_study, creds$example_repeating_data, page = -1)) - expect_error(castor_api$getRepeatingDataFormsPages(creds$example_study, + expect_error(castor_api$getRepeatingDataFormsPages(creds$output_study, creds$example_repeating_data, page = 100000000)) - expect_error(castor_api$getRepeatingDataFormsPages(creds$example_study, + expect_error(castor_api$getRepeatingDataFormsPages(creds$output_study, creds$example_repeating_data, page = pi)) }) test_that("getRepeatingDataForms returns an appropriate object.", { - repeating_data_form_data <- castor_api$getRepeatingDataForms(creds$example_study) + repeating_data_form_data <- castor_api$getRepeatingDataForms(creds$output_study) expect_is(repeating_data_form_data, "data.frame") expect_gt(nrow(repeating_data_form_data), 0) diff --git a/tests/testthat/test_studies.R b/tests/testthat/test_studies.R index 758d54d..26ded13 100644 --- a/tests/testthat/test_studies.R +++ b/tests/testthat/test_studies.R @@ -5,10 +5,10 @@ creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) test_that("getStudy returns an appropriate object.", { - study <- castor_api$getStudy(creds$example_study) + study <- castor_api$getStudy(creds$output_study) expect_is(study, "list") - expect_equal(study$study_id, creds$example_study) + expect_equal(study$study_id, creds$output_study) expect_gt(length(study), 0) }) diff --git a/tests/testthat/test_studydatapoints.R b/tests/testthat/test_studydatapoints.R index 1d08fc5..f595434 100644 --- a/tests/testthat/test_studydatapoints.R +++ b/tests/testthat/test_studydatapoints.R @@ -5,7 +5,7 @@ creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) test_that("getStudyDataPoint returns an appropriate object.", { - sdp <- castor_api$getStudyDataPoint(creds$example_study, + sdp <- castor_api$getStudyDataPoint(creds$output_study, creds$example_participant, creds$example_field) @@ -16,28 +16,28 @@ test_that("getStudyDataPoint returns an appropriate object.", { }) test_that("getStudyDataPointsPages returns an appropriate object.", { - sdps <- castor_api$getStudyDataPointsPages(creds$example_study, + sdps <- castor_api$getStudyDataPointsPages(creds$output_study, creds$example_participant) - one_sdp <- castor_api$getStudyDataPointsPages(creds$example_study, + one_sdp <- castor_api$getStudyDataPointsPages(creds$output_study, creds$example_participant, page = 1) expect_is(sdps, "list") expect_is(one_sdp, "list") expect_true(length(one_sdp) == 1) - expect_error(castor_api$getStudyDataPointsPages(creds$example_study, + expect_error(castor_api$getStudyDataPointsPages(creds$output_study, creds$example_participant, page = -1)) - expect_error(castor_api$getStudyDataPointsPages(creds$example_study, + expect_error(castor_api$getStudyDataPointsPages(creds$output_study, creds$example_participant, page = 100000000)) - expect_error(castor_api$getStudyDataPointsPages(creds$example_study, + expect_error(castor_api$getStudyDataPointsPages(creds$output_study, creds$example_participant, page = pi)) }) test_that("getStudyDataPoints returns an appropriate object.", { - sdp_data <- castor_api$getStudyDataPoints(creds$example_study, + sdp_data <- castor_api$getStudyDataPoints(creds$output_study, creds$example_participant, filter_types = creds$filter_vals) diff --git a/tests/testthat/test_surveys.R b/tests/testthat/test_surveys.R index 03b71a5..83384b4 100644 --- a/tests/testthat/test_surveys.R +++ b/tests/testthat/test_surveys.R @@ -5,7 +5,7 @@ creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) test_that("getSurvey returns an appropriate object.", { - survey <- castor_api$getSurvey(creds$example_study, creds$example_survey) + survey <- castor_api$getSurvey(creds$output_study, creds$example_survey) expect_is(survey, "list") expect_equal(survey$survey_id, creds$example_survey) @@ -13,19 +13,19 @@ test_that("getSurvey returns an appropriate object.", { }) test_that("getSurveysPages returns an appropriate object.", { - surveys <- castor_api$getSurveysPages(creds$example_study) - one_survey <- castor_api$getSurveysPages(creds$example_study, page = 1) + surveys <- castor_api$getSurveysPages(creds$output_study) + one_survey <- castor_api$getSurveysPages(creds$output_study, page = 1) expect_is(surveys, "list") expect_is(one_survey, "list") expect_true(length(one_survey) == 1) - expect_error(castor_api$getSurveysPages(creds$example_study, page = -1)) - expect_error(castor_api$getSurveysPages(creds$example_study, page = 100000000)) - expect_error(castor_api$getSurveysPages(creds$example_study, page = pi)) + expect_error(castor_api$getSurveysPages(creds$output_study, page = -1)) + expect_error(castor_api$getSurveysPages(creds$output_study, page = 100000000)) + expect_error(castor_api$getSurveysPages(creds$output_study, page = pi)) }) test_that("getSurveys returns an appropriate object.", { - survey_data <- castor_api$getSurveys(creds$example_study) + survey_data <- castor_api$getSurveys(creds$output_study) expect_is(survey_data, "data.frame") expect_gt(nrow(survey_data), 0) diff --git a/tests/testthat/test_visits.R b/tests/testthat/test_visits.R index 5d2b0bc..f0f7959 100644 --- a/tests/testthat/test_visits.R +++ b/tests/testthat/test_visits.R @@ -5,7 +5,7 @@ creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) test_that("getVisit returns an appropriate object.", { - visit <- castor_api$getVisit(creds$example_study, creds$example_visit) + visit <- castor_api$getVisit(creds$output_study, creds$example_visit) expect_is(visit, "list") expect_equal(visit$visit_id, creds$example_visit) @@ -13,19 +13,19 @@ test_that("getVisit returns an appropriate object.", { }) test_that("getVisitsPages returns an appropriate object.", { - visits <- castor_api$getVisitsPages(creds$example_study) - one_visit <- castor_api$getVisitsPages(creds$example_study, page = 1) + visits <- castor_api$getVisitsPages(creds$output_study) + one_visit <- castor_api$getVisitsPages(creds$output_study, page = 1) expect_is(visits, "list") expect_is(one_visit, "list") expect_true(length(one_visit) == 1) - expect_error(castor_api$getVisitsPages(creds$example_study, page = -1)) - expect_error(castor_api$getVisitsPages(creds$example_study, page = 100000000)) - expect_error(castor_api$getVisitsPages(creds$example_study, page = pi)) + expect_error(castor_api$getVisitsPages(creds$output_study, page = -1)) + expect_error(castor_api$getVisitsPages(creds$output_study, page = 100000000)) + expect_error(castor_api$getVisitsPages(creds$output_study, page = pi)) }) test_that("getVisits returns an appropriate object.", { - visit_data <- castor_api$getVisits(creds$example_study) + visit_data <- castor_api$getVisits(creds$output_study) expect_is(visit_data, "data.frame") expect_gt(nrow(visit_data), 0) From 7568ce05ebcb92b6cdae931296848c567db56238 Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 30 Jun 2024 12:45:40 +0200 Subject: [PATCH 09/30] Added tests for getStudyDataBulk --- .../files_output/.~lock.CastorStudy.csv# | 1 + tests/testthat/test_getStudyDataBulk.R | 99 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 tests/test_files/files_output/.~lock.CastorStudy.csv# create mode 100644 tests/testthat/test_getStudyDataBulk.R diff --git a/tests/test_files/files_output/.~lock.CastorStudy.csv# b/tests/test_files/files_output/.~lock.CastorStudy.csv# new file mode 100644 index 0000000..38d080f --- /dev/null +++ b/tests/test_files/files_output/.~lock.CastorStudy.csv# @@ -0,0 +1 @@ +,reinier,Reinier-PC,30.06.2024 12:37,file:///home/reinier/.config/libreoffice/4; \ No newline at end of file diff --git a/tests/testthat/test_getStudyDataBulk.R b/tests/testthat/test_getStudyDataBulk.R new file mode 100644 index 0000000..f5e435c --- /dev/null +++ b/tests/testthat/test_getStudyDataBulk.R @@ -0,0 +1,99 @@ +creds <- readRDS("testing_credentials.Rds") +castor_api <- CastorData$new( + key = creds$client_id, + secret = creds$client_secret, + base_url = creds$base_url +) + +test_that("getStudyDataPointsBulk returns an appropriate object.", { + study_data_points_bulk <- castor_api$getStudyDataPointsBulk(creds$output_study) + + expect_s3_class(study_data_points_bulk, "data.frame") + expect_equal(nrow(study_data_points_bulk), 56) + expect_equal(ncol(study_data_points_bulk), 4) +}) + +test_that("getStudyDataPointsBulk fails appropriately", { + error <- expect_error(castor_api$getStudyDataPointsBulk("THISISNOTASTUDYID")) + expect_match(error$message, "Error code: 404") +}) + + +test_that("getStudyDataBulk returns an appropriate object.", { + field_info <- castor_api$getFields(output_study) + participant_info <- castor_api$getParticipants(output_study) + study_data_bulk <- castor_api$getStudyDataBulk(creds$output_study, field_info, participant_info) + + expect_s3_class(study_data_bulk, "data.frame") + expect_equal(nrow(study_data_bulk), 2) + expect_equal(ncol(study_data_bulk), 57) + expect_named( + study_data_bulk, + c( + "participant_id", + "archived", + "randomisation_group" , + "randomisation_datetime" , + "institute" , + "base_bl_date", + "base_bmi" , + "base_creat" , + "base_CRP" , + "base_date" , + "base_dbp" , + "base_gluc" , + "base_hb", + "base_hr" , + "base_ht" , + "base_leucoc" , + "base_sbp" , + "base_tromboc" , + "base_urea" , + "base_weight", + "conc_med" , + "fac_V_leiden" , + "fu_bl_date" , + "fu_bmi" , + "fu_creat" , + "fu_CRP" , + "fu_date", + "fu_dbp" , + "fu_gluc" , + "fu_hb" , + "fu_hr" , + "fu_ht" , + "fu_leucoc" , + "fu_sbp", + "fu_tromboc" , + "fu_urea" , + "fu_weight" , + "his_cvd" , + "his_diabetes" , + "his_family" , + "his_smoke", + "ic_date" , + "ic_language" , + "ic_main_version" , + "ic_versions" , + "inc_age" , + "inc_criteria" , + "inc_dx", + "inc_ic" , + "inc_trials" , + "onset_stroke" , + "onset_trombectomy" , + "pat_birth_year" , + "pat_height" , + "pat_race", + "pat_sex" , + "unscheduled" + ) + ) +}) + +test_that("getStudyDataBulk fails appropriately", { + field_info <- castor_api$getFields(output_study) + participant_info <- castor_api$getParticipants(output_study) + error <- expect_error(castor_api$getStudyDataBulk("THISISNOTASTUDYID", field_info, participant_info)) + expect_match(error$message, "Error code: 404") +}) From ab276f4fcc2232c729d3457419d5bc1fa8a51f3f Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 30 Jun 2024 12:47:18 +0200 Subject: [PATCH 10/30] Small bugfixes so getStudyDataBulk and getFields tests run --- tests/testthat/test_fields.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test_fields.R b/tests/testthat/test_fields.R index d78d09b..f7bb40f 100644 --- a/tests/testthat/test_fields.R +++ b/tests/testthat/test_fields.R @@ -4,7 +4,7 @@ castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, ba test_that("getField returns an appropriate object.", { field <- castor_api$getField(creds$output_study, creds$example_field) - expect_is(field, "list") + expect_type(field, "list") expect_equal(field$field_id, creds$example_field) expect_gt(length(field), 0) }) From 42a2f5cb4a75e910636bcba4cdd1c8cd5a5b5eec Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 30 Jun 2024 12:51:16 +0200 Subject: [PATCH 11/30] Add getStudyDataPoints tests --- tests/testthat/test_getStudyDataPoints.R | 81 ++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/testthat/test_getStudyDataPoints.R diff --git a/tests/testthat/test_getStudyDataPoints.R b/tests/testthat/test_getStudyDataPoints.R new file mode 100644 index 0000000..da212c5 --- /dev/null +++ b/tests/testthat/test_getStudyDataPoints.R @@ -0,0 +1,81 @@ +creds <- readRDS("testing_credentials.Rds") +castor_api <- CastorData$new( + key = creds$client_id, + secret = creds$client_secret, + base_url = creds$base_url +) + +test_that("getStudyDataPoints returns an appropriate object.", { + study_data_points <- castor_api$getStudyDataPoints(creds$output_study, creds$example_participant) + + expect_s3_class(study_data_points, "data.frame") + expect_equal(nrow(study_data_points), 1) + expect_equal(ncol(study_data_points), 57) + expect_named( + study_data_points, + c( + "participant_id", + "archived", + "randomisation_group" , + "randomisation_datetime" , + "institute" , + "base_bl_date", + "base_bmi" , + "base_creat" , + "base_CRP" , + "base_date" , + "base_dbp" , + "base_gluc" , + "base_hb", + "base_hr" , + "base_ht" , + "base_leucoc" , + "base_sbp" , + "base_tromboc" , + "base_urea" , + "base_weight", + "conc_med" , + "fac_V_leiden" , + "fu_bl_date" , + "fu_bmi" , + "fu_creat" , + "fu_CRP" , + "fu_date", + "fu_dbp" , + "fu_gluc" , + "fu_hb" , + "fu_hr" , + "fu_ht" , + "fu_leucoc" , + "fu_sbp", + "fu_tromboc" , + "fu_urea" , + "fu_weight" , + "his_cvd" , + "his_diabetes" , + "his_family" , + "his_smoke", + "ic_date" , + "ic_language" , + "ic_main_version" , + "ic_versions" , + "inc_age" , + "inc_criteria" , + "inc_dx", + "inc_ic" , + "inc_trials" , + "onset_stroke" , + "onset_trombectomy" , + "pat_birth_year" , + "pat_height" , + "pat_race", + "pat_sex" , + "unscheduled" + ) + ) +}) + +test_that("getStudyDataPoints fails appropriately", { + error <- expect_error(castor_api$getStudyDataPoints("THISISNOTASTUDYID", creds$example_participant)) + expect_match(error$message, "Error code: 404") +}) From 10fe551546e14d8897ea0ff6c8af4093d9fd65c5 Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 30 Jun 2024 12:53:37 +0200 Subject: [PATCH 12/30] Naming convention of test files --- .../{test_getStudyDataBulk.R => test_get_study_data_bulk.R} | 0 .../{test_getStudyDataPoints.R => test_get_study_data_points.R} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tests/testthat/{test_getStudyDataBulk.R => test_get_study_data_bulk.R} (100%) rename tests/testthat/{test_getStudyDataPoints.R => test_get_study_data_points.R} (100%) diff --git a/tests/testthat/test_getStudyDataBulk.R b/tests/testthat/test_get_study_data_bulk.R similarity index 100% rename from tests/testthat/test_getStudyDataBulk.R rename to tests/testthat/test_get_study_data_bulk.R diff --git a/tests/testthat/test_getStudyDataPoints.R b/tests/testthat/test_get_study_data_points.R similarity index 100% rename from tests/testthat/test_getStudyDataPoints.R rename to tests/testthat/test_get_study_data_points.R From 649d46d0b195892562ed06f211caf2fbb8c1ef6d Mon Sep 17 00:00:00 2001 From: Reinier Date: Wed, 3 Jul 2024 21:02:33 +0200 Subject: [PATCH 13/30] Fixed tests for getStudyDataBulk and getStudyDataPoints --- R/CastorData.R | 105 ++++++++++-------- .../files_output/.~lock.CastorStudy.csv# | 1 - tests/testthat/test_get_study_data_bulk.R | 11 +- tests/testthat/test_get_study_data_points.R | 3 +- 4 files changed, 65 insertions(+), 55 deletions(-) delete mode 100644 tests/test_files/files_output/.~lock.CastorStudy.csv# diff --git a/R/CastorData.R b/R/CastorData.R index 0a159b7..7c4c9e5 100644 --- a/R/CastorData.R +++ b/R/CastorData.R @@ -498,15 +498,19 @@ CastorData <- R6::R6Class("CastorData", "returning data frame with just participant metadata.") empty.df <- data.frame( - "Site Abbreviation" = - participant_metadata[["_embedded"]][["site"]][["abbreviation"]], - "Randomization Group" = - ifelse(is.null(participant_metadata[["randomization_group"]]), NA, - participant_metadata[["randomization_group"]]), - "Participant Creation" = participant_metadata[["created_on"]][["date"]], - "Participant_ID" = participant_id + "institute" = + participant_metadata[["_embedded"]][["site"]][["name"]], + "randomisation_group" = + ifelse(is.null(participant_metadata[["randomization_group_name"]]), + NA, + participant_metadata[["randomization_group_name"]]), + "participant_id" = participant_id, + "archived" = participant_metadata[["archived"]], + "randomisation_datetime" = + ifelse(is.null(participant_metadata[["randomizated_on"]]), + NA, + participant_metadata[["randomizated_on"]]) ) - return(empty.df) } # Filter out any field types that are specified in the filter_types @@ -524,54 +528,59 @@ CastorData <- R6::R6Class("CastorData", stringsAsFactors = FALSE) # Add the participant id as a column to the data. - study_data_points.df[["Participant_ID"]] <- participant_id + study_data_points.df[["participant_id"]] <- participant_id # Randomization ID should be in participant data. - study_data_points.df[["Site_Abbreviation"]] <- - participant_metadata[["_embedded"]][["site"]][["abbreviation"]] - study_data_points.df[["Randomization_Group"]] <- - ifelse(is.null(participant_metadata[["randomization_group"]]), + study_data_points.df[["institute"]] <- + participant_metadata[["_embedded"]][["site"]][["name"]] + study_data_points.df[["archived"]] <- + participant_metadata[["archived"]] + study_data_points.df[["randomisation_group"]] <- + ifelse(is.null(participant_metadata[["randomization_group_name"]]), + NA, + participant_metadata[["randomization_group_name"]]) + study_data_points.df[["randomisation_datetime"]] <- + ifelse(is.null(participant_metadata[["randomizated_on"]]), NA, - participant_metadata[["randomization_group"]]) - study_data_points.df[["Participant_Creation"]] <- - participant_metadata[["created_on"]][["date"]] + participant_metadata[["randomizated_on"]]) return(study_data_points.df) }, - getStudyDataBulk = function(study_id., field_info., participant_metadata) { - study_data <- self$getStudyDataPointsBulk(study_id.) - if (isTRUE(nrow(study_data) > 0)) { - study_data_field_info <- distinct(left_join(study_data, field_info., by = "field_id")) - study_data_long <- select(study_data_field_info, - field_variable_name, participant_id, field_value) - study_data_wide <- spread(study_data_long, - field_variable_name, field_value) - study_data_complete_cases <- filter_all(study_data_wide, - any_vars(!is.na(.))) - - # Add randomized on date for studies without randomization - # Is not retrieved from API, but needs to exist for further steps - if (!("randomized_on.date" %in% names(participant_metadata))) { - participant_metadata <- participant_metadata %>% - mutate(randomized_on.date = NA_character_) - } + getStudyDataBulk = function(study_id, field_info, participant_metadata) { + study_data <- self$getStudyDataPointsBulk(study_id) + + if (nrow(study_data) > 0) { + study_data <- left_join(select(study_data, -updated_on), + select(field_info, c(field_variable_name, field_id)), + by = "field_id") - rename( - left_join( - select( - participant_metadata, - participant_id, - Randomization_Group = randomization_group, - Randomization_Group_Name = randomization_group_name, - Randomized_On = randomized_on.date, - Site_Abbreviation = `_embedded.site.abbreviation`, - Participant_Creation = created_on.date), - study_data_complete_cases, - by="participant_id" + study_data <- pivot_wider(select(study_data, -field_id), + names_from = field_variable_name, + values_from = field_value) + + study_data <- left_join( + select( + participant_metadata, + participant_id, + `_embedded.site.name`, + archived, + randomization_group_name, + randomized_on, ), - Participant_ID = participant_id + study_data, + by = "participant_id" + ) + + study_data <- rename(study_data, + randomisation_group = randomization_group_name, + randomisation_datetime = randomized_on, + institute = `_embedded.site.name` ) + + study_data + } else + # TODO: hier verder, tests runnen nog niet, en bij 0 rows het hier verwerken ipv in getStudyDat NULL }, generateFieldMetadata = function(study_id, field_info) { @@ -683,8 +692,8 @@ CastorData <- R6::R6Class("CastorData", all_data_points.df <- self$getStudyDataBulk(study_id, field_info, participant_metadata) } else { - # Get study data from getStudyDataPoints and collect them by participant in a - # list. + # Get study data from getStudyDataPoints + # Collect them by participant in a list study_data <- lapply(participant_metadata$participant_id, function(participant) { if (self$verbose) message("getting participant ", participant) return(self$getStudyDataPoints(study_id, participant, filter_types)) diff --git a/tests/test_files/files_output/.~lock.CastorStudy.csv# b/tests/test_files/files_output/.~lock.CastorStudy.csv# deleted file mode 100644 index 38d080f..0000000 --- a/tests/test_files/files_output/.~lock.CastorStudy.csv# +++ /dev/null @@ -1 +0,0 @@ -,reinier,Reinier-PC,30.06.2024 12:37,file:///home/reinier/.config/libreoffice/4; \ No newline at end of file diff --git a/tests/testthat/test_get_study_data_bulk.R b/tests/testthat/test_get_study_data_bulk.R index f5e435c..d1e6020 100644 --- a/tests/testthat/test_get_study_data_bulk.R +++ b/tests/testthat/test_get_study_data_bulk.R @@ -20,8 +20,8 @@ test_that("getStudyDataPointsBulk fails appropriately", { test_that("getStudyDataBulk returns an appropriate object.", { - field_info <- castor_api$getFields(output_study) - participant_info <- castor_api$getParticipants(output_study) + field_info <- castor_api$getFields(creds$output_study) + participant_info <- castor_api$getParticipants(creds$output_study) study_data_bulk <- castor_api$getStudyDataBulk(creds$output_study, field_info, participant_info) expect_s3_class(study_data_bulk, "data.frame") @@ -87,13 +87,14 @@ test_that("getStudyDataBulk returns an appropriate object.", { "pat_race", "pat_sex" , "unscheduled" - ) + ), + ignore.order=T ) }) test_that("getStudyDataBulk fails appropriately", { - field_info <- castor_api$getFields(output_study) - participant_info <- castor_api$getParticipants(output_study) + field_info <- castor_api$getFields(creds$output_study) + participant_info <- castor_api$getParticipants(creds$output_study) error <- expect_error(castor_api$getStudyDataBulk("THISISNOTASTUDYID", field_info, participant_info)) expect_match(error$message, "Error code: 404") }) diff --git a/tests/testthat/test_get_study_data_points.R b/tests/testthat/test_get_study_data_points.R index da212c5..e4139e7 100644 --- a/tests/testthat/test_get_study_data_points.R +++ b/tests/testthat/test_get_study_data_points.R @@ -71,7 +71,8 @@ test_that("getStudyDataPoints returns an appropriate object.", { "pat_race", "pat_sex" , "unscheduled" - ) + ), + ignore.order=T ) }) From cc5af172824249939eb0a9962f236221b8d27ec8 Mon Sep 17 00:00:00 2001 From: Reinier Date: Wed, 3 Jul 2024 21:08:23 +0200 Subject: [PATCH 14/30] Refactored getStudyDataBulk to return participant metadata only when there is no study data, and to extract field_info and participant_metadata when it is not given to the function --- R/CastorData.R | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/R/CastorData.R b/R/CastorData.R index 7c4c9e5..106bff9 100644 --- a/R/CastorData.R +++ b/R/CastorData.R @@ -546,7 +546,9 @@ CastorData <- R6::R6Class("CastorData", return(study_data_points.df) }, - getStudyDataBulk = function(study_id, field_info, participant_metadata) { + getStudyDataBulk = function(study_id, field_info=NULL, participant_metadata=NULL) { + if (is.null(field_info)) {self$getFields(study_id)} + if (is.null(participant_metadata)) {self$getParticipants(study_id)} study_data <- self$getStudyDataPointsBulk(study_id) if (nrow(study_data) > 0) { @@ -579,9 +581,23 @@ CastorData <- R6::R6Class("CastorData", study_data - } else - # TODO: hier verder, tests runnen nog niet, en bij 0 rows het hier verwerken ipv in getStudyDat - NULL + } else { + # If no study data, only return participant information + study_data <- + rename(select( + participant_metadata, + participant_id, + `_embedded.site.name`, + archived, + randomization_group_name, + randomized_on, + ), + randomisation_group = randomization_group_name, + randomisation_datetime = randomized_on, + institute = `_embedded.site.name` + ) + } + study_data }, generateFieldMetadata = function(study_id, field_info) { forms <- self$getForms(study_id) @@ -702,10 +718,6 @@ CastorData <- R6::R6Class("CastorData", all_data_points.df <- bind_rows(study_data) } } else { - all_data_points.df <- NULL - } - - if (is.null(all_data_points.df)) { all_data_points.df <- rename( select( participant_metadata, From e6666f6e7f02f9ed39ce77b6ff2ab8c0b51d952b Mon Sep 17 00:00:00 2001 From: Reinier Date: Wed, 3 Jul 2024 21:20:57 +0200 Subject: [PATCH 15/30] Added documentation --- R/CastorData.R | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/R/CastorData.R b/R/CastorData.R index 106bff9..5a123fd 100644 --- a/R/CastorData.R +++ b/R/CastorData.R @@ -547,19 +547,27 @@ CastorData <- R6::R6Class("CastorData", return(study_data_points.df) }, getStudyDataBulk = function(study_id, field_info=NULL, participant_metadata=NULL) { + # Extract the necessary info that wasnt given to the function if (is.null(field_info)) {self$getFields(study_id)} if (is.null(participant_metadata)) {self$getParticipants(study_id)} + + # Extract all study data points in long format + # One row per datapoint study_data <- self$getStudyDataPointsBulk(study_id) + # If there is study data if (nrow(study_data) > 0) { + # Add the field names to the field ids and values in long format study_data <- left_join(select(study_data, -updated_on), select(field_info, c(field_variable_name, field_id)), by = "field_id") + # Move from long (each data point has a row) to wide format per participant study_data <- pivot_wider(select(study_data, -field_id), names_from = field_variable_name, values_from = field_value) + # Add participant metadata to the dataframe study_data <- left_join( select( participant_metadata, @@ -578,9 +586,6 @@ CastorData <- R6::R6Class("CastorData", randomisation_datetime = randomized_on, institute = `_embedded.site.name` ) - - study_data - } else { # If no study data, only return participant information study_data <- From 81712ca8abff617a501c3a53010a9709b62b88c6 Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 23 Jun 2024 12:37:29 +0200 Subject: [PATCH 16/30] Moved to version 2.1.0; Prepared testing for getStudyData Fixed conflicts --- DESCRIPTION | 5 ++- .../files_output/CastorAdverseEvent.csv | 3 ++ .../files_output/CastorComorbidities.csv | 2 + .../files_output/CastorMedication.csv | 3 ++ .../files_output/CastorQOLSurvey.csv | 3 ++ tests/test_files/files_output/CastorStudy.csv | 3 ++ .../files_output/CastorUnscheduledVisit.csv | 2 + tests/testthat/test_getStudyData.R | 45 +++++++++++++++++-- 8 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 tests/test_files/files_output/CastorAdverseEvent.csv create mode 100644 tests/test_files/files_output/CastorComorbidities.csv create mode 100644 tests/test_files/files_output/CastorMedication.csv create mode 100644 tests/test_files/files_output/CastorQOLSurvey.csv create mode 100644 tests/test_files/files_output/CastorStudy.csv create mode 100644 tests/test_files/files_output/CastorUnscheduledVisit.csv diff --git a/DESCRIPTION b/DESCRIPTION index 00917d0..f797ca1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: castoRedc Title: Wraps Castor API for easy data access -Version: 2.0.1 +Version: 2.1.0 Authors@R: c( person("Thomas", "Johnson", role = "aut"), person("Reinier", "van Linschoten", @@ -26,10 +26,11 @@ Imports: glue Suggests: knitr, - testthat + testthat (>= 3.0.0) RoxygenNote: 7.3.0 Collate: 'imports.R' 'utils.R' 'CastorData.R' 'CastorAPIWrapper.R' +Config/testthat/edition: 3 diff --git a/tests/test_files/files_output/CastorAdverseEvent.csv b/tests/test_files/files_output/CastorAdverseEvent.csv new file mode 100644 index 0000000..8ba49b6 --- /dev/null +++ b/tests/test_files/files_output/CastorAdverseEvent.csv @@ -0,0 +1,3 @@ +record_id,institute,created_on,custom_name,parent,archived,AE_type,AE_startdate,AE_stopdate,AE_severity,AE_relation,AE_action,AE_outcome,AE_expected,AE_SAE +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:58:13,Adverse event - 22-06-2021 09:58:12,No parent,FALSE,,,,,,,,, +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:55:13,Adverse event - 22-06-2021 09:55:12,Follow-up Visit,FALSE,A bug in the API,23-06-2021,24-06-2021,Life-threatening,Definitely related,Medical intervention,Resolved,Yes,Yes diff --git a/tests/test_files/files_output/CastorComorbidities.csv b/tests/test_files/files_output/CastorComorbidities.csv new file mode 100644 index 0000000..db92eed --- /dev/null +++ b/tests/test_files/files_output/CastorComorbidities.csv @@ -0,0 +1,2 @@ +record_id,institute,created_on,custom_name,parent,archived,comorbidities +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:56:07,Comorbidities - 22-06-2021 09:56:06,No parent,FALSE,-- Uploaded File: comorbidities.txt -- diff --git a/tests/test_files/files_output/CastorMedication.csv b/tests/test_files/files_output/CastorMedication.csv new file mode 100644 index 0000000..f775820 --- /dev/null +++ b/tests/test_files/files_output/CastorMedication.csv @@ -0,0 +1,3 @@ +record_id,institute,created_on,custom_name,parent,archived,med_name,med_start,med_stop,med_dose,med_units,med_other_unit +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:54:28,Medication - 22-06-2021 09:54:28,Unscheduled visits and Medication,FALSE,Diclofenac,25-06-2021,28-06-2021,150,mg/day, +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:58:27,Medication - 22-06-2021 09:58:26,No parent,FALSE,,,,,, diff --git a/tests/test_files/files_output/CastorQOLSurvey.csv b/tests/test_files/files_output/CastorQOLSurvey.csv new file mode 100644 index 0000000..baac99c --- /dev/null +++ b/tests/test_files/files_output/CastorQOLSurvey.csv @@ -0,0 +1,3 @@ +record_id,institute,survey_name,survey_instance_id,created_on,sent_on,progress,completed_on,package_id,package_name,archived,SF12_1,SF12_2,SF12_3,SF12_12,VAS +110001,Franciscus Gasthuis & Vlietland,QOL Survey,4FF130AD-274C-4C8F-A4A0-A7816A5A88E9,22-06-2021 10:12:19,22-06-2021 10:12:19,100,22-06-2021 10:12:44,0761A9BA-9802-483D-8EB3-D07233A56F2B,My first survey package,FALSE,Excellent,Mostly,Mostly,Rarely,85 +110001,Franciscus Gasthuis & Vlietland,QOL Survey,B99F0082-1B20-456B-8D4D-47D16867B211,22-06-2021 10:13:01,22-06-2021 10:13:01,0,,5B3FE56F-89FA-4C6D-A638-E8FA46113EB0,My first survey package,FALSE,,,,, diff --git a/tests/test_files/files_output/CastorStudy.csv b/tests/test_files/files_output/CastorStudy.csv new file mode 100644 index 0000000..7e3e8b4 --- /dev/null +++ b/tests/test_files/files_output/CastorStudy.csv @@ -0,0 +1,3 @@ +record_id,archived,institute,randomisation_group,randomisation_datetime,ic_date,ic_versions#Main study CF,ic_versions#Other,ic_main_version,ic_other_version,ic_language#English,ic_language#Dutch,ic_language#French,ic_language#Other,inc_ic,inc_trials,inc_age,inc_dx,inc_criteria,pat_birth_year,pat_sex,pat_race,pat_height,his_cvd,his_diabetes,his_smoke,his_smoke_start,his_smoke_dose,his_family#None,his_family#Unknown,his_family#Deafness,his_family#(Cardio)myopathy,his_family#Encephalopathy,his_family#Diabetes Mellitus,his_family#Hypertension/Cardiovascular disease,his_family#Thrombosis,his_family#Malignancy,randalloc,randdisp,disp_datetime,base_date,onset_stroke,onset_trombectomy,base_weight,base_bmi,base_sbp,base_dbp,base_hr,base_bl_date,base_hb,base_ht,base_leucoc,base_tromboc,base_urea,base_creat,base_CRP,fac_V_leiden_number,fac_V_leiden_date,base_gluc,fu_date,fu_weight,fu_bmi,fu_sbp,fu_dbp,fu_hr,fu_bl_date,fu_hb,fu_ht,fu_leucoc,fu_tromboc,fu_urea,fu_creat,fu_CRP,fu_gluc,unscheduled,conc_med +110001,FALSE,Franciscus Gasthuis & Vlietland,,,22-06-2021,TRUE,FALSE,2.5,,FALSE,FALSE,TRUE,FALSE,Yes,No,Yes,No,Yes,1996,Female,African/black,1.75,Yes,No,No,,,FALSE,FALSE,FALSE,TRUE,FALSE,FALSE,FALSE,FALSE,FALSE,,,,22-06-2021,21-06-2021 01:15,01:30,88,28.7,120,65,66,22-06-2021,7.4,0.23,4.1,252,4.6,88,2.1,55,22-06-2021,5.4,29-06-2021,66,21.6,132,72,69,29-06-2021,8.1,0.36,9.5,366,8.2,99,4.4,3.1,Yes,Yes +110002,FALSE,Franciscus Gasthuis & Vlietland,,,,FALSE,FALSE,,,-99,-99,-99,-99,,,,,,,,,-95,,,,,,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,,,,31-12-2999,,,,,,,,,,,,,,,,-96,01-01-2996,,,,,,,,,,,,,,,,,, diff --git a/tests/test_files/files_output/CastorUnscheduledVisit.csv b/tests/test_files/files_output/CastorUnscheduledVisit.csv new file mode 100644 index 0000000..5f0690e --- /dev/null +++ b/tests/test_files/files_output/CastorUnscheduledVisit.csv @@ -0,0 +1,2 @@ +record_id,institute,created_on,custom_name,parent,archived,unsvis_type,unsvis_date,unsvis_weight,unsvis_sbp,unsvis_dbp,unsvis_hr,unslab_date,unslab_s_hb,unslab_ht,unslab_leucoc,unslab_tromboc,unslab_urea,unslab_creat,unslab_CRP,unslab_gluc +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:57:38,Unscheduled visit - 22-06-2021 09:57:36,Unscheduled visits and Medication,FALSE,admission to day-care,24-06-2021,98.3,233,156,112,,,,,,,,, diff --git a/tests/testthat/test_getStudyData.R b/tests/testthat/test_getStudyData.R index bd37d70..8cb4980 100644 --- a/tests/testthat/test_getStudyData.R +++ b/tests/testthat/test_getStudyData.R @@ -1,10 +1,47 @@ -context("Test getStudyData in all its glory.") +context("Test if the output of getStudyData matches the expected output.") creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) +complete_study_output <- castor_api$getStudyData(creds$output_study) -test_that("getStudyData returns an appropriate object.", { - sdf <- castor_api$getStudyData(creds$example_study) +test_that("getStudyData returns the expected study data.", { + actual <- complete_study_output$Study + expected <- read.csv("tests/test_files/files_output/CastorStudy.csv", check.names=F) - expect_equal(creds$study_hash, digest::digest(sdf)) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Unscheduled Visit", { + actual <- complete_study_output$`Repeating data`$`Unscheduled visit` + expected <- read.csv("tests/test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Medication", { + actual <- complete_study_output$`Repeating data`$Medication + expected <- read.csv("tests/test_files/files_output/CastorMedication.csv", check.names=F) + + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Comorbidities", { + actual <- complete_study_output$`Repeating data`$Comorbidities + expected <- read.csv("tests/test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Adverse Events", { + actual <- complete_study_output$`Repeating data`$`Adverse event` + expected <- read.csv("tests/test_files/files_output/CastorAdverseEvent.csv", check.names=F) + + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the survey Quality of Life", { + actual <- complete_study_output$Surveys$`QOL Survey` + expected <- read.csv("tests/test_files/files_output/CastorQOLSurvey.csv", check.names=F) + + expect_identical(actual, expected) }) From 91112f74d65d4c5632dae036cd1cdcdb93eccf58 Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 23 Jun 2024 12:44:41 +0200 Subject: [PATCH 17/30] Testing of getStudyData runs --- tests/testthat/test_getStudyData.R | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tests/testthat/test_getStudyData.R b/tests/testthat/test_getStudyData.R index 8cb4980..593c52e 100644 --- a/tests/testthat/test_getStudyData.R +++ b/tests/testthat/test_getStudyData.R @@ -1,47 +1,45 @@ -context("Test if the output of getStudyData matches the expected output.") - creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) complete_study_output <- castor_api$getStudyData(creds$output_study) test_that("getStudyData returns the expected study data.", { actual <- complete_study_output$Study - expected <- read.csv("tests/test_files/files_output/CastorStudy.csv", check.names=F) + expected <- read.csv("../test_files/files_output/CastorStudy.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the repeated measure Unscheduled Visit", { actual <- complete_study_output$`Repeating data`$`Unscheduled visit` - expected <- read.csv("tests/test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + expected <- read.csv("../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the repeated measure Medication", { actual <- complete_study_output$`Repeating data`$Medication - expected <- read.csv("tests/test_files/files_output/CastorMedication.csv", check.names=F) + expected <- read.csv("../test_files/files_output/CastorMedication.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the repeated measure Comorbidities", { actual <- complete_study_output$`Repeating data`$Comorbidities - expected <- read.csv("tests/test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + expected <- read.csv("../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the repeated measure Adverse Events", { actual <- complete_study_output$`Repeating data`$`Adverse event` - expected <- read.csv("tests/test_files/files_output/CastorAdverseEvent.csv", check.names=F) + expected <- read.csv("../test_files/files_output/CastorAdverseEvent.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the survey Quality of Life", { actual <- complete_study_output$Surveys$`QOL Survey` - expected <- read.csv("tests/test_files/files_output/CastorQOLSurvey.csv", check.names=F) + expected <- read.csv("../test_files/files_output/CastorQOLSurvey.csv", check.names=F) expect_identical(actual, expected) }) From 7ff04ad6d265c02cdf25fbe9f4d15c8ead5e2f06 Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 23 Jun 2024 13:04:32 +0200 Subject: [PATCH 18/30] Changed testing folder structure; added tests for parametrised getStudyData --- .../files_output/CastorAdverseEvent.csv | 2 +- .../files_output/CastorComorbidities.csv | 2 +- .../files_output/CastorMedication.csv | 2 +- .../files_output/CastorQOLSurvey.csv | 2 +- tests/test_files/files_output/CastorStudy.csv | 2 +- .../files_output/CastorUnscheduledVisit.csv | 2 +- tests/testthat/test_fields.R | 1 + tests/testthat/test_forms.R | 1 + .../test_getStudyData.R | 20 +-- .../test_getStudyData_Parts.R | 124 ++++++++++++++++++ .../test_getStudyData_noBulk.R | 39 ++++++ tests/testthat/test_instantiation.R | 1 + tests/testthat/test_participants.R | 1 + tests/testthat/test_repeating_data.R | 1 + tests/testthat/test_studies.R | 1 + tests/testthat/test_studydatapoints.R | 1 + tests/testthat/test_surveys.R | 1 + tests/testthat/test_visits.R | 1 + 18 files changed, 185 insertions(+), 19 deletions(-) rename tests/testthat/{ => test_getStudyData}/test_getStudyData.R (68%) create mode 100644 tests/testthat/test_getStudyData/test_getStudyData_Parts.R create mode 100644 tests/testthat/test_getStudyData/test_getStudyData_noBulk.R diff --git a/tests/test_files/files_output/CastorAdverseEvent.csv b/tests/test_files/files_output/CastorAdverseEvent.csv index 8ba49b6..6674029 100644 --- a/tests/test_files/files_output/CastorAdverseEvent.csv +++ b/tests/test_files/files_output/CastorAdverseEvent.csv @@ -1,3 +1,3 @@ -record_id,institute,created_on,custom_name,parent,archived,AE_type,AE_startdate,AE_stopdate,AE_severity,AE_relation,AE_action,AE_outcome,AE_expected,AE_SAE +participant_id,institute,created_on,custom_name,parent,archived,AE_type,AE_startdate,AE_stopdate,AE_severity,AE_relation,AE_action,AE_outcome,AE_expected,AE_SAE 110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:58:13,Adverse event - 22-06-2021 09:58:12,No parent,FALSE,,,,,,,,, 110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:55:13,Adverse event - 22-06-2021 09:55:12,Follow-up Visit,FALSE,A bug in the API,23-06-2021,24-06-2021,Life-threatening,Definitely related,Medical intervention,Resolved,Yes,Yes diff --git a/tests/test_files/files_output/CastorComorbidities.csv b/tests/test_files/files_output/CastorComorbidities.csv index db92eed..1962c39 100644 --- a/tests/test_files/files_output/CastorComorbidities.csv +++ b/tests/test_files/files_output/CastorComorbidities.csv @@ -1,2 +1,2 @@ -record_id,institute,created_on,custom_name,parent,archived,comorbidities +participant_id,institute,created_on,custom_name,parent,archived,comorbidities 110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:56:07,Comorbidities - 22-06-2021 09:56:06,No parent,FALSE,-- Uploaded File: comorbidities.txt -- diff --git a/tests/test_files/files_output/CastorMedication.csv b/tests/test_files/files_output/CastorMedication.csv index f775820..34e5075 100644 --- a/tests/test_files/files_output/CastorMedication.csv +++ b/tests/test_files/files_output/CastorMedication.csv @@ -1,3 +1,3 @@ -record_id,institute,created_on,custom_name,parent,archived,med_name,med_start,med_stop,med_dose,med_units,med_other_unit +participant_id,institute,created_on,custom_name,parent,archived,med_name,med_start,med_stop,med_dose,med_units,med_other_unit 110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:54:28,Medication - 22-06-2021 09:54:28,Unscheduled visits and Medication,FALSE,Diclofenac,25-06-2021,28-06-2021,150,mg/day, 110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:58:27,Medication - 22-06-2021 09:58:26,No parent,FALSE,,,,,, diff --git a/tests/test_files/files_output/CastorQOLSurvey.csv b/tests/test_files/files_output/CastorQOLSurvey.csv index baac99c..2685903 100644 --- a/tests/test_files/files_output/CastorQOLSurvey.csv +++ b/tests/test_files/files_output/CastorQOLSurvey.csv @@ -1,3 +1,3 @@ -record_id,institute,survey_name,survey_instance_id,created_on,sent_on,progress,completed_on,package_id,package_name,archived,SF12_1,SF12_2,SF12_3,SF12_12,VAS +participant_id,institute,survey_name,survey_instance_id,created_on,sent_on,progress,completed_on,package_id,package_name,archived,SF12_1,SF12_2,SF12_3,SF12_12,VAS 110001,Franciscus Gasthuis & Vlietland,QOL Survey,4FF130AD-274C-4C8F-A4A0-A7816A5A88E9,22-06-2021 10:12:19,22-06-2021 10:12:19,100,22-06-2021 10:12:44,0761A9BA-9802-483D-8EB3-D07233A56F2B,My first survey package,FALSE,Excellent,Mostly,Mostly,Rarely,85 110001,Franciscus Gasthuis & Vlietland,QOL Survey,B99F0082-1B20-456B-8D4D-47D16867B211,22-06-2021 10:13:01,22-06-2021 10:13:01,0,,5B3FE56F-89FA-4C6D-A638-E8FA46113EB0,My first survey package,FALSE,,,,, diff --git a/tests/test_files/files_output/CastorStudy.csv b/tests/test_files/files_output/CastorStudy.csv index 7e3e8b4..808f809 100644 --- a/tests/test_files/files_output/CastorStudy.csv +++ b/tests/test_files/files_output/CastorStudy.csv @@ -1,3 +1,3 @@ -record_id,archived,institute,randomisation_group,randomisation_datetime,ic_date,ic_versions#Main study CF,ic_versions#Other,ic_main_version,ic_other_version,ic_language#English,ic_language#Dutch,ic_language#French,ic_language#Other,inc_ic,inc_trials,inc_age,inc_dx,inc_criteria,pat_birth_year,pat_sex,pat_race,pat_height,his_cvd,his_diabetes,his_smoke,his_smoke_start,his_smoke_dose,his_family#None,his_family#Unknown,his_family#Deafness,his_family#(Cardio)myopathy,his_family#Encephalopathy,his_family#Diabetes Mellitus,his_family#Hypertension/Cardiovascular disease,his_family#Thrombosis,his_family#Malignancy,randalloc,randdisp,disp_datetime,base_date,onset_stroke,onset_trombectomy,base_weight,base_bmi,base_sbp,base_dbp,base_hr,base_bl_date,base_hb,base_ht,base_leucoc,base_tromboc,base_urea,base_creat,base_CRP,fac_V_leiden_number,fac_V_leiden_date,base_gluc,fu_date,fu_weight,fu_bmi,fu_sbp,fu_dbp,fu_hr,fu_bl_date,fu_hb,fu_ht,fu_leucoc,fu_tromboc,fu_urea,fu_creat,fu_CRP,fu_gluc,unscheduled,conc_med +participant_id,archived,institute,randomisation_group,randomisation_datetime,ic_date,ic_versions#Main study CF,ic_versions#Other,ic_main_version,ic_other_version,ic_language#English,ic_language#Dutch,ic_language#French,ic_language#Other,inc_ic,inc_trials,inc_age,inc_dx,inc_criteria,pat_birth_year,pat_sex,pat_race,pat_height,his_cvd,his_diabetes,his_smoke,his_smoke_start,his_smoke_dose,his_family#None,his_family#Unknown,his_family#Deafness,his_family#(Cardio)myopathy,his_family#Encephalopathy,his_family#Diabetes Mellitus,his_family#Hypertension/Cardiovascular disease,his_family#Thrombosis,his_family#Malignancy,randalloc,randdisp,disp_datetime,base_date,onset_stroke,onset_trombectomy,base_weight,base_bmi,base_sbp,base_dbp,base_hr,base_bl_date,base_hb,base_ht,base_leucoc,base_tromboc,base_urea,base_creat,base_CRP,fac_V_leiden_number,fac_V_leiden_date,base_gluc,fu_date,fu_weight,fu_bmi,fu_sbp,fu_dbp,fu_hr,fu_bl_date,fu_hb,fu_ht,fu_leucoc,fu_tromboc,fu_urea,fu_creat,fu_CRP,fu_gluc,unscheduled,conc_med 110001,FALSE,Franciscus Gasthuis & Vlietland,,,22-06-2021,TRUE,FALSE,2.5,,FALSE,FALSE,TRUE,FALSE,Yes,No,Yes,No,Yes,1996,Female,African/black,1.75,Yes,No,No,,,FALSE,FALSE,FALSE,TRUE,FALSE,FALSE,FALSE,FALSE,FALSE,,,,22-06-2021,21-06-2021 01:15,01:30,88,28.7,120,65,66,22-06-2021,7.4,0.23,4.1,252,4.6,88,2.1,55,22-06-2021,5.4,29-06-2021,66,21.6,132,72,69,29-06-2021,8.1,0.36,9.5,366,8.2,99,4.4,3.1,Yes,Yes 110002,FALSE,Franciscus Gasthuis & Vlietland,,,,FALSE,FALSE,,,-99,-99,-99,-99,,,,,,,,,-95,,,,,,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,,,,31-12-2999,,,,,,,,,,,,,,,,-96,01-01-2996,,,,,,,,,,,,,,,,,, diff --git a/tests/test_files/files_output/CastorUnscheduledVisit.csv b/tests/test_files/files_output/CastorUnscheduledVisit.csv index 5f0690e..0ad24e3 100644 --- a/tests/test_files/files_output/CastorUnscheduledVisit.csv +++ b/tests/test_files/files_output/CastorUnscheduledVisit.csv @@ -1,2 +1,2 @@ -record_id,institute,created_on,custom_name,parent,archived,unsvis_type,unsvis_date,unsvis_weight,unsvis_sbp,unsvis_dbp,unsvis_hr,unslab_date,unslab_s_hb,unslab_ht,unslab_leucoc,unslab_tromboc,unslab_urea,unslab_creat,unslab_CRP,unslab_gluc +participant_id,institute,created_on,custom_name,parent,archived,unsvis_type,unsvis_date,unsvis_weight,unsvis_sbp,unsvis_dbp,unsvis_hr,unslab_date,unslab_s_hb,unslab_ht,unslab_leucoc,unslab_tromboc,unslab_urea,unslab_creat,unslab_CRP,unslab_gluc 110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:57:38,Unscheduled visit - 22-06-2021 09:57:36,Unscheduled visits and Medication,FALSE,admission to day-care,24-06-2021,98.3,233,156,112,,,,,,,,, diff --git a/tests/testthat/test_fields.R b/tests/testthat/test_fields.R index 2680273..db0167a 100644 --- a/tests/testthat/test_fields.R +++ b/tests/testthat/test_fields.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test Field related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_forms.R b/tests/testthat/test_forms.R index 0d50dc3..99a5237 100644 --- a/tests/testthat/test_forms.R +++ b/tests/testthat/test_forms.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test Form related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_getStudyData.R b/tests/testthat/test_getStudyData/test_getStudyData.R similarity index 68% rename from tests/testthat/test_getStudyData.R rename to tests/testthat/test_getStudyData/test_getStudyData.R index 593c52e..3bc3db9 100644 --- a/tests/testthat/test_getStudyData.R +++ b/tests/testthat/test_getStudyData/test_getStudyData.R @@ -1,45 +1,39 @@ -creds <- readRDS("testing_credentials.Rds") +creds <- readRDS("../testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) complete_study_output <- castor_api$getStudyData(creds$output_study) test_that("getStudyData returns the expected study data.", { actual <- complete_study_output$Study - expected <- read.csv("../test_files/files_output/CastorStudy.csv", check.names=F) - + expected <- read.csv("../../test_files/files_output/CastorStudy.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the repeated measure Unscheduled Visit", { actual <- complete_study_output$`Repeating data`$`Unscheduled visit` - expected <- read.csv("../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) - + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the repeated measure Medication", { actual <- complete_study_output$`Repeating data`$Medication - expected <- read.csv("../test_files/files_output/CastorMedication.csv", check.names=F) - + expected <- read.csv("../../test_files/files_output/CastorMedication.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the repeated measure Comorbidities", { actual <- complete_study_output$`Repeating data`$Comorbidities - expected <- read.csv("../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) - + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the repeated measure Adverse Events", { actual <- complete_study_output$`Repeating data`$`Adverse event` - expected <- read.csv("../test_files/files_output/CastorAdverseEvent.csv", check.names=F) - + expected <- read.csv("../../test_files/files_output/CastorAdverseEvent.csv", check.names=F) expect_identical(actual, expected) }) test_that("getStudyData returns the expected data for the survey Quality of Life", { actual <- complete_study_output$Surveys$`QOL Survey` - expected <- read.csv("../test_files/files_output/CastorQOLSurvey.csv", check.names=F) - + expected <- read.csv("../../test_files/files_output/CastorQOLSurvey.csv", check.names=F) expect_identical(actual, expected) }) diff --git a/tests/testthat/test_getStudyData/test_getStudyData_Parts.R b/tests/testthat/test_getStudyData/test_getStudyData_Parts.R new file mode 100644 index 0000000..e683da3 --- /dev/null +++ b/tests/testthat/test_getStudyData/test_getStudyData_Parts.R @@ -0,0 +1,124 @@ +creds <- readRDS("../testing_credentials.Rds") +castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) +study_output_nostudy <- castor_api$getStudyData(creds$output_study, load_study_data=FALSE) +study_output_nosurveys <- castor_api$getStudyData(creds$output_study, survey_instances=FALSE) +study_output_norepeatingdata <- castor_api$getStudyData(creds$output_study, repeating_data_instances=FALSE) + +test_that("getStudyData returns the expected study data when omitting study data.", { + actual <- study_output_nostudy$Study + # if study data is omitted, we should only retrieve participant metadata + expected <- read.csv("../../test_files/files_output/CastorStudy.csv", check.names=F)[c("participant_id", "archived", "institute", "randomisation_group", "randomisation_datetime")] + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected study data when omitting surveys.", { + actual <- study_output_nosurveys$Study + expected <- read.csv("../../test_files/files_output/CastorStudy.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected study data when omitting repeating data.", { + actual <- study_output_norepeatingdata$Study + expected <- read.csv("../../test_files/files_output/CastorStudy.csv", check.names=F) + expect_identical(actual, expected) +}) + + + +test_that("getStudyData returns the expected data for the repeated measure Unscheduled Visit when omitting study data", { + actual <- study_output_nostudy$`Repeating data`$`Unscheduled visit` + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Unscheduled Visit when omitting surveys", { + actual <- study_output_nosurveys$`Repeating data`$`Unscheduled visit` + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Unscheduled Visit when omitting repeated data", { + actual <- study_output_norepeatingdata$`Repeating data`$`Unscheduled visit` + expected <- NULL + expect_identical(actual, expected) +}) + + + +test_that("getStudyData returns the expected data for the repeated measure Medication when omitting study data", { + actual <- study_output_nostudy$`Repeating data`$Medication + expected <- read.csv("../../test_files/files_output/CastorMedication.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Medication when omitting surveys", { + actual <- study_output_nosurveys$`Repeating data`$Medication + expected <- read.csv("../../test_files/files_output/CastorMedication.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Medication when omitting repeated data", { + actual <- study_output_norepeatingdata$`Repeating data`$Medication + expected <- NULL + expect_identical(actual, expected) +}) + + + +test_that("getStudyData returns the expected data for the repeated measure Comorbidities when omitting study data", { + actual <- study_output_nostudy$`Repeating data`$Comorbidities + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Comorbidities when omitting surveys", { + actual <- study_output_nosurveys$`Repeating data`$Comorbidities + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Comorbidities when omitting repeated data", { + actual <- study_output_norepeatingdata$`Repeating data`$Comorbidities + expected <- NULL + expect_identical(actual, expected) +}) + + + +test_that("getStudyData returns the expected data for the repeated measure Adverse Events when omitting study data", { + actual <- study_output_nostudy$`Repeating data`$`Adverse event` + expected <- read.csv("../../test_files/files_output/CastorAdverseEvent.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Adverse Events when omitting surveys", { + actual <- study_output_nosurveys$`Repeating data`$`Adverse event` + expected <- read.csv("../../test_files/files_output/CastorAdverseEvent.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Adverse Events when omitting repeated data", { + actual <- study_output_norepeatingdata$`Repeating data`$`Adverse event` + expected <- NULL + expect_identical(actual, expected) +}) + + + +test_that("getStudyData returns the expected data for the survey Quality of Life when omitting study data", { + actual <- study_output_nostudy$Surveys$`QOL Survey` + expected <- read.csv("../../test_files/files_output/CastorQOLSurvey.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the survey Quality of Life when omitting surveys", { + actual <- study_output_nosurveys$Surveys$`QOL Survey` + expected <- NULL + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the survey Quality of Life when omitting repeated data", { + actual <- study_output_norepeatingdata$Surveys$`QOL Survey` + expected <- read.csv("../../test_files/files_output/CastorQOLSurvey.csv", check.names=F) + expect_identical(actual, expected) +}) diff --git a/tests/testthat/test_getStudyData/test_getStudyData_noBulk.R b/tests/testthat/test_getStudyData/test_getStudyData_noBulk.R new file mode 100644 index 0000000..4551902 --- /dev/null +++ b/tests/testthat/test_getStudyData/test_getStudyData_noBulk.R @@ -0,0 +1,39 @@ +creds <- readRDS("../testing_credentials.Rds") +castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) +complete_study_output <- castor_api$getStudyData(creds$output_study, bulk=FALSE) + +test_that("getStudyData returns the expected study data when not using bulk.", { + actual <- complete_study_output$Study + expected <- read.csv("../../test_files/files_output/CastorStudy.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Unscheduled Visit when not using bulk", { + actual <- complete_study_output$`Repeating data`$`Unscheduled visit` + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Medication when not using bulk", { + actual <- complete_study_output$`Repeating data`$Medication + expected <- read.csv("../../test_files/files_output/CastorMedication.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Comorbidities when not using bulk", { + actual <- complete_study_output$`Repeating data`$Comorbidities + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Adverse Events when not using bulk", { + actual <- complete_study_output$`Repeating data`$`Adverse event` + expected <- read.csv("../../test_files/files_output/CastorAdverseEvent.csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the survey Quality of Life when not using bulk", { + actual <- complete_study_output$Surveys$`QOL Survey` + expected <- read.csv("../../test_files/files_output/CastorQOLSurvey.csv", check.names=F) + expect_identical(actual, expected) +}) diff --git a/tests/testthat/test_instantiation.R b/tests/testthat/test_instantiation.R index a154f49..d00e909 100644 --- a/tests/testthat/test_instantiation.R +++ b/tests/testthat/test_instantiation.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test instantiation and authentication.") test_that("Castor object instantiation produces valid R6 class object.", { diff --git a/tests/testthat/test_participants.R b/tests/testthat/test_participants.R index 134a935..01e6529 100644 --- a/tests/testthat/test_participants.R +++ b/tests/testthat/test_participants.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test Participant related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_repeating_data.R b/tests/testthat/test_repeating_data.R index 2950880..ed7652b 100644 --- a/tests/testthat/test_repeating_data.R +++ b/tests/testthat/test_repeating_data.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test RepeatingData related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_studies.R b/tests/testthat/test_studies.R index 83fe633..b97246d 100644 --- a/tests/testthat/test_studies.R +++ b/tests/testthat/test_studies.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test Study related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_studydatapoints.R b/tests/testthat/test_studydatapoints.R index ef6bd2b..ae4c80c 100644 --- a/tests/testthat/test_studydatapoints.R +++ b/tests/testthat/test_studydatapoints.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test StudyDataPoint related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_surveys.R b/tests/testthat/test_surveys.R index 97c2fdc..e2eb83b 100644 --- a/tests/testthat/test_surveys.R +++ b/tests/testthat/test_surveys.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test Survey related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_visits.R b/tests/testthat/test_visits.R index 0aaec24..c4155d3 100644 --- a/tests/testthat/test_visits.R +++ b/tests/testthat/test_visits.R @@ -1,3 +1,4 @@ +# Needs updates to tests context("Test Visit related methods.") creds <- readRDS("testing_credentials.Rds") From 3acd48ecc7e5d91b875c24eb57a242e5a8f95ec4 Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 23 Jun 2024 13:14:01 +0200 Subject: [PATCH 19/30] Added getStudyData tests for not translating checkbox values --- .../CastorAdverseEvent (noLabel).csv | 3 ++ .../CastorComorbidities (noLabel).csv | 2 + .../CastorMedication (noLabel).csv | 3 ++ .../CastorQOLSurvey (noLabel).csv | 3 ++ .../files_output/CastorStudy (noLabel).csv | 3 ++ .../CastorUnscheduledVisit (noLabel).csv | 2 + .../test_getStudyData_noLabels.R | 39 +++++++++++++++++++ 7 files changed, 55 insertions(+) create mode 100644 tests/test_files/files_output/CastorAdverseEvent (noLabel).csv create mode 100644 tests/test_files/files_output/CastorComorbidities (noLabel).csv create mode 100644 tests/test_files/files_output/CastorMedication (noLabel).csv create mode 100644 tests/test_files/files_output/CastorQOLSurvey (noLabel).csv create mode 100644 tests/test_files/files_output/CastorStudy (noLabel).csv create mode 100644 tests/test_files/files_output/CastorUnscheduledVisit (noLabel).csv create mode 100644 tests/testthat/test_getStudyData/test_getStudyData_noLabels.R diff --git a/tests/test_files/files_output/CastorAdverseEvent (noLabel).csv b/tests/test_files/files_output/CastorAdverseEvent (noLabel).csv new file mode 100644 index 0000000..053c14d --- /dev/null +++ b/tests/test_files/files_output/CastorAdverseEvent (noLabel).csv @@ -0,0 +1,3 @@ +participant_id,institute,created_on,custom_name,parent,archived,AE_type,AE_startdate,AE_stopdate,AE_severity,AE_relation,AE_action,AE_outcome,AE_expected,AE_SAE +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:58:13,Adverse event - 22-06-2021 09:58:12,No parent,FALSE,,,,,,,,, +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:55:13,Adverse event - 22-06-2021 09:55:12,Follow-up Visit,FALSE,A bug in the API,23-06-2021,24-06-2021,4,4,2,1,1,1 diff --git a/tests/test_files/files_output/CastorComorbidities (noLabel).csv b/tests/test_files/files_output/CastorComorbidities (noLabel).csv new file mode 100644 index 0000000..1962c39 --- /dev/null +++ b/tests/test_files/files_output/CastorComorbidities (noLabel).csv @@ -0,0 +1,2 @@ +participant_id,institute,created_on,custom_name,parent,archived,comorbidities +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:56:07,Comorbidities - 22-06-2021 09:56:06,No parent,FALSE,-- Uploaded File: comorbidities.txt -- diff --git a/tests/test_files/files_output/CastorMedication (noLabel).csv b/tests/test_files/files_output/CastorMedication (noLabel).csv new file mode 100644 index 0000000..46c64e3 --- /dev/null +++ b/tests/test_files/files_output/CastorMedication (noLabel).csv @@ -0,0 +1,3 @@ +participant_id,institute,created_on,custom_name,parent,archived,med_name,med_start,med_stop,med_dose,med_units,med_other_unit +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:54:28,Medication - 22-06-2021 09:54:28,Unscheduled visits and Medication,FALSE,Diclofenac,25-06-2021,28-06-2021,150,2, +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:58:27,Medication - 22-06-2021 09:58:26,No parent,FALSE,,,,,, diff --git a/tests/test_files/files_output/CastorQOLSurvey (noLabel).csv b/tests/test_files/files_output/CastorQOLSurvey (noLabel).csv new file mode 100644 index 0000000..c157315 --- /dev/null +++ b/tests/test_files/files_output/CastorQOLSurvey (noLabel).csv @@ -0,0 +1,3 @@ +participant_id,institute,survey_name,survey_instance_id,created_on,sent_on,progress,completed_on,package_id,package_name,archived,SF12_1,SF12_2,SF12_3,SF12_12,VAS +110001,Franciscus Gasthuis & Vlietland,QOL Survey,4FF130AD-274C-4C8F-A4A0-A7816A5A88E9,22-06-2021 10:12:19,22-06-2021 10:12:19,100,22-06-2021 10:12:44,0761A9BA-9802-483D-8EB3-D07233A56F2B,My first survey package,FALSE,5,2,2,5,85 +110001,Franciscus Gasthuis & Vlietland,QOL Survey,B99F0082-1B20-456B-8D4D-47D16867B211,22-06-2021 10:13:01,22-06-2021 10:13:01,0,,5B3FE56F-89FA-4C6D-A638-E8FA46113EB0,My first survey package,FALSE,,,,, diff --git a/tests/test_files/files_output/CastorStudy (noLabel).csv b/tests/test_files/files_output/CastorStudy (noLabel).csv new file mode 100644 index 0000000..370aa86 --- /dev/null +++ b/tests/test_files/files_output/CastorStudy (noLabel).csv @@ -0,0 +1,3 @@ +participant_id,archived,institute,randomisation_group,randomisation_datetime,ic_date,ic_versions#Main study CF,ic_versions#Other,ic_main_version,ic_other_version,ic_language#English,ic_language#Dutch,ic_language#French,ic_language#Other,inc_ic,inc_trials,inc_age,inc_dx,inc_criteria,pat_birth_year,pat_sex,pat_race,pat_height,his_cvd,his_diabetes,his_smoke,his_smoke_start,his_smoke_dose,his_family#None,his_family#Unknown,his_family#Deafness,his_family#(Cardio)myopathy,his_family#Encephalopathy,his_family#Diabetes Mellitus,his_family#Hypertension/Cardiovascular disease,his_family#Thrombosis,his_family#Malignancy,randalloc,randdisp,disp_datetime,base_date,onset_stroke,onset_trombectomy,base_weight,base_bmi,base_sbp,base_dbp,base_hr,base_bl_date,base_hb,base_ht,base_leucoc,base_tromboc,base_urea,base_creat,base_CRP,fac_V_leiden_number,fac_V_leiden_date,base_gluc,fu_date,fu_weight,fu_bmi,fu_sbp,fu_dbp,fu_hr,fu_bl_date,fu_hb,fu_ht,fu_leucoc,fu_tromboc,fu_urea,fu_creat,fu_CRP,fu_gluc,unscheduled,conc_med +110001,FALSE,Franciscus Gasthuis & Vlietland,,,22-06-2021,TRUE,FALSE,2.5,,FALSE,FALSE,TRUE,FALSE,1,0,1,0,Yes,1996,0,2,1.75,1,0,0,,,FALSE,FALSE,FALSE,TRUE,FALSE,FALSE,FALSE,FALSE,FALSE,,,,22-06-2021,21-06-2021 01:15,01:30,88,28.7,120,65,66,22-06-2021,7.4,0.23,4.1,252,4.6,88,2.1,55,22-06-2021,5.4,29-06-2021,66,21.6,132,72,69,29-06-2021,8.1,0.36,9.5,366,8.2,99,4.4,3.1,1,1 +110002,FALSE,Franciscus Gasthuis & Vlietland,,,,FALSE,FALSE,,,-99,-99,-99,-99,,,,,,,,,-95,,,,,,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,,,,31-12-2999,,,,,,,,,,,,,,,,-96,01-01-2996,,,,,,,,,,,,,,,,,, diff --git a/tests/test_files/files_output/CastorUnscheduledVisit (noLabel).csv b/tests/test_files/files_output/CastorUnscheduledVisit (noLabel).csv new file mode 100644 index 0000000..9e8ca98 --- /dev/null +++ b/tests/test_files/files_output/CastorUnscheduledVisit (noLabel).csv @@ -0,0 +1,2 @@ +participant_id,institute,created_on,custom_name,parent,archived,unsvis_type,unsvis_date,unsvis_weight,unsvis_sbp,unsvis_dbp,unsvis_hr,unslab_date,unslab_s_hb,unslab_ht,unslab_leucoc,unslab_tromboc,unslab_urea,unslab_creat,unslab_CRP,unslab_gluc +110001,Franciscus Gasthuis & Vlietland,22-06-2021 09:57:38,Unscheduled visit - 22-06-2021 09:57:36,Unscheduled visits and Medication,FALSE,3,24-06-2021,98.3,233,156,112,,,,,,,,, diff --git a/tests/testthat/test_getStudyData/test_getStudyData_noLabels.R b/tests/testthat/test_getStudyData/test_getStudyData_noLabels.R new file mode 100644 index 0000000..6f06126 --- /dev/null +++ b/tests/testthat/test_getStudyData/test_getStudyData_noLabels.R @@ -0,0 +1,39 @@ +creds <- readRDS("../testing_credentials.Rds") +castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) +complete_study_output <- castor_api$getStudyData(creds$output_study, translate_option_values = FALSE) + +test_that("getStudyData returns the expected study data when not translating checkboxes.", { + actual <- complete_study_output$Study + expected <- read.csv("../../test_files/files_output/CastorStudy (noLabel).csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Unscheduled Visit when not translating checkboxes", { + actual <- complete_study_output$`Repeating data`$`Unscheduled visit` + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit (noLabel).csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Medication when not translating checkboxes", { + actual <- complete_study_output$`Repeating data`$Medication + expected <- read.csv("../../test_files/files_output/CastorMedication (noLabel).csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Comorbidities when not translating checkboxes", { + actual <- complete_study_output$`Repeating data`$Comorbidities + expected <- read.csv("../../test_files/files_output/CastorUnscheduledVisit (noLabel).csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the repeated measure Adverse Events when not translating checkboxes", { + actual <- complete_study_output$`Repeating data`$`Adverse event` + expected <- read.csv("../../test_files/files_output/CastorAdverseEvent (noLabel).csv", check.names=F) + expect_identical(actual, expected) +}) + +test_that("getStudyData returns the expected data for the survey Quality of Life when not translating checkboxes", { + actual <- complete_study_output$Surveys$`QOL Survey` + expected <- read.csv("../../test_files/files_output/CastorQOLSurvey (noLabel).csv", check.names=F) + expect_identical(actual, expected) +}) From eae31e1008eacf8630b769d89388dec7b233756c Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 23 Jun 2024 13:37:39 +0200 Subject: [PATCH 20/30] Added tests for getParticipants --- tests/testthat/test_fields.R | 2 +- tests/testthat/test_forms.R | 2 +- tests/testthat/test_instantiation.R | 2 +- tests/testthat/test_participants.R | 60 +++++++++++++++++++-------- tests/testthat/test_repeating_data.R | 2 +- tests/testthat/test_studies.R | 2 +- tests/testthat/test_studydatapoints.R | 2 +- tests/testthat/test_surveys.R | 2 +- tests/testthat/test_visits.R | 2 +- 9 files changed, 50 insertions(+), 26 deletions(-) diff --git a/tests/testthat/test_fields.R b/tests/testthat/test_fields.R index db0167a..8800271 100644 --- a/tests/testthat/test_fields.R +++ b/tests/testthat/test_fields.R @@ -1,4 +1,4 @@ -# Needs updates to tests +# TODO: Needs updates to tests context("Test Field related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_forms.R b/tests/testthat/test_forms.R index 99a5237..47ffe4f 100644 --- a/tests/testthat/test_forms.R +++ b/tests/testthat/test_forms.R @@ -1,4 +1,4 @@ -# Needs updates to tests +# TODO: Needs updates to tests context("Test Form related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_instantiation.R b/tests/testthat/test_instantiation.R index d00e909..bb3da20 100644 --- a/tests/testthat/test_instantiation.R +++ b/tests/testthat/test_instantiation.R @@ -1,4 +1,4 @@ -# Needs updates to tests +# TODO: Needs updates to tests context("Test instantiation and authentication.") test_that("Castor object instantiation produces valid R6 class object.", { diff --git a/tests/testthat/test_participants.R b/tests/testthat/test_participants.R index 01e6529..fcb10d3 100644 --- a/tests/testthat/test_participants.R +++ b/tests/testthat/test_participants.R @@ -1,34 +1,58 @@ -# Needs updates to tests -context("Test Participant related methods.") - creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) test_that("getParticipant returns an appropriate object.", { - participant <- castor_api$getParticipant(creds$example_study, creds$example_participant) + participant <- castor_api$getParticipant(creds$output_study, creds$example_participant) - expect_is(participant, "list") + expect_type(participant, "list") expect_equal(participant$participant_id, creds$example_participant) expect_gt(length(participant), 0) }) +test_that("getParticipant fails appropriately", { + error <- expect_error(castor_api$getParticipant(creds$output_study, creds$fail_participant)) + expect_match(error$message, "Error code: 404") +}) + test_that("getParticipantsPages returns an appropriate object.", { - participants <- castor_api$getParticipantsPages(creds$example_study) - one_participant <- castor_api$getParticipantsPages(creds$example_study, page = 1) + participants <- castor_api$getParticipantsPages(creds$output_study) + expect_type(participants, "list") +}) - expect_is(participants, "list") - expect_is(one_participant, "list") - expect_true(length(one_participant) == 1) - expect_error(castor_api$getParticipantsPages(creds$example_study, page = -1)) - expect_error(castor_api$getParticipantsPages(creds$example_study, page = 100000000)) - expect_error(castor_api$getParticipantsPages(creds$example_study, page = pi)) +test_that("getParticipantsPages returns an appropriate object when retrieving a single page.", { + one_page <- castor_api$getParticipantsPages(creds$output_study, page = 1) + expect_type(one_page, "list") + expect_true(length(one_page) == 1) +}) + +test_that("getParticipantsPages fails appropriately", { + error1 <- expect_error(castor_api$getParticipantsPages(creds$example_study, page = -1)) + expect_match(error1$message, "Error code: 400") + + error2 <- expect_error(castor_api$getParticipantsPages(creds$example_study, page = 100000000)) + expect_match(error2$message, "Error code: 409") + + error3 <- expect_error(castor_api$getParticipantsPages(creds$example_study, page = pi)) + expect_match(error3$message, "page must be an integer") }) test_that("getParticipants returns an appropriate object.", { - participant_data <- castor_api$getParticipants(creds$example_study) + participant_data <- castor_api$getParticipants(creds$output_study) + + expect_s3_class(participant_data, "data.frame") + expect_equal(nrow(participant_data), 2) + expect_equal(ncol(participant_data), 34) +}) + +test_that("getParticipants returns an appropriate object when allowing archived.", { + participant_data <- castor_api$getParticipants(creds$output_study, filter_archived = FALSE) + + expect_s3_class(participant_data, "data.frame") + expect_equal(nrow(participant_data), 3) + expect_equal(ncol(participant_data), 34) +}) - expect_is(participant_data, "data.frame") - expect_gt(nrow(participant_data), 0) - expect_gt(ncol(participant_data), 0) - expect_error(castor_api$getParticipants("this is not a stuy id")) +test_that("getParticipants fails appropriately", { + error <- expect_error(castor_api$getParticipants("THISISNOTASTUDYID")) + expect_match(error$message, "Error code: 404") }) diff --git a/tests/testthat/test_repeating_data.R b/tests/testthat/test_repeating_data.R index ed7652b..68a8a39 100644 --- a/tests/testthat/test_repeating_data.R +++ b/tests/testthat/test_repeating_data.R @@ -1,4 +1,4 @@ -# Needs updates to tests +# TODO: Needs updates to tests context("Test RepeatingData related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_studies.R b/tests/testthat/test_studies.R index b97246d..758d54d 100644 --- a/tests/testthat/test_studies.R +++ b/tests/testthat/test_studies.R @@ -1,4 +1,4 @@ -# Needs updates to tests +# TODO: Needs updates to tests context("Test Study related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_studydatapoints.R b/tests/testthat/test_studydatapoints.R index ae4c80c..1d08fc5 100644 --- a/tests/testthat/test_studydatapoints.R +++ b/tests/testthat/test_studydatapoints.R @@ -1,4 +1,4 @@ -# Needs updates to tests +# TODO: Needs updates to tests context("Test StudyDataPoint related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_surveys.R b/tests/testthat/test_surveys.R index e2eb83b..03b71a5 100644 --- a/tests/testthat/test_surveys.R +++ b/tests/testthat/test_surveys.R @@ -1,4 +1,4 @@ -# Needs updates to tests +# TODO: Needs updates to tests context("Test Survey related methods.") creds <- readRDS("testing_credentials.Rds") diff --git a/tests/testthat/test_visits.R b/tests/testthat/test_visits.R index c4155d3..5d2b0bc 100644 --- a/tests/testthat/test_visits.R +++ b/tests/testthat/test_visits.R @@ -1,4 +1,4 @@ -# Needs updates to tests +# TODO: Needs updates to tests context("Test Visit related methods.") creds <- readRDS("testing_credentials.Rds") From 248d6db9b45687b70c8498b418419a4eb270e8d1 Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 30 Jun 2024 12:24:37 +0200 Subject: [PATCH 21/30] Added field tests --- tests/testthat/test_fields.R | 56 ++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/tests/testthat/test_fields.R b/tests/testthat/test_fields.R index 8800271..d78d09b 100644 --- a/tests/testthat/test_fields.R +++ b/tests/testthat/test_fields.R @@ -1,34 +1,54 @@ -# TODO: Needs updates to tests -context("Test Field related methods.") - creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) test_that("getField returns an appropriate object.", { - field <- castor_api$getField(creds$example_study, creds$example_field) + field <- castor_api$getField(creds$output_study, creds$example_field) expect_is(field, "list") expect_equal(field$field_id, creds$example_field) expect_gt(length(field), 0) }) +test_that("getField fails appropriately", { + error <- expect_error(castor_api$getField(creds$output_study, creds$fail_field)) + expect_match(error$message, "Error code: 404") +}) + + test_that("getFieldsPages returns an appropriate object.", { - fields <- castor_api$getFieldsPages(creds$example_study) - one_field <- castor_api$getFieldsPages(creds$example_study, page = 1) - - expect_is(fields, "list") - expect_is(one_field, "list") - expect_true(length(one_field) == 1) - expect_error(castor_api$getFieldsPages(creds$example_study, page = -1)) - expect_error(castor_api$getFieldsPages(creds$example_study, page = 100000000)) - expect_error(castor_api$getFieldsPages(creds$example_study, page = pi)) + fields <- castor_api$getFieldsPages(creds$output_study) + expect_type(fields, "list") }) +test_that("getFieldPages returns an appropriate object when retrieving a single page.", { + one_page <- castor_api$getFieldsPages(creds$output_study, page = 1) + expect_type(one_page, "list") + expect_true(length(one_page) == 1) +}) + +test_that("getFieldPages fails appropriately", { + error1 <- expect_error(castor_api$getFieldsPages(creds$output_study, page = -1)) + expect_match(error1$message, "Error code: 400") + + error2 <- expect_error(castor_api$getFieldsPages(creds$output_study, page = 100000000)) + expect_match(error2$message, "Error code: 409") + + error3 <- expect_error(castor_api$getFieldsPages(creds$output_study, page = pi)) + expect_match(error3$message, "page must be an integer") +}) + + + test_that("getFields returns an appropriate object.", { - field_data <- castor_api$getFields(creds$example_study) + field_data <- castor_api$getFields(creds$output_study) - expect_is(field_data, "data.frame") - expect_gt(nrow(field_data), 0) - expect_gt(ncol(field_data), 0) - expect_error(castor_api$getFields("this is not a study id")) + expect_s3_class(field_data, "data.frame") + expect_equal(nrow(field_data), 115) + expect_equal(ncol(field_data), 37) }) + +test_that("getFields fails appropriately", { + error <- expect_error(castor_api$getFields("THISISNOTASTUDYID")) + expect_match(error$message, "Error code: 404") +}) + From 8345a2b986b92baff1ada785d6d1050bc9e6df31 Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 30 Jun 2024 12:25:08 +0200 Subject: [PATCH 22/30] Switched to 1 study for all tests --- tests/testthat/test_participants.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test_participants.R b/tests/testthat/test_participants.R index fcb10d3..c2b5831 100644 --- a/tests/testthat/test_participants.R +++ b/tests/testthat/test_participants.R @@ -26,13 +26,13 @@ test_that("getParticipantsPages returns an appropriate object when retrieving a }) test_that("getParticipantsPages fails appropriately", { - error1 <- expect_error(castor_api$getParticipantsPages(creds$example_study, page = -1)) + error1 <- expect_error(castor_api$getParticipantsPages(creds$output_study, page = -1)) expect_match(error1$message, "Error code: 400") - error2 <- expect_error(castor_api$getParticipantsPages(creds$example_study, page = 100000000)) + error2 <- expect_error(castor_api$getParticipantsPages(creds$output_study, page = 100000000)) expect_match(error2$message, "Error code: 409") - error3 <- expect_error(castor_api$getParticipantsPages(creds$example_study, page = pi)) + error3 <- expect_error(castor_api$getParticipantsPages(creds$output_study, page = pi)) expect_match(error3$message, "page must be an integer") }) From 8a122f1cd97916b29bd130fe92cfbeecf247bb7f Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 30 Jun 2024 12:25:50 +0200 Subject: [PATCH 23/30] Switched to 1 study for all tests --- create_testing_credentials.R | 4 ++-- tests/testthat/test_forms.R | 14 +++++++------- tests/testthat/test_repeating_data.R | 28 +++++++++++++-------------- tests/testthat/test_studies.R | 4 ++-- tests/testthat/test_studydatapoints.R | 14 +++++++------- tests/testthat/test_surveys.R | 14 +++++++------- tests/testthat/test_visits.R | 14 +++++++------- 7 files changed, 46 insertions(+), 46 deletions(-) diff --git a/create_testing_credentials.R b/create_testing_credentials.R index 22a96f7..4a8e961 100644 --- a/create_testing_credentials.R +++ b/create_testing_credentials.R @@ -1,7 +1,7 @@ creds = list( client_id = "", client_secret = "", - example_study = "", + output_study = "", base_url = "https://data.castoredc.com", example_field = "", example_visit = "", @@ -14,7 +14,7 @@ creds = list( ) castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) -entire_study <- castor_api$getStudyData(creds$example_study) +entire_study <- castor_api$getStudyData(creds$output_study) study_hash <- digest::digest(entire_study) creds$study_hash <- study_hash diff --git a/tests/testthat/test_forms.R b/tests/testthat/test_forms.R index 47ffe4f..95806c6 100644 --- a/tests/testthat/test_forms.R +++ b/tests/testthat/test_forms.R @@ -5,7 +5,7 @@ creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) test_that("getForm returns an appropriate object.", { - form <- castor_api$getForm(creds$example_study, creds$example_form) + form <- castor_api$getForm(creds$output_study, creds$example_form) expect_is(form, "list") expect_equal(form$form_id, creds$example_form) @@ -13,19 +13,19 @@ test_that("getForm returns an appropriate object.", { }) test_that("getFormsPages returns an appropriate object.", { - forms <- castor_api$getFormsPages(creds$example_study) - one_form <- castor_api$getFormsPages(creds$example_study, page = 1) + forms <- castor_api$getFormsPages(creds$output_study) + one_form <- castor_api$getFormsPages(creds$output_study, page = 1) expect_is(forms, "list") expect_is(one_form, "list") expect_true(length(one_form) == 1) - expect_error(castor_api$getFormsPages(creds$example_study, page = -1)) - expect_error(castor_api$getFormsPages(creds$example_study, page = 100000000)) - expect_error(castor_api$getFormsPages(creds$example_study, page = pi)) + expect_error(castor_api$getFormsPages(creds$output_study, page = -1)) + expect_error(castor_api$getFormsPages(creds$output_study, page = 100000000)) + expect_error(castor_api$getFormsPages(creds$output_study, page = pi)) }) test_that("getForms returns an appropriate object.", { - form_data <- castor_api$getForms(creds$example_study) + form_data <- castor_api$getForms(creds$output_study) expect_is(form_data, "data.frame") expect_gt(nrow(form_data), 0) diff --git a/tests/testthat/test_repeating_data.R b/tests/testthat/test_repeating_data.R index 68a8a39..02c4826 100644 --- a/tests/testthat/test_repeating_data.R +++ b/tests/testthat/test_repeating_data.R @@ -5,7 +5,7 @@ creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) test_that("getRepeatingData returns an appropriate object.", { - repeating_data <- castor_api$getRepeatingData(creds$example_study, creds$example_repeating_data) + repeating_data <- castor_api$getRepeatingData(creds$output_study, creds$example_repeating_data) expect_is(repeating_data, "list") expect_equal(repeating_data$repeating_data_id, creds$example_repeating_data) @@ -13,20 +13,20 @@ test_that("getRepeatingData returns an appropriate object.", { }) test_that("getRepeatingDatasPages returns an appropriate object.", { - repeating_datas <- castor_api$getRepeatingDatasPages(creds$example_study) - one_repeating_data <- castor_api$getRepeatingDatasPages(creds$example_study, page = 1) + repeating_datas <- castor_api$getRepeatingDatasPages(creds$output_study) + one_repeating_data <- castor_api$getRepeatingDatasPages(creds$output_study, page = 1) expect_is(repeating_datas, "list") expect_is(one_repeating_data, "list") expect_true(length(one_repeating_data) == 1) - expect_error(castor_api$getRepeatingDatasPages(creds$example_study, page = -1)) - expect_error(castor_api$getRepeatingDatasPages(creds$example_study, + expect_error(castor_api$getRepeatingDatasPages(creds$output_study, page = -1)) + expect_error(castor_api$getRepeatingDatasPages(creds$output_study, page = 100000000)) - expect_error(castor_api$getRepeatingDatasPages(creds$example_study, page = pi)) + expect_error(castor_api$getRepeatingDatasPages(creds$output_study, page = pi)) }) test_that("getRepeatingDatas returns an appropriate object.", { - repeating_data_data <- castor_api$getRepeatingDatas(creds$example_study) + repeating_data_data <- castor_api$getRepeatingDatas(creds$output_study) expect_is(repeating_data_data, "data.frame") expect_gt(nrow(repeating_data_data), 0) @@ -36,7 +36,7 @@ test_that("getRepeatingDatas returns an appropriate object.", { test_that("getRepeatingDataForm returns an appropriate object.", { - repeating_data_form <- castor_api$getRepeatingDataForm(creds$example_study, + repeating_data_form <- castor_api$getRepeatingDataForm(creds$output_study, creds$example_repeating_data, creds$example_repeating_data_form) @@ -46,28 +46,28 @@ test_that("getRepeatingDataForm returns an appropriate object.", { }) test_that("getRepeatingDataFormsPages returns an appropriate object.", { - repeating_data_forms <- castor_api$getRepeatingDataFormsPages(creds$example_study, + repeating_data_forms <- castor_api$getRepeatingDataFormsPages(creds$output_study, creds$example_repeating_data) - one_repeating_data_form <- castor_api$getRepeatingDataFormsPages(creds$example_study, + one_repeating_data_form <- castor_api$getRepeatingDataFormsPages(creds$output_study, creds$example_repeating_data, page = 1) expect_is(repeating_data_forms, "list") expect_is(one_repeating_data_form, "list") expect_true(length(one_repeating_data_form) == 1) - expect_error(castor_api$getRepeatingDataFormsPages(creds$example_study, + expect_error(castor_api$getRepeatingDataFormsPages(creds$output_study, creds$example_repeating_data, page = -1)) - expect_error(castor_api$getRepeatingDataFormsPages(creds$example_study, + expect_error(castor_api$getRepeatingDataFormsPages(creds$output_study, creds$example_repeating_data, page = 100000000)) - expect_error(castor_api$getRepeatingDataFormsPages(creds$example_study, + expect_error(castor_api$getRepeatingDataFormsPages(creds$output_study, creds$example_repeating_data, page = pi)) }) test_that("getRepeatingDataForms returns an appropriate object.", { - repeating_data_form_data <- castor_api$getRepeatingDataForms(creds$example_study) + repeating_data_form_data <- castor_api$getRepeatingDataForms(creds$output_study) expect_is(repeating_data_form_data, "data.frame") expect_gt(nrow(repeating_data_form_data), 0) diff --git a/tests/testthat/test_studies.R b/tests/testthat/test_studies.R index 758d54d..26ded13 100644 --- a/tests/testthat/test_studies.R +++ b/tests/testthat/test_studies.R @@ -5,10 +5,10 @@ creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) test_that("getStudy returns an appropriate object.", { - study <- castor_api$getStudy(creds$example_study) + study <- castor_api$getStudy(creds$output_study) expect_is(study, "list") - expect_equal(study$study_id, creds$example_study) + expect_equal(study$study_id, creds$output_study) expect_gt(length(study), 0) }) diff --git a/tests/testthat/test_studydatapoints.R b/tests/testthat/test_studydatapoints.R index 1d08fc5..f595434 100644 --- a/tests/testthat/test_studydatapoints.R +++ b/tests/testthat/test_studydatapoints.R @@ -5,7 +5,7 @@ creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) test_that("getStudyDataPoint returns an appropriate object.", { - sdp <- castor_api$getStudyDataPoint(creds$example_study, + sdp <- castor_api$getStudyDataPoint(creds$output_study, creds$example_participant, creds$example_field) @@ -16,28 +16,28 @@ test_that("getStudyDataPoint returns an appropriate object.", { }) test_that("getStudyDataPointsPages returns an appropriate object.", { - sdps <- castor_api$getStudyDataPointsPages(creds$example_study, + sdps <- castor_api$getStudyDataPointsPages(creds$output_study, creds$example_participant) - one_sdp <- castor_api$getStudyDataPointsPages(creds$example_study, + one_sdp <- castor_api$getStudyDataPointsPages(creds$output_study, creds$example_participant, page = 1) expect_is(sdps, "list") expect_is(one_sdp, "list") expect_true(length(one_sdp) == 1) - expect_error(castor_api$getStudyDataPointsPages(creds$example_study, + expect_error(castor_api$getStudyDataPointsPages(creds$output_study, creds$example_participant, page = -1)) - expect_error(castor_api$getStudyDataPointsPages(creds$example_study, + expect_error(castor_api$getStudyDataPointsPages(creds$output_study, creds$example_participant, page = 100000000)) - expect_error(castor_api$getStudyDataPointsPages(creds$example_study, + expect_error(castor_api$getStudyDataPointsPages(creds$output_study, creds$example_participant, page = pi)) }) test_that("getStudyDataPoints returns an appropriate object.", { - sdp_data <- castor_api$getStudyDataPoints(creds$example_study, + sdp_data <- castor_api$getStudyDataPoints(creds$output_study, creds$example_participant, filter_types = creds$filter_vals) diff --git a/tests/testthat/test_surveys.R b/tests/testthat/test_surveys.R index 03b71a5..83384b4 100644 --- a/tests/testthat/test_surveys.R +++ b/tests/testthat/test_surveys.R @@ -5,7 +5,7 @@ creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) test_that("getSurvey returns an appropriate object.", { - survey <- castor_api$getSurvey(creds$example_study, creds$example_survey) + survey <- castor_api$getSurvey(creds$output_study, creds$example_survey) expect_is(survey, "list") expect_equal(survey$survey_id, creds$example_survey) @@ -13,19 +13,19 @@ test_that("getSurvey returns an appropriate object.", { }) test_that("getSurveysPages returns an appropriate object.", { - surveys <- castor_api$getSurveysPages(creds$example_study) - one_survey <- castor_api$getSurveysPages(creds$example_study, page = 1) + surveys <- castor_api$getSurveysPages(creds$output_study) + one_survey <- castor_api$getSurveysPages(creds$output_study, page = 1) expect_is(surveys, "list") expect_is(one_survey, "list") expect_true(length(one_survey) == 1) - expect_error(castor_api$getSurveysPages(creds$example_study, page = -1)) - expect_error(castor_api$getSurveysPages(creds$example_study, page = 100000000)) - expect_error(castor_api$getSurveysPages(creds$example_study, page = pi)) + expect_error(castor_api$getSurveysPages(creds$output_study, page = -1)) + expect_error(castor_api$getSurveysPages(creds$output_study, page = 100000000)) + expect_error(castor_api$getSurveysPages(creds$output_study, page = pi)) }) test_that("getSurveys returns an appropriate object.", { - survey_data <- castor_api$getSurveys(creds$example_study) + survey_data <- castor_api$getSurveys(creds$output_study) expect_is(survey_data, "data.frame") expect_gt(nrow(survey_data), 0) diff --git a/tests/testthat/test_visits.R b/tests/testthat/test_visits.R index 5d2b0bc..f0f7959 100644 --- a/tests/testthat/test_visits.R +++ b/tests/testthat/test_visits.R @@ -5,7 +5,7 @@ creds <- readRDS("testing_credentials.Rds") castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, base_url=creds$base_url) test_that("getVisit returns an appropriate object.", { - visit <- castor_api$getVisit(creds$example_study, creds$example_visit) + visit <- castor_api$getVisit(creds$output_study, creds$example_visit) expect_is(visit, "list") expect_equal(visit$visit_id, creds$example_visit) @@ -13,19 +13,19 @@ test_that("getVisit returns an appropriate object.", { }) test_that("getVisitsPages returns an appropriate object.", { - visits <- castor_api$getVisitsPages(creds$example_study) - one_visit <- castor_api$getVisitsPages(creds$example_study, page = 1) + visits <- castor_api$getVisitsPages(creds$output_study) + one_visit <- castor_api$getVisitsPages(creds$output_study, page = 1) expect_is(visits, "list") expect_is(one_visit, "list") expect_true(length(one_visit) == 1) - expect_error(castor_api$getVisitsPages(creds$example_study, page = -1)) - expect_error(castor_api$getVisitsPages(creds$example_study, page = 100000000)) - expect_error(castor_api$getVisitsPages(creds$example_study, page = pi)) + expect_error(castor_api$getVisitsPages(creds$output_study, page = -1)) + expect_error(castor_api$getVisitsPages(creds$output_study, page = 100000000)) + expect_error(castor_api$getVisitsPages(creds$output_study, page = pi)) }) test_that("getVisits returns an appropriate object.", { - visit_data <- castor_api$getVisits(creds$example_study) + visit_data <- castor_api$getVisits(creds$output_study) expect_is(visit_data, "data.frame") expect_gt(nrow(visit_data), 0) From 3aedf8d230c96b26191f8b3feafc995a89e418dc Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 30 Jun 2024 12:45:40 +0200 Subject: [PATCH 24/30] Added tests for getStudyDataBulk --- .../files_output/.~lock.CastorStudy.csv# | 1 + tests/testthat/test_getStudyDataBulk.R | 99 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 tests/test_files/files_output/.~lock.CastorStudy.csv# create mode 100644 tests/testthat/test_getStudyDataBulk.R diff --git a/tests/test_files/files_output/.~lock.CastorStudy.csv# b/tests/test_files/files_output/.~lock.CastorStudy.csv# new file mode 100644 index 0000000..38d080f --- /dev/null +++ b/tests/test_files/files_output/.~lock.CastorStudy.csv# @@ -0,0 +1 @@ +,reinier,Reinier-PC,30.06.2024 12:37,file:///home/reinier/.config/libreoffice/4; \ No newline at end of file diff --git a/tests/testthat/test_getStudyDataBulk.R b/tests/testthat/test_getStudyDataBulk.R new file mode 100644 index 0000000..f5e435c --- /dev/null +++ b/tests/testthat/test_getStudyDataBulk.R @@ -0,0 +1,99 @@ +creds <- readRDS("testing_credentials.Rds") +castor_api <- CastorData$new( + key = creds$client_id, + secret = creds$client_secret, + base_url = creds$base_url +) + +test_that("getStudyDataPointsBulk returns an appropriate object.", { + study_data_points_bulk <- castor_api$getStudyDataPointsBulk(creds$output_study) + + expect_s3_class(study_data_points_bulk, "data.frame") + expect_equal(nrow(study_data_points_bulk), 56) + expect_equal(ncol(study_data_points_bulk), 4) +}) + +test_that("getStudyDataPointsBulk fails appropriately", { + error <- expect_error(castor_api$getStudyDataPointsBulk("THISISNOTASTUDYID")) + expect_match(error$message, "Error code: 404") +}) + + +test_that("getStudyDataBulk returns an appropriate object.", { + field_info <- castor_api$getFields(output_study) + participant_info <- castor_api$getParticipants(output_study) + study_data_bulk <- castor_api$getStudyDataBulk(creds$output_study, field_info, participant_info) + + expect_s3_class(study_data_bulk, "data.frame") + expect_equal(nrow(study_data_bulk), 2) + expect_equal(ncol(study_data_bulk), 57) + expect_named( + study_data_bulk, + c( + "participant_id", + "archived", + "randomisation_group" , + "randomisation_datetime" , + "institute" , + "base_bl_date", + "base_bmi" , + "base_creat" , + "base_CRP" , + "base_date" , + "base_dbp" , + "base_gluc" , + "base_hb", + "base_hr" , + "base_ht" , + "base_leucoc" , + "base_sbp" , + "base_tromboc" , + "base_urea" , + "base_weight", + "conc_med" , + "fac_V_leiden" , + "fu_bl_date" , + "fu_bmi" , + "fu_creat" , + "fu_CRP" , + "fu_date", + "fu_dbp" , + "fu_gluc" , + "fu_hb" , + "fu_hr" , + "fu_ht" , + "fu_leucoc" , + "fu_sbp", + "fu_tromboc" , + "fu_urea" , + "fu_weight" , + "his_cvd" , + "his_diabetes" , + "his_family" , + "his_smoke", + "ic_date" , + "ic_language" , + "ic_main_version" , + "ic_versions" , + "inc_age" , + "inc_criteria" , + "inc_dx", + "inc_ic" , + "inc_trials" , + "onset_stroke" , + "onset_trombectomy" , + "pat_birth_year" , + "pat_height" , + "pat_race", + "pat_sex" , + "unscheduled" + ) + ) +}) + +test_that("getStudyDataBulk fails appropriately", { + field_info <- castor_api$getFields(output_study) + participant_info <- castor_api$getParticipants(output_study) + error <- expect_error(castor_api$getStudyDataBulk("THISISNOTASTUDYID", field_info, participant_info)) + expect_match(error$message, "Error code: 404") +}) From ecfd48ad9fcf7d089bc8ec7373f934ad044943a0 Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 30 Jun 2024 12:47:18 +0200 Subject: [PATCH 25/30] Small bugfixes so getStudyDataBulk and getFields tests run --- tests/testthat/test_fields.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test_fields.R b/tests/testthat/test_fields.R index d78d09b..f7bb40f 100644 --- a/tests/testthat/test_fields.R +++ b/tests/testthat/test_fields.R @@ -4,7 +4,7 @@ castor_api <- CastorData$new(key=creds$client_id, secret=creds$client_secret, ba test_that("getField returns an appropriate object.", { field <- castor_api$getField(creds$output_study, creds$example_field) - expect_is(field, "list") + expect_type(field, "list") expect_equal(field$field_id, creds$example_field) expect_gt(length(field), 0) }) From a329be90c0be52ae98229169e7626ce5cad450c4 Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 30 Jun 2024 12:51:16 +0200 Subject: [PATCH 26/30] Add getStudyDataPoints tests --- tests/testthat/test_getStudyDataPoints.R | 81 ++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/testthat/test_getStudyDataPoints.R diff --git a/tests/testthat/test_getStudyDataPoints.R b/tests/testthat/test_getStudyDataPoints.R new file mode 100644 index 0000000..da212c5 --- /dev/null +++ b/tests/testthat/test_getStudyDataPoints.R @@ -0,0 +1,81 @@ +creds <- readRDS("testing_credentials.Rds") +castor_api <- CastorData$new( + key = creds$client_id, + secret = creds$client_secret, + base_url = creds$base_url +) + +test_that("getStudyDataPoints returns an appropriate object.", { + study_data_points <- castor_api$getStudyDataPoints(creds$output_study, creds$example_participant) + + expect_s3_class(study_data_points, "data.frame") + expect_equal(nrow(study_data_points), 1) + expect_equal(ncol(study_data_points), 57) + expect_named( + study_data_points, + c( + "participant_id", + "archived", + "randomisation_group" , + "randomisation_datetime" , + "institute" , + "base_bl_date", + "base_bmi" , + "base_creat" , + "base_CRP" , + "base_date" , + "base_dbp" , + "base_gluc" , + "base_hb", + "base_hr" , + "base_ht" , + "base_leucoc" , + "base_sbp" , + "base_tromboc" , + "base_urea" , + "base_weight", + "conc_med" , + "fac_V_leiden" , + "fu_bl_date" , + "fu_bmi" , + "fu_creat" , + "fu_CRP" , + "fu_date", + "fu_dbp" , + "fu_gluc" , + "fu_hb" , + "fu_hr" , + "fu_ht" , + "fu_leucoc" , + "fu_sbp", + "fu_tromboc" , + "fu_urea" , + "fu_weight" , + "his_cvd" , + "his_diabetes" , + "his_family" , + "his_smoke", + "ic_date" , + "ic_language" , + "ic_main_version" , + "ic_versions" , + "inc_age" , + "inc_criteria" , + "inc_dx", + "inc_ic" , + "inc_trials" , + "onset_stroke" , + "onset_trombectomy" , + "pat_birth_year" , + "pat_height" , + "pat_race", + "pat_sex" , + "unscheduled" + ) + ) +}) + +test_that("getStudyDataPoints fails appropriately", { + error <- expect_error(castor_api$getStudyDataPoints("THISISNOTASTUDYID", creds$example_participant)) + expect_match(error$message, "Error code: 404") +}) From 53bb7b27f821ca10c93698381d0ff1feff1ad4ab Mon Sep 17 00:00:00 2001 From: Reinier Date: Sun, 30 Jun 2024 12:53:37 +0200 Subject: [PATCH 27/30] Naming convention of test files --- .../{test_getStudyDataBulk.R => test_get_study_data_bulk.R} | 0 .../{test_getStudyDataPoints.R => test_get_study_data_points.R} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tests/testthat/{test_getStudyDataBulk.R => test_get_study_data_bulk.R} (100%) rename tests/testthat/{test_getStudyDataPoints.R => test_get_study_data_points.R} (100%) diff --git a/tests/testthat/test_getStudyDataBulk.R b/tests/testthat/test_get_study_data_bulk.R similarity index 100% rename from tests/testthat/test_getStudyDataBulk.R rename to tests/testthat/test_get_study_data_bulk.R diff --git a/tests/testthat/test_getStudyDataPoints.R b/tests/testthat/test_get_study_data_points.R similarity index 100% rename from tests/testthat/test_getStudyDataPoints.R rename to tests/testthat/test_get_study_data_points.R From 84662ad18321242b0a5e94d4fd6b87837f9470f6 Mon Sep 17 00:00:00 2001 From: Reinier Date: Wed, 3 Jul 2024 21:02:33 +0200 Subject: [PATCH 28/30] Fixed tests for getStudyDataBulk and getStudyDataPoints --- R/CastorData.R | 105 ++++++++++-------- .../files_output/.~lock.CastorStudy.csv# | 1 - tests/testthat/test_get_study_data_bulk.R | 11 +- tests/testthat/test_get_study_data_points.R | 3 +- 4 files changed, 65 insertions(+), 55 deletions(-) delete mode 100644 tests/test_files/files_output/.~lock.CastorStudy.csv# diff --git a/R/CastorData.R b/R/CastorData.R index 0a159b7..7c4c9e5 100644 --- a/R/CastorData.R +++ b/R/CastorData.R @@ -498,15 +498,19 @@ CastorData <- R6::R6Class("CastorData", "returning data frame with just participant metadata.") empty.df <- data.frame( - "Site Abbreviation" = - participant_metadata[["_embedded"]][["site"]][["abbreviation"]], - "Randomization Group" = - ifelse(is.null(participant_metadata[["randomization_group"]]), NA, - participant_metadata[["randomization_group"]]), - "Participant Creation" = participant_metadata[["created_on"]][["date"]], - "Participant_ID" = participant_id + "institute" = + participant_metadata[["_embedded"]][["site"]][["name"]], + "randomisation_group" = + ifelse(is.null(participant_metadata[["randomization_group_name"]]), + NA, + participant_metadata[["randomization_group_name"]]), + "participant_id" = participant_id, + "archived" = participant_metadata[["archived"]], + "randomisation_datetime" = + ifelse(is.null(participant_metadata[["randomizated_on"]]), + NA, + participant_metadata[["randomizated_on"]]) ) - return(empty.df) } # Filter out any field types that are specified in the filter_types @@ -524,54 +528,59 @@ CastorData <- R6::R6Class("CastorData", stringsAsFactors = FALSE) # Add the participant id as a column to the data. - study_data_points.df[["Participant_ID"]] <- participant_id + study_data_points.df[["participant_id"]] <- participant_id # Randomization ID should be in participant data. - study_data_points.df[["Site_Abbreviation"]] <- - participant_metadata[["_embedded"]][["site"]][["abbreviation"]] - study_data_points.df[["Randomization_Group"]] <- - ifelse(is.null(participant_metadata[["randomization_group"]]), + study_data_points.df[["institute"]] <- + participant_metadata[["_embedded"]][["site"]][["name"]] + study_data_points.df[["archived"]] <- + participant_metadata[["archived"]] + study_data_points.df[["randomisation_group"]] <- + ifelse(is.null(participant_metadata[["randomization_group_name"]]), + NA, + participant_metadata[["randomization_group_name"]]) + study_data_points.df[["randomisation_datetime"]] <- + ifelse(is.null(participant_metadata[["randomizated_on"]]), NA, - participant_metadata[["randomization_group"]]) - study_data_points.df[["Participant_Creation"]] <- - participant_metadata[["created_on"]][["date"]] + participant_metadata[["randomizated_on"]]) return(study_data_points.df) }, - getStudyDataBulk = function(study_id., field_info., participant_metadata) { - study_data <- self$getStudyDataPointsBulk(study_id.) - if (isTRUE(nrow(study_data) > 0)) { - study_data_field_info <- distinct(left_join(study_data, field_info., by = "field_id")) - study_data_long <- select(study_data_field_info, - field_variable_name, participant_id, field_value) - study_data_wide <- spread(study_data_long, - field_variable_name, field_value) - study_data_complete_cases <- filter_all(study_data_wide, - any_vars(!is.na(.))) - - # Add randomized on date for studies without randomization - # Is not retrieved from API, but needs to exist for further steps - if (!("randomized_on.date" %in% names(participant_metadata))) { - participant_metadata <- participant_metadata %>% - mutate(randomized_on.date = NA_character_) - } + getStudyDataBulk = function(study_id, field_info, participant_metadata) { + study_data <- self$getStudyDataPointsBulk(study_id) + + if (nrow(study_data) > 0) { + study_data <- left_join(select(study_data, -updated_on), + select(field_info, c(field_variable_name, field_id)), + by = "field_id") - rename( - left_join( - select( - participant_metadata, - participant_id, - Randomization_Group = randomization_group, - Randomization_Group_Name = randomization_group_name, - Randomized_On = randomized_on.date, - Site_Abbreviation = `_embedded.site.abbreviation`, - Participant_Creation = created_on.date), - study_data_complete_cases, - by="participant_id" + study_data <- pivot_wider(select(study_data, -field_id), + names_from = field_variable_name, + values_from = field_value) + + study_data <- left_join( + select( + participant_metadata, + participant_id, + `_embedded.site.name`, + archived, + randomization_group_name, + randomized_on, ), - Participant_ID = participant_id + study_data, + by = "participant_id" + ) + + study_data <- rename(study_data, + randomisation_group = randomization_group_name, + randomisation_datetime = randomized_on, + institute = `_embedded.site.name` ) + + study_data + } else + # TODO: hier verder, tests runnen nog niet, en bij 0 rows het hier verwerken ipv in getStudyDat NULL }, generateFieldMetadata = function(study_id, field_info) { @@ -683,8 +692,8 @@ CastorData <- R6::R6Class("CastorData", all_data_points.df <- self$getStudyDataBulk(study_id, field_info, participant_metadata) } else { - # Get study data from getStudyDataPoints and collect them by participant in a - # list. + # Get study data from getStudyDataPoints + # Collect them by participant in a list study_data <- lapply(participant_metadata$participant_id, function(participant) { if (self$verbose) message("getting participant ", participant) return(self$getStudyDataPoints(study_id, participant, filter_types)) diff --git a/tests/test_files/files_output/.~lock.CastorStudy.csv# b/tests/test_files/files_output/.~lock.CastorStudy.csv# deleted file mode 100644 index 38d080f..0000000 --- a/tests/test_files/files_output/.~lock.CastorStudy.csv# +++ /dev/null @@ -1 +0,0 @@ -,reinier,Reinier-PC,30.06.2024 12:37,file:///home/reinier/.config/libreoffice/4; \ No newline at end of file diff --git a/tests/testthat/test_get_study_data_bulk.R b/tests/testthat/test_get_study_data_bulk.R index f5e435c..d1e6020 100644 --- a/tests/testthat/test_get_study_data_bulk.R +++ b/tests/testthat/test_get_study_data_bulk.R @@ -20,8 +20,8 @@ test_that("getStudyDataPointsBulk fails appropriately", { test_that("getStudyDataBulk returns an appropriate object.", { - field_info <- castor_api$getFields(output_study) - participant_info <- castor_api$getParticipants(output_study) + field_info <- castor_api$getFields(creds$output_study) + participant_info <- castor_api$getParticipants(creds$output_study) study_data_bulk <- castor_api$getStudyDataBulk(creds$output_study, field_info, participant_info) expect_s3_class(study_data_bulk, "data.frame") @@ -87,13 +87,14 @@ test_that("getStudyDataBulk returns an appropriate object.", { "pat_race", "pat_sex" , "unscheduled" - ) + ), + ignore.order=T ) }) test_that("getStudyDataBulk fails appropriately", { - field_info <- castor_api$getFields(output_study) - participant_info <- castor_api$getParticipants(output_study) + field_info <- castor_api$getFields(creds$output_study) + participant_info <- castor_api$getParticipants(creds$output_study) error <- expect_error(castor_api$getStudyDataBulk("THISISNOTASTUDYID", field_info, participant_info)) expect_match(error$message, "Error code: 404") }) diff --git a/tests/testthat/test_get_study_data_points.R b/tests/testthat/test_get_study_data_points.R index da212c5..e4139e7 100644 --- a/tests/testthat/test_get_study_data_points.R +++ b/tests/testthat/test_get_study_data_points.R @@ -71,7 +71,8 @@ test_that("getStudyDataPoints returns an appropriate object.", { "pat_race", "pat_sex" , "unscheduled" - ) + ), + ignore.order=T ) }) From bba2a83ce45a7d5ac461af418f323fee05ed8752 Mon Sep 17 00:00:00 2001 From: Reinier Date: Wed, 3 Jul 2024 21:08:23 +0200 Subject: [PATCH 29/30] Refactored getStudyDataBulk to return participant metadata only when there is no study data, and to extract field_info and participant_metadata when it is not given to the function --- R/CastorData.R | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/R/CastorData.R b/R/CastorData.R index 7c4c9e5..106bff9 100644 --- a/R/CastorData.R +++ b/R/CastorData.R @@ -546,7 +546,9 @@ CastorData <- R6::R6Class("CastorData", return(study_data_points.df) }, - getStudyDataBulk = function(study_id, field_info, participant_metadata) { + getStudyDataBulk = function(study_id, field_info=NULL, participant_metadata=NULL) { + if (is.null(field_info)) {self$getFields(study_id)} + if (is.null(participant_metadata)) {self$getParticipants(study_id)} study_data <- self$getStudyDataPointsBulk(study_id) if (nrow(study_data) > 0) { @@ -579,9 +581,23 @@ CastorData <- R6::R6Class("CastorData", study_data - } else - # TODO: hier verder, tests runnen nog niet, en bij 0 rows het hier verwerken ipv in getStudyDat - NULL + } else { + # If no study data, only return participant information + study_data <- + rename(select( + participant_metadata, + participant_id, + `_embedded.site.name`, + archived, + randomization_group_name, + randomized_on, + ), + randomisation_group = randomization_group_name, + randomisation_datetime = randomized_on, + institute = `_embedded.site.name` + ) + } + study_data }, generateFieldMetadata = function(study_id, field_info) { forms <- self$getForms(study_id) @@ -702,10 +718,6 @@ CastorData <- R6::R6Class("CastorData", all_data_points.df <- bind_rows(study_data) } } else { - all_data_points.df <- NULL - } - - if (is.null(all_data_points.df)) { all_data_points.df <- rename( select( participant_metadata, From 6c52a1bbfcda4b4a8d19e83ad8af7a59ca534e8f Mon Sep 17 00:00:00 2001 From: Reinier Date: Wed, 3 Jul 2024 21:20:57 +0200 Subject: [PATCH 30/30] Added documentation --- R/CastorData.R | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/R/CastorData.R b/R/CastorData.R index 106bff9..5a123fd 100644 --- a/R/CastorData.R +++ b/R/CastorData.R @@ -547,19 +547,27 @@ CastorData <- R6::R6Class("CastorData", return(study_data_points.df) }, getStudyDataBulk = function(study_id, field_info=NULL, participant_metadata=NULL) { + # Extract the necessary info that wasnt given to the function if (is.null(field_info)) {self$getFields(study_id)} if (is.null(participant_metadata)) {self$getParticipants(study_id)} + + # Extract all study data points in long format + # One row per datapoint study_data <- self$getStudyDataPointsBulk(study_id) + # If there is study data if (nrow(study_data) > 0) { + # Add the field names to the field ids and values in long format study_data <- left_join(select(study_data, -updated_on), select(field_info, c(field_variable_name, field_id)), by = "field_id") + # Move from long (each data point has a row) to wide format per participant study_data <- pivot_wider(select(study_data, -field_id), names_from = field_variable_name, values_from = field_value) + # Add participant metadata to the dataframe study_data <- left_join( select( participant_metadata, @@ -578,9 +586,6 @@ CastorData <- R6::R6Class("CastorData", randomisation_datetime = randomized_on, institute = `_embedded.site.name` ) - - study_data - } else { # If no study data, only return participant information study_data <-