Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Test #26

Merged
merged 2 commits into from
Feb 14, 2024
Merged

Test #26

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export(risk_alcohol)
export(risk_colorectal_screening)
export(risk_diet_exercise)
export(risk_smoking)
export(risk_vaccine)
export(risk_vaccines)
export(risk_whealth)
importFrom(cdlTools,fips)
importFrom(cli,cli_abort)
Expand Down
2 changes: 1 addition & 1 deletion R/handle-vaccine.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ handle_vaccine <- function(vaccine) {
# "percent who received 3+ doses of hpv vaccine, ages 13-17" = "v71"

"percent with up to date hpv vaccination coverage, ages 13-15" = "v281",
"percent with up to date hpv vaccination coverage, ages 13-15" = "v282"
"percent with up to date hpv vaccination coverage, ages 13-17" = "v282"
)

vaccine_code <- vaccine_mapping[vaccine]
Expand Down
5 changes: 3 additions & 2 deletions R/risk-smoking.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ risk_smoking <- function(smoking, race=NULL, sex=NULL, datatype=NULL, area=NULL)

#smoking group 4
if (smoking %in% smoking_group4 && ((is.null(sex) || is.null(area) || is.null(datatype)) || !is.null(race))) {
cli_abort("For this smoking type, Sex, Datatype, and Area must not be NULL AND Race and Datatype must be NULL")
cli_abort("For this smoking type, Sex, Datatype, and Area must not be NULL AND Race must be NULL")
} else if (smoking %in% smoking_group4 && datatype == "direct estimates") {
cli_abort("For this smoking type, Datatype must be county level modeled estimates")
}
Expand All @@ -111,6 +111,7 @@ risk_smoking <- function(smoking, race=NULL, sex=NULL, datatype=NULL, area=NULL)
cli_abort("For this smoking type, Datatype must be direct estimates")
}

