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 session ["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 session ["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)