Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
add some unittest
  • Loading branch information
flystar233 committed Aug 28, 2024
1 parent 0159db5 commit 4022944
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
20 changes: 20 additions & 0 deletions tests/testthat/test-evaluateOutliers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
X <- data.frame(a = 1:100, b = 1:100)
X_outlier <- data.frame(a = 1:100, b = 1:100)
X_outlier[1L, "a"] <- 1000

test_that("Generate results as expected with iris", {
anomaly_data <- generateOutliers(iris, p = 0.05, sd_factor = 5, seed = 123)
qrf<- outqrf(anomaly_data,verbose = F)
result<- evaluateOutliers(iris,anomaly_data,qrf$outliers)
expect_type(result, "double")
expect_length(result, n = 5)
expect_true(length(result) > 0)
})

test_that("Generate results as expected with 2 cols data", {
qrf<- outqrf(X_outlier,verbose = F)
result<- evaluateOutliers(X,X_outlier,qrf$outliers)
expect_type(result, "double")
expect_length(result, n = 5)
expect_true(length(result) > 0)
})
2 changes: 0 additions & 2 deletions tests/testthat/test-generateOutliers.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
library(testthat)
library(outqrf)
test_that("generateOutliers(p = 0.2) changes vector as expected", {
x <- 1:10
p <- 0.2
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-method.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test_that("plot() works normally in iris", {
qrf <- outqrf(iris)
expect_no_error(plot(qrf))
})
26 changes: 24 additions & 2 deletions tests/testthat/test-outqrf.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,35 @@ test_that("outqrf(iris,quantiles_type=400) Generate results as expected", {
expect_true(nrow(qrf$outliers)>0)
})

test_that("outqrf(iris,quantiles_type=400,verbose=0) silent", {
test_that("verbose=0 has an effect", {
expect_silent(qrf<- outqrf(iris,quantiles_type=400,verbose=0))
})

test_that("outqrf(X_NA,quantiles_type=400,impute=TRUE) impute normally", {
test_that("imputation on data with 2 cols can work", {
qrf <- outqrf(X_NA,quantiles_type=400,impute=TRUE)
expect_true(nrow(qrf$outliers)>0)
})

test_that("imputation on iris data with missing can work", {
nrow <- nrow(iris)
ncol <- ncol(iris)
iris_NA<- iris
num_missing <- 20
missing_rows <- sample(1:nrow, num_missing)
missing_cols <- sample(1:ncol, ncol)
iris_NA[missing_rows, missing_cols] <- NA
qrf <- outqrf(iris_NA,quantiles_type=400,impute=TRUE)
expect_true(nrow(qrf$outliers)>0)
})

test_that("threshold can work normally in marginal scenarios.", {
qrf <- outqrf(iris,threshold=0)
expect_true(nrow(qrf$outliers)==0)
})

test_that("threshold can work normally in marginal scenarios.", {
sum_numeric<-sum(sapply(iris, is.numeric))
qrf <- outqrf(iris,threshold=1)
expect_true(nrow(qrf$outliers)==sum_numeric*nrow(iris))
})

0 comments on commit 4022944

Please sign in to comment.