#smoking group 6
if (smoking %in% smoking_group6 && (is.null(race) || is.null(sex))) {
cli_abort("For this smoking group, Race and Sex must not be NULL")
} else if (smoking %in% smoking_group6 && (!is.null(race) && !is.null(sex)) && race == "all races (includes hispanic)") {
Expand Down Expand Up @@ -161,7 +162,7 @@ risk_smoking <- function(smoking, race=NULL, sex=NULL, datatype=NULL, area=NULL)

if (smoking %in% smoking_group1) {
resp %>%
setNames(c("State", "FIPS", "Percent", "Number_of_Respondents"))
setNames(c("State", "FIPS", "Percent"))
} else if ((smoking %in% c(smoking_group2, smoking_group3, smoking_group4, smoking_group5, smoking_group6))
&& (datatype=="direct estimates")) {
resp %>%
Expand Down
16 changes: 5 additions & 11 deletions R/risk-vaccines.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
#'
#' This function returns a data frame from Vaccines in State Cancer Profiles
#'
#' @param vaccine Either "mammogram in past 2 years, ages 50-74", "mammogram in past 2 years, ages 40+",
#' "pap smear in past 3 years, no hysterectomy, ages 21-65",
#' "pap smear in past 3 years, no hysterectomy, ages 18+"
#' @param vaccine Either "percent with up to date hpv vaccination coverage, ages 13-15",
#' "percent with up to date hpv vaccination coverage, ages 13-17"
#' @param sex Either "both sexes", "males", "females"
#'
#' @returns A data frame with the following columns "State", "FIPS", "Percent", "Lower 95% CI", "Upper 95% CI", "Number of Respondents"
Expand All @@ -13,11 +12,10 @@
#'
#' @examples
#' \dontrun{
#' risk_vaccine("percent with up to date hpv vaccination coverage, ages 13-15", "both sexes")
#' risk_vaccine("percent with up to date hpv vaccination coverage, ages 13-15", "both sexes")
#' risk_vaccine("percent with up to date hpv vaccination coverage, ages 13-15", "females")
#' risk_vaccines("percent with up to date hpv vaccination coverage, ages 13-15", "both sexes")
#' risk_vaccines("percent with up to date hpv vaccination coverage, ages 13-17", "females")
#' }
risk_vaccine <- function(vaccine, sex) {
risk_vaccines <- function(vaccine, sex) {

req <- create_request("risk")

Expand All @@ -42,10 +40,6 @@ risk_vaccine <- function(vaccine, sex) {
"percent who received 3+ doses of HPV vaccine, ages 13-15"
)

vaccine_type2 = c("percent who received 2+ doses of HPV vaccine, ages 13-17",
"percent who received 3+ doses of HPV vaccine, ages 13-17"
)

if (vaccine %in% vaccine_type1) {
resp %>%
setNames(c("State", "FIPS", "Met_Objective_of_80.0%?", "Percent", "Lower_95%_CI", "Upper_95%_CI", "Number_of_Respondents"))
Expand Down
4 changes: 2 additions & 2 deletions R/risk-womens-health.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' This function returns a data frame from Women's Health in State Cancer Profiles
#'
#' @param whealth Either "mammogram in past 2 years, ages 50-74", "mammogram in past 2 years, ages 40+",
#' "pap smear in past 3 years, no hysterectomy, ages 21-65", "pap smear in past 3 years, no hysterectomy, ages 18+"
#' "pap smear in past 3 years, no hysterectomy, ages 21-65"
#' @param race One of the following values: "all races (includes hispanic)", "white (non-hispanic)",
#' "black (non-hispanic)", "amer. indian / ak native (non-hispanic)",
#' "asian / pacific islander (non-hispanic)","hispanic (any race)"
Expand Down Expand Up @@ -65,7 +65,7 @@ risk_whealth <- function(whealth, race, datatype="direct estimates", area=NULL)
resp <- process_screening(resp)

if (datatype == "county level modeled estimates") {
if(whealth == "pap smear in past 3 years, no hysterectomy, ages 18+") {
if(whealth == "pap smear in past 3 years, no hysterectomy, ages 21-65") {
resp %>%
setNames(c("State", "FIPS", "Percent", "Lower_95%_CI", "Upper_95%_CI", "Number_of_Respondents"))
} else {
Expand Down
28 changes: 0 additions & 28 deletions man/risk_vaccine.Rd

This file was deleted.

26 changes: 26 additions & 0 deletions man/risk_vaccines.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/risk_whealth.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

137 changes: 135 additions & 2 deletions tests/testthat/test-risk-smoking.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,136 @@
test_that("multiplication works", {
expect_equal(2 * 2, 4)
#' Test risk-smoking
#'
#' This testthat file test the risk-smoking function
#'
#tests class and typeof output
test_that("Output data type is correct", {
output <- risk_smoking("smoking laws (any)")

expect_true(inherits(output, "data.frame"))
})

#Ensures that variables are present and working on SCP

#group1
smoking_group1_options <- c("smoking laws (any)",
"smoking laws (bars)",
"smoking laws (restaurants)",
"smoking laws (workplace)",
"smoking laws (workplace; restaurant; & bar)")

for (option in smoking_group1_options) {
test_that("smoking group1 returns non-empty data frame", {
result <- risk_smoking(option)
expect_true(is.data.frame(result))
})
}

#group2
smoking_group2_options <- c("smokers (stopped for 1 day or longer)",
"smoking not allowed at work (all people)",
"smoking not allowed in home (all people)")

for (option in smoking_group2_options) {
test_that("smoking group2 returns non-empty data frame", {
result <- risk_smoking(option, sex="both sexes", datatype="direct estimates")
expect_true(is.data.frame(result))
})
}

#group3
smoking_group3_options <- c("smoking not allowed at work (current smokers)",
"smoking not allowed at work (former/never smokers)",
"smoking not allowed in home (current smokers)",
"smoking not allowed in home (former/never smokers)")

for (option in smoking_group3_options) {
test_that("smoking group3 returns non-empty data frame", {
result <- risk_smoking(option, sex="both sexes", datatype="direct estimates")
expect_true(is.data.frame(result))
})
}

#group4
smoking_group4_options <- c("former smoker; ages 18+",
"former smoker, quit 1 year+; ages 18+")

for (option in smoking_group4_options) {
test_that("smoking group4 returns non-empty data frame", {
result <- risk_smoking(option, sex="both sexes",
datatype="county level modeled estimates", area="ca")
expect_true(is.data.frame(result))
})
}

#group5
smoking_group5_options <- c("smokers (ever); ages 18+",
"e-cigarette use; ages 18+")

for (option in smoking_group5_options) {
test_that("smoking group5 returns non-empty data frame", {
result <- risk_smoking(option, race="hispanic (any race)",
sex="both sexes", datatype="direct estimates")
expect_true(is.data.frame(result))
})
}

#group6
test_that("smoking group6 returns non-empty data frame", {
result <- risk_smoking("smokers (current); ages 18+", race="hispanic (any race)",
sex="both sexes", datatype="direct estimates")
expect_true(is.data.frame(result))
})


#risk-smoking must have 5 columns
test_that("risk-smoking has correct number of columns", {
df1 <- risk_smoking("smoking laws (any)")
df2 <- risk_smoking("smokers (stopped for 1 day or longer)", sex="both sexes",
datatype="county level modeled estimates", area="wa")
df3 <- risk_smoking("smoking not allowed at work (current smokers)", sex="both sexes",
datatype="direct estimates")
expected_columns1 <- 3
expected_columns2 <- 5
expected_columns3 <- 6
expect_equal(ncol(df1), expected_columns1)
expect_equal(ncol(df2), expected_columns2)
expect_equal(ncol(df3), expected_columns3)
})

#test error handling
test_that("risk-smoking handles invalid smoking parameters", {
expect_error(
risk_smoking("smoking laws (any)", sex="both sexes"),
"For this smoking type, Race, Sex, Datatype, and Area must ALL be NULL"
)
expect_error(
risk_smoking("smokers (stopped for 1 day or longer)", sex="both sexes",
datatype="county level modeled estimates"),
"For county level modeled estimates on this smoking type, area must NOT be null"
)
expect_error(
risk_smoking("smoking not allowed at work (current smokers)",
race="all races (includes hispanic)", sex="both sexes",
datatype="direct estimates"),
"For all sexes in this smoking type, race and area should be NULL"
)
expect_error(
risk_smoking("former smoker; ages 18+", sex="both sexes",
datatype="county level modeled estimates"),
"For this smoking type, Sex, Datatype, and Area must not be NULL AND Race must be NULL"
)
expect_error(
risk_smoking("smokers (ever); ages 18+", race="hispanic (any race)", sex="both sexes"),
"For this smoking type, Race, Sex, and Datatype must not be NULL AND Datatype and Area must be NULL"
)
expect_error(
risk_smoking("smokers (current); ages 18+", race="all races (includes hispanic)",
sex="both sexes", area="wa"),
"For all races for this smoking type, Datatype must not be NULL"
)
})

#parameter
test_that("risk-smoking has correct parameters", {
expect_error(risk_smoking())
})
37 changes: 35 additions & 2 deletions tests/testthat/test-risk-vaccines.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
test_that("multiplication works", {
expect_equal(2 * 2, 4)
#' Test risk-vaccines
#'
#' This testthat file test the risk-vaccines function
#'
# tests class and typeof output
test_that("Output data type is correct", {
output <- risk_vaccines("percent with up to date hpv vaccination coverage, ages 13-15",
"both sexes")

expect_true(inherits(output, "data.frame"))
})

#Ensures that variables are present and working on SCP
vaccine_options <- c("percent with up to date hpv vaccination coverage, ages 13-15",
"percent with up to date hpv vaccination coverage, ages 13-17")

for (option in vaccine_options) {
test_that("risk_womens_health returns non-empty data frame", {
result <- risk_vaccines(option, "both sexes")
expect_true(is.data.frame(result))
})
}

#risk-vaccines must have 5 columns
test_that("risk-vaccines has correct number of columns", {
df <- risk_vaccines("percent with up to date hpv vaccination coverage, ages 13-15",
"both sexes")
expected_columns <- 6
expect_equal(ncol(df), expected_columns)

})

#parameter
test_that("risk-vaccines has correct parameters", {
expect_error(risk_vaccines())
})
Loading