From 8818b8c546e9dec5c881cc453b91050c0aa4374e Mon Sep 17 00:00:00 2001 From: Jim Thorson <50178738+James-Thorson-NOAA@users.noreply.github.com> Date: Wed, 23 Aug 2023 20:55:37 -0700 Subject: [PATCH] add testthat CI --- .github/.gitignore | 1 + .github/workflows/R-CMD-check.yaml | 29 +++++++++++++++ tests/testthat.R | 10 ++++++ tests/testthat/test-platform.R | 57 ++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 .github/.gitignore create mode 100644 .github/workflows/R-CMD-check.yaml create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test-platform.R diff --git a/.github/.gitignore b/.github/.gitignore new file mode 100644 index 0000000..2d19fc7 --- /dev/null +++ b/.github/.gitignore @@ -0,0 +1 @@ +*.html diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml new file mode 100644 index 0000000..f4b17a4 --- /dev/null +++ b/.github/workflows/R-CMD-check.yaml @@ -0,0 +1,29 @@ +# 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: R-CMD-check + +jobs: + R-CMD-check: + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + R_KEEP_PKG_SOURCE: yes + steps: + - uses: actions/checkout@v3 + + - uses: r-lib/actions/setup-r@v2 + with: + use-public-rspm: true + + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::rcmdcheck + needs: check + + - uses: r-lib/actions/check-r-package@v2 diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..ee84583 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,10 @@ + + +library(testthat) +library(dsem) + +# Run tests +testthat::test_check("dsem") + +# Run from local directory +# testthat::test_dir( "C:/Users/James.Thorson/Desktop/Git/dsem/tests/testthat/", reporter="check" ) diff --git a/tests/testthat/test-platform.R b/tests/testthat/test-platform.R new file mode 100644 index 0000000..915cf34 --- /dev/null +++ b/tests/testthat/test-platform.R @@ -0,0 +1,57 @@ + + +context("Testing cross platform and R version compatibility") + +# Eastern Bering Sea pollcok +test_that("dsem example is working ", { + #skip_on_ci() + sem = " + Profits -> Consumption, 0, a2 + Profits -> Consumption, -1, a3 + Priv_wage -> Consumption, 0, a4 + Gov_wage -> Consumption, 0, a4 + Consumption <-> Consumption, 0, v1 + Consumption -> Consumption, -1, ar1 + Consumption -> Consumption, -2, ar2 + Profits -> Investment, 0, b2 + Profits -> Investment, -1, b3 + Capital_stock -> Investment, -1, b4 + Investment <-> Investment, 0, v2 + neg_Gov_wage <-> neg_Gov_wage, 0, v3 + GNP -> Priv_wage, 0, c2 + Taxes -> Priv_wage, 0, c2 + neg_Gov_wage -> Priv_wage, 0, c2 + GNP -> Priv_wage, -1, c3 + Taxes -> Priv_wage, -1, c3 + neg_Gov_wage -> Priv_wage, -1, c3 + Time -> Priv_wage, 0, c4 + Priv_wage <-> Priv_wage, 0, v4 + GNP <-> GNP, 0, v5 + Profits <-> Profits, 0, v6 + Capital_stock <-> Capital_stock, 0, v7 + Taxes <-> Taxes, 0, v8 + Time <-> Time, 0, v9 + Gov_wage <-> Gov_wage, 0, v10 + Gov_expense <-> Gov_expense, 0, v11 + " + + # Load data + data(KleinI, package="AER") + Data = as.data.frame(KleinI) + Data = cbind( Data, "time" = seq(1,22)-11 ) + colnames(Data) = sapply( colnames(Data), FUN=switch, + "consumption"="Consumption", "invest"="Investment", + "cprofits"="Profits", "capital"="Capital_stock", "gwage"="Gov_wage", + "pwage"="Priv_wage", "gexpenditure"="Gov_expense", "taxes"="Taxes", + "time"="Time", "gnp"="GNP") + Z = ts( cbind(Data, "neg_Gov_wage"=-1*Data[,'Gov_wage']) ) + + # Fit model + fit = dsem( sem=sem, tsdata=Z ) + # Check objective function + expect_equal( as.numeric(fit$opt$obj), 587.4755, tolerance=1e-2 ) + + # Convert and plot using phylopath + as_fitted_DAG(fit) +}) +