Leveraged whitespace-insensitive match to make expected SQL readable
Pleased with this!
This commit is contained in:
parent
dcbe9ee01b
commit
fa704d1a07
|
@ -38,7 +38,7 @@
|
||||||
(str
|
(str
|
||||||
"WHERE " entity-name "."
|
"WHERE " entity-name "."
|
||||||
(s/join
|
(s/join
|
||||||
(str " AND\n\t" entity-name ".")
|
(str "\n\tAND " entity-name ".")
|
||||||
(map
|
(map
|
||||||
#(let [target (keyword (-> % :attrs :name))]
|
#(let [target (keyword (-> % :attrs :name))]
|
||||||
(str
|
(str
|
||||||
|
@ -63,9 +63,15 @@
|
||||||
"ORDER BY " entity-name "."
|
"ORDER BY " entity-name "."
|
||||||
(s/join
|
(s/join
|
||||||
(str ",\n\t" entity-name ".")
|
(str ",\n\t" entity-name ".")
|
||||||
(doall (flatten (cons preferred (filter
|
(doall
|
||||||
#(not (#{"all", "user"} %))
|
(flatten
|
||||||
(key-names entity-map)))))))))
|
(cons
|
||||||
|
preferred
|
||||||
|
(map
|
||||||
|
#(-> % :attrs :name)
|
||||||
|
(filter
|
||||||
|
#(not (#{"all", "user"} (-> % :attrs :distinct)))
|
||||||
|
(key-properties entity-map))))))))))
|
||||||
|
|
||||||
|
|
||||||
(defn insert-query [entity-map]
|
(defn insert-query [entity-map]
|
||||||
|
|
|
@ -46,7 +46,10 @@
|
||||||
{:distinct "user", :size "12", :type "string", :name "postcode"},
|
{:distinct "user", :size "12", :type "string", :name "postcode"},
|
||||||
:content nil}]}]
|
:content nil}]}]
|
||||||
(testing "user distinct properties should provide the default ordering"
|
(testing "user distinct properties should provide the default ordering"
|
||||||
(let [expected "ORDER BY address.street,\n\taddress.postcode,\n\taddress.id"
|
(let [expected
|
||||||
|
"ORDER BY address.street,
|
||||||
|
address.postcode,
|
||||||
|
address.id"
|
||||||
actual (order-by-clause xml)]
|
actual (order-by-clause xml)]
|
||||||
(is (string-equal-ignore-whitespace actual expected))))
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
(testing "keys name extraction"
|
(testing "keys name extraction"
|
||||||
|
@ -62,7 +65,15 @@
|
||||||
actual (has-non-key-properties? xml)]
|
actual (has-non-key-properties? xml)]
|
||||||
(is (string-equal-ignore-whitespace actual expected))))
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
(testing "insert query generation"
|
(testing "insert query generation"
|
||||||
(let [expected "-- :name create-addres! :! :n\n-- :doc creates a new addres record\nINSERT INTO address (street,\n\ttown,\n\tpostcode)\nVALUES (':street',\n\t':town',\n\t':postcode')\nreturning id\n\n"
|
(let [expected "-- :name create-addres! :! :n
|
||||||
|
-- :doc creates a new addres record
|
||||||
|
INSERT INTO address (street,
|
||||||
|
town,
|
||||||
|
postcode)
|
||||||
|
VALUES (':street',
|
||||||
|
':town',
|
||||||
|
':postcode')
|
||||||
|
returning id\n\n"
|
||||||
actual (:query (first (vals (insert-query xml))))]
|
actual (:query (first (vals (insert-query xml))))]
|
||||||
(is (string-equal-ignore-whitespace actual expected))))
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
(testing "insert query signature"
|
(testing "insert query signature"
|
||||||
|
@ -70,7 +81,12 @@
|
||||||
actual (:signature (first (vals (insert-query xml))))]
|
actual (:signature (first (vals (insert-query xml))))]
|
||||||
(is (string-equal-ignore-whitespace actual expected))))
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
(testing "update query generation"
|
(testing "update query generation"
|
||||||
(let [expected "-- :name update-addres! :! :n\n-- :doc updates an existing addres record\nUPDATE address\nSET street = :street,\n\ttown = :town,\n\tpostcode = :postcode\nWHERE address.id = :id\n\n"
|
(let [expected "-- :name update-addres! :! :n
|
||||||
|
-- :doc updates an existing addres record
|
||||||
|
UPDATE address\nSET street = :street,
|
||||||
|
town = :town,
|
||||||
|
postcode = :postcode
|
||||||
|
WHERE address.id = :id\n\n"
|
||||||
actual (:query (first (vals (update-query xml))))]
|
actual (:query (first (vals (update-query xml))))]
|
||||||
(is (string-equal-ignore-whitespace actual expected))))
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
(testing "update query signature"
|
(testing "update query signature"
|
||||||
|
@ -78,7 +94,17 @@
|
||||||
actual (:signature (first (vals (update-query xml))))]
|
actual (:signature (first (vals (update-query xml))))]
|
||||||
(is (string-equal-ignore-whitespace actual expected))))
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
(testing "search query generation"
|
(testing "search query generation"
|
||||||
(let [expected "-- :name search-strings-addres :? :1\n-- :doc selects existing address records having any string field matching `:pattern` by substring match\nSELECT * FROM address\nWHERE street LIKE '%:pattern%'\n\tOR town LIKE '%:pattern%'\n\tOR postcode LIKE '%:pattern%'\nORDER BY address.street,\n\taddress.postcode,\n\taddress.id\n--~ (if (:offset params) \"OFFSET :offset \") \n--~ (if (:limit params) \"LIMIT :limit\" \"LIMIT 100\")\n\n"
|
(let [expected "-- :name search-strings-addres :? :1
|
||||||
|
-- :doc selects existing address records having any string field matching `:pattern` by substring match
|
||||||
|
SELECT * FROM address
|
||||||
|
WHERE street LIKE '%:pattern%'
|
||||||
|
OR town LIKE '%:pattern%'
|
||||||
|
OR postcode LIKE '%:pattern%'
|
||||||
|
ORDER BY address.street,
|
||||||
|
address.postcode,
|
||||||
|
address.id
|
||||||
|
--~ (if (:offset params) \"OFFSET :offset \")
|
||||||
|
--~ (if (:limit params) \"LIMIT :limit\" \"LIMIT 100\")\n\n"
|
||||||
actual (:query (first (vals (search-query xml))))]
|
actual (:query (first (vals (search-query xml))))]
|
||||||
(is (string-equal-ignore-whitespace actual expected))))
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
(testing "search query signature"
|
(testing "search query signature"
|
||||||
|
@ -86,7 +112,10 @@
|
||||||
actual (:signature (first (vals (search-query xml))))]
|
actual (:signature (first (vals (search-query xml))))]
|
||||||
(is (string-equal-ignore-whitespace actual expected))))
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
(testing "select query generation"
|
(testing "select query generation"
|
||||||
(let [expected "-- :name get-addres :? :1\n-- :doc selects an existing addres record\nSELECT * FROM address\nWHERE address.id = :id\n\n"
|
(let [expected "-- :name get-addres :? :1
|
||||||
|
-- :doc selects an existing addres record
|
||||||
|
SELECT * FROM address
|
||||||
|
WHERE address.id = :id\n\n"
|
||||||
actual (:query (first (vals (select-query xml))))]
|
actual (:query (first (vals (select-query xml))))]
|
||||||
(is (string-equal-ignore-whitespace actual expected))))
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
(testing "select query signature"
|
(testing "select query signature"
|
||||||
|
@ -94,7 +123,14 @@
|
||||||
actual (:signature (first (vals (select-query xml))))]
|
actual (:signature (first (vals (select-query xml))))]
|
||||||
(is (string-equal-ignore-whitespace actual expected))))
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
(testing "list query generation"
|
(testing "list query generation"
|
||||||
(let [expected "-- :name list-address :? :*\n-- :doc lists all existing addres records\nSELECT * FROM address\nORDER BY address.street,\n\taddress.postcode,\n\taddress.id\n--~ (if (:offset params) \"OFFSET :offset \") \n--~ (if (:limit params) \"LIMIT :limit\" \"LIMIT 100\")\n\n"
|
(let [expected "-- :name list-address :? :*
|
||||||
|
-- :doc lists all existing addres records
|
||||||
|
SELECT * FROM address
|
||||||
|
ORDER BY address.street,
|
||||||
|
address.postcode,
|
||||||
|
address.id
|
||||||
|
--~ (if (:offset params) \"OFFSET :offset \")
|
||||||
|
--~ (if (:limit params) \"LIMIT :limit\" \"LIMIT 100\")\n\n"
|
||||||
actual (:query (first (vals (list-query xml))))]
|
actual (:query (first (vals (list-query xml))))]
|
||||||
(is (string-equal-ignore-whitespace actual expected))))
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
(testing "list query signature"
|
(testing "list query signature"
|
||||||
|
@ -102,7 +138,10 @@
|
||||||
actual (:signature (first (vals (list-query xml))))]
|
actual (:signature (first (vals (list-query xml))))]
|
||||||
(is (string-equal-ignore-whitespace actual expected))))
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
(testing "delete query generation"
|
(testing "delete query generation"
|
||||||
(let [expected "-- :name delete-addres! :! :n\n-- :doc updates an existing addres record\nDELETE FROM address\nWHERE address.id = :id\n\n"
|
(let [expected "-- :name delete-addres! :! :n
|
||||||
|
-- :doc updates an existing addres record
|
||||||
|
DELETE FROM address
|
||||||
|
WHERE address.id = :id\n\n"
|
||||||
actual (:query (first (vals (delete-query xml))))]
|
actual (:query (first (vals (delete-query xml))))]
|
||||||
(is (string-equal-ignore-whitespace actual expected))))
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
(testing "delete query signature"
|
(testing "delete query signature"
|
||||||
|
@ -148,20 +187,59 @@
|
||||||
:attrs {:size "64", :type "string", :name "town"},
|
:attrs {:size "64", :type "string", :name "town"},
|
||||||
:content nil}
|
:content nil}
|
||||||
]}]
|
]}]
|
||||||
|
(testing "user distinct properties should provide the default ordering"
|
||||||
|
(let [expected "ORDER BY address.street,
|
||||||
|
address.postcode,
|
||||||
|
address.id"
|
||||||
|
actual (order-by-clause xml)]
|
||||||
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
|
(testing "keys name extraction"
|
||||||
|
(let [expected '("id" "postcode")
|
||||||
|
actual (key-names xml)]
|
||||||
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
(testing "insert query generation - compound key, non system generated field in key"
|
(testing "insert query generation - compound key, non system generated field in key"
|
||||||
(let [expected "-- :name create-addres! :! :n\n-- :doc creates a new addres record\nINSERT INTO address (street,\n\ttown,\n\tpostcode)\nVALUES (':street',\n\t':town',\n\t':postcode')\nreturning id,\n\tpostcode\n\n"
|
(let [expected "-- :name create-addres! :! :n
|
||||||
|
-- :doc creates a new addres record
|
||||||
|
INSERT INTO address (street,
|
||||||
|
town,
|
||||||
|
postcode)
|
||||||
|
VALUES (':street',
|
||||||
|
':town',
|
||||||
|
':postcode')
|
||||||
|
returning id,
|
||||||
|
postcode\n\n"
|
||||||
actual (:query (first (vals (insert-query xml))))]
|
actual (:query (first (vals (insert-query xml))))]
|
||||||
(is (string-equal-ignore-whitespace actual expected))))
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
(testing "update query generation - compound key"
|
(testing "update query generation - compound key"
|
||||||
(let [expected "-- :name update-addres! :! :n\n-- :doc updates an existing addres record\nUPDATE address\nSET street = :street,\n\ttown = :town\nWHERE address.id = :id AND\n\taddress.postcode = ':postcode'\n\n"
|
(let [expected "-- :name update-addres! :! :n
|
||||||
|
-- :doc updates an existing addres record
|
||||||
|
UPDATE address
|
||||||
|
SET street = :street,
|
||||||
|
town = :town
|
||||||
|
WHERE address.id = :id
|
||||||
|
AND address.postcode = ':postcode'\n\n"
|
||||||
actual (:query (first (vals (update-query xml))))]
|
actual (:query (first (vals (update-query xml))))]
|
||||||
(is (string-equal-ignore-whitespace actual expected))))
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
(testing "search query generation - user-distinct field in key"
|
(testing "search query generation - user-distinct field in key"
|
||||||
(let [expected "-- :name search-strings-addres :? :1\n-- :doc selects existing address records having any string field matching `:pattern` by substring match\nSELECT * FROM address\nWHERE street LIKE '%:pattern%'\n\tOR town LIKE '%:pattern%'\n\tOR postcode LIKE '%:pattern%'\nORDER BY address.street,\n\taddress.postcode,\n\taddress.id,\n\taddress.postcode\n--~ (if (:offset params) \"OFFSET :offset \") \n--~ (if (:limit params) \"LIMIT :limit\" \"LIMIT 100\")\n\n"
|
(let [expected "-- :name search-strings-addres :? :1
|
||||||
|
-- :doc selects existing address records having any string field matching `:pattern` by substring match
|
||||||
|
SELECT * FROM address
|
||||||
|
WHERE street LIKE '%:pattern%'
|
||||||
|
OR town LIKE '%:pattern%'
|
||||||
|
OR postcode LIKE '%:pattern%'
|
||||||
|
ORDER BY address.street,
|
||||||
|
address.postcode,
|
||||||
|
address.id
|
||||||
|
--~ (if (:offset params) \"OFFSET :offset \")
|
||||||
|
--~ (if (:limit params) \"LIMIT :limit\" \"LIMIT 100\")\n\n"
|
||||||
actual (:query (first (vals (search-query xml))))]
|
actual (:query (first (vals (search-query xml))))]
|
||||||
(is (string-equal-ignore-whitespace actual expected))))
|
(is (string-equal-ignore-whitespace actual expected))))
|
||||||
(testing "delete query generation - compound key"
|
(testing "delete query generation - compound key"
|
||||||
(let [expected "-- :name delete-addres! :! :n\n-- :doc updates an existing addres record\nDELETE FROM address\nWHERE address.id = :id AND\n\taddress.postcode = ':postcode'\n\n"
|
(let [expected "-- :name delete-addres! :! :n
|
||||||
|
-- :doc updates an existing addres record
|
||||||
|
DELETE FROM address
|
||||||
|
WHERE address.id = :id
|
||||||
|
AND address.postcode = ':postcode'\n\n"
|
||||||
actual (:query (first (vals (delete-query xml))))]
|
actual (:query (first (vals (delete-query xml))))]
|
||||||
(is (string-equal-ignore-whitespace actual expected))))))
|
(is (string-equal-ignore-whitespace actual expected))))))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue