Addresses #293 by porting nilenso tests

I intended to keep all the original tests inline but it got harder than
I expected, so I'm going to make another pass over this and insert the
original tests back in later.
This commit is contained in:
Sean Corfield 2021-02-13 16:02:13 -08:00
parent f6975ef6bd
commit 7b928fecb2

View file

@ -14,7 +14,9 @@
;; pull in all the PostgreSQL helpers that the nilenso ;; pull in all the PostgreSQL helpers that the nilenso
;; library provided (as well as the regular HoneySQL ones): ;; library provided (as well as the regular HoneySQL ones):
[honey.sql.helpers :as sqlh :refer [honey.sql.helpers :as sqlh :refer
[upsert on-conflict do-nothing on-constraint [;; not needed because on-conflict accepts clauses
#_upsert
on-conflict do-nothing on-constraint
returning do-update-set returning do-update-set
;; not needed because do-update-set can do this directly ;; not needed because do-update-set can do this directly
#_do-update-set! #_do-update-set!
@ -26,8 +28,9 @@
window create-view over with-columns window create-view over with-columns
;; temporarily disable until these are also implemented: ;; temporarily disable until these are also implemented:
#_#_create-extension drop-extension #_#_create-extension drop-extension
select-distinct-on
;; already part of HoneySQL ;; already part of HoneySQL
insert-into values where select columns insert-into values where select
from order-by update set]] from order-by update set]]
[honey.sql :as sql])) [honey.sql :as sql]))
@ -37,43 +40,41 @@
(-> (insert-into :distributors) (-> (insert-into :distributors)
(values [{:did 5 :dname "Gizmo Transglobal"} (values [{:did 5 :dname "Gizmo Transglobal"}
{:did 6 :dname "Associated Computing, Inc"}]) {:did 6 :dname "Associated Computing, Inc"}])
(upsert (-> (on-conflict :did) (on-conflict :did)
(do-update-set :dname))) (do-update-set :dname)
(returning :*) (returning :*)
sql/format))) sql/format)))
(is (= ["INSERT INTO distributors (did, dname) VALUES (?, ?) ON CONFLICT (did) DO NOTHING" 7 "Redline GmbH"] (is (= ["INSERT INTO distributors (did, dname) VALUES (?, ?) ON CONFLICT (did) DO NOTHING" 7 "Redline GmbH"]
(-> (insert-into :distributors) (-> (insert-into :distributors)
(values [{:did 7 :dname "Redline GmbH"}]) (values [{:did 7 :dname "Redline GmbH"}])
(upsert (-> (on-conflict :did) (on-conflict :did)
do-nothing)) do-nothing
sql/format))) sql/format)))
(is (= ["INSERT INTO distributors (did, dname) VALUES (?, ?) ON CONFLICT ON CONSTRAINT distributors_pkey DO NOTHING" 9 "Antwerp Design"] (is (= ["INSERT INTO distributors (did, dname) VALUES (?, ?) ON CONFLICT ON CONSTRAINT distributors_pkey DO NOTHING" 9 "Antwerp Design"]
(-> (insert-into :distributors) (-> (insert-into :distributors)
(values [{:did 9 :dname "Antwerp Design"}]) (values [{:did 9 :dname "Antwerp Design"}])
(upsert (-> #_(on-conflict-constraint :distributors_pkey) (on-conflict (on-constraint :distributors_pkey))
(on-conflict (on-constraint :distributors_pkey)) do-nothing
do-nothing))
sql/format))) sql/format)))
(is (= ["INSERT INTO distributors (did, dname) VALUES (?, ?), (?, ?) ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname" 10 "Pinp Design" 11 "Foo Bar Works"] (is (= ["INSERT INTO distributors (did, dname) VALUES (?, ?), (?, ?) ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname" 10 "Pinp Design" 11 "Foo Bar Works"]
(sql/format {:insert-into :distributors (sql/format {:insert-into :distributors
:values [{:did 10 :dname "Pinp Design"} :values [{:did 10 :dname "Pinp Design"}
{:did 11 :dname "Foo Bar Works"}] {:did 11 :dname "Foo Bar Works"}]
:upsert {:on-conflict [:did] :on-conflict :did
:do-update-set [:dname]}}))) :do-update-set :dname})))
(is (= ["INSERT INTO distributors (did, dname) VALUES (?, ?) ON CONFLICT (did) DO UPDATE SET dname = ?" 23 "Foo Distributors" " (formerly " ")"] (is (= ["INSERT INTO distributors (did, dname) VALUES (?, ?) ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname || ? || d.dname || ?" 23 "Foo Distributors" " (formerly " ")"]
(-> (insert-into :distributors) (-> (insert-into :distributors)
(values [{:did 23 :dname "Foo Distributors"}]) (values [{:did 23 :dname "Foo Distributors"}])
(on-conflict :did) (on-conflict :did)
#_(do-update-set! [:dname "EXCLUDED.dname || ' (formerly ' || d.dname || ')'"]) #_(do-update-set! [:dname "EXCLUDED.dname || ' (formerly ' || d.dname || ')'"])
(do-update-set {:dname [:|| :EXCLUDED.dname " (formerly " :d.dname ")"]}) (do-update-set {:dname [:|| :EXCLUDED.dname " (formerly " :d.dname ")"]})
sql/format))) sql/format)))
(is (= ["INSERT INTO distributors (did, dname) (SELECT ?, ?) ON CONFLICT ON CONSTRAINT distributors_pkey DO NOTHING" 1 "whatever"] (is (= ["INSERT INTO distributors (did, dname) SELECT ?, ? ON CONFLICT ON CONSTRAINT distributors_pkey DO NOTHING" 1 "whatever"]
(-> (insert-into :distributors) (-> (insert-into [:distributors [:did :dname]]
(columns :did :dname) (select 1 "whatever"))
(select 1 "whatever")
#_(query-values (select 1 "whatever")) #_(query-values (select 1 "whatever"))
(upsert (-> (on-conflict (on-constraint :distributors_pkey)) (on-conflict (on-constraint :distributors_pkey))
do-nothing)) do-nothing
sql/format))))) sql/format)))))
(deftest upsert-where-test (deftest upsert-where-test
@ -81,10 +82,10 @@
(sql/format (sql/format
{:insert-into :user {:insert-into :user
:values [{:phone "5555555" :name "John"}] :values [{:phone "5555555" :name "John"}]
:upsert {:on-conflict [:phone] :on-conflict [:phone
:where [:<> :phone nil] {:where [:<> :phone nil]}]
:do-update-set {:fields [:phone :name] :do-update-set {:fields [:phone :name]
:where [:= :user.active false]}}})))) :where [:= :user.active false]}}))))
(deftest returning-test (deftest returning-test
(testing "returning clause in sql generation for postgresql" (testing "returning clause in sql generation for postgresql"
@ -143,7 +144,7 @@
[[:constraint :code_title] [:primary-key :code :title]]]) [[:constraint :code_title] [:primary-key :code :title]]])
sql/format)))) sql/format))))
(testing "creating table with column level constraint" (testing "creating table with column level constraint"
(is (= ["CREATE TABLE films (code CHAR(5) CONSTRAINT firstkey PRIMARY KEY, title VARCHAR(40) NOT NULL, did INTEGER NOT NULL, date_prod DATE, kind VARCHAR(10))"] (is (= ["CREATE TABLE films (code CHAR(5) CONSTRAINT FIRSTKEY PRIMARY KEY, title VARCHAR(40) NOT NULL, did INTEGER NOT NULL, date_prod DATE, kind VARCHAR(10))"]
(-> (create-table :films) (-> (create-table :films)
(with-columns [[:code [:char 5] [:constraint :firstkey] [:primary-key]] (with-columns [[:code [:char 5] [:constraint :firstkey] [:primary-key]]
[:title [:varchar 40] [:not nil]] [:title [:varchar 40] [:not nil]]
@ -169,18 +170,26 @@
(deftest over-test (deftest over-test
(testing "window function over on select statemt" (testing "window function over on select statemt"
(is (= ["SELECT id, AVG(salary) OVER (PARTITION BY department ORDER BY designation) AS Average, max(salary) OVER w AS MaxSalary FROM employee WINDOW w AS (PARTITION BY department)"] (is (= ["SELECT id, AVG(salary) OVER (PARTITION BY department ORDER BY designation ASC) AS Average, MAX(salary) OVER w AS MaxSalary FROM employee WINDOW w AS (PARTITION BY department)"]
(-> (select :id) (-> (select :id
(over ;; honeysql treats over as a function:
[[:avg :salary] (-> (partition-by :department) (order-by [:designation])) :Average] (over
[[:max :salary] :w :MaxSalary]) [[:avg :salary] (-> (partition-by :department) (order-by [:designation])) :Average]
[[:max :salary] :w :MaxSalary]))
(from :employee) (from :employee)
(window :w (partition-by :department)) (window :w (partition-by :department))
sql/format))))) sql/format)
#_(-> (select :id)
(over
[[:avg :salary] (-> (partition-by :department) (order-by [:designation])) :Average]
[[:max :salary] :w :MaxSalary])
(from :employee)
(window :w (partition-by :department))
sql/format)))))
(deftest alter-table-test (deftest alter-table-test
(testing "alter table add column generates the required sql" (testing "alter table add column generates the required sql"
(is (= ["ALTER TABLE employees ADD COLUMN address text"] (is (= ["ALTER TABLE employees ADD COLUMN address TEXT"]
(-> (alter-table :employees) (-> (alter-table :employees)
(add-column :address :text) (add-column :address :text)
sql/format)))) sql/format))))
@ -207,9 +216,12 @@
(insert-into :distributors :d) (insert-into :distributors :d)
(values [{:did 5 :dname "Gizmo Transglobal"} (values [{:did 5 :dname "Gizmo Transglobal"}
{:did 6 :dname "Associated Computing, Inc"}]) {:did 6 :dname "Associated Computing, Inc"}])
(upsert (-> (on-conflict :did) (on-conflict :did)
(do-update-set :dname) (do-update-set (-> {:fields [:dname]}
(where [:<> :d.zipcode "21201"]))) (where [:<> :d.zipcode "21201"])))
#_(upsert (-> (on-conflict :did)
(do-update-set :dname)
(where [:<> :d.zipcode "21201"])))
(returning :d.*) (returning :d.*)
sql/format))))) sql/format)))))
@ -243,7 +255,7 @@
(deftest values-except-select (deftest values-except-select
(testing "select which values are not not present in a table" (testing "select which values are not not present in a table"
(is (= ["VALUES (?), (?), (?) EXCEPT SELECT id FROM images" 4 5 6] (is (= ["(VALUES (?), (?), (?)) EXCEPT (SELECT id FROM images)" 4 5 6]
(sql/format (sql/format
{:except {:except
[{:values [[4] [5] [6]]} [{:values [[4] [5] [6]]}
@ -251,7 +263,7 @@
(deftest select-except-select (deftest select-except-select
(testing "select which rows are not present in another table" (testing "select which rows are not present in another table"
(is (= ["SELECT ip EXCEPT SELECT ip FROM ip_location"] (is (= ["(SELECT ip) EXCEPT (SELECT ip FROM ip_location)"]
(sql/format (sql/format
{:except {:except
[{:select [:ip]} [{:select [:ip]}
@ -259,7 +271,7 @@
(deftest values-except-all-select (deftest values-except-all-select
(testing "select which values are not not present in a table" (testing "select which values are not not present in a table"
(is (= ["VALUES (?), (?), (?) EXCEPT ALL SELECT id FROM images" 4 5 6] (is (= ["(VALUES (?), (?), (?)) EXCEPT ALL (SELECT id FROM images)" 4 5 6]
(sql/format (sql/format
{:except-all {:except-all
[{:values [[4] [5] [6]]} [{:values [[4] [5] [6]]}
@ -267,16 +279,16 @@
(deftest select-except-all-select (deftest select-except-all-select
(testing "select which rows are not present in another table" (testing "select which rows are not present in another table"
(is (= ["SELECT ip EXCEPT ALL SELECT ip FROM ip_location"] (is (= ["(SELECT ip) EXCEPT ALL (SELECT ip FROM ip_location)"]
(sql/format (sql/format
{:except-all {:except-all
[{:select [:ip]} [{:select [:ip]}
{:select [:ip] :from [:ip_location]}]}))))) {:select [:ip] :from [:ip_location]}]})))))
(deftest select-distinct-on (deftest select-distinct-on-test
(testing "select distinct on" (testing "select distinct on"
(is (= ["SELECT DISTINCT ON(\"a\", \"b\") \"c\" FROM \"products\""] (is (= ["SELECT DISTINCT ON(\"a\", \"b\") \"c\" FROM \"products\""]
(-> (select [[:distinct-on :a :b]] :c) (-> (select-distinct-on [:a :b] :c)
(from :products) (from :products)
(sql/format {:quoted true})) (sql/format {:quoted true}))
#_(-> (select :c) #_(-> (select :c)