Skip to content

Commit

Permalink
Merge pull request #5 from alekseysotnikov/Test-coverage-not-less-tha…
Browse files Browse the repository at this point in the history
…n-75-percentage

#4 Test coverage not less than 75 percentage
  • Loading branch information
alekseysotnikov authored Feb 22, 2020
2 parents 418391d + a8d0d9d commit 53d35a0
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ After the modifications, Buran can generate from it your own feed, for example i

### Installation

1. Add to *project.clj* - ```[buran "0.1.3"]```
1. Add to *project.clj* - ```[buran "0.1.4"]```

2. Import

Expand Down
21 changes: 1 addition & 20 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,37 +1,18 @@
(defproject buran "0.1.3"


(defproject buran "0.1.4"
:description "Bidirectional, data-driven RSS/Atom feed consumer, producer and feeds aggregator"


:url "https://github.com/alekseysotnikov/buran"


:license {:name "Apache License 2.0"
:url "https://www.apache.org/licenses/LICENSE-2.0.html"}


:dependencies [[org.clojure/clojure "1.8.0"]
[com.rometools/rome "1.12.0"]]


:plugins [[lein-cloverage "1.1.1"]]


:aot :all


:profiles {:uberjar {:aot :all}
:linters {:dependencies [[org.clojure/clojure "1.10.0"]
[clj-kondo "2019.07.18-alpha-SNAPSHOT"]]
:plugins [[lein-kibit "0.1.7"]
[jonase/eastwood "0.3.6"]]}}


:deploy-repositories {"clojars" {:url "https://clojars.org/repo"
:sign-releases false}}


:aliases {"deploy" ["deploy" "clojars"]
"kibit" ["with-profile" "+linters" "kibit"]
"eastwood" ["with-profile" "+linters" "eastwood" "{:continue-on-exception true :namespaces [:source-paths]}"]
Expand Down
24 changes: 13 additions & 11 deletions src/buran/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@


(defmacro with-exception [throw? & body]
(if throw?
`(do ~@body)
`(try
~@body
(catch Throwable e#
{:message (.getMessage e#)
:error e#}))))
(list 'if throw?
`(do ~@body)
`(try
~@body
(catch Exception e#
{:message (.getMessage e#)
:error e#}))))


(defn consume
Expand All @@ -38,8 +38,11 @@
allow-doctypes false
throw-exception false}}]
(with-exception throw-exception
(let [from (if (string? source) (StringReader. source) from)
from (if (string? from) (File. from) from)
(let [from (if (string? source)
(StringReader. source)
(cond
(fn? from) (from)
(string? from) (StringReader. from)))
consumer (doto
(SyndFeedInput. validate locale)
(.setAllowDoctypes allow-doctypes)
Expand Down Expand Up @@ -68,8 +71,7 @@
(let [request (if (string? request)
{:from request}
request)
reader (http-reader request)
request (assoc request :from reader)]
request (assoc request :from #(http-reader request))]
(consume request)))


Expand Down
160 changes: 147 additions & 13 deletions test/buran/core_test.clj
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
(ns buran.core-test
(:require
[buran.core :refer :all]
[clojure.test :refer :all]))
[clojure.test :refer :all])
(:import [java.io ByteArrayInputStream File]))


(def short-feed {:info {:feed-type "atom_1.0"
:title "Feed title"}
:entries [{:title "Entry title"
:description {:value "entry description"}}]})


(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")


(deftest produce-feed
(testing "Producing a feed"
(is (= (produce {:feed short-feed
:pretty-print false})
"<?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"))))
(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"]
(testing "Produce the feed to various types"
(is (= expected (produce {:feed short-feed
:pretty-print false})))
(is (instance? org.w3c.dom.Document (produce {:feed short-feed
:to :w3cdom})))
(is (instance? org.jdom2.Document (produce {:feed short-feed
:to :jdom}))))
(testing "Produce the feed to a file"
(let [pathname "target/feed.xml"]
(produce {:feed short-feed
:to pathname
:pretty-print false})
(is (= expected (slurp pathname))))
(let [pathname "target/feed2.xml"]
(produce {:feed short-feed
:to (File. pathname)
:pretty-print false})
(is (= expected (slurp pathname)))))))


(deftest produce-pretty-printed-feed
Expand Down Expand Up @@ -70,15 +87,132 @@
:description {:value "entry description 2"}}]}))))


(deftest consume-from-string
(is (= short-feed (shrink (consume short-feed-str))))
(is (= short-feed (shrink (consume {:from short-feed-str})))))


(deftest consume-http-test
(testing "from input stream"
(is (= short-feed (shrink (consume-http {:from (ByteArrayInputStream. (.getBytes short-feed-str))}))))))


(deftest negative-consume-http
(let [{:keys [error message]} (consume-http "invalid://url")]
(is (instance? Throwable error))
(is (= "unknown protocol: invalid" message))))


(deftest negative-consume
(testing "throwable"
(is (instance? Throwable (try
(consume {:throw-exception true
:from "<invalid rss>"})
(catch Throwable e
e)))))
(testing "map with an exception"
(let [{:keys [error message]} (consume {:throw-exception false
:from "<invalid rss>"})]
(is (instance? Throwable error))
(is (string? message)))))


(deftest consume-produce-roundtrip
(is (= short-feed (-> short-feed
produce
consume
shrink))
(is (= short-feed-str (-> short-feed-str
consume
shrink
produce)))))



shrink)))
(is (= short-feed-str (-> short-feed-str
consume
shrink
produce))))


(def real-feed '{:info {:description "most recent 30 from stackoverflow.com"
:feed-type "atom_1.0"
:link "https://stackoverflow.com/questions/tagged/?tagnames=clojure&sort=active"
:links ({:href "https://stackoverflow.com/feeds/tag?tagnames=clojure"
:length 0
:rel "self"
:type "application/atom+xml"}
{:href "https://stackoverflow.com/questions/tagged/?tagnames=clojure&sort=active"
:length 0
:rel "alternate"
:type "text/html"})
:published-date #inst "2020-02-22T16:12:21.000-00:00"
:title "Active questions tagged clojure - Stack Overflow"
:uri "https://stackoverflow.com/feeds/tag?tagnames=clojure"}
:entries [{:author "Jim"
:authors ({:name "Jim"
:uri "https://stackoverflow.com/users/5673289"})
:categories ({:name "bidi"
:taxonomy-uri "https://stackoverflow.com/tags"}
{:name "ring"
:taxonomy-uri "https://stackoverflow.com/tags"}
{:name "clojure"
:taxonomy-uri "https://stackoverflow.com/tags"}
{:name "server"
:taxonomy-uri "https://stackoverflow.com/tags"}
{:name "web"
:taxonomy-uri "https://stackoverflow.com/tags"})
:description {:type "html"
:value "I am learning Clojure for the web..."}
:link "https://stackoverflow.com/questions/60234899/simple-web-app-of-bidi-ring-and-lein-gives-a-500-error"
:links ({:href "https://stackoverflow.com/questions/60234899/simple-web-app-of-bidi-ring-and-lein-gives-a-500-error"
:length 0
:rel "alternate"})
:published-date #inst "2020-02-15T00:05:23.000-00:00"
:title "Simple web app of Bidi, Ring and Lein gives a 500 error"
:updated-date #inst "2020-02-15T00:47:32.000-00:00"
:uri "https://stackoverflow.com/q/60234899"}
{:author "Bob"
:authors ({:name "Bob"
:uri "https://stackoverflow.com/users/5440125"})
:categories ({:name "cider"
:taxonomy-uri "https://stackoverflow.com/tags"}
{:name "clojure"
:taxonomy-uri "https://stackoverflow.com/tags"})
:description {:type "html"
:value "The default cider-test-report reporter is ..."}
:link "https://stackoverflow.com/questions/60235197/how-to-use-the-eftest-library-with-cider-test-report"
:links ({:href "https://stackoverflow.com/questions/60235197/how-to-use-the-eftest-library-with-cider-test-report"
:length 0
:rel "alternate"})
:published-date #inst "2020-02-15T01:08:30.000-00:00"
:title "How to use the eftest library with cider-test-report?"
:updated-date #inst "2020-02-15T01:08:30.000-00:00"
:uri "https://stackoverflow.com/q/60235197"}
{:author "Bob"
:authors ({:name "Bob"
:uri "https://stackoverflow.com/users/5440125"})
:categories ({:name "clojure"
:taxonomy-uri "https://stackoverflow.com/tags"}
{:name "regex"
:taxonomy-uri "https://stackoverflow.com/tags"})
:description {:type "html"
:value "Suppose I want to unmap all the namespaces..."}
:link "https://stackoverflow.com/questions/60243053/how-to-return-namespaces-by-regex-in-clojure"
:links ({:href "https://stackoverflow.com/questions/60243053/how-to-return-namespaces-by-regex-in-clojure"
:length 0
:rel "alternate"})
:published-date #inst "2020-02-15T20:56:55.000-00:00"
:title "How to return namespaces by regex in clojure?"
:updated-date #inst "2020-02-15T23:34:26.000-00:00"
:uri "https://stackoverflow.com/q/60243053"}]})


(deftest real-feed-roundtrip
(is (= real-feed (-> real-feed
produce
consume
shrink
produce
consume
shrink))))


(deftest feed-utilities
(is (= 2 (count (:entries (filter-entries #(= "Bob" (:author %)) real-feed)))))
(is (= ["Bob" "Bob" "Jim"] (->> (sort-entries-by :author real-feed)
:entries
(map :author)))))

0 comments on commit 53d35a0

Please sign in to comment.