From b3cff82c7513845ef46d25a9c72d10fca4e28dca Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alexandre=20Delano=C3=AB?= <devel+git@delanoe.org>
Date: Fri, 17 Jul 2020 14:23:25 +0200
Subject: [PATCH] [Tests] crypto tests added

---
 package.yaml                    |  1 +
 src-test/Graph/Distance.hs      | 28 +++++++++++++++++++++
 src-test/Main.hs                |  4 ++-
 src-test/Utils/Crypto.hs        | 44 +++++++++++++++++++++++++++++++++
 src/Gargantext/Prelude/Utils.hs |  6 ++---
 5 files changed, 79 insertions(+), 4 deletions(-)
 create mode 100644 src-test/Graph/Distance.hs
 create mode 100644 src-test/Utils/Crypto.hs

diff --git a/package.yaml b/package.yaml
index 15db6a2b..44708a23 100644
--- a/package.yaml
+++ b/package.yaml
@@ -56,6 +56,7 @@ library:
   - Gargantext.Database.Admin.Types.Hyperdata
   - Gargantext.Database.Admin.Types.Node
   - Gargantext.Prelude
+  - Gargantext.Prelude.Utils
   - Gargantext.Text
   - Gargantext.Text.Context
   - Gargantext.Text.Corpus.Parsers
diff --git a/src-test/Graph/Distance.hs b/src-test/Graph/Distance.hs
new file mode 100644
index 00000000..3630d8df
--- /dev/null
+++ b/src-test/Graph/Distance.hs
@@ -0,0 +1,28 @@
+
+{-|
+Module      : Graph.Distance
+Description : 
+Copyright   : (c) CNRS, 2017-Present
+License     : AGPL + CECILL v3
+Maintainer  : team@gargantext.org
+Stability   : experimental
+Portability : POSIX
+
+Here is a longer description of this module, containing some
+commentary with @some markup@.
+-}
+
+
+module Graph.Distance where
+
+import Test.Hspec
+import Gargantext.Viz.Graph.Distances.Matrice
+import Gargantext.Prelude
+
+test :: IO ()
+test = hspec $ do
+  describe "Cross" $ do
+    let result = cross' $ matrix 3 ([1,1..] :: [Double])
+    it "compare" $ do
+      shouldBe result (matrix 3 ([2,2..] :: [Double]))
+
diff --git a/src-test/Main.hs b/src-test/Main.hs
index 64cbeb65..5188f9ac 100644
--- a/src-test/Main.hs
+++ b/src-test/Main.hs
@@ -17,6 +17,7 @@ import qualified Ngrams.Lang.Occurrences as Occ
 import qualified Ngrams.Metrics as Metrics
 import qualified Parsers.Date as PD
 import qualified Graph.Distance as GD
+import qualified Utils.Crypto as Crypto
 
 main :: IO ()
 main = do
@@ -25,4 +26,5 @@ main = do
 --    Lang.ngramsExtractionTest EN
 --    Metrics.main
     PD.testFromRFC3339
-    GD.test
+--    GD.test
+    Crypto.test
diff --git a/src-test/Utils/Crypto.hs b/src-test/Utils/Crypto.hs
new file mode 100644
index 00000000..da89dc42
--- /dev/null
+++ b/src-test/Utils/Crypto.hs
@@ -0,0 +1,44 @@
+
+{-|
+Module      : Utils.Crypto
+Description : 
+Copyright   : (c) CNRS, 2017-Present
+License     : AGPL + CECILL v3
+Maintainer  : team@gargantext.org
+Stability   : experimental
+Portability : POSIX
+
+-}
+
+module Utils.Crypto where
+
+import Data.Text (Text)
+import Test.Hspec
+import Gargantext.Prelude
+import Gargantext.Prelude.Utils
+
+-- | Crypto Hash tests
+test :: IO ()
+test = hspec $ do
+  describe "Hash String with frontend works" $ do
+    let text = "To hash with backend" :: Text
+    let hashed = "8a69a94d164279af2b7d1443ce08da6184b3d7e815406076e148159c284b53c3" :: Hash
+                 -- ^ hash from fronted with text above
+    it "compare" $ do
+      hash text `shouldBe` hashed
+
+  describe "Hash List with backend works" $ do
+    let list = ["a","b"] :: [Text]
+    let hashed = "ab19ec537f09499b26f0f62eed7aefad46ab9f498e06a7328ce8e8ef90da6d86" :: Hash
+                 -- ^ hash from frontend with text above
+    it "compare" $ do
+      hash list `shouldBe` hashed
+
+------------------------------------------------------------------------
+-- | TODO property based tests
+  describe "Hash works with any order of list" $ do
+    let hash1 = hash (["a","b"] :: [Text])
+    let hash2 = hash (["b","a"] :: [Text])
+    it "compare" $ do
+      hash1 `shouldBe` hash2
+
diff --git a/src/Gargantext/Prelude/Utils.hs b/src/Gargantext/Prelude/Utils.hs
index 7b0af5aa..3a741510 100644
--- a/src/Gargantext/Prelude/Utils.hs
+++ b/src/Gargantext/Prelude/Utils.hs
@@ -54,7 +54,7 @@ instance IsHashable Char.ByteString where
         . SHA.showDigest
         . SHA.sha256
 
-instance IsHashable String where
+instance {-# OVERLAPPING #-} IsHashable String where
   hash = hash . Char.pack
 
 instance IsHashable Text where
@@ -63,8 +63,8 @@ instance IsHashable Text where
 instance IsHashable (Set Hash) where
   hash = hash . foldl (<>) "" . Set.toList
 
-instance IsHashable [Hash] where
-  hash = hash . Set.fromList
+instance {-# OVERLAPPABLE #-} IsHashable a => IsHashable [a] where
+  hash = hash . Set.fromList . map hash
 
 --------------------------------------------------------------------------
 data NodeToHash = NodeToHash { nodeType :: NodeType
-- 
2.21.0