Skip to content

Commit

Permalink
fix wrong data when provided a non-exist primaryids
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Nov 6, 2023
1 parent e3b0073 commit 39853ec
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion R/methods-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ methods::setMethod("faers_keep", "FAERSascii", function(object, primaryid = NULL
})
} else {
object@data <- lapply(object@data, function(x) {
x[.__primaryid__., on = "primaryid"]
x[.__primaryid__., on = "primaryid", nomatch = NULL]
})
}
object
Expand Down
21 changes: 16 additions & 5 deletions tests/testthat/test_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,25 @@ testthat::test_that("faers_keep works well", {
testthat::expect_in(data2_invert$ther$primaryid, ids2_invert)
testthat::expect_in(data2_invert$rpsr$primaryid, ids2_invert)
testthat::expect_in(data2_invert$outc$primaryid, ids2_invert)

ids3 <- "none_exist_ids"
data3 <- faers_keep(data, ids3)
testthat::expect_equal(nrow(data3$drug), 0L)
testthat::expect_equal(nrow(data3$indi), 0L)
testthat::expect_equal(nrow(data3$reac), 0L)
testthat::expect_equal(nrow(data3$ther), 0L)
testthat::expect_equal(nrow(data3$rpsr), 0L)
testthat::expect_equal(nrow(data3$outc), 0L)
data4 <- faers_keep(data, ids3, invert = TRUE)
testthat::expect_identical(data4, data)
})

testthat::test_that("faers_filter works well", {
testthat::expect_error(faers_filter(data, ~ FALSE))
testthat::expect_error(faers_filter(data, ~FALSE))
ids1 <- sample(faers_primaryid(data), 1L)
ids1_invert <- setdiff(faers_primaryid(data), ids1)
data1 <- faers_filter(data, ~ ids1)
data1_invert <- faers_filter(data, ~ ids1, invert = TRUE)
data1 <- faers_filter(data, ~ids1)
data1_invert <- faers_filter(data, ~ids1, invert = TRUE)
testthat::expect_setequal(data1$demo$primaryid, ids1)
testthat::expect_in(data1$indi$primaryid, ids1)
testthat::expect_in(data1$reac$primaryid, ids1)
Expand All @@ -145,8 +156,8 @@ testthat::test_that("faers_filter works well", {

ids2 <- sample(faers_primaryid(data), 10L)
ids2_invert <- setdiff(faers_primaryid(data), ids2)
data2 <- faers_filter(data, ~ ids2)
data2_invert <- faers_filter(data, ~ ids2, invert = TRUE)
data2 <- faers_filter(data, ~ids2)
data2_invert <- faers_filter(data, ~ids2, invert = TRUE)
testthat::expect_setequal(data2$demo$primaryid, ids2)
testthat::expect_in(data2$drug$primaryid, ids2)
testthat::expect_in(data2$indi$primaryid, ids2)
Expand Down

0 comments on commit 39853ec

Please sign in to comment.