Skip to content

Commit

Permalink
refactor: lintr code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
Gero1999 committed Jan 31, 2025
1 parent 7eaa42f commit d79803e
Showing 1 changed file with 55 additions and 41 deletions.
96 changes: 55 additions & 41 deletions tests/testthat/test-l_pkconc.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,94 +35,106 @@ describe("l_pkconc", {
displaying_vars = c("NFRLT", "AFRLT", "AVAL"))
expect_length(listings_1l, length(unique(adpc$PCSPEC)))
expect_named(listings_1l, expected = as.character(unique(adpc$PCSPEC)))

# For 2 case
listings_2l <- l_pkconc(adpc, listgroup_vars = c("PARAM", "PCSPEC", "ROUTE"),
grouping_vars = c("TRT01A", "USUBJID", "AVISIT"),
displaying_vars = c("NFRLT", "AFRLT", "AVAL"))
expect_length(listings_2l, length(unique(interaction(adpc$PARAM, adpc$PCSPEC, adpc$ROUTE))))
expect_named(listings_2l, expected = as.character(unique(interaction(adpc$PARAM, adpc$PCSPEC, adpc$ROUTE))))

expect_named(listings_2l, expected = as.character(
unique(
interaction(adpc$PARAM, adpc$PCSPEC, adpc$ROUTE)
)
)
)

# For 4 case
listings_4l <- l_pkconc(adpc, listgroup_vars = c("PARAM", "PCSPEC", "ROUTE", "USUBJID"),
grouping_vars = c("TRT01A", "USUBJID", "AVISIT"),
displaying_vars = c("NFRLT", "AFRLT", "AVAL"))

# Include lengths and names expected
expect_length(listings_4l, length(unique(interaction(adpc$PARAM, adpc$PCSPEC, adpc$ROUTE, adpc$USUBJID))))
expect_named(listings_4l, expected = as.character(interaction(adpc$PARAM, adpc$PCSPEC, adpc$ROUTE, adpc$USUBJID)))

expect_length(listings_4l, length(unique(interaction(adpc$PARAM,
adpc$PCSPEC,
adpc$ROUTE,
adpc$USUBJID))))
expect_named(listings_4l, expected = as.character(interaction(adpc$PARAM,
adpc$PCSPEC,
adpc$ROUTE,
adpc$USUBJID)))

# All in correct format (list) and each internal element internally inherits listing_df
expect_true(class(listings_1l) == "list" &&
class(listings_2l) == "list" &&
class(listings_2l) == "list" &&
class(listings_4l) == "list")
expect_true(all(sapply(listings_1l, inherits, "listing_df")) &&
all(sapply(listings_2l, inherits, "listing_df")) &&
expect_true(all(sapply(listings_1l, inherits, "listing_df")) &&
all(sapply(listings_2l, inherits, "listing_df")) &&
all(sapply(listings_4l, inherits, "listing_df")))
})

