From 0d73256947c7c79890a5f94622f0948645ea4da7 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Thu, 5 Mar 2026 14:32:30 +0100 Subject: Make test robust against unavailable services Test logs are without internet. --- tests/testthat/setup.R | 4 ++++ tests/testthat/test_chent.R | 17 +++++++++++------ tests/testthat/test_pai.R | 9 +++++---- 3 files changed, 20 insertions(+), 10 deletions(-) (limited to 'tests/testthat') diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R index fb55272..4232120 100644 --- a/tests/testthat/setup.R +++ b/tests/testthat/setup.R @@ -2,3 +2,7 @@ skip_if_no_rdkit <- function() { if (!chents:::rdkit_available) skip("RDKit is not available via reticulate") } + +# Check availability of BCPC and PubChem +bcpc_up <- webchem::ping_service("bcpc") +pc_up <- webchem::ping_service("pc") diff --git a/tests/testthat/test_chent.R b/tests/testthat/test_chent.R index 76af7aa..4017042 100644 --- a/tests/testthat/test_chent.R +++ b/tests/testthat/test_chent.R @@ -3,15 +3,20 @@ test_that("We can initialise an object and add information", { expect_equal(oct$identifier, c(X1.octanol = "1-octanol")) # The name of the identifier is generated using make.names() expect_equal(oct$inchikey, structure(NA, source = "user")) expect_equal(oct$smiles, c(user = "CCCCCCCCO")) - oct$try_pubchem() |> - expect_message("Querying PubChem for name 1-octanol") |> - expect_warning("Overwriting uninitialized InChIKey") - expect_equal(oct$inchikey, structure("KBPLFHHGFOOTCA-UHFFFAOYSA-N", source = "pubchem")) - expect_equal(oct$smiles[["PubChem"]], "CCCCCCCCO") + + if (webchem::ping_service("pc")) { + oct$try_pubchem() |> + expect_message("Querying PubChem for name 1-octanol") |> + expect_warning("Overwriting uninitialized InChIKey") + expect_equal(oct$inchikey, structure("KBPLFHHGFOOTCA-UHFFFAOYSA-N", source = "pubchem")) + expect_equal(oct$smiles[["PubChem"]], "CCCCCCCCO") + expect_equal(round(oct$mw, 2), structure(130.23, source = "pubchem")) + } skip_if_no_rdkit() oct$get_rdkit() |> expect_message("Get chemical information from RDKit using user SMILES") - expect_equal(round(oct$mw, 2), structure(130.23, source = "pubchem")) + + skip_if_not(pc_up) expect_snapshot(print(oct)) }) diff --git a/tests/testthat/test_pai.R b/tests/testthat/test_pai.R index 9d73b58..7d9c566 100644 --- a/tests/testthat/test_pai.R +++ b/tests/testthat/test_pai.R @@ -1,14 +1,15 @@ test_that("a pai object is correctly generated", { - {glyphosate <- pai$new("glyphosate", rdkit = FALSE)} |> - expect_message("Querying BCPC for glyphosate") |> + skip_if_not(bcpc_up & pc_up) + {glyphosate <- pai$new("glyphosate", rdkit = FALSE)} |> + expect_message("Querying BCPC for glyphosate") |> expect_message("Querying PubChem for inchikey ") - + # Check BCPC results expect_equal(glyphosate$bcpc$cas, "1071-83-6") expect_equal(glyphosate$bcpc$formula, "C3H8NO5P") expect_equal(glyphosate$bcpc$iupac_name, "N-(phosphonomethyl)glycine") - + expect_equal(names(glyphosate$identifier), "glyphosate") # Check PubChem results -- cgit v1.2.3