Skip to content

Commit

Permalink
starting tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pachadotdev committed Feb 19, 2024
1 parent d3451ca commit aad78e1
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 3 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: test-coverage

jobs:
test-coverage:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::covr
needs: coverage

- name: Test coverage
run: covr::codecov(quiet = FALSE)
shell: Rscript {0}
2 changes: 1 addition & 1 deletion R/feglm_control.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ feglm_control <- function(
# Check validity of tolerance parameters
if (dev.tol <= 0.0 || center.tol <= 0.0) {
stop(
"All tolerance paramerters should be greater than zero.",
"All tolerance parameters should be greater than zero.",
call. = FALSE
)
}
Expand Down
4 changes: 2 additions & 2 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ check_family_ <- function(family) {
c("quasi", "quasipoisson", "quasibinomial")) {
stop("Quasi-variants of 'family' are not supported.", call. = FALSE)
} else if (startsWith(family[["family"]], "Negative Binomial")) {
stop("Please use 'feglm.nb' instead.", call. = FALSE)
stop("Please use 'fenegbin' instead.", call. = FALSE)
}

return(TRUE)
Expand All @@ -93,7 +93,7 @@ update_formula_ <- function(formula) {

if (length(formula)[[2L]] < 2L || length(formula)[[1L]] > 1L) {
stop(paste(
"'formula' uncorrectly specified. Perhaps you forgot to add the",
"'formula' incorrectly specified. Perhaps you forgot to add the",
"fixed effects as 'mpg ~ wt | cyl' or similar."
), call. = FALSE)
}
Expand Down
197 changes: 197 additions & 0 deletions tests/testthat/test-errors.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
test_that("multiplication works", {
trade_panel_2002 <- trade_panel[trade_panel$year == 2002, ]
trade_panel_2002$trade_100 <- ifelse(trade_panel_2002$trade >= 100, 1, 0)
trade_panel_2002$trade_200_100 <- as.factor(ifelse(trade_panel_2002$trade >= 200, 1,
ifelse(trade_panel_2002$trade >= 100, 0.5, 0)
))
trade_panel_2002$trade_1_minus1 <- ifelse(trade_panel_2002$trade >= 100, 1, -1)


# APEs ----

# TODO: test n.pop argument and the rest of apes()

expect_error(apes(), "specified")

expect_error(
apes(lm(trade ~ log_dist, data = trade_panel_2002)),
"non-'feglm'"
)

expect_error(
apes(fepoisson(trade ~ log_dist | rta, data = trade_panel_2002)),
"binary choice"
)

expect_error(
apes(
feglm(
trade_100 ~ log_dist | rta + cntg + clny + lang,
data = trade_panel_2002,
family = binomial()
),
panel.structure = "classic"
), "two-way"
)

expect_error(
apes(
feglm(
trade_100 ~ log_dist | rta + cntg + clny + lang,
data = trade_panel_2002,
family = binomial()
),
panel.structure = "network"
), "three-way"
)

# GLMs ----

expect_error(
fepoisson(
trade ~ log_dist | rta,
data = trade_panel_2002,
control = list(dev.tol = -1.0)
),
"greater than zero"
)

expect_error(
fepoisson(
trade ~ log_dist | rta,
data = trade_panel_2002,
control = list(iter.max = 0)
),
"at least one"
)

expect_error(
fepoisson(
trade ~ log_dist | rta,
data = trade_panel_2002,
control = list(limit = 0)
),
"at least one"
)

# Helpers ----

# TODO:
# weights
# linear dependence
# init.theta
# beta.start
# eta.start

expect_error(
feglm(data = trade_panel_2002), "'formula' has to be specified"
)

expect_error(
feglm(formula = "a ~ b", data = trade_panel_2002), "'formula' has to be of class 'formula'"
)

expect_error(
fepoisson(
trade ~ log_dist | rta,
data = NULL
),
"'data' has to be specified"
)

expect_error(
fepoisson(
trade ~ log_dist | rta,
data = list()
),
"'data' has to be of class data.frame"
)

expect_error(
fepoisson(
trade ~ log_dist | rta,
data = list()
),
"'data' has to be of class data.frame"
)

expect_error(
fepoisson(
trade ~ log_dist | rta,
data = trade_panel_2002,
control = c(1, 2)
),
"'control' has to be a list"
)

expect_error(
feglm(
trade ~ log_dist | rta,
data = trade_panel_2002,
family = "poisson"
),
"'family' has to be of class family"
)

expect_error(
feglm(
trade ~ log_dist | rta,
data = trade_panel_2002,
family = quasipoisson()
),
"Quasi-variants of 'family' are not supported"
)

expect_error(
feglm(
trade ~ log_dist | rta,
data = trade_panel_2002,
family = MASS::neg.bin(theta = 1)
),
"use 'fenegbin' instead"
)

expect_error(
fepoisson(
trade ~ log_dist,
data = trade_panel_2002
),
"'formula' incorrectly specified"
)

expect_error(
feglm(
trade ~ log_dist | rta,
data = trade_panel_2002,
family = binomial()
),
"response has to be within the unit interval"
)

expect_error(
feglm(
trade_200_100 ~ log_dist | rta,
data = trade_panel_2002,
family = binomial()
),
"response has to be binary"
)

expect_error(
feglm(
trade_1_minus1 ~ log_dist | rta,
data = trade_panel_2002,
family = Gamma()
),
"response has to be strictly positive"
)

expect_error(
feglm(
trade_1_minus1 ~ log_dist | rta,
data = trade_panel_2002,
family = inverse.gaussian()
),
"response has to be strictly positive"
)
})
1 change: 1 addition & 0 deletions tests/testthat/test-pairwise_cor.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("cor(x,x) = 1", {
x <- c(1, 2, 3, 4, 5)
expect_equal(pairwise_cor_(x, x), 1)
expect_equal(pairwise_cor_(x, -x), -1)
})

0 comments on commit aad78e1

Please sign in to comment.