Commits

Edoardo Tosca committed ecbb1af

added tests for artist information retrieval

Comments (0)

Files changed (3)

src/clj_grooveshark/artist.clj

 (ns clj-grooveshark.artist
   (:use clj-grooveshark.core)
-  (:use [clojure.tools.logging :only (error debug)])
   )
 
-
 (defn get-artist-search-results
   "Search for an artist given a query. A limit and a page number can be specified optionally"
   [query & [limit page]]
   (let [query-params (into {} (filter second {:query query :limit limit :page page}))]
-    (execute (a-query "getArtistSearchResults" query-params))
-    )
-  )
-
-(defn handle
-  [response callback]
-  (if (contains? response :errors )
-    (:errors response)
-    (do
-      (debug response)
-      (callback response)
+    (let [response (execute (a-query2 "getArtistSearchResults" query-params))]
+      (handle response (fn [x] (:artists (:result x))))
       )
     )
-
   )
 
-
 (defn get-artist-albums
   "Retrieve albums of an artist, given an artist id"
   [artist-id]
-  (let [response (execute (a-query "getArtistAlbums" {:artistID artist-id}))]
-    (handle response (fn [x] (:albums (:result response))))
+  (let [response (execute (a-query2 "getArtistAlbums" {:artistID artist-id}))]
+    (handle response (fn [x] (:albums (:result x))))
     )
   )
 
 (defn get-artist-popular-songs
   "Retrieves artists most popular songs"
   [artist-id]
-  (let [response (execute (a-query "getArtistPopularSongs", {:artistID artist-id}))]
+  (let [response (execute (a-query2 "getArtistPopularSongs", {:artistID artist-id}))]
     (handle response (fn [x] (:songs (:result x))))
     )
   )
 (defn get-artist-verified-albums
   "Retrieves artist's verified album given an artist id"
   [artist-id]
-  (execute (a-query "getArtistVerifiedAlbums", {:artistID artist-id}))
+  (let [response (execute (a-query2 "getArtistVerifiedAlbums", {:artistID artist-id}))]
+    (handle response (fn [x] (:albums (:result x))))
+    )
   )
 
 (defn get-does-artist-exists?
   "Checks if a given artist id exists"
   [artist-id]
-  (let [response (execute (a-query "getDoesArtistExist", {:artistID artist-id}))]
-    (:result response)
+  (let [response (execute (a-query2 "getDoesArtistExist", {:artistID artist-id}))]
+    (handle response (fn [x] (:result x)))
     )
   )
 
 (defn get-artists-info
   "Returns information for the given list of artist ids."
   [artist-id & more]
-  (execute (a-query "getArtistsInfo" {:artistIDs (concat (list artist-id) more)}))
-  )
-
-(defn get-artists-info2
-  "Returns information for the given list of artist ids."
-  [artist-id & more]
-  (execute (a-query2 "getArtistsInfo" {:artistIDs (concat (list artist-id) more)}))
+  (let [response (execute (a-query2 "getArtistsInfo" {:artistIDs (concat (list artist-id) more)}))]
+    (handle response (fn [x] (:artists (:result x))))
+    )
   )
-
-
-
-
-

src/clj_grooveshark/core.clj

   (:require [clojure.data.json :as json])
   (:use clj-grooveshark.crypto)
   (:use clj-grooveshark.configuration)
+  (:use [clojure.tools.logging :only (error debug)])
   )
 
-;(org.apache.log4j.BasicConfigurator/configure)
+(org.apache.log4j.BasicConfigurator/configure)
 
 (defn a-query
   ([method] (json/write-str {:method method :header {:wsKey (:key configuration)}}))
   )
 
 
+(defn handle
+  [response callback]
+  (if (contains? response :errors )
+    (:errors response)
+    (do
+      (debug response)
+      (callback response)
+      )
+    )
+  )
+
+
 ;TO REFACTOR!
 (defn execute-basic [query]
   (client/get (:url configuration) {:body query} {:as :json})
   )
 
-;(defn execute [query]
-;  ;  (json/read-str (:body (client/get (str (:url configuration) "?sig=" (toHexString (create-signature (:secret configuration) query))) {:body query} {:as :json})) :key-fn keyword )
-;  (json/read-str (:body (client/get (str (:url configuration) "?sig=" (sign (:secret configuration) query)) {:body query} {:as :json})) :key-fn keyword)
-;  )
-
 (defn execute-secure [query]
-  ;  (json/read-str (:body (client/get (str (:secure-url configuration) "?sig=" (toHexString (create-signature (:secret configuration) query))) {:body query} {:as :json})) :key-fn keyword )
   (json/read-str (:body (client/get (str (:secure-url configuration) "?sig=" (sign (:secret configuration) query)) {:body query} {:as :json})) :key-fn keyword)
   )
 
-
 (def secure
   (:secure-url configuration)
   )
 (defn execute
   ([query] (do-execute plain query))
   ([url query] (do-execute url query))
-  ;  (json/read-str (:body (client/get (str url "?sig=" (sign (:secret configuration) query)) {:body query} {:as :json})) :key-fn keyword )
   )
 
 

test/clj_grooveshark/artist_test.clj

 (deftest should-get-artists-info
   (let [query (stringify {"result" {"artists" [{"ArtistID" 1261,"ArtistName" "Mogwai","IsVerified" "1"}]}})]
     (with-redefs [client/get (fn [url body json] query)]
-      (is (= {:result {:artists [{:ArtistID 1261, :ArtistName "Mogwai", :IsVerified "1"}]}} (get-artists-info2 1261)))
+      (is (= [{:ArtistID 1261, :ArtistName "Mogwai", :IsVerified "1"}] (get-artists-info 1261)))
       )
     )
   )
 
-
-
 (deftest should-return-albums-for-an-artists
   (let [response (stringify {:header {:hostname "RHL109"}, :result {:pager {:numPages 342, :hasPrevPage false, :hasNextPage true}, :albums [{:AlbumID 1550, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename nil, :IsVerified false, :AlbumName "Unknown Album"}]}})]
     (with-redefs [client/get (fn [url body json] response)]
     )
   )
 
+(deftest should-check-if-an-artist-exists
+  (let [response (stringify {"header" {"hostname" "RHL073"}, "result" "true"})]
+    (with-redefs [client/get (fn [url body json] response)])
+      (is (true? (get-does-artist-exists? 1261)))
+    )
+  )
+
+(deftest should-search-for-an-artist
+  (let [response (stringify {:header {:hostname "RHL061"}, :result {:pager {:numPages 5, :hasPrevPage false, :hasNextPage false}, :artists [{:ArtistID 1261, :ArtistName "Mogwai", :IsVerified true} {:ArtistID 1410559, :ArtistName "Mogwai", :IsVerified false} {:ArtistID 3437, :ArtistName "Bloc Party", :IsVerified true} {:ArtistID 1086380, :ArtistName "Mogwai & Bardo Pond", :IsVerified false} {:ArtistID 357876, :ArtistName "Fuck Buttons", :IsVerified false}]}})]
+    (with-redefs [client/get (fn [url body json] response)])
+      (is (= [{:ArtistID 1261, :ArtistName "Mogwai", :IsVerified true} {:ArtistID 1410559, :ArtistName "Mogwai", :IsVerified false} {:ArtistID 3437, :ArtistName "Bloc Party", :IsVerified true} {:ArtistID 1086380, :ArtistName "Mogwai & Bardo Pond", :IsVerified false} {:ArtistID 357876, :ArtistName "Fuck Buttons", :IsVerified false}] (get-artist-search-results "Mogwai")))
+    )
+  )
+
+(deftest should-return-artist-verified-albums
+  (let [response (stringify {:header {:hostname "RHL061"}, :result {:pager {:numPages 27, :hasPrevPage false, :hasNextPage false}, :albums [{:AlbumID 121379, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "121379.jpg", :IsVerified true, :AlbumName "Come On Die Young"} {:AlbumID 121381, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "121381.jpg", :IsVerified true, :AlbumName "Happy Songs for Happy People"} {:AlbumID 138795, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "138795.jpg", :IsVerified true, :AlbumName "Zidane: A 21st Century Portrait"} {:AlbumID 177201, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "177201.jpg", :IsVerified true, :AlbumName "Young Team"} {:AlbumID 182451, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "182451.jpg", :IsVerified true, :AlbumName "Rock Action"} {:AlbumID 182453, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "182453.jpg", :IsVerified true, :AlbumName "Ten Rapid: Collected Recordings 1996-1997"} {:AlbumID 189945, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "189945.jpg", :IsVerified true, :AlbumName "4 Satin EP"} {:AlbumID 391834, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "391834.jpg", :IsVerified true, :AlbumName "EP+6"} {:AlbumID 494059, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "494059.jpg", :IsVerified true, :AlbumName "Kicking a Dead Pig: Mogwai Songs Remixed"} {:AlbumID 606939, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "606939.jpg", :IsVerified true, :AlbumName "5 Track Tour Single"} {:AlbumID 663218, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "663218.jpg", :IsVerified true, :AlbumName "Travel Is Dangerous"} {:AlbumID 1382953, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "1382953.jpg", :IsVerified true, :AlbumName "No Education = No Future (Fuck the Curfew)"} {:AlbumID 1382955, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "1382955.jpg", :IsVerified true, :AlbumName "Travels in Constants, Volume 12"} {:AlbumID 1536356, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "1536356.jpg", :IsVerified true, :AlbumName "Mr. Beast"} {:AlbumID 1951386, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "1951386.jpg", :IsVerified true, :AlbumName "The Hawk Is Howling"} {:AlbumID 2557534, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "2557534.jpg", :IsVerified true, :AlbumName "My Father My King"} {:AlbumID 2863750, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "2863750.jpg", :IsVerified true, :AlbumName "Batcat"} {:AlbumID 3259718, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "3259718.jpg", :IsVerified true, :AlbumName "EP"} {:AlbumID 3588758, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "3588758.jpg", :IsVerified true, :AlbumName "Friend of the Night"} {:AlbumID 3992417, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "3992417.jpg", :IsVerified true, :AlbumName "Government Commissions: BBC Sessions 1996-2003"} {:AlbumID 4514590, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "4514590.jpg", :IsVerified true, :AlbumName "Special Moves"} {:AlbumID 4955954, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "4955954.jpg", :IsVerified true, :AlbumName "EP + 2"} {:AlbumID 5263098, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "5263098.jpg", :IsVerified true, :AlbumName "Hardcore Will Never Die, but You Will"} {:AlbumID 6141751, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "6141751.jpg", :IsVerified true, :AlbumName "Mexican Grand Prix"} {:AlbumID 6680038, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename nil, :IsVerified true, :AlbumName "Earth Division EP"} {:AlbumID 7165413, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "7165413.jpg", :IsVerified true, :AlbumName "Rano Pano"} {:AlbumID 8593636, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename nil, :IsVerified true, :AlbumName "A Wrenched Virile Lore"}]}}
+    )]
+    (with-redefs [client/get (fn [url body json] response)])
+      (is (= [{:AlbumID 121379, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "121379.jpg", :IsVerified true, :AlbumName "Come On Die Young"} {:AlbumID 121381, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "121381.jpg", :IsVerified true, :AlbumName "Happy Songs for Happy People"} {:AlbumID 138795, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "138795.jpg", :IsVerified true, :AlbumName "Zidane: A 21st Century Portrait"} {:AlbumID 177201, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "177201.jpg", :IsVerified true, :AlbumName "Young Team"} {:AlbumID 182451, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "182451.jpg", :IsVerified true, :AlbumName "Rock Action"} {:AlbumID 182453, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "182453.jpg", :IsVerified true, :AlbumName "Ten Rapid: Collected Recordings 1996-1997"} {:AlbumID 189945, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "189945.jpg", :IsVerified true, :AlbumName "4 Satin EP"} {:AlbumID 391834, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "391834.jpg", :IsVerified true, :AlbumName "EP+6"} {:AlbumID 494059, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "494059.jpg", :IsVerified true, :AlbumName "Kicking a Dead Pig: Mogwai Songs Remixed"} {:AlbumID 606939, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "606939.jpg", :IsVerified true, :AlbumName "5 Track Tour Single"} {:AlbumID 663218, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "663218.jpg", :IsVerified true, :AlbumName "Travel Is Dangerous"} {:AlbumID 1382953, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "1382953.jpg", :IsVerified true, :AlbumName "No Education = No Future (Fuck the Curfew)"} {:AlbumID 1382955, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "1382955.jpg", :IsVerified true, :AlbumName "Travels in Constants, Volume 12"} {:AlbumID 1536356, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "1536356.jpg", :IsVerified true, :AlbumName "Mr. Beast"} {:AlbumID 1951386, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "1951386.jpg", :IsVerified true, :AlbumName "The Hawk Is Howling"} {:AlbumID 2557534, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "2557534.jpg", :IsVerified true, :AlbumName "My Father My King"} {:AlbumID 2863750, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "2863750.jpg", :IsVerified true, :AlbumName "Batcat"} {:AlbumID 3259718, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "3259718.jpg", :IsVerified true, :AlbumName "EP"} {:AlbumID 3588758, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "3588758.jpg", :IsVerified true, :AlbumName "Friend of the Night"} {:AlbumID 3992417, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "3992417.jpg", :IsVerified true, :AlbumName "Government Commissions: BBC Sessions 1996-2003"} {:AlbumID 4514590, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "4514590.jpg", :IsVerified true, :AlbumName "Special Moves"} {:AlbumID 4955954, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "4955954.jpg", :IsVerified true, :AlbumName "EP + 2"} {:AlbumID 5263098, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "5263098.jpg", :IsVerified true, :AlbumName "Hardcore Will Never Die, but You Will"} {:AlbumID 6141751, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "6141751.jpg", :IsVerified true, :AlbumName "Mexican Grand Prix"} {:AlbumID 6680038, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename nil, :IsVerified true, :AlbumName "Earth Division EP"} {:AlbumID 7165413, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename "7165413.jpg", :IsVerified true, :AlbumName "Rano Pano"} {:AlbumID 8593636, :ArtistID 1261, :ArtistName "Mogwai", :CoverArtFilename nil, :IsVerified true, :AlbumName "A Wrenched Virile Lore"}] (get-artist-verified-albums 1261)))
+    )
+  )
+
+
+