Commit 8ea81468 authored by Johannes Knödtel's avatar Johannes Knödtel 🎶
Browse files

test database queries

parent 90d95019
......@@ -8,18 +8,305 @@
[clojure.java.io :as io]))
(def db-file "./gms_test.db")
(def db-uri (to-jdbc-uri (format "jdbc:h2:%s;MODE=MYSQL" db-file)))
(def config {:store :database
:db {:connection-uri
(to-jdbc-uri (format "jdbc:h2:%s;MODE=MYSQL" db-file))}})
:db {:connection-uri db-uri}})
(use-fixtures :each #((try (io/delete-file (io/file db-file))) %))
(def migrations (sort-by :id (.migrations (migratus-proto/make-store config))))
(defn- delete-test-db []
(try
(io/delete-file (io/file (str db-file ".mv.db")))
(io/delete-file (io/file (str db-file ".trace.db")))
(catch Exception _)))
(use-fixtures :each #(do (delete-test-db) (%)))
(deftest test-migrations
(let [migrations (sort-by :id (.migrations (migratus-proto/make-store config)))]
(doseq [migration migrations]
(testing (format "schema migration up %s" (:name migration))
(is (do (migratus/up config (:id migration)) true))))
(doseq [migration (reverse migrations)]
(testing (format "schema migration down %s" (:name migration))
(is (do (migratus/down config (:id migration)) true))))))
(doseq [migration migrations]
(testing (format "schema migration up %s" (:name migration))
(is (do (migratus/up config (:id migration)) true))))
(doseq [migration (reverse migrations)]
(testing (format "schema migration down %s" (:name migration))
(is (do (migratus/down config (:id migration)) true)))))
(defn- food-requests-1 [session]
[{:session session
:food_name "Bratwurst"
:comment "Vom Metzger"
:customer "Max Musteradmin"
:quantity 4M}
{:session session
:food_name "Bratwurst"
:comment "Vom Metzger"
:customer "Blubb"
:quantity 6M}
{:session session
:food_name "Champinons"
:comment ""
:customer "Max Musteradmin"
:quantity 6M}
{:session session
:food_name "Grillkäse"
:comment ""
:customer "Maria Musteradminette"
:quantity 1M}
{:session session
:food_name "to delete"
:comment ""
:customer "Hans Mustermann"
:quantity 10M}])
(defn- purchases-1 [session]
[{:session session
:food_name "Bratwurst"
:comment "Vom Metzger"
:ammount_per_package 42M
:number_of_packages 3M
:package_price 10.30M}
{:session session
:food_name "Champinons"
:comment ""
:ammount_per_package 9M
:number_of_packages 4M
:package_price 13.37M}])
(defn- changes-1 [session]
[{:session session
:food_name "Champinons"
:comment ""
:customer "Max Musteradmin"
:quantity 7M
:old_food_name "Champinons"
:old_comment ""
:old_customer "Max Musteradmin"}
{:session session
:food_name "Champinons"
:comment ""
:customer "Max Musteradmon"
:quantity 7M
:old_food_name "Champinons"
:old_comment ""
:old_customer "Max Musteradmin"}])
(defn- deletions-1 [session]
[{:session session
:food_name "to delete"
:comment ""
:customer "Hans Mustermann"}])
(defn- payments-1 [session]
[{:session session
:customer "Max Musteradmin"}])
(def expected-results-1
{:food
'(({:food_name "Bratwurst"
:comment "Vom Metzger"
:customer "Max Musteradmin"
:quantity 4.0M}
{:food_name "Bratwurst"
:comment "Vom Metzger"
:customer "Blubb"
:quantity 6.0M}
{:food_name "Champinons"
:comment ""
:customer "Max Musteradmon"
:quantity 7.0M}
{:food_name "Grillkäse"
:comment ""
:customer "Maria Musteradminette"
:quantity 1.0M}))
:already-paid
'(({:customer "Max Musteradmin"}))
:summed-food
'(({:food_name "Bratwurst"
:comment "Vom Metzger"
:number 11.0M
:total_ammount 126.0M}
{:food_name "Champinons"
:comment ""
:number 6.0M
:total_ammount 36.0M}
{:food_name "Grillkäse"
:comment ""
:number 1.0M
:total_ammount nil}))
:food-numbers
'(({:food_name "Bratwurst"
:comment "Vom Metzger"
:ammount_per_package 42.0M
:number_of_packages 3
:package_price 10.30M
:single_price 0.25
:total_ammount 126.0M}
{:food_name "Champinons"
:comment ""
:ammount_per_package 9.0M
:number_of_packages 4
:package_price 13.37M
:single_price 1.49
:total_ammount 36.0M}))})
(defn- food-requests-2 [session]
[{:session session
:food_name "Watburst"
:comment "Vom Mettzger"
:customer "Max Musteradmin"
:quantity 4M}
{:session session
:food_name "Watburst"
:comment "Vom Mettzger"
:customer "Blubb"
:quantity 6M}
{:session session
:food_name "Champinons"
:comment ""
:customer "Max Musteradmin"
:quantity 6M}])
(defn- purchases-2 [session]
[{:session session
:food_name "Watburst"
:comment "Vom Mettzger"
:ammount_per_package 42M
:number_of_packages 3M
:package_price 434M}
{:session session
:food_name "Watburst"
:comment "Vom Mettzger"
:ammount_per_package 2M
:number_of_packages 1M
:package_price 434M}
{:session session
:food_name "Champinons"
:comment ""
:ammount_per_package 1M
:number_of_packages 1M
:package_price 21M}])
(defn- changes-2 [session]
[])
(defn- deletions-2 [session]
[])
(defn- payments-2 [session]
[{:session session
:customer "Max Musteradmin"}
{:session session
:customer "Blubb"}])
(def expected-results-2
{:food
'(({:food_name "Champinons"
:comment ""
:customer "Max Musteradmin"
:quantity 6.0M}
{:food_name "Watburst"
:comment "Vom Mettzger"
:customer "Max Musteradmin"
:quantity 4.0M}
{:food_name "Watburst"
:comment "Vom Mettzger"
:customer "Blubb"
:quantity 6.0M}))
:already-paid
'(({:customer "Blubb"}
{:customer "Max Musteradmin"}))
:summed-food
'(({:food_name "Champinons"
:comment ""
:number 6.0M
:total_ammount 1.0M}
{:food_name "Watburst"
:comment "Vom Mettzger"
:number 10.0M
:total_ammount 128.0M}))
:food-numbers
'(({:food_name "Watburst"
:comment "Vom Mettzger"
:ammount_per_package 42.0M
:number_of_packages 3
:package_price 434.00M
:single_price 10.34
:total_ammount 126.0M}
{:food_name "Watburst"
:comment "Vom Mettzger"
:ammount_per_package 2.0M
:number_of_packages 1
:package_price 434.00M
:single_price 217.0
:total_ammount 2.0M}
{:food_name "Champinons"
:comment ""
:ammount_per_package 1.0M
:number_of_packages 1
:package_price 21.00M
:single_price 21.0
:total_ammount 1.0M}))})
(defn- migrate-test-db []
(delete-test-db)
(apply migratus/up config (map :id migrations)))
(def test-connection
(atom {:classname "org.h2.Driver"
:connection-uri db-uri
:make-pool? true
:naming {:keys clojure.string/lower-case
:fields clojure.string/upper-case}}))
(defn test-query-set [session-name
food-requests-fn
purchases-fn
changes-fn
deletions-fn
payments-fn
expected-results]
(testing session-name
(let [connection test-connection
session (second (first (db/create-session<!
{:name session-name}
{:connection @connection})))]
(letfn [(run-query [query-fn data]
(doall (map #(query-fn % {:connection @connection}) data)))]
(run-query db/add-food! (food-requests-fn session))
(run-query db/add-price! (purchases-fn session))
(run-query db/edit-order! (changes-fn session))
(run-query db/delete-order! (deletions-fn session))
(run-query db/pay! (payments-fn session))
(is (= {:food (run-query db/get-food
[{:session session}])
:already-paid (run-query db/get-already-paid
[{:session session}])
:summed-food (run-query db/get-summed-food-numbers
[{:session session}])
:food-numbers (run-query db/get-food-numbers
[{:session session}])}
expected-results))))))
(deftest test-queries
(migrate-test-db)
(test-query-set "test query set 1"
food-requests-1
purchases-1
changes-1
deletions-1
payments-1
expected-results-1)
(test-query-set "test query set 2"
food-requests-2
purchases-2
changes-2
deletions-2
payments-2
expected-results-2))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment