Skip to content

Commit

Permalink
Fix tests for quarter code.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbosak01 committed Jun 13, 2024
1 parent a5f07ff commit a06e9a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
28 changes: 14 additions & 14 deletions tests/testthat/test-fapply.R
Original file line number Diff line number Diff line change
Expand Up @@ -282,17 +282,17 @@ test_that("fapply11: format_quarter() works as expected.", {

res

expect_equal(res %eq% c("2000-1", "2025-1", "2000-2", "2025-2", "2000-3", "2025-3",
"2000-4", "2025-4", NA, NA), TRUE)
expect_equal(all(res[1:8] == c("2000-1", "2025-1", "2000-2", "2025-2", "2000-3", "2025-3",
"2000-4", "2025-4")), TRUE)

f2 <- format(v1, "%Y-%Q")

res2 <- format_quarter(v1, f2, "%Y-%Q")

res2

expect_equal(res2 %eq% c("2000-Q1", "2025-Q1", "2000-Q2", "2025-Q2", "2000-Q3",
"2025-Q3", "2000-Q4", "2025-Q4", NA, NA), TRUE)
expect_equal(all(res2[1:8] == c("2000-Q1", "2025-Q1", "2000-Q2", "2025-Q2", "2000-Q3",
"2025-Q3", "2000-Q4", "2025-Q4")), TRUE)

})

Expand All @@ -307,8 +307,8 @@ test_that("fapply12: fapply with quarter works as expected.", {

print(res1)

expect_equal(res1 %eq% c("00-1", "25-1", "00-2","25-2", "00-3", "25-3", "00-4",
"25-4", NA, NA), TRUE)
expect_equal(all(res1[1:8] == c("00-1", "25-1", "00-2","25-2", "00-3", "25-3", "00-4",
"25-4")), TRUE)


res2 <- fapply(v1, "%Y-%Q")
Expand All @@ -317,10 +317,10 @@ test_that("fapply12: fapply with quarter works as expected.", {

print(res2)

expect_equal(res2 %eq% c("2000-Q1", "2025-Q1", "2000-Q2","2025-Q2", "2000-Q3",
expect_equal(all(res2[1:8] == c("2000-Q1", "2025-Q1", "2000-Q2","2025-Q2", "2000-Q3",
"2025-Q3",
"2000-Q4",
"2025-Q4", NA, NA), TRUE)
"2025-Q4")), TRUE)


res3 <- fapply(v1, "%q")
Expand All @@ -329,8 +329,8 @@ test_that("fapply12: fapply with quarter works as expected.", {

print(res3)

expect_equal(res3 %eq% c("1", "1", "2","2", "3", "3", "4",
"4", NA, NA), TRUE)
expect_equal(all(res3[1:8] == c("1", "1", "2","2", "3", "3", "4",
"4")), TRUE)

res4 <- fapply(Sys.time(), "%Q")

Expand Down Expand Up @@ -359,8 +359,8 @@ test_that("fapply13: fapply with quarter and date works as expected.", {



expect_equal(res1 %eq% c("00-1-15", "25-1-15", "00-2-15","25-2-15", "00-3-15",
"25-3-15", "00-4-15", "25-4-15", NA, NA), TRUE)
expect_equal(all(res1[1:8] == c("00-1-15", "25-1-15", "00-2-15","25-2-15", "00-3-15",
"25-3-15", "00-4-15", "25-4-15")), TRUE)


res2 <- fapply(v1, "%Y-%Q-%d")
Expand All @@ -369,10 +369,10 @@ test_that("fapply13: fapply with quarter and date works as expected.", {

res2

expect_equal(res2 %eq% c("2000-Q1-15", "2025-Q1-15", "2000-Q2-15","2025-Q2-15", "2000-Q3-15",
expect_equal(all(res2[1:8] == c("2000-Q1-15", "2025-Q1-15", "2000-Q2-15","2025-Q2-15", "2000-Q3-15",
"2025-Q3-15",
"2000-Q4-15",
"2025-Q4-15", NA, NA), TRUE)
"2025-Q4-15")), TRUE)


})
13 changes: 8 additions & 5 deletions tests/testthat/test-utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ test_that("utils2: get_quarter() works as expected.", {

res <- get_quarter(v1)

expect_equal(res %eq% c(1, 1, 2, 2, 3, 3, 4, 4, NA, NA), TRUE)
expect_equal(all(res[1:8] == c(1, 1, 2, 2, 3, 3, 4, 4)), TRUE)
expect_equal("numeric" %in% class(res), TRUE)

})
Expand All @@ -77,8 +77,10 @@ test_that("utils3: replace_quarter() works as expected.", {

res

expect_equal(res %eq% c("2000-Q1", "2025-Q1", "2000-Q2", "2025-Q2", "2000-Q3",
"2025-Q3", "2000-Q4", "2025-Q4", NA, NA), TRUE)
expect_equal(all(res[1:8] == c("2000-Q1", "2025-Q1", "2000-Q2", "2025-Q2", "2000-Q3",
"2025-Q3", "2000-Q4", "2025-Q4")), TRUE)

expect_equal(all(is.na(res[9:10]) == c(TRUE, TRUE)), TRUE)

val <- format(v1, "%Y-%q")
q <- get_quarter(v1)
Expand All @@ -89,9 +91,10 @@ test_that("utils3: replace_quarter() works as expected.", {

res2

expect_equal(res2 %eq% c("2000-q1", "2025-q1", "2000-q2", "2025-q2", "2000-q3",
"2025-q3", "2000-q4", "2025-q4", NA, NA), TRUE)
expect_equal(all(res2[1:8] == c("2000-q1", "2025-q1", "2000-q2", "2025-q2", "2000-q3",
"2025-q3", "2000-q4", "2025-q4")), TRUE)

expect_equal(all(is.na(res2[9:10]) == c(TRUE, TRUE)), TRUE)

})

Expand Down

0 comments on commit a06e9a0

Please sign in to comment.