Commit 3eebda86 authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Make Phylo tests using tasty-golden properly

parent 893b4cf8
Pipeline #6874 failed with stages
in 45 minutes and 9 seconds
...@@ -416,3 +416,34 @@ with `ghcup`: ...@@ -416,3 +416,34 @@ with `ghcup`:
ghcup compile hls --version 2.7.0.0 --ghc 9.4.7 ghcup compile hls --version 2.7.0.0 --ghc 9.4.7
``` ```
https://haskell-language-server.readthedocs.io/en/latest/installation.html https://haskell-language-server.readthedocs.io/en/latest/installation.html
## Running the tests
Running the tests can be done via the following command:
```hs
cabal v2-test --test-show-details=streaming --flags 'test-crypto no-phylo-debug-logs'
```
The flags have the following meaning:
* `test-crypto`: Switch to use very fast (but not production-secure) cryptography, so that tests runs
faster;
* `no-phylo-debug-logs`: Suppresses the debugging logs which would normally be present in phylo pure (!) code.
In order for some tests to run (like the phylo ones) is **required** to install the `gargantext-cli` via:
```hs
cabal v2-install gargantext:exe:gargantext-cli
```
### Modifying a golden test to accept a new (expected) output
Some tests, like the Phylo one, use golden testing to ensure that the JSON Phylo we generate is
the same as an expected one. This allows us to catch regressions in the serialisation or in the algorithm.
Sometimes, however, we genuinely want to modify the output so that it's the new reference (i.e. the new
golden reference). To do so, it's enough to run the testsuite passing the `--accept` flag, for example:
```hs
cabal v2-test garg-test-tasty --test-show-details=streaming --flags 'test-crypto no-phylo-debug-logs' --test-option=--pattern='/Phylo/' --test-option=--accept"
```
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -7,21 +7,24 @@ module Test.Offline.Phylo (tests) where ...@@ -7,21 +7,24 @@ module Test.Offline.Phylo (tests) where
import CLI.Phylo.Common import CLI.Phylo.Common
import Data.Aeson as JSON import Data.Aeson as JSON
import Data.Aeson.Types qualified as JSON import Data.Aeson.Types qualified as JSON
import Data.Aeson.Encode.Pretty qualified as JSON
import Data.ByteString.Lazy qualified as BL
import Data.GraphViz.Attributes.Complete qualified as Graphviz import Data.GraphViz.Attributes.Complete qualified as Graphviz
import Data.Text.Lazy as TL import Data.Text.Lazy as TL
import Data.TreeDiff import Data.TreeDiff
import Data.Vector qualified as V import Data.Vector qualified as V
import Gargantext.Core.Text.List.Formats.TSV import Gargantext.Core.Text.List.Formats.TSV
import Gargantext.Core.Types.Phylo hiding (Phylo(..)) import Gargantext.Core.Types.Phylo hiding (Phylo(..))
import Gargantext.Core.Viz.Phylo hiding (EdgeType(..))
import Gargantext.Core.Viz.Phylo.API.Tools (readPhylo, phylo2dot2json) import Gargantext.Core.Viz.Phylo.API.Tools (readPhylo, phylo2dot2json)
import Gargantext.Core.Viz.Phylo.Example qualified as Cleopatre import Gargantext.Core.Viz.Phylo.Example qualified as Cleopatre
import Gargantext.Core.Viz.Phylo hiding (EdgeType(..))
import Gargantext.Core.Viz.Phylo.PhyloExport import Gargantext.Core.Viz.Phylo.PhyloExport
import Gargantext.Core.Viz.Phylo.PhyloMaker (toPhylo, toPhyloWithoutLink) import Gargantext.Core.Viz.Phylo.PhyloMaker (toPhylo, toPhyloWithoutLink)
import Gargantext.Core.Viz.Phylo.PhyloTools import Gargantext.Core.Viz.Phylo.PhyloTools
import Paths_gargantext import Paths_gargantext
import Prelude import Prelude
import Test.Tasty import Test.Tasty
import Test.Tasty.Golden (goldenVsStringDiff)
import Test.Tasty.HUnit import Test.Tasty.HUnit
phyloTestConfig :: PhyloConfig phyloTestConfig :: PhyloConfig
...@@ -48,6 +51,12 @@ phyloTestConfig = PhyloConfig { ...@@ -48,6 +51,12 @@ phyloTestConfig = PhyloConfig {
, exportFilter = [ByBranchSize {_branch_size = 3.0}] , exportFilter = [ByBranchSize {_branch_size = 3.0}]
} }
phyloGolden :: TestName -> (FilePath, IO BL.ByteString) -> TestTree
phyloGolden testName (fp, action) =
goldenVsStringDiff testName differ fp action
where
differ ref new = [ "diff", "-u", "-w", "--color=always", ref, new]
tests :: TestTree tests :: TestTree
tests = testGroup "Phylo" [ tests = testGroup "Phylo" [
testGroup "Export" [ testGroup "Export" [
...@@ -56,7 +65,7 @@ tests = testGroup "Phylo" [ ...@@ -56,7 +65,7 @@ tests = testGroup "Phylo" [
] ]
, testGroup "toPhyloWithoutLink" [ , testGroup "toPhyloWithoutLink" [
testCase "returns expected data" testSmallPhyloWithoutLinkExpectedOutput testCase "returns expected data" testSmallPhyloWithoutLinkExpectedOutput
, testCase "phyloCleopatre returns expected data" testCleopatreWithoutLinkExpectedOutput , phyloGolden "phyloCleopatre returns expected data" testCleopatreWithoutLinkExpectedOutput
, testCase "Nadal canned corpus returns expected data" testNadalWithoutLinkExpectedOutput , testCase "Nadal canned corpus returns expected data" testNadalWithoutLinkExpectedOutput
] ]
, testGroup "phylo2dot2json" [ , testGroup "phylo2dot2json" [
...@@ -71,11 +80,10 @@ tests = testGroup "Phylo" [ ...@@ -71,11 +80,10 @@ tests = testGroup "Phylo" [
, testCase "parses csv phylo" testCsvPhylo , testCase "parses csv phylo" testCsvPhylo
] ]
testCleopatreWithoutLinkExpectedOutput :: Assertion testCleopatreWithoutLinkExpectedOutput :: (FilePath, IO BL.ByteString)
testCleopatreWithoutLinkExpectedOutput = do testCleopatreWithoutLinkExpectedOutput =
let actual = toPhyloWithoutLink Cleopatre.docs Cleopatre.config let actual = toPhyloWithoutLink Cleopatre.docs Cleopatre.config
expected <- readPhylo =<< getDataFileName "test-data/phylo/cleopatre.golden.json" in ("test-data/phylo/cleopatre.golden.json", pure $ JSON.encodePretty actual)
assertBool (show $ ansiWlEditExprCompact $ ediff expected actual) (expected == actual)
testNadalWithoutLinkExpectedOutput :: Assertion testNadalWithoutLinkExpectedOutput :: Assertion
testNadalWithoutLinkExpectedOutput = do testNadalWithoutLinkExpectedOutput = do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment