aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorJoris2019-09-05 20:46:36 +0200
committerJoris2019-09-05 20:46:36 +0200
commit317e7b1e7319182e5caa5169119aea9fc8d660b6 (patch)
treedc9f9c458c42d1e2c60a12ff55267e042c88f6ba /src/test
parent223ae6aa0b14c071d5719ada0cc6b43e9199a81b (diff)
Enable the listener only during some hours
Diffstat (limited to 'src/test')
-rw-r--r--src/test/haskell/Main.hs67
-rw-r--r--src/test/haskell/ParserSpec.hs63
-rw-r--r--src/test/haskell/TimeSpec.hs30
3 files changed, 101 insertions, 59 deletions
diff --git a/src/test/haskell/Main.hs b/src/test/haskell/Main.hs
index 541cc54..5422108 100644
--- a/src/test/haskell/Main.hs
+++ b/src/test/haskell/Main.hs
@@ -1,62 +1,11 @@
-import Data.Maybe (catMaybes)
-import qualified Data.Text.IO as T
-import qualified Network.Wreq.Session as Session
-import Test.Hspec
+module Main (main) where
-import qualified Ads
-import qualified FetchAd
-import Model.Ad (Ad (..))
-import qualified Parser.LeboncoinParser as LeboncoinParser
--- import qualified Parser.OuestFranceParser as OuestFranceParser
--- import qualified Parser.SeLogerParser as SeLogerParser
+import qualified Test.Hspec as Hspec
-main :: IO ()
-main = do
- session <- Session.newSession
-
- hspec $ do
- describe "LeboncoinParser" $ do
-
- it "should parse no results from empty string" $ do
- LeboncoinParser.parse "" `shouldBe` []
-
- it "should parse ads from local page" $ do
- ads <- T.readFile "src/test/resources/leboncoin.html"
- LeboncoinParser.parse ads `shouldBe` Ads.leboncoin
+import qualified ParserSpec
+import qualified TimeSpec
- it "should parse ads from remote page" $ do
- ads <- FetchAd.leboncoin
- session
- ["https://www.leboncoin.fr/annonces/offres/ile_de_france/"]
- checkAds ads
-
- -- describe "OuestFranceParser" $ do
- --
- -- it "should parse no results from empty string" $ do
- -- OuestFranceParser.parse "" `shouldBe` []
- --
- -- it "should parse ads from page" $ do
- -- rawOuestFranceAds <- T.readFile "src/test/resources/ouestFrance.html"
- -- OuestFranceParser.parse rawOuestFranceAds `shouldBe` Ads.ouestFrance
- --
- -- it "should parse ads from remote page" $ do
- -- ads <- FetchAd.ouestFrance ["https://www.ouestfrance-immo.com/louer/appartement/rennes-35-35000/"]
- -- checkAds ads
- --
- -- describe "SeLogerParser" $ do
- --
- -- it "should parse no results from empty string" $ do
- -- SeLogerParser.parse "" `shouldBe` []
- --
- -- it "should parse ads from page" $ do
- -- ads <- T.readFile "src/test/resources/seLoger.html"
- -- SeLogerParser.parse ads `shouldBe` Ads.seLoger
- --
- -- it "should parse ads from remote page" $ do
- -- ads <- FetchAd.seLoger ["https://www.seloger.com/list.htm?tri=initial&idtypebien=2,1&idtt=2,5&naturebien=1,2,4&ci=690123"]
- -- checkAds ads
-
-checkAds :: [Ad] -> IO ()
-checkAds ads = do
- length ads `shouldSatisfy` (\n -> n > 10)
- (length . catMaybes . map price $ ads) `shouldSatisfy` (\n -> n > 10)
+main :: IO ()
+main = Hspec.hspec $ do
+ ParserSpec.spec
+ TimeSpec.spec
diff --git a/src/test/haskell/ParserSpec.hs b/src/test/haskell/ParserSpec.hs
new file mode 100644
index 0000000..64b2b33
--- /dev/null
+++ b/src/test/haskell/ParserSpec.hs
@@ -0,0 +1,63 @@
+module ParserSpec (spec) where
+
+import Data.Maybe (catMaybes)
+import qualified Data.Text.IO as T
+import qualified Network.Wreq.Session as Session
+import Test.Hspec
+
+import qualified Ads
+import qualified FetchAd
+import Model.Ad (Ad (..))
+import qualified Parser.LeboncoinParser as LeboncoinParser
+-- import qualified Parser.OuestFranceParser as OuestFranceParser
+-- import qualified Parser.SeLogerParser as SeLogerParser
+
+spec :: Spec
+spec = do
+ describe "Parser" $ do
+ session <- runIO Session.newSession
+ describe "LeBonCoin" $ do
+
+ it "should parse no results from empty string" $ do
+ LeboncoinParser.parse "" `shouldBe` []
+
+ it "should parse ads from local page" $ do
+ ads <- T.readFile "src/test/resources/leboncoin.html"
+ LeboncoinParser.parse ads `shouldBe` Ads.leboncoin
+
+ it "should parse ads from remote page" $ do
+ ads <- FetchAd.leboncoin
+ session
+ ["https://www.leboncoin.fr/annonces/offres/ile_de_france/"]
+ checkAds ads
+
+ -- describe "OuestFrance" $ do
+ --
+ -- it "should parse no results from empty string" $ do
+ -- OuestFranceParser.parse "" `shouldBe` []
+ --
+ -- it "should parse ads from page" $ do
+ -- rawOuestFranceAds <- T.readFile "src/test/resources/ouestFrance.html"
+ -- OuestFranceParser.parse rawOuestFranceAds `shouldBe` Ads.ouestFrance
+ --
+ -- it "should parse ads from remote page" $ do
+ -- ads <- FetchAd.ouestFrance ["https://www.ouestfrance-immo.com/louer/appartement/rennes-35-35000/"]
+ -- checkAds ads
+ --
+ -- describe "SeLoger" $ do
+ --
+ -- it "should parse no results from empty string" $ do
+ -- SeLogerParser.parse "" `shouldBe` []
+ --
+ -- it "should parse ads from page" $ do
+ -- ads <- T.readFile "src/test/resources/seLoger.html"
+ -- SeLogerParser.parse ads `shouldBe` Ads.seLoger
+ --
+ -- it "should parse ads from remote page" $ do
+ -- ads <- FetchAd.seLoger ["https://www.seloger.com/list.htm?tri=initial&idtypebien=2,1&idtt=2,5&naturebien=1,2,4&ci=690123"]
+ -- checkAds ads
+
+checkAds :: [Ad] -> IO ()
+checkAds ads = do
+ length ads `shouldSatisfy` (\n -> n > 10)
+ (length . catMaybes . map price $ ads) `shouldSatisfy` (\n -> n > 10)
diff --git a/src/test/haskell/TimeSpec.hs b/src/test/haskell/TimeSpec.hs
new file mode 100644
index 0000000..4248e68
--- /dev/null
+++ b/src/test/haskell/TimeSpec.hs
@@ -0,0 +1,30 @@
+module TimeSpec (spec) where
+
+import Data.Time.Clock (DiffTime)
+import qualified Data.Time.Clock as Clock
+import Test.Hspec
+
+import qualified Utils.Time as TimeUtils
+
+spec :: Spec
+spec =
+ describe "Utils.Time" $
+ describe "asleepDuration" $ do
+
+ it "should not be asleep in range" $ do
+ TimeUtils.asleepDuration (hour 8) (hour 22) (hour 15) `shouldBe` Nothing
+
+ it "should not be asleep in day overlapping range" $ do
+ TimeUtils.asleepDuration (hour 22) (hour 8) (hour 6) `shouldBe` Nothing
+
+ it "should be asleep before the range" $ do
+ TimeUtils.asleepDuration (hour 10) (hour 12) (hour 6) `shouldBe` Just (hour 4)
+
+ it "should be asleep after the range" $ do
+ TimeUtils.asleepDuration (hour 10) (hour 14) (hour 15) `shouldBe` Just (hour 19)
+
+ it "should be asleep before a day overlapping range" $ do
+ TimeUtils.asleepDuration (hour 23) (hour 1) (hour 3) `shouldBe` Just (hour 20)
+
+hour :: Int -> DiffTime
+hour h = Clock.secondsToDiffTime (fromIntegral h * 60 * 60)