traitecoevo / traits.build

Source for the traits.build R package, used to build AusTraits
https://traitecoevo.github.io/traits.build/
Other
8 stars 0 forks source link

Test breaking if no contexts in database #150

Closed ehwenk closed 1 month ago

ehwenk commented 7 months ago

One of the tests in dataset_test() is failing for a database where the contexts table is empty. We've discovered this starting the new thermal tolerance database. The datasets pass with no test failures if added to the austraits.build repository, but has a single error pertaining to contexts if it is in the new repository; error likely seems related to empty tables.

Kris gets the following error:

[ FAIL 1 | WARN 0 | SKIP 0 | PASS 50 ]
── Error: Morphology_geometry ──────────────────────────────────────────────────
<subscriptOutOfBoundsError/error/condition>
Error in `FUN(X[[i]], ...)`: subscript out of bounds
Backtrace:
1. └─base::sapply(metadata[["contexts"]], "[[", "context_property")
2.   └─base::lapply(X = X, FUN = FUN, ...)
[ FAIL 1 | WARN 0 | SKIP 0 | PASS 50 ]

Oddly, when I create a new repository with the same traits.yml file and the same dataset I get no error.

ehwenk commented 7 months ago

It seems to be caused by line 595 in testdata.R. I get the error when I load any individual metadata.yml file without contexts, but for some reason it doesn't throw an error when run on my machine. A solution is to change the code to:

        if(is.na(metadata[["contexts"]])) {
          context_properties <- metadata[["contexts"]]
          context_vars_in <- metadata[["contexts"]]
        } else {
          context_properties <- sapply(metadata[["contexts"]], "[[", "context_property")
          context_vars_in <- sapply(metadata[["contexts"]], "[[", "var_in")
        }