it("handles missing formatting_vars_table and uses a default built:", {
listings <- l_pkconc(adpc,
listings <- l_pkconc(adpc,
listgroup_vars = c("PARAM", "PCSPEC", "ROUTE"),
grouping_vars = c("TRT01A", "USUBJID", "AVISIT"),
displaying_vars = c("NFRLT", "AFRLT", "AVAL"),
formatting_vars_table = NULL)

# Adds labels as column names & includes units when obvious and present (i.e, AVALU for AVAL)
expect_equal(var_labels(listings[[1]]),
expected = c(TRT01A = attr(adpc$TRT01A, "label"),
USUBJID = attr(adpc$USUBJID, "label"),
AVISIT = attr(adpc$AVISIT, "label"),
NFRLT = attr(adpc$NFRLT, "label"),
AFRLT = attr(adpc$AFRLT, "label"),
AVAL = paste0(attr(adpc$AVAL, "label"), " (", unique(adpc$AVALU), ")"),
NFRLT = attr(adpc$NFRLT, "label"),
AFRLT = attr(adpc$AFRLT, "label"),
AVAL = paste0(attr(adpc$AVAL, "label"),
" (", unique(adpc$AVALU), ")"),
AVALU = "AVALU"))
})

it("handles missing subtitle_lists and creates a default", {
listings <- l_pkconc(adpc, listgroup_vars = c("PARAM", "PCSPEC", "ROUTE"),
grouping_vars = c("TRT01A", "USUBJID", "AVISIT"),
displaying_vars = c("NFRLT", "AFRLT", "AVAL"),
subtitle_lists = NULL)

# Check if the subtitle is in the correct format
expect_equal(attr(listings[[1]], "subtitles"),
expected = "Analyte: A\nSpecimen: Plasma\nAdministration: Oral")

})

it("handles missing footnote_table (no footnote)", {
listings <- l_pkconc(adpc, listgroup_vars = c("PARAM", "PCSPEC", "ROUTE"),
grouping_vars = c("TRT01A", "USUBJID", "AVISIT"),
displaying_vars = c("NFRLT", "AFRLT", "AVAL"),
footnote_table = NULL)
expect_equal(attr(listings[[1]], "main_footer"), expected = character())
})

it("handles empty data frame by providing empty list", {

# Define the input for the function
empty_adpc <- adpc[0, ]

# Define the expected output
empty_list <- list()
names(empty_list) <- character()

# Check if the output is as expected
expect_equal(l_pkconc(empty_adpc, listgroup_vars = c("PARAM", "PCSPEC", "ROUTE"),
grouping_vars = c("TRT01A", "USUBJID", "AVISIT"),
displaying_vars = c("NFRLT", "AFRLT", "AVAL")),
expected = empty_list)
})

it("handles missing required columns", {
incomplete_adpc <- adpc %>% select(-AFRLT)
expect_error(l_pkconc(incomplete_adpc, listgroup_vars = c("PARAM", "PCSPEC", "ROUTE"),
grouping_vars = c("TRT01A", "USUBJID", "AVISIT"),
displaying_vars = c("NFRLT", "AFRLT", "AVAL")),
"Missing required columns: AFRLT")
})

it("handles non-unique units", {
non_unique_units_adpc <- adpc
non_unique_units_adpc$AVALU <- c("ng/mL", "ng/mL", "ng/L", "g/L")
Expand All @@ -131,7 +143,7 @@ describe("l_pkconc", {
displaying_vars = c("NFRLT", "AFRLT", "AVAL")),
"pkcl01, but not unique unit in ")
})

it("handles custom formatting_vars_table", {
custom_formatting_vars_table <- data.frame(
var_name = c("TRT01A", "USUBJID", "AVISIT", "NFRLT", "AFRLT", "AVAL"),
Expand All @@ -144,12 +156,12 @@ describe("l_pkconc", {
unit = c(NA, NA, NA, NA, NA, "AVALU"),
stringsAsFactors = FALSE
)

listings <- l_pkconc(adpc, listgroup_vars = c("PARAM", "PCSPEC", "ROUTE"),
grouping_vars = c("TRT01A", "USUBJID", "AVISIT"),
displaying_vars = c("NFRLT", "AFRLT", "AVAL"),
formatting_vars_table = custom_formatting_vars_table)

# Check if 0s, NAs, and units are formatted correctly
expect_equal(as.vector(listings$`A.Plasma.Oral`$NFRLT), c("0", "1"))
expect_equal(as.vector(listings$`A.Plasma.Oral`$AFRLT), c("0", "1.12"))
Expand All @@ -158,21 +170,23 @@ describe("l_pkconc", {
expect_equal(as.vector(listings$`B.Plasma.IV`$AFRLT), c("0", "1.05"))
expect_equal(as.vector(listings$`B.Plasma.IV`$AVAL), c("31", NA))
expect_equal(attr(listings$`B.Plasma.IV`$AVAL, "format_na_str"), "missing")

# Check the structure of the listings
expect_equal(attr(listings$`A.Plasma.Oral`, "main_title"),
"Listing of PK Concentration by Treatment Group, Subject and Nominal Time, PK Population")
expect_equal(attr(listings$`A.Plasma.Oral`, "subtitles"),
expect_equal(attr(listings$`A.Plasma.Oral`, "main_title"),
paste0("Listing of PK Concentration by Treatment Group,",
"Subject and Nominal Time, PK Population"))
expect_equal(attr(listings$`A.Plasma.Oral`, "subtitles"),
"Analyte: A\nSpecimen: Plasma\nAdministration: Oral")
expect_equal(attr(listings$`A.Plasma.Oral`, "main_footer"),
expect_equal(attr(listings$`A.Plasma.Oral`, "main_footer"),
"*: Patients excluded from the summary table and mean plots")
expect_equal(attr(listings$`B.Plasma.IV`, "main_title"),
"Listing of PK Concentration by Treatment Group, Subject and Nominal Time, PK Population")
expect_equal(attr(listings$`B.Plasma.IV`, "subtitles"),
expect_equal(attr(listings$`B.Plasma.IV`, "main_title"),
paste0("Listing of PK Concentration by Treatment Group,",
"Subject and Nominal Time, PK Population"))
expect_equal(attr(listings$`B.Plasma.IV`, "subtitles"),
"Analyte: B\nSpecimen: Plasma\nAdministration: IV")
expect_equal(attr(listings$`B.Plasma.IV`, "main_footer"),
expect_equal(attr(listings$`B.Plasma.IV`, "main_footer"),
"*: Patients excluded from the summary table and mean plots")

# Check the attributes of the columns
expect_equal(attr(listings$`A.Plasma.Oral`$TRT01A, "label"), "Actual treatment")
expect_equal(attr(listings$`A.Plasma.Oral`$USUBJID, "label"), "Unique Subject ID")
Expand All @@ -181,14 +195,14 @@ describe("l_pkconc", {
expect_equal(attr(listings$`A.Plasma.Oral`$AFRLT, "label"), "Actual time from first dose")
expect_equal(attr(listings$`A.Plasma.Oral`$AVAL, "label"), "Analysis value (mg/L)")
expect_equal(attr(listings$`A.Plasma.Oral`$AVALU, "label"), "AVALU")

expect_equal(attr(listings$`B.Plasma.IV`$TRT01A, "label"), "Actual treatment")
expect_equal(attr(listings$`B.Plasma.IV`$USUBJID, "label"), "Unique Subject ID")
expect_equal(attr(listings$`B.Plasma.IV`$AVISIT, "label"), "Actual visit")
expect_equal(attr(listings$`B.Plasma.IV`$NFRLT, "label"), "Planned time from first dose")
expect_equal(attr(listings$`B.Plasma.IV`$AFRLT, "label"), "Actual time from first dose")
expect_equal(attr(listings$`B.Plasma.IV`$AVAL, "label"), "Analysis value (mg/L)")
expect_equal(attr(listings$`B.Plasma.IV`$AVALU, "label"), "AVALU")

})
})

0 comments on commit d79803e

Please sign in to comment.