-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMain.hs
More file actions
261 lines (246 loc) · 13.4 KB
/
Main.hs
File metadata and controls
261 lines (246 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
109
110
111
112
113
114
115
116
117
118
119
120
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
module Main where
import Prelude hiding ((++))
import Control.Concurrent.MVar
import Control.Lens hiding ((.=))
import Control.Monad (void)
import Control.Monad.State (evalStateT)
import Control.Monad.Trans (liftIO)
import Data.Aeson hiding (Success)
import Data.Monoid
import qualified Data.Set as Set
import qualified Data.Text.Encoding as T
import qualified Misc
import Network.Wai (defaultRequest, rawPathInfo)
import Test.Hspec
import Web.Fn
import Web.Larceny
import Web.Offset
import Common
runTests :: IO ()
runTests = hspec $ do
Misc.tests
larcenyFillTests
cacheTests
queryTests
liveTests
main :: IO ()
main = runTests
--larcenyFillTests :: SpecM () ()
larcenyFillTests :: Spec
larcenyFillTests = do
describe "<wpPosts>" $ do
it "should show the title, id, and excerpt" $ do
"<wpPosts><wpTitle/></wpPosts>" `shouldRender` "Foo bar"
"<wpPosts><wpId/></wpPosts>" `shouldRender` "1"
"<wpPosts><wpExcerpt/></wpPosts>" `shouldRender` "summary"
it "should allow formating of dates" $
"<wpPosts> \
\ <wpDate> \
\ <wpMonth format=\"%B\"/> <wpDay format=\"%-d\"/>, <wpYear /> \
\ </wpDate> \
\</wpPosts>" `shouldRender` "October 20, 2014"
it "should render customly parsed fields" $
"<wpPosts><wpDepartments><name /></wpDepartments></wpPosts>"
`shouldRender` "some department"
describe "<wpPage>" $
it "should show the content" $
"<wpPage name=a-first-page />" `shouldRender` "<b>rendered</b> page content"
describe "<wpNoPostDuplicates/>" $ do
it "should not duplicate any posts after call to wpNoPostDuplicates" $
"<wpNoPostDuplicates/><wpPosts><wpTitle/></wpPosts><wpPosts><wpTitle/></wpPosts>" `shouldRender` "Foo bar"
it "should ignore duplicates if they were rendered before wpNoPostDuplicates" $ do
"<wpPosts><wpTitle/></wpPosts><wpNoPostDuplicates/><wpPosts><wpTitle/></wpPosts>" `shouldRender` "Foo barFoo bar"
"<wpPosts><wpTitle/></wpPosts><wpNoPostDuplicates/><wpPosts><wpTitle/></wpPosts><wpPosts><wpTitle/></wpPosts>" `shouldRender` "Foo barFoo bar"
it "should have no effect if it's at the end of the template" $
"<wpPosts><wpTitle/></wpPosts><wpPosts><wpTitle/></wpPosts><wpNoPostDuplicates/>" `shouldRender` "Foo barFoo bar"
describe "<wpPostByPermalink>" $ do
it "should query at a certain url" $ do
record <- liftIO $ newMVar []
let requestWithUrl = defaultRequest {rawPathInfo = T.encodeUtf8 "/2009/10/the-post/" }
ctxt <- liftIO $ initializer
(Right $ Requester $ fauxRequester (Just record))
(CacheSeconds 10)
""
let ctxt' = setRequest ctxt
$ (\(_,y) -> (requestWithUrl, y)) defaultFnRequest
let s = _cmssubs ctxt'
let tpl = toTpl "<wp><wpPostByPermalink><wpTitle/></wpPostByPermalink></wp"
void $ evalStateT (runTemplate tpl [] s mempty) ctxt'
liftIO (tryTakeMVar record) `shouldReturn` Just ["/wp/v2/posts?slug=the-post"]
it "should render stuff" $ do
ctxt <- initFauxRequestNoCache
let requestWithUrl = defaultRequest {rawPathInfo = T.encodeUtf8 "/2009/10/the-post/"}
let ctxt' = setRequest ctxt
$ (\(_,y) -> (requestWithUrl, y)) defaultFnRequest
let s = view cmssubs ctxt'
let tpl = toTpl "<wp><wpNoPostDuplicates/><wpPostByPermalink><wpTitle/></wpPostByPermalink><wpPosts limit=1><wpTitle/></wpPosts></wp>"
rendered <- evalStateT (runTemplate tpl [] s mempty) ctxt'
rendered `shouldBe` "Foo bar"
describe "<wpCustom>" $
it "should render an HTML comment if JSON field is null" $
"<cmsCustom endpoint=\"dev/null\"><cmsThisIsNull /></cmsCustom>" `shouldRender` "<!-- JSON field found, but value is null. -->"
describe "<cmsCustomDate>" $ do
it "should parse a date field with the format string it's given" $
"<cmsCustomDate date=\"2013-04-26 10:11:52\" format=\"%Y-%m-%d %H:%M:%S\"> \
\ <cmsDay />~<cmsMonth />~<cmsYear /> \
\ </cmsCustomDate>" `shouldRender` "26~04~2013"
it "should format a date field with the format strings it's given" $
"<cmsCustomDate date=\"2013-04-26 10:11:52\" format=\"%Y-%m-%d %H:%M:%S\"> \
\ <cmsMonth format=\"%B\"/> <cmsDay format=\"%-d\"/>, <cmsYear /> \
\ </cmsCustomDate>" `shouldRender` "April 26, 2013"
it "should use default WordPress date format if none specified" $
"<cmsCustomDate date=\"2013-04-26 10:11:52\"> \
\ <cmsDay />~<cmsMonth />~<cmsYear /> \
\ </cmsCustomDate>" `shouldRender` "26~04~2013"
it "should allow formatting the whole date in a single tag" $
"<cmsCustomDate date=\"2013-04-26 10:11:52\"> \
\ <cmsFullDate /> \
\ </cmsCustomDate>" `shouldRender` "04/26/13"
-- Caching tests
cacheTests :: Spec
cacheTests = do
describe "should grab post from cache if it's there" $
it "should render the post even w/o json source" $ do
let (Object a2) = article2
ctxt <- liftIO initNoRequestWithCache
cmsCacheSet' (view cms ctxt) (PostByPermalinkKey "2001" "10" "the-post")
(enc [a2])
("single", ctxt) `shouldRenderAtUrlContaining` ("/2001/10/the-post/", "The post")
describe "caching" $ do
it "should find nothing for a non-existent post" $ do
ctxt <- initNoRequestWithCache
p <- cmsCacheGet' (view cms ctxt) (PostByPermalinkKey "2000" "1" "the-article")
p `shouldBe` Nothing
it "should find something if there is a post in cache" $ do
ctxt <- initNoRequestWithCache
void $ cmsCacheSet' (view cms ctxt) (PostByPermalinkKey "2000" "1" "the-article")
(enc article1)
p <- cmsCacheGet' (view cms ctxt) (PostByPermalinkKey "2000" "1" "the-article")
p `shouldBe` (Just $ enc article1)
it "should not find single post after expire handler is called" $ do
ctxt <- initNoRequestWithCache
void $ cmsCacheSet' (view cms ctxt) (PostByPermalinkKey "2000" "1" "the-article")
(enc article1)
void $ cmsExpirePost' (view cms ctxt) (PostByPermalinkKey "2000" "1" "the-article")
cmsCacheGet' (view cms ctxt) (PostByPermalinkKey "2000" "1" "the-article")
>>= shouldBe Nothing
it "should find post aggregates in cache" $
do ctxt <- initNoRequestWithCache
let key = PostsKey (Set.fromList [NumFilter 20, OffsetFilter 0])
void $ cmsCacheSet' (view cms ctxt) key ("[" <> enc article1 <> "]")
void $ cmsCacheGet' (view cms ctxt) key
>>= shouldBe (Just $ "[" <> enc article1 <> "]")
it "should not find post aggregates after expire handler is called" $
do ctxt <- initNoRequestWithCache
let key = PostsKey (Set.fromList [NumFilter 20, OffsetFilter 0])
void $ cmsCacheSet' (view cms ctxt) key ("[" <> enc article1 <> "]")
void $ cmsExpirePost' (view cms ctxt) (PostByPermalinkKey "2000" "1" "the-article")
cmsCacheGet' (view cms ctxt) key
>>= shouldBe Nothing
it "should find single post after expiring aggregates" $
do ctxt <- initNoRequestWithCache
void $ cmsCacheSet' (view cms ctxt) (PostByPermalinkKey "2000" "1" "the-article")
(enc article1)
void $ cmsExpireAggregates' (view cms ctxt)
cmsCacheGet' (view cms ctxt) (PostByPermalinkKey "2000" "1" "the-article")
>>= shouldNotBe Nothing
it "should find a different single post after expiring another" $
do ctxt <- initNoRequestWithCache
let key1 = PostByPermalinkKey "2000" "1" "the-article"
key2 = PostByPermalinkKey "2001" "2" "another-article"
void $ cmsCacheSet' (view cms ctxt) key1 (enc article1)
void $ cmsCacheSet' (view cms ctxt) key2 (enc article2)
void $ cmsExpirePost' (view cms ctxt) (PostByPermalinkKey "2000" "1" "the-article")
cmsCacheGet' (view cms ctxt) key2 >>= shouldBe (Just (enc article2))
it "should be able to cache and retrieve post" $
do ctxt <- initNoRequestWithCache
let key = PostKey 200
cmsCacheSet' (view cms ctxt) key (enc article1)
cmsCacheGet' (view cms ctxt) key >>= shouldBe (Just (enc article1))
queryTests :: Spec
queryTests =
describe "generate queries from <wpPosts>" $ do
"<wpPosts></wpPosts>" `shouldQueryTo`
["/wp/v2/posts?offset=0&per_page=20"]
"<wpPosts limit=2></wpPosts>" `shouldQueryTo`
["/wp/v2/posts?offset=0&per_page=20"]
"<wpPosts offset=1 limit=1></wpPosts>" `shouldQueryTo`
["/wp/v2/posts?offset=1&per_page=20"]
"<wpPosts offset=0 limit=1></wpPosts>" `shouldQueryTo`
["/wp/v2/posts?offset=0&per_page=20"]
"<wpPosts limit=10 page=1></wpPosts>" `shouldQueryTo`
["/wp/v2/posts?offset=0&per_page=20"]
"<wpPosts limit=10 page=2></wpPosts>" `shouldQueryTo`
["/wp/v2/posts?offset=20&per_page=20"]
"<wpPosts num=2></wpPosts>" `shouldQueryTo`
["/wp/v2/posts?offset=0&per_page=2"]
"<wpPosts num=2 page=2 limit=1></wpPosts>" `shouldQueryTo`
["/wp/v2/posts?offset=2&per_page=2"]
"<wpPosts num=1 page=3></wpPosts>" `shouldQueryTo`
["/wp/v2/posts?offset=2&per_page=1"]
"<wpPosts tags=\"+home-featured\" limit=10></wpPosts>" `shouldQueryTo`
["/wp/v2/posts?offset=0&per_page=20&tags[]=177"]
"<wpPosts tags=\"-home-featured\" limit=1></wpPosts>" `shouldQueryTo`
["/wp/v2/posts?offset=0&per_page=20&tags_exclude[]=177"]
"<wpPosts tags=\"+home-featured,-featured-global\" limit=1><wpTitle/></wpPosts>" `shouldQueryTo`
["/wp/v2/posts?offset=0&per_page=20&tags[]=177&tags_exclude[]=160"]
"<wpPosts tags=\"+home-featured,+featured-global\" limit=1><wpTitle/></wpPosts>" `shouldQueryTo`
["/wp/v2/posts?offset=0&per_page=20&tags[]=160&tags[]=177"]
"<wpPosts categories=\"bookmarx\" limit=10><wpTitle/></wpPosts>" `shouldQueryTo`
["/wp/v2/posts?categories[]=159&offset=0&per_page=20"]
"<wpPosts categories=\"-bookmarx\" limit=10><wpTitle/></wpPosts>" `shouldQueryTo`
["/wp/v2/posts?categories_exclude[]=159&offset=0&per_page=20"]
"<wp><div><wpPosts categories=\"bookmarx\" limit=10><wpTitle/></wpPosts></div></wp>" `shouldQueryTo`
replicate 2 "/wp/v2/posts?categories[]=159&offset=0&per_page=20"
"<wpPage name=blah />" `shouldQueryTo`
["/wp/v2/pages?slug=blah"]
liveTests :: Spec
liveTests =
describe "live tests (which require running wordpress server)" $ do
ctxt <- runIO $ initializer (Left ("offset", "111")) NoCache "http://localhost:5555/wp-json"
void $ runIO $ clearRedisCache ctxt
do it "should have title on page" $
("single", ctxt)
`shouldRenderAtUrlContaining` ("/2014/10/a-first-post", "A first post")
it "should be able to limit" $
("many", ctxt) `rendersDifferentlyFrom` "many1"
it "should be able to offset" $
("many2", ctxt) `rendersDifferentlyFrom` "many3"
it "should be able to get page 2" $
("page1", ctxt) `rendersDifferentlyFrom` "page2"
it "should be able to use page, num, and limit" $ do
("num1", ctxt) `rendersDifferentlyFrom` "num2"
("num2", ctxt) `rendersSameAs` "num3"
it "should be able to restrict based on tags" $
("tag1", ctxt) `rendersDifferentlyFrom` "tag2"
it "should be able to say +tag instead of tag" $
("tag1", ctxt) `rendersSameAs` "tag3"
it "should be able to say -tag to NOT match a tag" $
("/tag4", ctxt) `rendersDifferentlyFrom` "tag5"
it "should be able to have multiple tag queries" $
("/tag6", ctxt) `rendersDifferentlyFrom` "tag7"
it "should be able to get customly parsed attribute date" $
("author-date", ctxt) `shouldRenderAtUrlContaining`
("/2014/10/a-second-post/", "2014/10")
it "should be able to restrict based on category" $
("cat1", ctxt) `rendersDifferentlyFrom` "cat2"
it "should be able to make negative category queries" $
("cat1", ctxt) `rendersDifferentlyFrom` "cat3"
it "should be able to render a single page" $
("single-page", ctxt) `shouldRenderAtUrlContaining`
("/pages?slug=a-first-post", "This is the first page content")
it "should be able to query custom taxonomies" $ do
("department", ctxt) `shouldRenderContaining` "A sports post"
("department", ctxt) `shouldNotRenderContaining` "A first post"
it "should be able to query custom endpoints" $ do
("custom-endpoint-object", ctxt) `shouldRenderContaining` "categories"
("custom-endpoint-object", ctxt) `shouldNotRenderContaining` "departments"
it "should be able to query custom endpoints" $ do
("custom-endpoint-array", ctxt) `shouldRenderContaining` "2014-10-01"
("custom-endpoint-array", ctxt) `shouldRenderContaining` "2014-10-02"
("custom-endpoint-array", ctxt) `shouldRenderContaining` "2014-10-15"
it "should be able to reference fields from the custom endpoint in another custom endpoint query" $
("custom-endpoint-array", ctxt) `rendersSameAs` "custom-endpoint-enter-the-matrix"