Skip to content

Commit

Permalink
address lintr
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiyuC committed Jul 19, 2024
1 parent 6b24f18 commit 0008b2e
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 77 deletions.
68 changes: 38 additions & 30 deletions tests/testthat/test-derive_blfl.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,21 @@ test_that("derive_blfl DOMAIN validation works", {
})

test_that("`dtc_datepart`: basic usage", {
expect_identical(dtc_datepart(
c(NA, "", "2021", "2021-12", "2021-12-25", "2021-12-25T12:00:00")
),
c(NA, NA, NA, NA, "2021-12-25", "2021-12-25"))
expect_identical(
dtc_datepart(
c(NA, "", "2021", "2021-12", "2021-12-25", "2021-12-25T12:00:00")
),
c(NA, NA, NA, NA, "2021-12-25", "2021-12-25")
)

## Prevent partial or missing dates from being set to NA
expect_identical(dtc_datepart(
c(NA, "", "2021", "2021-12", "2021-12-25", "2021-12-25T12:00:00"),
partial_as_na = FALSE
expect_identical(
dtc_datepart(
c(NA, "", "2021", "2021-12", "2021-12-25", "2021-12-25T12:00:00"),
partial_as_na = FALSE
),
c(NA, "", "2021", "2021-12", "2021-12-25", "2021-12-25"))
c(NA, "", "2021", "2021-12", "2021-12-25", "2021-12-25")
)
})

test_that("`dtc_timepart`: basic usage", {
Expand All @@ -156,32 +160,36 @@ test_that("`dtc_timepart`: basic usage", {
), c(NA, NA, NA, NA, "12:30", "12:30"))

# Prevent partial or missing times from being set to NA
expect_identical(dtc_timepart(
c(
NA,
"",
"2021-12-25",
"2021-12-25T12",
"2021-12-25T12:30",
"2021-12-25T12:30:59"
expect_identical(
dtc_timepart(
c(
NA,
"",
"2021-12-25",
"2021-12-25T12",
"2021-12-25T12:30",
"2021-12-25T12:30:59"
),
partial_as_na = FALSE
),
partial_as_na = FALSE
),
c(NA, "", "", "12", "12:30", "12:30"))
c(NA, "", "", "12", "12:30", "12:30")
)

# Do not ignore seconds, partial or missing times set to NA
expect_identical(dtc_timepart(
c(
NA,
"",
"2021-12-25",
"2021-12-25T12",
"2021-12-25T12:30",
"2021-12-25T12:30:59"
expect_identical(
dtc_timepart(
c(
NA,
"",
"2021-12-25",
"2021-12-25T12",
"2021-12-25T12:30",
"2021-12-25T12:30:59"
),
ignore_seconds = FALSE
),
ignore_seconds = FALSE
),
c(NA, NA, NA, NA, NA, "12:30:59"))
c(NA, NA, NA, NA, NA, "12:30:59")
)

# Do not ignore seconds and prevent partial or missing times from being set to NA
expect_identical(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ test_that("`derive_seq()` works as expected", {
rec_vars <- c("STUDYID", "USUBJID", "VSTESTCD", "VSDTC", "VSTPTNUM")
observed_vsseq <- derive_seq(tgt_dat = vs, tgt_var = "VSSEQ", rec_vars = rec_vars)

# nolint start
expected_vsseq <- tibble::tribble(
~STUDYID, ~DOMAIN, ~USUBJID, ~VSSPID, ~VSTESTCD, ~VSDTC, ~VSTPTNUM, ~VSSEQ,
"ABC123", "VS", "ABC123-375", "/F:VTLS1-D:9795532-R:2", "DIABP", "2020-09-01T13:31", NA, 1L,
"ABC123", "VS", "ABC123-375", "/F:VTLS2-D:9795533-R:2", "DIABP", "2020-09-28T11:00", 2, 2L,
"ABC123", "VS", "ABC123-375", "/F:VTLS2-D:9795533-R:2", "DIABP", "2020-09-28T11:00", 2L, 2L,
"ABC123", "VS", "ABC123-375", "/F:VTLS1-D:9795532-R:2", "TEMP", "2020-09-01T13:31", NA, 3L,
"ABC123", "VS", "ABC123-375", "/F:VTLS2-D:9795533-R:2", "TEMP", "2020-09-28T11:00", 2, 4L,
"ABC123", "VS", "ABC123-375", "/F:VTLS2-D:9795533-R:2", "TEMP", "2020-09-28T11:00", 2L, 4L,
"ABC123", "VS", "ABC123-376", "/F:VTLS1-D:9795591-R:1", "DIABP", "2020-09-20", NA, 1L,
"ABC123", "VS", "ABC123-376", "/F:VTLS1-D:9795591-R:1", "TEMP", "2020-09-20", NA, 2L
)
# nolint end

expect_identical(observed_vsseq, expected_vsseq)

Expand Down
20 changes: 10 additions & 10 deletions tests/testthat/test-dtc_parse_dttm.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ test_that("`parse_dttm()` works as expected", {

# Year, Month, Day
expected_ymd <- tibble::tribble(
~year, ~mon, ~mday, ~hour, ~min, ~sec,
"2020", "05", "11", NA, NA, NA
~year, ~mon, ~mday, ~hour, ~min, ~sec,
"2020", "05", "11", NA, NA, NA
) |>
as.matrix()

Expand Down Expand Up @@ -59,7 +59,7 @@ test_that("`parse_dttm()` works as expected", {
expected_ymdhm_1 <- tibble::tribble(
~year, ~mon, ~mday, ~hour, ~min, ~sec,
"2002", "05", "11", "11", "45", NA,
NA, NA, NA, NA, NA, NA
NA, NA, NA, NA, NA, NA
) |>
as.matrix()

Expand All @@ -69,7 +69,7 @@ test_that("`parse_dttm()` works as expected", {
expected_ymdhm_2 <- tibble::tribble(
~year, ~mon, ~mday, ~hour, ~min, ~sec,
NA, NA, NA, NA, NA, NA,
NA, "05", "11", "11", "45", NA,
NA, "05", "11", "11", "45", NA
) |>
as.matrix()

Expand All @@ -81,19 +81,19 @@ test_that("`parse_dttm()` works as expected", {
expected_ymdhm_3 <- tibble::tribble(
~year, ~mon, ~mday, ~hour, ~min, ~sec,
"2002", "05", "11", "11", "45", NA,
NA, "05", "11", "11", "45", NA
NA, "05", "11", "11", "45", NA
) |>
as.matrix()

expect_identical(
parse_dttm(c("2002-05-11 11:45", "-05-11 11:45"), c("y-m-d H:M", "-m-d H:M")),
expected_ymdhm_3
)
)

# Different date formats
expected_ymdhm_4 <- tibble::tribble(
~year, ~mon, ~mday, ~hour, ~min, ~sec,
"1985", "feb", "05", "12", "55", "02",
"1985", "feb", "05", "12", "55", "02"
) |>
as.matrix()

Expand All @@ -108,7 +108,7 @@ test_that("`parse_dttm()` works as expected", {
~year, ~mon, ~mday, ~hour, ~min, ~sec,
"2020", "05", "18", NA, NA, NA,
NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA
) |>
as.matrix()

Expand All @@ -118,7 +118,7 @@ test_that("`parse_dttm()` works as expected", {
~year, ~mon, ~mday, ~hour, ~min, ~sec,
"2020", "05", "18", NA, NA, NA,
"2020", "UN", "18", NA, NA, NA,
NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA
) |>
as.matrix()

Expand All @@ -131,7 +131,7 @@ test_that("`parse_dttm()` works as expected", {
~year, ~mon, ~mday, ~hour, ~min, ~sec,
"2020", "05", "18", NA, NA, NA,
"2020", "UN", "18", NA, NA, NA,
"2020", "UNK", "UN", NA, NA, NA,
"2020", "UNK", "UN", NA, NA, NA
) |>
as.matrix()

Expand Down
44 changes: 22 additions & 22 deletions tests/testthat/test-dtc_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@ test_that("`assert_capture_matrix()`: basic usage", {
expect_identical(assert_capture_matrix(m), m)

expect_error(assert_capture_matrix(character()))
expect_error(assert_capture_matrix(matrix(data = NA_character_, nrow = 0, ncol = 0)))
expect_error(assert_capture_matrix(matrix(data = NA_character_, nrow = 1)))
expect_error(assert_capture_matrix(matrix(data = NA_character_, nrow = 0L, ncol = 0L)))
expect_error(assert_capture_matrix(matrix(data = NA_character_, nrow = 1L)))
})

test_that("`complete_capture_matrix()`: basic usage", {

# Input with no cols and rows
input <- matrix(data = NA_character_, nrow = 0, ncol = 0)
input <- matrix(data = NA_character_, nrow = 0L, ncol = 0L)
expected_output <- matrix(
data = NA_character_,
nrow = 0,
ncol = 6,
nrow = 0L,
ncol = 6L,
dimnames = list(NULL, c("year", "mon", "mday", "hour", "min", "sec"))
)

expect_identical(complete_capture_matrix(input),
expected_output)

# Input with no cols and 1 row
input <- matrix(data = NA_character_, nrow = 1)
input <- matrix(data = NA_character_, nrow = 1L)
expected_output <- matrix(
data = NA_character_,
nrow = 1,
ncol = 6,
nrow = 1L,
ncol = 6L,
dimnames = list(NULL, c("year", "mon", "mday", "hour", "min", "sec"))
)

Expand All @@ -59,15 +59,15 @@ test_that("`complete_capture_matrix()`: basic usage", {
input <-
matrix(
NA_character_,
nrow = 1,
ncol = 2,
nrow = 1L,
ncol = 2L,
dimnames = list(NULL, c("year", "sec"))
)

expected_output <- matrix(
data = NA_character_,
nrow = 1,
ncol = 6,
nrow = 1L,
ncol = 6L,
dimnames = list(NULL, c("year", "mon", "mday", "hour", "min", "sec"))
)

Expand All @@ -78,16 +78,16 @@ test_that("`complete_capture_matrix()`: basic usage", {
input <-
matrix(
c("2020", "10"),
nrow = 1,
ncol = 2,
nrow = 1L,
ncol = 2L,
dimnames = list(NULL, c("year", "sec"))
)

expected_output <-
matrix(
data = c("2020", rep(NA, 4), "10"),
nrow = 1,
ncol = 6,
data = c("2020", rep(NA, 4L), "10"),
nrow = 1L,
ncol = 6L,
dimnames = list(NULL, c("year", "mon", "mday", "hour", "min", "sec"))
)

Expand All @@ -98,15 +98,15 @@ test_that("`complete_capture_matrix()`: basic usage", {
input <-
matrix(
c("2020", "10"),
nrow = 1,
ncol = 2,
nrow = 1L,
ncol = 2L,
dimnames = list(NULL, c("semester", "quarter"))
)

expected_output <- matrix(
data = NA_character_,
nrow = 1,
ncol = 6,
nrow = 1L,
ncol = 6L,
dimnames = list(NULL, c("year", "mon", "mday", "hour", "min", "sec"))
)

Expand Down Expand Up @@ -151,7 +151,7 @@ test_that("`coalesce_capture_matrices()`: basic usage", {

expected_output <- tibble::tribble(
~year, ~mon, ~mday, ~hour, ~min, ~sec,
NA, NA, NA, "10", "00", "05",
NA, NA, NA, "10", "00", "05"
) |>
as.matrix()

Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-iso8601.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test_that("`iso8601_year()`: basic usage", {
expect_identical(
iso8601_year(c("0", "1", "2", "50", "68", "69", "90", "99", "00")),
c("2000", "2001", "2002", "2050", "2068", "1969", "1990", "1999", "2000")
)
)

# By default, `cutoff_2000` is at 68.
expect_identical(
Expand All @@ -62,11 +62,11 @@ test_that("`iso8601_year()`: basic usage", {
)

# Set cutoff_2000 to something else
expect_identical(iso8601_year(as.character(0:50), cutoff_2000 = 25),
as.character(c(2000:2025, 1926:1950)))
expect_identical(iso8601_year(as.character(0L:50L), cutoff_2000 = 25L),
as.character(c(2000L:2025L, 1926L:1950L)))

expect_identical(iso8601_year(as.character(1900:1950), cutoff_2000 = 25),
as.character(c(1900:1950)))
expect_identical(iso8601_year(as.character(1900L:1950L), cutoff_2000 = 25L),
as.character(c(1900L:1950L)))

})

Expand All @@ -87,15 +87,15 @@ test_that("`iso8601_mon()`: basic usage", {

mon <- month.abb
expect_identical(iso8601_mon(mon),
"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")
c("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"))

})

test_that("`iso8601_sec()`: basic usage", {

expect_identical(
iso8601_sec(c(NA, "0", "1", "10", "59", "99", "100")),
c( NA, "00", "01", "10", "59", "99", NA)
c(NA, "00", "01", "10", "59", "99", NA)
)
})

Expand Down
5 changes: 1 addition & 4 deletions tests/testthat/test-parse_dttm_fmt.R
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,7 @@ test_that("`parse_dttm_fmt` works as expected", {
observed_cus <- parse_dttm_fmt("year month day",
fmt_cmp(year = "year",
mon = "month",
mday = "day")
)
mday = "day"))

expected_cus <- tibble::tribble(
~fmt_c, ~pat, ~cap, ~start, ~end, ~len, ~ord,
Expand All @@ -326,5 +325,3 @@ test_that("`parse_dttm_fmt` works as expected", {

expect_identical(observed_cus, expected_cus)
})


4 changes: 2 additions & 2 deletions tests/testthat/test-recode.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ test_that("recode(): notable cases", {
})

test_that("index_for_recode(): basic usage", {
expect_identical(index_for_recode(x = 1:5, from = c(2, 4)),
as.integer(c(NA, 1, NA, 2, NA)))
expect_identical(index_for_recode(x = 1L:5L, from = c(2L, 4L)),
as.integer(c(NA, 1L, NA, 2L, NA)))

})

0 comments on commit 0008b2e

Please sign in to comment.