Skip to content

Commit

Permalink
Added fixtures.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Courtney committed Jul 31, 2009
1 parent 3247dab commit f42293f
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 50 deletions.
7 changes: 7 additions & 0 deletions file_structure/default/db/flavors/h2.clj
Expand Up @@ -175,6 +175,12 @@ create-table method.
drop-table [db-spec table]
(sql/with-connection db-spec
(sql/drop-table (table-name table))))

(defn
#^{:doc "Deletes the table with the given name."}
delete [db-spec table where]
(sql/with-connection db-spec
(sql/drop-table (table-name table))))

(defn
#^{:doc "Returns a database flavor for a derby database."}
Expand All @@ -187,6 +193,7 @@ create-table method.
:sql-find sql-find
:create-table create-table
:drop-table drop-table
:delete delete
:integer integer
:id id
:string string
Expand Down
10 changes: 9 additions & 1 deletion file_structure/default/script/destroy.clj
Expand Up @@ -3,7 +3,9 @@
[destroyers.view-destroyer :as view-destroyer]
[destroyers.controller-destroyer :as controller-destroyer]
[destroyers.controller-test-destroyer :as controller-test-destroyer]
[destroyers.model-destroyer :as model-destroyer]))
[destroyers.model-destroyer :as model-destroyer]
[destroyers.model-test-destroyer :as model-test-destroyer]
[destroyers.view-test-destroyer :as view-test-destroyer]))

(defn
#^{:doc "Prints the usage information to standard out."}
Expand All @@ -25,6 +27,9 @@

(. command equals "view")
(view-destroyer/destroy-view params)

(. command equals "view-test")
(view-test-destroyer/destroy-view-test params)

(. command equals "controller")
(controller-destroyer/destroy-controller params)
Expand All @@ -34,6 +39,9 @@

(. command equals "model")
(model-destroyer/destroy-model params)

(. command equals "model-test")
(model-test-destroyer/destroy-model-test params)

true ; Default condition.
(print-unknown-command command)))
Expand Down
36 changes: 36 additions & 0 deletions file_structure/default/script/destroyers/fixture_destroyer.clj
@@ -0,0 +1,36 @@
(ns destroyers.fixture-destroyer
(:require [conjure.test.util :as util]
[conjure.util.file-utils :as file-utils]))

(defn
#^{:doc "Prints out how to use the destroy fixture command."}
usage []
(println "You must supply a model (Like message).")
(println "Usage: ./run.sh script/destroy.clj fixture <model>"))

(defn
#^{:doc "Destroys the fixture from the given model."}
destroy-fixture-file [model]
(if model
(let [fixture-dir (util/find-fixture-directory)]
(if fixture-dir
(let [fixture-file (util/fixture-file model fixture-dir)]
(if fixture-file
(let [is-deleted (. fixture-file delete)]
(println "File" (. fixture-file getName) (if is-deleted "destroyed." "not destroyed."))
(file-utils/delete-all-if-empty fixture-dir))
(println "Fixture file not found. Doing nothing.")))
(do
(println "Could not find the fixture directory.")
(println "Command ignored."))))
(usage)))

(defn
#^{:doc "Destroys a fixture file for the model given in params."}
destroy-fixture [params]
(destroy-fixture-file (first params)))

(defn
#^{:doc "Destroys all of the files created by the model_test_generator."}
destroy-all-dependencies [model]
(destroy-fixture-file model))
@@ -1,6 +1,7 @@
(ns destroyers.model-test-destroyer
(:require [conjure.test.util :as util]
[conjure.util.file-utils :as file-utils]))
[conjure.util.file-utils :as file-utils]
[destroyers.fixture-destroyer :as fixture-destroyer]))

(defn
#^{:doc "Prints out how to use the destroy model test command."}
Expand All @@ -9,7 +10,7 @@
(println "Usage: ./run.sh script/destroy.clj model-test <model>"))

(defn
#^{:doc "Destroys the controller file from the given controller."}
#^{:doc "Destroys the model test file from the given model."}
destroy-model-test-file [model]
(if model
(let [model-unit-test-dir (util/find-model-unit-test-directory)]
Expand All @@ -26,11 +27,12 @@
(usage)))

(defn
#^{:doc "Destroys a controller test file for the controller name given in params."}
#^{:doc "Destroys a model test file for the model name given in params."}
destroy-model-test [params]
(destroy-model-test-file (first params)))

(defn
#^{:doc "Destroys all of the files created by the model_test_generator."}
destroy-all-dependencies [model]
(destroy-model-test-file model))
(destroy-model-test-file model)
(fixture-destroyer/destroy-all-dependencies model))
10 changes: 9 additions & 1 deletion file_structure/default/script/generate.clj
Expand Up @@ -3,7 +3,9 @@
[generators.view-generator :as view-generator]
[generators.controller-generator :as controller-generator]
[generators.controller-test-generator :as controller-test-generator]
[generators.model-generator :as model-generator]))
[generators.model-generator :as model-generator]
[generators.model-test-generator :as model-test-generator]
[generators.view-test-generator :as view-test-generator]))

