Skip to content

Commit

Permalink
style issue and failed test
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiyuC committed Jul 19, 2024
1 parent 0008b2e commit 7a5988e
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 104 deletions.
5 changes: 2 additions & 3 deletions tests/testthat/test-derive_seq.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ test_that("`derive_seq()` works as expected", {
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", 2L, 2L,
"ABC123", "VS", "ABC123-375", "/F:VTLS2-D:9795533-R:2", "DIABP", "2020-09-28T11:00", 2, 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", 2L, 4L,
"ABC123", "VS", "ABC123-375", "/F:VTLS2-D:9795533-R:2", "TEMP", "2020-09-28T11:00", 2, 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
)
Expand Down Expand Up @@ -48,5 +48,4 @@ test_that("`is_seq_name()`: basic usage", {
expect_false(is_seq_name("AEseq"))

expect_false(is_seq_name("AESEQUENCE"))

})
105 changes: 65 additions & 40 deletions tests/testthat/test-dtc_parse_dttm.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
test_that("`parse_dttm()` works as expected", {

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

expect_identical(parse_dttm("2020", "y"),
expected_y)
expect_identical(
parse_dttm("2020", "y"),
expected_y
)

# Year, Month
expected_ym <- tibble::tribble(
Expand All @@ -17,8 +18,10 @@ test_that("`parse_dttm()` works as expected", {
) |>
as.matrix()

expect_identical(parse_dttm("2020-05", "y-m"),
expected_ym)
expect_identical(
parse_dttm("2020-05", "y-m"),
expected_ym
)

# Year, Month, Day
expected_ymd <- tibble::tribble(
Expand All @@ -27,8 +30,10 @@ test_that("`parse_dttm()` works as expected", {
) |>
as.matrix()

expect_identical(parse_dttm("2020-05-11", "y-m-d"),
expected_ymd)
expect_identical(
parse_dttm("2020-05-11", "y-m-d"),
expected_ymd
)

# Year, Month, Day in other formats
parse_dttm("2020 05 11", "y m d") |> expect_identical(expected_ymd)
Expand All @@ -38,50 +43,58 @@ test_that("`parse_dttm()` works as expected", {
# Year, Month, Day, Hour, Minute
expected_ymdhm <- tibble::tribble(
~year, ~mon, ~mday, ~hour, ~min, ~sec,
"2020", "05", "11", "11", "45", NA
"2020", "05", "11", "11", "45", NA
) |>
as.matrix()

expect_identical(parse_dttm("2020-05-11 11:45", "y-m-d H:M"),
expected_ymdhm)
expect_identical(
parse_dttm("2020-05-11 11:45", "y-m-d H:M"),
expected_ymdhm
)

# Year, Month, Day, Hour, Minute, Second
expected_ymdhms <- tibble::tribble(
~year, ~mon, ~mday, ~hour, ~min, ~sec,
"2020", "05", "11", "11", "45", "15.6"
~year, ~mon, ~mday, ~hour, ~min, ~sec,
"2020", "05", "11", "11", "45", "15.6"
) |>
as.matrix()

expect_identical(parse_dttm("2020-05-11 11:45:15.6", "y-m-d H:M:S"),
expected_ymdhms)
expect_identical(
parse_dttm("2020-05-11 11:45:15.6", "y-m-d H:M:S"),
expected_ymdhms
)

# Multiple records
expected_ymdhm_1 <- tibble::tribble(
~year, ~mon, ~mday, ~hour, ~min, ~sec,
"2002", "05", "11", "11", "45", NA,
NA, NA, NA, NA, NA, NA
~year, ~mon, ~mday, ~hour, ~min, ~sec,
"2002", "05", "11", "11", "45", NA,
NA, NA, NA, NA, NA, NA
) |>
as.matrix()

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

expected_ymdhm_2 <- tibble::tribble(
~year, ~mon, ~mday, ~hour, ~min, ~sec,
NA, NA, NA, NA, NA, NA,
NA, "05", "11", "11", "45", NA
NA, NA, NA, NA, NA, NA,
NA, "05", "11", "11", "45", NA
) |>
as.matrix()

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



expected_ymdhm_3 <- tibble::tribble(
~year, ~mon, ~mday, ~hour, ~min, ~sec,
"2002", "05", "11", "11", "45", NA,
NA, "05", "11", "11", "45", NA
~year, ~mon, ~mday, ~hour, ~min, ~sec,
"2002", "05", "11", "11", "45", NA,
NA, "05", "11", "11", "45", NA
) |>
as.matrix()

Expand All @@ -97,11 +110,15 @@ test_that("`parse_dttm()` works as expected", {
) |>
as.matrix()

expect_identical(parse_dttm("05 feb 1985 12 55 02", "d m y H M S"),
expected_ymdhm_4)
expect_identical(
parse_dttm("05 feb 1985 12 55 02", "d m y H M S"),
expected_ymdhm_4
)

expect_identical(parse_dttm("12 55 02 05 feb 1985", "H M S d m y"),
expected_ymdhm_4)
expect_identical(
parse_dttm("12 55 02 05 feb 1985", "H M S d m y"),
expected_ymdhm_4
)

# UNK included
expected_unk <- tibble::tribble(
Expand All @@ -112,8 +129,10 @@ test_that("`parse_dttm()` works as expected", {
) |>
as.matrix()

expect_identical(parse_dttm(c("2020-05-18", "2020-UN-18", "2020-UNK-UN"), "y-m-d"),
expected_unk)
expect_identical(
parse_dttm(c("2020-05-18", "2020-UN-18", "2020-UNK-UN"), "y-m-d"),
expected_unk
)
expected_unk_1 <- tibble::tribble(
~year, ~mon, ~mday, ~hour, ~min, ~sec,
"2020", "05", "18", NA, NA, NA,
Expand All @@ -122,10 +141,13 @@ test_that("`parse_dttm()` works as expected", {
) |>
as.matrix()

expect_identical(parse_dttm(c("2020-05-18", "2020-UN-18", "2020-UNK-UN"),
"y-m-d",
na = "UN"),
expected_unk_1)
expect_identical(
parse_dttm(c("2020-05-18", "2020-UN-18", "2020-UNK-UN"),
"y-m-d",
na = "UN"
),
expected_unk_1
)

expected_unk_2 <- tibble::tribble(
~year, ~mon, ~mday, ~hour, ~min, ~sec,
Expand All @@ -135,8 +157,11 @@ test_that("`parse_dttm()` works as expected", {
) |>
as.matrix()

expect_identical(parse_dttm(c("2020-05-18", "2020-UN-18", "2020-UNK-UN"),
"y-m-d",
na = c("UN", "UNK")),
expected_unk_2)
expect_identical(
parse_dttm(c("2020-05-18", "2020-UN-18", "2020-UNK-UN"),
"y-m-d",
na = c("UN", "UNK")
),
expected_unk_2
)
})
6 changes: 4 additions & 2 deletions tests/testthat/test-dtc_problems.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ test_that("`any_problems()`: basic usage", {
cap_matrix_time <- parse_dttm(time, "H:M:S")

cap_matrices <- list(cap_matrix_date, cap_matrix_time)
expect_identical(any_problems(cap_matrices),
c(TRUE, TRUE, FALSE, FALSE))
expect_identical(
any_problems(cap_matrices),
c(TRUE, TRUE, FALSE, FALSE)
)
})
48 changes: 28 additions & 20 deletions tests/testthat/test-dtc_utils.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
test_that("`assert_dtc_fmt()`: basic usage", {

fmt <- c("ymd", "y m d", "dmy", "HM", "H:M:S", "y-m-d H:M:S")
expect_identical(assert_dtc_fmt(fmt),
fmt)
expect_identical(
assert_dtc_fmt(fmt),
fmt
)

expect_error(assert_dtc_fmt("y years m months d days"))
})

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

expect_identical(assert_dtc_format("ymd"), "ymd")
expect_identical(assert_dtc_format(c("ymd", "y-m-d")), c("ymd", "y-m-d"))
expect_identical(assert_dtc_format(list(c("ymd", "y-m-d"), "H:M:S")),
list(c("ymd", "y-m-d"), "H:M:S"))
expect_identical(
assert_dtc_format(list(c("ymd", "y-m-d"), "H:M:S")),
list(c("ymd", "y-m-d"), "H:M:S")
)

expect_error(assert_dtc_format("year, month, day"))

})

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

cols <- c("year", "mon", "mday", "hour", "min", "sec")
m <- matrix(NA_character_, nrow = 1L, ncol = 6L, dimnames = list(NULL, cols))
expect_identical(assert_capture_matrix(m), m)
Expand All @@ -30,7 +30,6 @@ test_that("`assert_capture_matrix()`: basic usage", {
})

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

# Input with no cols and rows
input <- matrix(data = NA_character_, nrow = 0L, ncol = 0L)
expected_output <- matrix(
Expand All @@ -40,8 +39,10 @@ test_that("`complete_capture_matrix()`: basic usage", {
dimnames = list(NULL, c("year", "mon", "mday", "hour", "min", "sec"))
)

expect_identical(complete_capture_matrix(input),
expected_output)
expect_identical(
complete_capture_matrix(input),
expected_output
)

# Input with no cols and 1 row
input <- matrix(data = NA_character_, nrow = 1L)
Expand All @@ -52,8 +53,10 @@ test_that("`complete_capture_matrix()`: basic usage", {
dimnames = list(NULL, c("year", "mon", "mday", "hour", "min", "sec"))
)

expect_identical(complete_capture_matrix(input),
expected_output)
expect_identical(
complete_capture_matrix(input),
expected_output
)

# Input with incomplete cols
input <-
Expand All @@ -71,8 +74,10 @@ test_that("`complete_capture_matrix()`: basic usage", {
dimnames = list(NULL, c("year", "mon", "mday", "hour", "min", "sec"))
)

expect_identical(complete_capture_matrix(input),
expected_output)
expect_identical(
complete_capture_matrix(input),
expected_output
)

# Input with year and second specified
input <-
Expand All @@ -91,8 +96,10 @@ test_that("`complete_capture_matrix()`: basic usage", {
dimnames = list(NULL, c("year", "mon", "mday", "hour", "min", "sec"))
)

expect_identical(complete_capture_matrix(input),
expected_output)
expect_identical(
complete_capture_matrix(input),
expected_output
)

# Any other existing columns are dropped.
input <-
Expand All @@ -110,12 +117,13 @@ test_that("`complete_capture_matrix()`: basic usage", {
dimnames = list(NULL, c("year", "mon", "mday", "hour", "min", "sec"))
)

expect_identical(complete_capture_matrix(input),
expected_output)
expect_identical(
complete_capture_matrix(input),
expected_output
)
})

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

cols <- c("year", "mon", "mday", "hour", "min", "sec")
dates <- c("2020", "01", "01", "20", NA, NA)
times <- c(NA, NA, NA, "10", "00", "05")
Expand Down
Loading

0 comments on commit 7a5988e

Please sign in to comment.