Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix whitespace sensitive testing for aligned multi-line strings #37

Merged
merged 6 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/macaw/rewrite.clj
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,21 @@
(throw (ex-info (str "Unknown rename: " unknown) {:type k
:rename unknown}))))))

(defn- index-by-instances [xs]
(into {} (for [x xs
:let [c (:component x)]
i (:instances c)]
[i c])))

(defn replace-names
"Given a SQL query and its corresponding (untransformed) AST, apply the given table and column renames."
[sql parsed-ast {schema-renames :schemas
table-renames :tables
column-renames :columns
:as renames}]
(let [comps (collect/query->components parsed-ast {:with-instance true})
columns (into {} (for [c (:columns comps)
i (:instances (:component c))]
[i (:component c)]))
tables (into {} (for [t (:tables comps)
i (:instances (:component t))]
[i (:component t)]))
columns (index-by-instances (:columns comps))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't resist sneaking this in as well 🧼

tables (index-by-instances (:tables comps))
;; execute rename
updated-nodes (volatile! [])
res (-> parsed-ast
Expand Down
57 changes: 27 additions & 30 deletions test/macaw/core_test.clj
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
(ns ^:parallel macaw.core-test
(:require
[clojure.string :as str]
[clojure.test :refer [deftest is testing]]
[macaw.core :as m]
#_{:clj-kondo/ignore [:refer]}
crisptrutski marked this conversation as resolved.
Show resolved Hide resolved
[macaw.test-utils :refer [ws=]]
[macaw.walk :as mw])
(:import
(net.sf.jsqlparser.schema Table)))

(set! *warn-on-reflection* true)

(defn- normalize-ws [s]
(str/replace s #"\s\s+" " "))

(defn- and*
[x y]
(and x y))
Expand Down Expand Up @@ -236,34 +234,33 @@
(m/replace-names "SELECT p.id, q.id FROM public.orders p join private.orders q"
{:tables {{:schema "public" :table "orders"} "whatever"}})))

(is (= (normalize-ws "SELECT SUM(public.orders.total) AS s,
MAX(orders.total) AS max,
MIN(total) AS min
FROM public.orders")
(m/replace-names
(normalize-ws "SELECT SUM(public.orders.amount) AS s,
MAX(orders.amount) AS max,
MIN(amount) AS min
FROM public.orders")
{:columns {{:schema "public" :table "orders" :column "amount"} "total"}})))
(is (ws= "SELECT SUM(public.orders.total) AS s,
MAX(orders.total) AS max,
MIN(total) AS min
FROM public.orders"
(m/replace-names
"SELECT SUM(public.orders.amount) AS s,
MAX(orders.amount) AS max,
MIN(amount) AS min
FROM public.orders"
{:columns {{:schema "public" :table "orders" :column "amount"} "total"}})))

(is (= (normalize-ws "SELECT *, sturmunddrang
, oink AS oink
FROM /* /* lore */
floor_muser,
user, /* more */ vigilant_user ;")
(m/replace-names
(normalize-ws
"SELECT *, boink
, yoink AS oink
(is (ws= "SELECT *, sturmunddrang
, oink AS oink
FROM /* /* lore */
core_user,
bore_user, /* more */ snore_user ;")
{:tables {{:schema "public" :table "core_user"} "floor_muser"
{:schema "public" :table "bore_user"} "user"
{:schema "public" :table "snore_user"} "vigilant_user"}
:columns {{:schema "public" :table "core_user" :column "boink"} "sturmunddrang"
{:schema "public" :table "snore_user" :column "yoink"} "oink"}})))
floor_muser,
user, /* more */ vigilant_user ;"
(m/replace-names
"SELECT *, boink
, yoink AS oink
FROM /* /* lore */
core_user,
bore_user, /* more */ snore_user ;"
{:tables {{:schema "public" :table "core_user"} "floor_muser"
{:schema "public" :table "bore_user"} "user"
{:schema "public" :table "snore_user"} "vigilant_user"}
:columns {{:schema "public" :table "core_user" :column "boink"} "sturmunddrang"
{:schema "public" :table "snore_user" :column "yoink"} "oink"}})))

(is (thrown? Exception #"Unknown rename"
(m/replace-names "SELECT 1" {:tables {{:schema "public" :table "a"} "aa"}}))))
Expand Down
28 changes: 28 additions & 0 deletions test/macaw/test_utils.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns macaw.test-utils
crisptrutski marked this conversation as resolved.
Show resolved Hide resolved
(:require
[clojure.string :as str]
[clojure.test :refer :all]
[clojure.walk :as walk]))

(defn- indentation [s]
(count (re-find #"^\s*" s)))

(defn- trim-indent* [margin s]
(if (< (count s) margin)
""
(subs s margin)))

(defn trim-indent
"Given a multi-line string, remove the common margin from the remaining lines.
Used so that strings with significant whitespace may be visually aligned."
[s]
(let [lines (str/split-lines s)
margin (->> (rest lines)
(remove str/blank?)
(transduce (map indentation) min Integer/MAX_VALUE))]
(str/join "\n" (cons (first lines) (map (partial trim-indent* margin) (rest lines))))))

(defmacro ws=
"Trim the extra indentation from all string literals before evaluation a given equality form."
[& xs]
`(= ~@(walk/postwalk #(cond-> % (string? %) trim-indent) xs)))
crisptrutski marked this conversation as resolved.
Show resolved Hide resolved
Loading