Skip to content

Commit

Permalink
Added a test for model/util.clj.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Courtney committed Jul 7, 2009
1 parent 6891687 commit 459ee5b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
18 changes: 11 additions & 7 deletions file_structure/default/vendor/conjure/model/util.clj
Expand Up @@ -7,12 +7,13 @@
(defn
#^{:doc "Returns the model name for the given model file."}
model-from-file [model-file]
(loading-utils/clj-file-to-symbol-string (. model-file getName)))
(if model-file
(loading-utils/clj-file-to-symbol-string (. model-file getName))))

(defn
#^{:doc "Returns the model namespace for the given model."}
model-namespace [model]
(str "models." model))
(if model (str "models." model)))

(defn
#^{:doc "Finds the models directory."}
Expand All @@ -23,19 +24,22 @@
(defn
#^{:doc "Returns the model file name for the given model name."}
model-file-name-string [model-name]
(str (loading-utils/dashes-to-underscores model-name) ".clj"))
(if model-name (str (loading-utils/dashes-to-underscores model-name) ".clj")))

(defn
#^{:doc "Returns the name of the migration associated with the given model."}
migration-for-model [model]
(str "create-" (string-utils/pluralize model)))
(if model (str "create-" (string-utils/pluralize model))))

(defn
#^{:doc "Returns the table name for the given model."}
model-to-table-name [model]
(string-utils/pluralize (loading-utils/dashes-to-underscores model)))
(if model (string-utils/pluralize (loading-utils/dashes-to-underscores model))))

(defn
#^{:doc "Finds a model file with the given model name."}
find-model-file [models-directory model-name]
(file-utils/find-file models-directory (model-file-name-string model-name)))
find-model-file
([model-name] (find-model-file (find-models-directory) model-name))
([models-directory model-name]
(if (and models-directory model-name)
(file-utils/find-file models-directory (model-file-name-string model-name)))))
60 changes: 60 additions & 0 deletions test/model/test_util.clj
@@ -0,0 +1,60 @@
(ns test.model.test-util
(:import [java.io File])
(:use clojure.contrib.test-is
conjure.model.util)
(:require [generators.model-generator :as model-generator]
[destroyers.model-destroyer :as model-destroyer]))

(def model-name "test")

(defn setup-all [function]
(model-generator/generate-model-file model-name)
(function)
(model-destroyer/destroy-model-file model-name))

(use-fixtures :once setup-all)

(deftest test-model-from-file
(is (= model-name (model-from-file (new File (str "models/" model-name ".clj")))))
(is (nil? (model-from-file nil))))

(deftest test-model-namespace
(is (= (str "models." model-name) (model-namespace model-name)))
(is (nil? (model-namespace nil))))

(deftest test-find-models-directory
(let [models-dirctory (find-models-directory)]
(is (not (nil? models-dirctory)))
(is (instance? File models-dirctory))
(is (= "models" (. models-dirctory getName)))))

(deftest test-model-file-name-string
(is (= (str model-name ".clj") (model-file-name-string model-name)))
(is (= "test_foo.clj" )(model-file-name-string "test-foo"))
(is (= "test_foo.clj" )(model-file-name-string "test_foo"))
(is (nil? (model-file-name-string nil))))

(deftest test-migration-for-model
(let [migration-file-name (migration-for-model model-name)]
(is (not (nil? migration-file-name)))
(is (= "create-tests" migration-file-name)))
(is (nil? (migration-for-model nil))))

(deftest test-model-to-table-name
(is (= "tests" (model-to-table-name model-name)))
(is (= "test_foos" (model-to-table-name "test-foo")))
(is (= "test_foos" (model-to-table-name "test_foo")))
(is (nil? (model-to-table-name nil))))

(deftest test-find-model-file
(let [model-file (find-model-file (find-models-directory) model-name)]
(is (not (nil? model-file)))
(is (instance? File model-file))
(is (= "test.clj" (. model-file getName))))
(let [model-file (find-model-file model-name)]
(is (not (nil? model-file)))
(is (instance? File model-file))
(is (= "test.clj" (. model-file getName))))
(is (nil? (find-model-file nil)))
(is (nil? (find-model-file nil model-name)))
(is (nil? (find-model-file nil nil))))

0 comments on commit 459ee5b

Please sign in to comment.