This commit is contained in:
Daniel O'Connell 2021-03-02 22:39:30 +01:00
parent 38053bbdb2
commit 599472af37
4 changed files with 114 additions and 4 deletions

View File

@ -59,7 +59,7 @@
(jdbc/with-transaction [tx db/db-uri] (jdbc/with-transaction [tx db/db-uri]
(let [customer-id (or (:id who) (let [customer-id (or (:id who)
(:customers/id (db/get-by-id tx user-id :customers (:name who) :name))) (: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)) 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)] order-id (upsert-order! tx user-id customer-id order)]
(sql/delete! tx :order_products {:order_id order-id}) (sql/delete! tx :order_products {:order_id order-id})

View File

@ -4,14 +4,14 @@
[chicken-master.db :as db])) [chicken-master.db :as db]))
(defn get-all [user-id] (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])) (map (fn [{:products/keys [name amount]}] [(keyword name) amount]))
(into {}))) (into {})))
(defn products-map [tx products] (defn products-map [tx user-id products]
(when (seq products) (when (seq products)
(->> (map name (keys 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) (sql/query tx)
(map #(vector (:products/name %) (:products/id %))) (map #(vector (:products/name %) (:products/id %)))
(into {})))) (into {}))))

View 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}))))

View 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})))))