Spec.hs 6.23 KB
Newer Older
1
{-# LANGUAGE QuasiQuotes, OverloadedStrings #-}
Henrik Tramberend's avatar
Henrik Tramberend committed
2

3 4
import Test.Hspec

Henrik Tramberend's avatar
Henrik Tramberend committed
5 6
import Data.Maybe
import Data.Text
7
import Data.Text.Encoding
Henrik Tramberend's avatar
Henrik Tramberend committed
8 9
import Text.Pandoc
import Utilities
10
import Student
Henrik Tramberend's avatar
Henrik Tramberend committed
11 12 13 14
import System.FilePath
import System.FilePath.Posix
import System.FilePath.Glob
import qualified Data.Yaml as Y
15 16
import qualified Data.HashMap.Strict as Map
import NeatInterpolation
17

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
main = do
  projectDir <- calcProjectDirectory
  --
  let publicDir = projectDir </> "public"
  let cacheDir = publicDir </> "cache"
  let supportDir = publicDir </> "support"
  --
  metaFiles <- globDir1 (compile "**/*-meta.yaml") projectDir
  putStrLn $ show metaFiles
  genStudentData projectDir
  --
  hspec $
  --
    do describe "isCacheableURI" $
         do it "returns True if URL has http: or https: protocol" $
              do isCacheableURI "http://heise.de" `shouldBe` True
                 isCacheableURI "ftp://heise.de" `shouldBe` False
Henrik Tramberend's avatar
Henrik Tramberend committed
35
       --
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
       describe "adjustLocalUrl" $
         do it
              "adjusts URL to be relative to the project root or the provided base directory" $
              do adjustLocalUrl projectDir "base" "http://heise.de" `shouldBe`
                   "http://heise.de"
                 --
                 adjustLocalUrl projectDir "base" "/some/where" `shouldBe`
                   projectDir </>
                   "some/where"
                 --
                 adjustLocalUrl projectDir "base" "some/where" `shouldBe`
                   "base/some/where"
       --
       describe "makeRelativeTo" $
         do it "calculates the path of file relative to dir. Inlcudes '..'" $
              do makeRelativeTo "" "img.png" `shouldBe` "img.png"
                 makeRelativeTo "/one/two" "/one/two/img.png" `shouldBe`
                   "img.png"
                 makeRelativeTo "/one/two/three" "/one/two/four/img.png" `shouldBe`
                   "../four/img.png"
                 makeRelativeTo "/some/where/else" "/one/two/four/img.png" `shouldBe`
                   "../../../one/two/four/img.png"
                 makeRelativeTo
                   "/Users/henrik/tmp/decker-demo/public"
                   "/Users/henrik/tmp/decker-demo/public/cache/b48cadafb942dc1426316772321dd0c7.png" `shouldBe`
                   "cache/b48cadafb942dc1426316772321dd0c7.png"
       --
       describe "cacheRemoteFile" $
         it
           "Stores the data behind a URL locally, if possible. Return the local path to the cached file." $
         do cacheRemoteFile
              cacheDir
              "https://tramberend.beuth-hochschule.de/img/htr-beuth.jpg" `shouldReturn`
              cacheDir </>
              "bc137c359488beadbb61589f7fe9e208.jpg"
            cacheRemoteFile
              cacheDir
              "ftp://tramberend.beuth-hochschule.de/img/htr-beuth.jpg" `shouldReturn`
              "ftp://tramberend.beuth-hochschule.de/img/htr-beuth.jpg"
            cacheRemoteFile cacheDir "/img/htr-beuth.jpg" `shouldReturn`
              "/img/htr-beuth.jpg"
            cacheRemoteFile cacheDir "img/htr-beuth.jpg" `shouldReturn`
              "img/htr-beuth.jpg"
       --
       describe "cacheRemoteImages" $
         it
           "Replaces all remote images in the pandoc document with locally caches copies." $
         do cacheRemoteImages
              cacheDir
              (Pandoc
                 nullMeta
                 [ (Para
                      [ Image
                          nullAttr
                          []
                          ( "https://tramberend.beuth-hochschule.de/img/htr-beuth.jpg"
                          , "")
                      ])
                 ]) `shouldReturn`
              (Pandoc
                 nullMeta
                 [ (Para
                      [ Image
                          nullAttr
                          []
                          ( cacheDir </> "bc137c359488beadbb61589f7fe9e208.jpg"
                          , "")
                      ])
                 ])
       --
       describe "parseStudentData" $
         it "Parses student data in YAML format into a nifty data structure." $
         do parseStudentData projectDir `shouldReturn` Just realData
109 110

mockData :: Students
111 112 113 114 115 116 117 118
mockData =
  Students $
  Map.fromList
    [ ( "888888"
      , Student "mock" "mock" "mock" "mock" "mock" "mock" "mock" "mock" 1)
    , ( "888889"
      , Student "mock" "mock" "mock" "mock" "mock" "mock" "mock" "mock" 1)
    ]
119 120

realData :: Students
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
realData =
  Students
    (Map.fromList
       [ ( "836381"
         , Student
           { std_uid = "s64386"
           , std_department = "FB6"
           , std_displayName = "Justen, David Alexander"
           , std_employeeNumber = "836381"
           , std_givenName = "David Alexander"
           , std_mail = "s64386@beuth-hochschule.de"
           , std_sAMAccountName = "s64386"
           , std_sn = "Justen"
           , std_track = 1
           })
       , ( "798101"
         , Student
           { std_uid = "s53445"
           , std_department = "FB6"
           , std_displayName = "Mahmoud, Hassan"
           , std_employeeNumber = "798101"
           , std_givenName = "Hassan"
           , std_mail = "s53445@beuth-hochschule.de"
           , std_sAMAccountName = "s53445"
           , std_sn = "Mahmoud"
           , std_track = 1
           })
       , ( "814510"
         , Student
           { std_uid = "s57637"
           , std_department = "FB6"
           , std_displayName = "Sahli, Hanen"
           , std_employeeNumber = "814510"
           , std_givenName = "Hanen"
           , std_mail = "s57637@beuth-hochschule.de"
           , std_sAMAccountName = "s57637"
           , std_sn = "Sahli"
           , std_track = 1
           })
       , ( "832701"
         , Student
           { std_uid = "s61660"
           , std_department = "FB6"
           , std_displayName = "Naci Aydogan"
           , std_employeeNumber = "832701"
           , std_givenName = "Naci"
           , std_mail = "s61660@beuth-hochschule.de"
           , std_sAMAccountName = "s61660"
           , std_sn = "Aydogan"
           , std_track = 1
           })
       ])
173 174

genStudentData :: FilePath -> IO ()
175 176
genStudentData dir =
  Y.encodeFile (dir </> "test/student-mock-data.yaml") mockData
177 178

parseStudentData :: FilePath -> IO (Maybe Students)
179 180
parseStudentData dir = do
  Y.decodeFile $ dir </> "test/student-test-data.yaml"