diff options
Diffstat (limited to 'tests/testthat')
| -rw-r--r-- | tests/testthat/_snaps/chent.md | 17 | ||||
| -rw-r--r-- | tests/testthat/setup.R | 8 | ||||
| -rw-r--r-- | tests/testthat/test_chent.R | 40 | ||||
| -rw-r--r-- | tests/testthat/test_pai.R | 35 |
4 files changed, 65 insertions, 35 deletions
diff --git a/tests/testthat/_snaps/chent.md b/tests/testthat/_snaps/chent.md new file mode 100644 index 0000000..a512aec --- /dev/null +++ b/tests/testthat/_snaps/chent.md @@ -0,0 +1,17 @@ +# We can initialise an object and add information + + Code + print(oct) + Output + <chent> + Identifier $identifier 1-octanol + InChI Key $inchikey KBPLFHHGFOOTCA-UHFFFAOYSA-N + SMILES string $smiles: + user PubChem + "CCCCCCCCO" "CCCCCCCCO" + Molecular weight $mw: 130.2 + PubChem synonyms (up to 10): + [1] "1-octanol" "Octan-1-ol" "N-octanol" "111-87-5" + [5] "Octyl alcohol" "caprylic alcohol" "n-Octyl alcohol" "Heptyl carbinol" + [9] "1-Hydroxyoctane" "Octilin" + diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R new file mode 100644 index 0000000..4232120 --- /dev/null +++ b/tests/testthat/setup.R @@ -0,0 +1,8 @@ +# Check if we can use RDKit +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 4551cf7..a008599 100644 --- a/tests/testthat/test_chent.R +++ b/tests/testthat/test_chent.R @@ -1,24 +1,26 @@ -context("Generation of chent objects") +test_that("We can initialise an object and add information", { + oct <- chent$new("1-octanol", smiles = "CCCCCCCCO", rdkit = FALSE, pubchem = FALSE, chyaml = FALSE) + 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")) -# Check if we can use RDKit -skip_if_no_rdkit <- function() { - if (!chents:::rdkit_available) skip("RDKit is not available via reticulate") -} + 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")) + } -oct <- chent$new("1-octanol", smiles = "CCCCCCCCO", pubchem = FALSE, chyaml = FALSE) - -test_that("We can generate a chent object from SMILES using RDKit", { skip_if_no_rdkit() - expect_equivalent(round(oct$mw, 2), 130.23) - expect_equal(names(oct$identifier), "X1.octanol") - expect_equal(oct$smiles[["user"]], "CCCCCCCCO") -}) + oct$get_rdkit() |> + expect_message("Get chemical information from RDKit using user SMILES") + + # Check availability of BCPC and PubChem immediately before use + bcpc_up <- webchem::ping_service("bcpc") + pc_up <- webchem::ping_service("pc") -test_that("We can add information retrieved from PubChem via webchem", { - oct$try_pubchem() - expect_equivalent(round(oct$mw, 2), 130.23) - ik = "KBPLFHHGFOOTCA-UHFFFAOYSA-N" - attr(ik, "source") <- "pubchem" - expect_equal(oct$inchikey, ik) - expect_equal(oct$smiles[["PubChem_Canonical"]], "CCCCCCCCO") + skip_if_not(pc_up) + expect_snapshot(print(oct)) }) diff --git a/tests/testthat/test_pai.R b/tests/testthat/test_pai.R index 518fc56..fbc9757 100644 --- a/tests/testthat/test_pai.R +++ b/tests/testthat/test_pai.R @@ -1,22 +1,25 @@ -# For manual use of this file -require(chents) -require(testthat) +test_that("a pai object is correctly generated", { -context("Generation of pai objects") + # Check availability of BCPC and PubChem immediately before use + bcpc_up <- webchem::ping_service("bcpc") + pc_up <- webchem::ping_service("pc") -test_that("a pai object is correctly generated from an ambiguous name", { - skip_on_travis() # server certificate verification failed in curl_fetch_memory() - glyphosate <- pai$new("glyphosate", chyaml = FALSE) - expect_message(pai$new("benzalkonium chloride", chyaml = FALSE), "did not give results") + 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_equivalent(glyphosate$bcpc$cas, "1071-83-6") - expect_equivalent(glyphosate$bcpc$formula, "C3H8NO5P") - expect_equivalent(glyphosate$bcpc$iupac_name, "N-(phosphonomethyl)glycine") expect_equal(names(glyphosate$identifier), "glyphosate") + + # Check PubChem results ik = "XDDAORKBJWWYJS-UHFFFAOYSA-N" - attr(ik, "source") <- c("bcpc", "pubchem") - expect_equal(glyphosate$inchikey, ik) - expect_equivalent(round(glyphosate$mw, 2), 169.07) - smiles <- "C(C(=O)O)NCP(=O)(O)O" - expect_equal(glyphosate$smiles[["PubChem_Canonical"]], smiles) + expect_equal(glyphosate$inchikey, structure("XDDAORKBJWWYJS-UHFFFAOYSA-N", source = c("bcpc", "pubchem"))) + expect_equal(round(glyphosate$mw, 2), structure(169.07, source = "pubchem")) + expect_equal(glyphosate$smiles[["PubChem"]], "C(C(=O)O)NCP(=O)(O)O") }) |