(defn print-usage []
(println "Usage: ./run.sh script/generate.clj <generate type> <generate params>*"))
Expand All @@ -20,6 +22,9 @@
(. command equals "view")
(view-generator/generate-view params)

(. command equals "view-test")
(view-test-generator/generate-view-test params)

(. command equals "controller")
(controller-generator/generate-controller params)

Expand All @@ -28,6 +33,9 @@

(. command equals "model")
(model-generator/generate-model params)

(. command equals "model-test")
(model-test-generator/generate-model-test params)

true ; Default condition.
(print-unknown-command command)))
Expand Down
34 changes: 34 additions & 0 deletions file_structure/default/script/generators/fixture_generator.clj
@@ -0,0 +1,34 @@
(ns generators.fixture-generator
(:require [conjure.model.util :as model-util]
[conjure.test.builder :as test-builder]
[conjure.test.util :as test-util]
[conjure.util.file-utils :as file-utils]))

(defn
#^{:doc "Prints out how to use the generate fixture command."}
usage []
(println "You must supply a model name (Like hello-world).")
(println "Usage: ./run.sh script/generate.clj fixture <model>"))

(defn
#^{:doc "Generates the fixture file for the given model."}
generate-fixture-file [model]
(let [fixture-file (test-builder/create-fixture model)]
(if fixture-file
(let [fixture-namespace (test-util/fixture-namespace model)
table-name (model-util/model-to-table-name model)
fixture-content (str "(ns " fixture-namespace "
(:use conjure.model.database))
(def " model "-fixture [function]
(insert-into " table-name "
; Add your test data here.
{ :id 1 })
(function)
(delete " table-name " \"true\"))")]
(file-utils/write-file-content fixture-file fixture-content)))))

(defn
#^{:doc "Generates a fixture file for the model name in params."}
generate-fixture [params]
(generate-fixture-file (first params)))
4 changes: 2 additions & 2 deletions file_structure/default/script/generators/model_generator.clj
Expand Up @@ -36,12 +36,12 @@
generate-file-content [model-file]
(let [model (util/model-from-file model-file)
model-namespace (util/model-namespace model)
content (str "(ns " model-namespace "
model-content (str "(ns " model-namespace "
(:use conjure.model.base
clj-record.boot))
(init-model)")]
(file-utils/write-file-content model-file content)
(file-utils/write-file-content model-file model-content)
(generate-migration-file model)
(model-test-generator/generate-unit-test model)))

Expand Down
20 changes: 13 additions & 7 deletions file_structure/default/script/generators/model_test_generator.clj
Expand Up @@ -2,7 +2,8 @@
(:require [conjure.model.util :as util]
[conjure.test.builder :as test-builder]
[conjure.test.util :as test-util]
[conjure.util.file-utils :as file-utils]))
[conjure.util.file-utils :as file-utils]
[generators.fixture-generator :as fixture-generator]))

(defn
#^{:doc "Prints out how to use the generate model test command."}
Expand All @@ -11,23 +12,28 @@
(println "Usage: ./run.sh script/generate.clj model-test <model>"))

(defn
#^{:doc "Generates the functional test file for the given controller and actions."}
#^{:doc "Generates the unit test file for the given model."}
generate-unit-test [model]
(let [unit-test-file (test-builder/create-model-unit-test model)]
(if unit-test-file
(let [test-namespace (test-util/model-unit-test-namespace model)
model-namespace (util/model-namespace model)
fixture-namespace (test-util/fixture-namespace model)
test-content (str "(ns " test-namespace "
(:use clojure.contrib.test-is
" model-namespace "))
" model-namespace "
" fixture-namespace "))
(def model \"" model "\")
(deftest test-truth
(is true))")]
(file-utils/write-file-content unit-test-file test-content)))))
(use-fixtures :once " model "-fixture)
(deftest test-first-record
(is (get-record 1)))")]
(file-utils/write-file-content unit-test-file test-content)
(fixture-generator/generate-fixture-file model)))))

(defn
#^{:doc "Generates a controller file for the controller name and actions in params."}
#^{:doc "Generates a model test file for the model name in params."}
generate-model-test [params]
(generate-unit-test (first params)))
2 changes: 2 additions & 0 deletions file_structure/default/vendor/conjure/model/database.clj
Expand Up @@ -38,6 +38,8 @@ database flavor function with the current db spec and any arguments"}
(def-db-fn :create-table)

