mirror of
https://github.com/mruwnik/chicken-master.git
synced 2025-06-08 21:34:43 +02:00
tests
This commit is contained in:
parent
38053bbdb2
commit
599472af37
@ -59,7 +59,7 @@
|
||||
(jdbc/with-transaction [tx db/db-uri]
|
||||
(let [customer-id (or (:id who)
|
||||
(:customers/id (db/get-by-id tx user-id :customers (:name who) :name)))
|
||||
products-map (products/products-map tx products)
|
||||
products-map (products/products-map tx user-id products)
|
||||
previous-day (some->> order :id (db/get-by-id tx user-id :orders) :orders/order_date (.toInstant))
|
||||
order-id (upsert-order! tx user-id customer-id order)]
|
||||
(sql/delete! tx :order_products {:order_id order-id})
|
||||
|
@ -4,14 +4,14 @@
|
||||
[chicken-master.db :as db]))
|
||||
|
||||
(defn get-all [user-id]
|
||||
(->> (sql/query db/db-uri ["select * from products where deleted is null and user_id = ?" user-id])
|
||||
(->> (sql/query db/db-uri ["SELECT * FROM products WHERE deleted IS NULL AND user_id = ?" user-id])
|
||||
(map (fn [{:products/keys [name amount]}] [(keyword name) amount]))
|
||||
(into {})))
|
||||
|
||||
(defn products-map [tx products]
|
||||
(defn products-map [tx user-id products]
|
||||
(when (seq products)
|
||||
(->> (map name (keys products))
|
||||
(into [(str "SELECT id, name from products where name IN " (db/psql-list (keys products)))])
|
||||
(into [(str "SELECT id, name FROM products WHERE user_id = ? AND name IN " (db/psql-list (keys products))) user-id])
|
||||
(sql/query tx)
|
||||
(map #(vector (:products/name %) (:products/id %)))
|
||||
(into {}))))
|
||||
|
36
test/clj/chicken_master/customers_test.clj
Normal file
36
test/clj/chicken_master/customers_test.clj
Normal file
@ -0,0 +1,36 @@
|
||||
(ns clj.chicken-master.customers-test
|
||||
(:require
|
||||
[next.jdbc :as jdbc]
|
||||
[next.jdbc.sql :as sql]
|
||||
[chicken-master.customers :as sut]
|
||||
[chicken-master.orders :as orders]
|
||||
[clojure.test :refer [deftest is testing]]))
|
||||
|
||||
(deftest test-get-all
|
||||
(testing "query is correct"
|
||||
(with-redefs [sql/query (fn [_ query]
|
||||
(is (= query ["select * from customers where deleted is null AND user_id = ?" "1"]))
|
||||
[])]
|
||||
(sut/get-all "1")))
|
||||
|
||||
(testing "results are mapped correctly"
|
||||
(with-redefs [sql/query (constantly [{:customers/id 1 :customers/name "mr blobby" :bla 123}])]
|
||||
(= (sut/get-all "1")
|
||||
[{:id 1 :name "mr blobby"}]))))
|
||||
|
||||
(deftest test-create!
|
||||
(testing "correct format is returned"
|
||||
(with-redefs [jdbc/execute! (constantly [])
|
||||
sql/query (constantly [{:customers/id 1 :customers/name "mr blobby" :bla 123}])]
|
||||
(= (sut/create! "1" "mr blobby")
|
||||
{:customers [{:id 1 :name "mr blobby"}]}))))
|
||||
|
||||
|
||||
(deftest test-delete!
|
||||
(testing "correct format returned"
|
||||
(with-redefs [orders/get-all (constantly :orders)
|
||||
sql/update! (constantly [])
|
||||
sql/query (constantly [{:customers/id 1 :customers/name "mr blobby" :bla 123}])]
|
||||
(= (sut/delete! "1" "2")
|
||||
{:customers [{:id 1 :name "mr blobby"}]
|
||||
:orders :orders}))))
|
74
test/clj/chicken_master/products_test.clj
Normal file
74
test/clj/chicken_master/products_test.clj
Normal file
@ -0,0 +1,74 @@
|
||||
(ns clj.chicken-master.products-test
|
||||
(:require
|
||||
[next.jdbc :as jdbc]
|
||||
[next.jdbc.sql :as sql]
|
||||
[chicken-master.products :as sut]
|
||||
[clojure.test :refer [deftest is testing]]))
|
||||
|
||||
(deftest test-get-all
|
||||
(testing "query is correct"
|
||||
(with-redefs [sql/query (fn [_ query]
|
||||
(is (= query ["SELECT * FROM products WHERE deleted IS NULL AND user_id = ?" "1"]))
|
||||
[])]
|
||||
(sut/get-all "1")))
|
||||
|
||||
(testing "correct format"
|
||||
(with-redefs [sql/query (constantly [{:products/name "eggs" :products/amount 12}
|
||||
{:products/name "milk" :products/amount 3}])]
|
||||
(is (= (sut/get-all "1") {:eggs 12 :milk 3})))))
|
||||
|
||||
(deftest test-products-map
|
||||
(testing "no products"
|
||||
(is (nil? (sut/products-map :tx "1" {})))
|
||||
(is (nil? (sut/products-map :tx "1" nil))))
|
||||
|
||||
(testing "correct sql"
|
||||
(with-redefs [sql/query (fn [_ query]
|
||||
(is (= query ["SELECT id, name FROM products WHERE user_id = ? AND name IN (?, ?, ?)"
|
||||
"1" "eggs" "cows" "milk"]))
|
||||
[])]
|
||||
(sut/products-map :tz "1" {:eggs 2 :cows 2 :milk 3})))
|
||||
|
||||
(testing "correct format"
|
||||
(with-redefs [sql/query (constantly [{:products/id 1 :products/name "eggs"}
|
||||
{:products/id 2 :products/name "cows"}
|
||||
{:products/id 3 :products/name "milk"}])]
|
||||
(= (sut/products-map :tz "1" {:eggs 2 :cows 2 :milk 3})
|
||||
{"eggs" 1 "cows" 2 "milk" 3})))
|
||||
|
||||
(testing "not all items need have ids"
|
||||
(with-redefs [sql/query (constantly [{:products/id 1 :products/name "eggs"}
|
||||
{:products/id 3 :products/name "milk"}])]
|
||||
(= (sut/products-map :tx "1" {:eggs 2 :cows 2 :milk 3})
|
||||
{"eggs" 1 "milk" 3}))))
|
||||
|
||||
(deftest test-update!
|
||||
(testing "each item gets updated"
|
||||
(let [inserts (atom [])
|
||||
update-query "INSERT INTO products (name, amount, user_id) VALUES(?, ?, ?)\n ON CONFLICT (name, user_id) DO UPDATE SET amount = EXCLUDED.amount, deleted = NULL"]
|
||||
(with-redefs [jdbc/transact (fn [_ f & args] (apply f args))
|
||||
jdbc/execute! #(swap! inserts conj %2)
|
||||
sql/update! (constantly nil)
|
||||
sql/query (constantly [])]
|
||||
(sut/update! :user-id {:eggs 2 :milk 3 :cows 2})
|
||||
(is (= (sort @inserts)
|
||||
[[update-query "cows" 2 :user-id]
|
||||
[update-query "eggs" 2 :user-id]
|
||||
[update-query "milk" 3 :user-id]])))))
|
||||
|
||||
(testing "non selected items get removed"
|
||||
(let [updates (atom [])]
|
||||
(with-redefs [jdbc/transact (fn [_ f & args] (apply f args))
|
||||
jdbc/execute! (constantly nil)
|
||||
sql/update! (partial swap! updates conj)
|
||||
sql/query (constantly [])]
|
||||
(sut/update! :user-id {:eggs 2 :milk 3 :cows 2})
|
||||
(is (= @updates [{} :products {:deleted true} ["name NOT IN (?, ?, ?)" "eggs" "milk" "cows"]])))))
|
||||
|
||||
(testing "non selected items get removed"
|
||||
(with-redefs [jdbc/transact (fn [_ f & args] (apply f args))
|
||||
jdbc/execute! (constantly nil)
|
||||
sql/update! (constantly nil)
|
||||
sql/query (constantly [{:products/name "eggs" :products/amount 12}
|
||||
{:products/name "milk" :products/amount 3}])]
|
||||
(is (= (sut/update! :user-id {:eggs 2 :milk 3 :cows 2}) {:eggs 12 :milk 3})))))
|
Loading…
x
Reference in New Issue
Block a user