Skip to content
Closed
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
46 changes: 46 additions & 0 deletions tests/testthat/test-drop-na.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,49 @@ test_that("works with rcrd cols", {
tibble(col = new_rcrd(list(x = 1, y = 1)))
)
})

test_that("can drop NAs in factor columns", {
df <- tibble(x = factor(c("a", NA, "c")))
out <- drop_na(df, x)
expect_equal(levels(out$x), c("a", "c"))
expect_equal(as.character(out$x), c("a", "c"))
})

test_that("can drop NAs in Date columns", {
df <- tibble(x = as.Date(c("2020-01-01", NA, "2020-01-03")))
out <- drop_na(df, x)
expect_equal(out$x, as.Date(c("2020-01-01", "2020-01-03")))
})

test_that("can drop NAs in POSIXct columns", {
df <- tibble(
x = as.POSIXct(c("2020-01-01 10:00", NA, "2020-01-03 12:00"))
)
out <- drop_na(df, x)
expect_equal(
out$x,
as.POSIXct(c("2020-01-01 10:00", "2020-01-03 12:00"))
)
})

test_that("works with 0-row data frame", {
df <- tibble(x = integer(), y = character())
out <- drop_na(df)
expect_identical(nrow(out), 0L)
expect_named(out, c("x", "y"))
})

test_that("works with 0-row grouped data frame", {
df <- tibble(g = integer(), x = integer())
gdf <- dplyr::group_by(df, g)
out <- drop_na(gdf)
expect_identical(nrow(out), 0L)
expect_identical(dplyr::group_vars(out), "g")
})

test_that("all-NA data frame returns 0 rows", {
df <- tibble(x = c(NA, NA), y = c(NA, NA))
out <- drop_na(df)
expect_identical(nrow(out), 0L)
expect_named(out, c("x", "y"))
})
54 changes: 54 additions & 0 deletions tests/testthat/test-fill.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,57 @@ test_that("`.by` can't be used on a grouped data frame", {
fill(df, y, .by = x)
})
})

test_that("can fill Date columns", {
df <- tibble(x = as.Date(c("2020-01-01", NA, "2020-01-03", NA)))

out <- fill(df, x)
expect_equal(out$x, as.Date(c("2020-01-01", "2020-01-01", "2020-01-03", "2020-01-03")))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
expect_equal(out$x, as.Date(c("2020-01-01", "2020-01-01", "2020-01-03", "2020-01-03")))
expect_equal(
out$x,
as.Date(c("2020-01-01", "2020-01-01", "2020-01-03", "2020-01-03"))
)


out <- fill(df, x, .direction = "up")
expect_equal(out$x, as.Date(c("2020-01-01", "2020-01-03", "2020-01-03", NA)))
})

test_that("can fill POSIXct columns", {
df <- tibble(
x = as.POSIXct(c("2020-01-01 10:00", NA, "2020-01-03 12:00", NA))
)

out <- fill(df, x)
expect_equal(
out$x,
as.POSIXct(c("2020-01-01 10:00", "2020-01-01 10:00", "2020-01-03 12:00", "2020-01-03 12:00"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
as.POSIXct(c("2020-01-01 10:00", "2020-01-01 10:00", "2020-01-03 12:00", "2020-01-03 12:00"))
as.POSIXct(c(
"2020-01-01 10:00",
"2020-01-01 10:00",
"2020-01-03 12:00",
"2020-01-03 12:00"
))

)
})

test_that("fill preserves factor levels", {
df <- tibble(x = factor(c("a", NA, "c", NA), levels = c("a", "b", "c")))

out <- fill(df, x)
expect_equal(levels(out$x), c("a", "b", "c"))
expect_equal(as.character(out$x), c("a", "a", "c", "c"))

out <- fill(df, x, .direction = "up")
expect_equal(levels(out$x), c("a", "b", "c"))
expect_equal(as.character(out$x), c("a", "c", "c", NA))
})

test_that("fill with .by respects group boundaries on Date columns", {
df <- tibble(
g = c(1, 1, 2, 2),
x = as.Date(c("2020-01-01", NA, NA, "2020-01-04"))
)

out <- fill(df, x, .by = g)
expect_equal(
out$x,
as.Date(c("2020-01-01", "2020-01-01", NA, "2020-01-04"))
)
})

test_that("works with 0-row data frame", {
df <- tibble(x = as.Date(character()), y = integer())
out <- fill(df, x)
expect_identical(nrow(out), 0L)
expect_named(out, c("x", "y"))
})
46 changes: 46 additions & 0 deletions tests/testthat/test-replace_na.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,49 @@ test_that("validates its inputs", {
replace_na(df, replace = 1)
})
})

test_that("can replace NAs in factor columns", {
fct <- factor(c("a", NA, "b"), levels = c("a", "b", "c"))
df <- tibble(x = fct)
out <- replace_na(df, list(x = "c"))

expect_equal(out$x, factor(c("a", "c", "b"), levels = c("a", "b", "c")))
})

test_that("can replace NAs in factor vector", {
fct <- factor(c("a", NA, "b"), levels = c("a", "b", "c"))
out <- replace_na(fct, "c")

expect_equal(out, factor(c("a", "c", "b"), levels = c("a", "b", "c")))
})

test_that("empty data frame returns empty data frame", {
df <- tibble(x = integer(), y = character())
out <- replace_na(df, list(x = 0L, y = "unknown"))

expect_identical(out, df)
})

test_that("all-NA data frame is fully replaced", {
df <- tibble(x = c(NA_real_, NA_real_), y = c(NA_character_, NA_character_))
out <- replace_na(df, list(x = 0, y = "missing"))

expect_equal(out$x, c(0, 0))
expect_equal(out$y, c("missing", "missing"))
})

test_that("can replace NAs in Date columns", {
df <- tibble(x = as.Date(c("2024-01-01", NA, "2024-01-03")))
out <- replace_na(df, list(x = as.Date("2024-01-02")))

expect_equal(out$x, as.Date(c("2024-01-01", "2024-01-02", "2024-01-03")))
})

test_that("can replace NAs in POSIXct columns", {
dt <- as.POSIXct(c("2024-01-01 10:00:00", NA), tz = "UTC")
replacement <- as.POSIXct("2024-01-02 12:00:00", tz = "UTC")
df <- tibble(x = dt)
out <- replace_na(df, list(x = replacement))

expect_equal(out$x, c(dt[1], replacement))
})
Loading