(def-db-fn :drop-table)

(def-db-fn :delete)

(defmacro
#^{:doc "Given the type-key of a function in the database flavor, define a function named type-key which calls the
Expand Down
23 changes: 20 additions & 3 deletions file_structure/default/vendor/conjure/test/builder.clj
Expand Up @@ -10,7 +10,7 @@
([] (find-or-create-functional-test-directory (util/find-test-directory)))
([test-directory]
(if test-directory
(file-utils/create-dirs test-directory "functional")
(file-utils/create-dirs test-directory util/functional-dir-name)
(println "You must pass in a test directory."))))

(defn
Expand All @@ -19,7 +19,7 @@
([] (find-or-create-unit-test-directory (util/find-test-directory)))
([test-directory]
(if test-directory
(file-utils/create-dirs test-directory "unit")
(file-utils/create-dirs test-directory util/unit-dir-name)
(println "You must pass in a test directory."))))

(defn
Expand Down Expand Up @@ -48,6 +48,15 @@
(if test-directory
(file-utils/create-dirs test-directory util/unit-dir-name util/unit-model-dir-name)
(println "You must pass in a test directory."))))

(defn
#^{:doc "Finds (or creates if not found) the fixture directory."}
find-or-create-fixture-directory
([] (find-or-create-fixture-directory (util/find-test-directory)))
([test-directory]
(if test-directory
(file-utils/create-dirs test-directory util/fixture-dir-name)
(println "You must pass in a test directory."))))

(defn
#^{:doc "Creates a new functional test file from the given controller name."}
Expand All @@ -71,4 +80,12 @@
([model] (create-model-unit-test model (find-or-create-model-unit-test-directory)))
([model model-unit-test-directory]
(if (and model model-unit-test-directory)
(file-utils/create-file (util/model-unit-test-file model model-unit-test-directory)))))
(file-utils/create-file (util/model-unit-test-file model model-unit-test-directory)))))

(defn
#^{:doc "Creates a new fixture file from the given model."}
create-fixture
([model] (create-fixture model (find-or-create-fixture-directory)))
([model fixture-directory]
(if (and model fixture-directory)
(file-utils/create-file (util/fixture-file model fixture-directory)))))
30 changes: 29 additions & 1 deletion file_structure/default/vendor/conjure/test/util.clj
Expand Up @@ -55,6 +55,14 @@
(if unit-test-directory
(file-utils/find-directory unit-test-directory unit-model-dir-name))))

(defn
#^{:doc "Finds the fixture directory."}
find-fixture-directory
([] (find-fixture-directory (find-test-directory)))
([test-directory]
(if test-directory
(file-utils/find-directory test-directory fixture-dir-name))))

(defn
#^{:doc "Returns the functional test file name for the given controller name."}
functional-test-file-name [controller]
Expand All @@ -72,6 +80,12 @@
model-unit-test-file-name [model]
(if (and model (> (. model length) 0))
(str (loading-utils/dashes-to-underscores model) "_model_test.clj")))

(defn
#^{:doc "Returns the fixture file name for the given model name."}
fixture-file-name [model]
(if (and model (> (. model length) 0))
(str (loading-utils/dashes-to-underscores model) ".clj")))

(defn
#^{:doc "Returns the functional test file for the given controller name."}
Expand All @@ -97,6 +111,14 @@
(if (and model (> (. model length) 0) model-unit-test-dir)
(new File model-unit-test-dir (model-unit-test-file-name model)))))

(defn
#^{:doc "Returns the fixture file for the given model name."}
fixture-file
([model] (fixture-file model (find-fixture-directory)))
([model fixture-directory]
(if (and model (> (. model length) 0) fixture-directory)
(new File fixture-directory (fixture-file-name model)))))

(defn
#^{:doc "Returns the functional test namespace for the given controller."}
functional-test-namespace [controller]
Expand All @@ -113,4 +135,10 @@
#^{:doc "Returns the model test namespace for the given model."}
model-unit-test-namespace [model]
(if (and model (> (. model length) 0))
(str "test.unit.model." (loading-utils/underscores-to-dashes model) "-model-test")))
(str "test.unit.model." (loading-utils/underscores-to-dashes model) "-model-test")))

(defn
#^{:doc "Returns the fixture namespace for the given model."}
fixture-namespace [model]
(if (and model (> (. model length) 0))
(str "test.fixture." (loading-utils/underscores-to-dashes model))))

0 comments on commit f42293f

Please sign in to comment.