11(ns buran.core-test
22 (:require
33 [buran.core :refer :all ]
4- [clojure.test :refer :all ]))
4+ [clojure.test :refer :all ])
5+ (:import [java.io ByteArrayInputStream File]))
56
67
78(def short-feed {:info {:feed-type " atom_1.0"
89 :title " Feed title" }
910 :entries [{:title " Entry title"
1011 :description {:value " entry description" }}]})
1112
13+
1214(def short-feed-str " <?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\r\n <feed xmlns=\" http://www.w3.org/2005/Atom\" >\r\n <title>Feed title</title>\r\n <subtitle />\r\n <entry>\r\n <title>Entry title</title>\r\n <author>\r\n <name />\r\n </author>\r\n <summary>entry description</summary>\r\n </entry>\r\n </feed>\r\n " )
1315
1416
1517(deftest produce-feed
16- (testing " Producing a feed"
17- (is (= (produce {:feed short-feed
18- :pretty-print false })
19- " <?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\r\n <feed xmlns=\" http://www.w3.org/2005/Atom\" ><title>Feed title</title><subtitle /><entry><title>Entry title</title><author><name /></author><summary>entry description</summary></entry></feed>\r\n " ))))
18+ (let [expected " <?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\r\n <feed xmlns=\" http://www.w3.org/2005/Atom\" ><title>Feed title</title><subtitle /><entry><title>Entry title</title><author><name /></author><summary>entry description</summary></entry></feed>\r\n " ]
19+ (testing " Produce the feed to various types"
20+ (is (= expected (produce {:feed short-feed
21+ :pretty-print false })))
22+ (is (instance? org.w3c.dom.Document (produce {:feed short-feed
23+ :to :w3cdom })))
24+ (is (instance? org.jdom2.Document (produce {:feed short-feed
25+ :to :jdom }))))
26+ (testing " Produce the feed to a file"
27+ (let [pathname " target/feed.xml" ]
28+ (produce {:feed short-feed
29+ :to pathname
30+ :pretty-print false })
31+ (is (= expected (slurp pathname))))
32+ (let [pathname " target/feed2.xml" ]
33+ (produce {:feed short-feed
34+ :to (File. pathname)
35+ :pretty-print false })
36+ (is (= expected (slurp pathname)))))))
2037
2138
2239(deftest produce-pretty-printed-feed
7087 :description {:value " entry description 2" }}]}))))
7188
7289
90+ (deftest consume-from-string
91+ (is (= short-feed (shrink (consume short-feed-str))))
92+ (is (= short-feed (shrink (consume {:from short-feed-str})))))
93+
94+
95+ (deftest consume-http-test
96+ (testing " from input stream"
97+ (is (= short-feed (shrink (consume-http {:from (ByteArrayInputStream. (.getBytes short-feed-str))}))))))
98+
99+
100+ (deftest negative-consume-http
101+ (let [{:keys [error message]} (consume-http " invalid://url" )]
102+ (is (instance? Throwable error))
103+ (is (= " unknown protocol: invalid" message))))
104+
105+
106+ (deftest negative-consume
107+ (testing " throwable"
108+ (is (instance? Throwable (try
109+ (consume {:throw-exception true
110+ :from " <invalid rss>" })
111+ (catch Throwable e
112+ e)))))
113+ (testing " map with an exception"
114+ (let [{:keys [error message]} (consume {:throw-exception false
115+ :from " <invalid rss>" })]
116+ (is (instance? Throwable error))
117+ (is (string? message)))))
118+
119+
73120(deftest consume-produce-roundtrip
74121 (is (= short-feed (-> short-feed
75122 produce
76123 consume
77- shrink))
78- (is (= short-feed-str (-> short-feed-str
79- consume
80- shrink
81- produce)))))
82-
83-
84-
124+ shrink)))
125+ (is (= short-feed-str (-> short-feed-str
126+ consume
127+ shrink
128+ produce))))
129+
130+
131+ (def real-feed '{:info {:description " most recent 30 from stackoverflow.com"
132+ :feed-type " atom_1.0"
133+ :link " https://stackoverflow.com/questions/tagged/?tagnames=clojure&sort=active"
134+ :links ({:href " https://stackoverflow.com/feeds/tag?tagnames=clojure"
135+ :length 0
136+ :rel " self"
137+ :type " application/atom+xml" }
138+ {:href " https://stackoverflow.com/questions/tagged/?tagnames=clojure&sort=active"
139+ :length 0
140+ :rel " alternate"
141+ :type " text/html" })
142+ :published-date #inst " 2020-02-22T16:12:21.000-00:00"
143+ :title " Active questions tagged clojure - Stack Overflow"
144+ :uri " https://stackoverflow.com/feeds/tag?tagnames=clojure" }
145+ :entries [{:author " Jim"
146+ :authors ({:name " Jim"
147+ :uri " https://stackoverflow.com/users/5673289" })
148+ :categories ({:name " bidi"
149+ :taxonomy-uri " https://stackoverflow.com/tags" }
150+ {:name " ring"
151+ :taxonomy-uri " https://stackoverflow.com/tags" }
152+ {:name " clojure"
153+ :taxonomy-uri " https://stackoverflow.com/tags" }
154+ {:name " server"
155+ :taxonomy-uri " https://stackoverflow.com/tags" }
156+ {:name " web"
157+ :taxonomy-uri " https://stackoverflow.com/tags" })
158+ :description {:type " html"
159+ :value " I am learning Clojure for the web..." }
160+ :link " https://stackoverflow.com/questions/60234899/simple-web-app-of-bidi-ring-and-lein-gives-a-500-error"
161+ :links ({:href " https://stackoverflow.com/questions/60234899/simple-web-app-of-bidi-ring-and-lein-gives-a-500-error"
162+ :length 0
163+ :rel " alternate" })
164+ :published-date #inst " 2020-02-15T00:05:23.000-00:00"
165+ :title " Simple web app of Bidi, Ring and Lein gives a 500 error"
166+ :updated-date #inst " 2020-02-15T00:47:32.000-00:00"
167+ :uri " https://stackoverflow.com/q/60234899" }
168+ {:author " Bob"
169+ :authors ({:name " Bob"
170+ :uri " https://stackoverflow.com/users/5440125" })
171+ :categories ({:name " cider"
172+ :taxonomy-uri " https://stackoverflow.com/tags" }
173+ {:name " clojure"
174+ :taxonomy-uri " https://stackoverflow.com/tags" })
175+ :description {:type " html"
176+ :value " The default cider-test-report reporter is ..." }
177+ :link " https://stackoverflow.com/questions/60235197/how-to-use-the-eftest-library-with-cider-test-report"
178+ :links ({:href " https://stackoverflow.com/questions/60235197/how-to-use-the-eftest-library-with-cider-test-report"
179+ :length 0
180+ :rel " alternate" })
181+ :published-date #inst " 2020-02-15T01:08:30.000-00:00"
182+ :title " How to use the eftest library with cider-test-report?"
183+ :updated-date #inst " 2020-02-15T01:08:30.000-00:00"
184+ :uri " https://stackoverflow.com/q/60235197" }
185+ {:author " Bob"
186+ :authors ({:name " Bob"
187+ :uri " https://stackoverflow.com/users/5440125" })
188+ :categories ({:name " clojure"
189+ :taxonomy-uri " https://stackoverflow.com/tags" }
190+ {:name " regex"
191+ :taxonomy-uri " https://stackoverflow.com/tags" })
192+ :description {:type " html"
193+ :value " Suppose I want to unmap all the namespaces..." }
194+ :link " https://stackoverflow.com/questions/60243053/how-to-return-namespaces-by-regex-in-clojure"
195+ :links ({:href " https://stackoverflow.com/questions/60243053/how-to-return-namespaces-by-regex-in-clojure"
196+ :length 0
197+ :rel " alternate" })
198+ :published-date #inst " 2020-02-15T20:56:55.000-00:00"
199+ :title " How to return namespaces by regex in clojure?"
200+ :updated-date #inst " 2020-02-15T23:34:26.000-00:00"
201+ :uri " https://stackoverflow.com/q/60243053" }]})
202+
203+
204+ (deftest real-feed-roundtrip
205+ (is (= real-feed (-> real-feed
206+ produce
207+ consume
208+ shrink
209+ produce
210+ consume
211+ shrink))))
212+
213+
214+ (deftest feed-utilities
215+ (is (= 2 (count (:entries (filter-entries #(= " Bob" (:author %)) real-feed)))))
216+ (is (= [" Bob" " Bob" " Jim" ] (->> (sort-entries-by :author real-feed)
217+ :entries
218+ (map :author )))))
0 commit comments