CRI-iAtlas / iatlas-app

R Shiny app for CRI iAtlas, an interactive web portal for exploring immuno-oncology data. (iAtlas portal 2022 and beyond)
Other
12 stars 3 forks source link

Fix using testthat locally #16

Closed andrewelamb closed 4 years ago

andrewelamb commented 4 years ago

I've fixed some issues by having a file called:

tests/testthat/helpler-a_use_dev_globals.R

that contains this:

.GlobalEnv$DB_NAME <- "iatlas_dev" .GlobalEnv$DB_HOST <- "localhost" .GlobalEnv$DB_PORT <- "5432" .GlobalEnv$DB_USER <- "postgres" .GlobalEnv$DB_PW <- "docker" .GlobalEnv$pool <- connect_to_db()

But somethings do not work like:

devtools::test_coverage() Error: Failure in /tmp/RtmpS7sYhf/R_LIBS478d2f4bdbeb/iatlas.app/iatlas.app-tests/testthat.Rout.fail " Copyright (C) 2019 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R.

library(testthat) library(iatlas.app)

test_check("iatlas.app") Error in load_config("test") : could not find function "load_config" Calls: test_check ... source_test_helpers -> source_dir -> lapply -> FUN -> eval -> eval Execution halted Warning message: In (function (e) : You have a leaked pooled object.

Also runnig the rstudio "run tests" button result in:

==> Testing R file using 'testthat'

Loading iatlas.app Error in load_config("test") : could not find function "load_config" Calls: ... testthat_source_test_helpers -> source_dir -> lapply -> FUN -> eval -> eval In addition: There were 32 warnings (use warnings() to see them) Execution halted Warning message: In (function (e) : You have a leaked pooled object.

Exited with status 1.

shanebdavis commented 4 years ago

Okay, I fixed it. If you run devtools::test() in the consol, or any of the testthat commands, it works.

However, clicking the "test" buttons in RStudio don't. I'm not sure why. They seem to be running in an environment where .RProfile wasn't loaded... I think...

andrewelamb commented 4 years ago

There's still something weird going on:

Before I merge in the fixes:

test() Loading iatlas.app Database config loaded: test Testing iatlas.app ✓ | OK F W S | Context ✓ | 10 | clinical_outcomes_functions ✓ | 23 | create_queries ✓ | 14 | data_info_functions ✓ | 5 | immune_feature_distributions_functions ✓ | 1 | perform_query ✓ | 1 | plotly_functions ✓ | 36 | run_queries [0.2 s] ✓ | 44 | utils [0.2 s]

══ Results ═══════════════════════════════════════════════════════════════════════════════════════════════════════ Duration: 0.7 s

OK: 134 Failed: 0 Warnings: 0 Skipped: 0 There were 36 warnings (use warnings() to see them)

Restarting R session...

After I merge in:

test() Loading iatlas.app Testing iatlas.app ✓ | OK F W S | Context Database config loaded: test with_test_db_env iatlas_test ⠸ | 4 | clinical_outcomes_functionsDatabase config loaded: dev ✓ | 10 | clinical_outcomes_functions [0.2 s] Database config loaded: test ✓ | 23 | create_queriesmes_functions Database config loaded: test ⠸ | 23 1 | create_queriesDatabase config loaded: test Database config loaded: test ✓ | 1 | plotly_functions Database config loaded: test ⠙ | 1 1 | plotly_functionsDatabase config loaded: test

══ Results ═══════════════════════════════════════════════════════════════════════════════════════════════════════ Duration: 0.4 s

OK: 34 Failed: 6 Warnings: 0 Skipped: 0

"""""""""""""""""""""""""""""""""""""

It seems to be skipping a lot of the tests(and a few failed as well)

shanebdavis commented 4 years ago

Andrew, do you have local code changes you haven't pushed? I can't reproduce the problem here from the staging branch.

Also, I'm going to be out tomorrow and Monday, I'll be back on Tuesday. @jonryser , if you get a chance tomorrow, could you take a look at this?

andrewelamb commented 4 years ago

@shanebdavis @jonryser I do, but just pushed them:

shanebdavis commented 4 years ago

I'm not sure if it'll solve the problem on your end, but I made a few tweaks. Both staging and merge_staging are working here. I moved load_config into the R folder so it would for-sure be available.

Here's what I have working:

However, here's what does fail tests on my end:

In those cases the config package fails to find the config.yml file. Not sure why.

andrewelamb commented 4 years ago

devtools::check is handy since it runs all the tests, checks all the documentation files, checks that the function arguments match the headers etc... That said it's not needed.

Hmm something is still wrong on my end, I'm on merge_staging, and did git pull.

testthat::auto_test_package() Loading iatlas.app ✓ | OK F W S | Context Database config loaded: test ⠋ | 0 1 | Database config loaded: test ✓ | 23 | create_queries Database config loaded: test ⠸ | 23 1 | create_queriesDatabase config loaded: test Database config loaded: test ✓ | 1 | plotly_functions Database config loaded: test ⠙ | 1 1 | plotly_functionsDatabase config loaded: test

══ Results ═══════════════════════════════════════════════════════════════════════════════════════════════════════ Duration: 0.1 s

OK: 24 Failed: 7 Warnings: 0 Skipped: 0

The output here is a little confusing, but it seems it's only running tests on two of the test files(create_queries and plotly_functions) and runing them twice, and then not testing the rest of the files. The final error count doesn't make sense either.

Here's what that looks like on my staging_add_tests branch:

testthat::auto_test_package() Loading iatlas.app Database config loaded: test Database config loaded: test ✓ | OK F W S | Context ✓ | 10 | clinical_outcomes_functions [0.2 s] ✓ | 23 | create_queries ✓ | 14 | data_info_functions ✓ | 5 | immune_feature_distributions_functions ✓ | 1 | perform_query ✓ | 1 | plotly_functions ✓ | 36 | run_queries [0.3 s] ✓ | 44 | utils [0.2 s]

══ Results ═══════════════════════════════════════════════════════════════════════════════════════════════════════ Duration: 0.8 s

OK: 134 Failed: 0 Warnings: 0 Skipped: 0

shanebdavis commented 4 years ago

Okay, @andrewelamb Andrew, can you try it again? devtools::check() works now for me...

Running devtools:check() in R evidently packages up the directory to run its tests, and one side effect of that is R-packages don't include tests(*). Instead, devtools copies the test folder into a peer directory, so when running tests, the config package couldn't find config.yml by looking at parent folders.

The solution was to create a symlink in tests/config.yml to the config.yml in the parent. (This should work on Linux and Mac. I'm not sure about Windows.)

FYI: This is useful resource on the vagaries of R packaging: http://r-pkgs.had.co.nz/

shanebdavis commented 4 years ago

(Andrew, Jon and I worked out the last issue: the iatlas_test DB wasn't created yet on his machine)