Luke
Luke

Reputation: 1806

clojure - simple map function returns list of nils

This function takes a list of a files and is supposed to return a list of artists:

(defn get-artists [files]
    (map #(.get-artist (->Mp3 %)) files))

Here the rest of the code:

(ns musicdb.filesystem
    (:use [green-tags.core]))

(import '(java.io.File) '(java.net.url) '(java.io))
(require '[clojure.string :as str])

(defn get-files [search-path]
  (let [directory (clojure.java.io/file search-path)
        files (file-seq directory)
        fonly (filter #(.isFile %) files)]
   (map #(last (str/split (.toString %) #"/")) fonly)))

(defprotocol MusicFile
    (get-artist [this])
    (get-song [this])
    (get-album [this]))


(defrecord Mp3 [filename]
    MusicFile
    (get-artist [this]
        (:artist (get-all-info filename)))
    (get-song [this]
        (:title (get-all-info filename)))
    (get-album [this]
        (:album (get-all-info filename))))

And here are my tests:

(ns musicdb.core-test
  (:require [clojure.test :refer :all]
            [musicdb.core :refer :all]
            [musicdb.filesystem :refer :all]
            [clojure.pprint :refer :all]
            ))

(deftest test_0
  (testing "getFiles returns valid result"
    (is (> (count (get-files "/home/ls/books/books")) 50))))

(deftest test_1
  (testing "check for file included"
    (is (some #{"02 Backlit.mp3"} (get-files "/home/ls/Musik")))))

(deftest test_2
    (testing "creating music file record"
        (let [myfile (->Mp3 "/home/ls/Musik/Panopticon/02 Backlit.mp3")]
            (is (= "Isis" (.get-artist myfile)))
            (is (= "Backlit" (.get-song myfile))))))
(deftest test_3
    (testing "testing get-artists"
    (let [artists (get-artists (get-files "/home/ls/Musik"))
        ]
        (is (> (count artists) 10)))))

(deftest test_4
    (testing "testing get-artists check for artist"
        (let [artists (get-artists (get-files "/home/ls/Musik"))
            ]
            (is (some #{"Isis"} artists))))) ;artists is [nil nil nil ...]

From this tests only the last fails, which returns a list of nils.

If you want to reproduce ths be sure to include the green-tags dependency in your leiningen project.clj:

[green-tags "0.3.0-alpha"]  

Upvotes: 0

Views: 167

Answers (1)

ponzao
ponzao

Reputation: 20934

Your get-files function doesn't return the full path of the file so get-all-info just returns nil (https://github.com/DanPallas/green-tags/blob/master/src/green_tags/core.clj#L59 in combination with https://github.com/DanPallas/green-tags/blob/master/src/green_tags/core.clj#L120).

Here is a simple example that works:

(map (comp :artist get-all-info)
     (filter #(.isFile %)
             (file-seq (java.io.File. "/home/vema/Downloads/mp3"))))
;=> ("Yo Yo Honey Singh (DJJOhAL.Com)")

(Humoristic?) disclaimer: The MP3 should not be taken as an example of my musical taste, it was just the first free MP3 I found online.

Upvotes: 1

Related Questions