Skip to content

Commit

Permalink
Be more SELECTive with case
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmacdonald committed Apr 8, 2024
1 parent 4a66e55 commit 055569c
Showing 1 changed file with 44 additions and 44 deletions.
88 changes: 44 additions & 44 deletions test/macaw/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
(deftest query->tables-test
(testing "Simple queries"
(is (= #{"core_user"}
(tables "select * from core_user;")))
(tables "SELECT * FROM core_user;")))
(is (= #{"core_user"}
(tables "select id, email from core_user;"))))
(tables "SELECT id, email FROM core_user;"))))
(testing "With a schema (Postgres)" ;; TODO: only run this against supported DBs
;; It strips the schema
(is (= #{"core_user"}
(tables "select * from the_schema_name.core_user;"))))
(tables "SELECT * FROM the_schema_name.core_user;"))))
(testing "Sub-selects"
(is (= #{"core_user"}
(tables "select * from (select distinct email from core_user) q;")))))
(tables "SELECT * FROM (SELECT DISTINCT email FROM core_user) q;")))))

(deftest tables-with-complex-aliases-issue-14-test
(testing "With an alias that is also a table name"
Expand All @@ -37,62 +37,62 @@
(deftest query->columns-test
(testing "Simple queries"
(is (= #{"foo" "bar" "id" "quux_id"}
(columns "select foo, bar from baz inner join quux on quux.id = baz.quux_id"))))
(columns "SELECT foo, bar FROM baz INNER JOIN quux ON quux.id = baz.quux_id"))))
(testing "'group by' columns present"
(is (= #{"id" "user_id"}
(columns "select id from orders group by user_id")))))
(columns "SELECT id FROM orders GROUP BY user_id")))))

(deftest mutation-test
(is (= #{"alter-sequence"}
(mutations "alter sequence serial restart with 42")))
(mutations "ALTER SEQUENCE serial RESTART WITH 42")))
(is (= #{"alter-session"}
(mutations "alter session set foo = 'bar'")))
(mutations "ALTER SESSION SET foo = 'bar'")))
(is (= #{"alter-system"}
(mutations "alter system reset all")))
(mutations "ALTER SYSTEM RESET ALL")))
(is (= #{"alter-table"}
(mutations "alter table orders add column email text")))
(mutations "ALTER TABLE orders ADD COLUMN email text")))
(is (= #{"alter-view"}
(mutations "alter view foo as select bar;")))
(mutations "ALTER VIEW foo AS SELECT bar;")))
(is (= #{"create-function"} ; Postgres syntax
(mutations "create function multiply(integer, integer) returns integer as 'select $1 * $2;' language sql
immutable returns null on null input;")))
(mutations "CREATE FUNCTION multiply(integer, integer) RETURNS integer AS 'SELECT $1 * $2;' LANGUAGE SQL
IMMUTABLE RETURNS NULL ON NULL INPUT;")))
(is (= #{"create-function"} ; Conventional syntax
(mutations "create function multiply(a integer, b integer) returns integer language sql immutable returns
null on null input return a + b;")))
(mutations "CREATE FUNCTION multiply(a integer, b integer) RETURNS integer LANGUAGE SQL IMMUTABLE RETURNS
NULL ON NULL INPUT RETURN a + b;")))
(is (= #{"create-index"}
(mutations "create index idx_user_id on orders(user_id);")))
(mutations "CREATE INDEX idx_user_id ON orders(user_id);")))
(is (= #{"create-schema"}
(mutations "create schema perthshire")))
(mutations "CREATE SCHEMA perthshire")))
(is (= #{"create-sequence"}
(mutations "create sequence users_seq start with 42 increment by 17")))
(mutations "CREATE SEQUENCE users_seq START WITH 42 INCREMENT BY 17")))
(is (= #{"create-synonym"}
(mutations "create synonym folk for people")))
(mutations "CREATE SYNONYM folk FOR people")))
(is (= #{"create-table"}
(mutations "create table poets (name text, id integer)")))
(mutations "CREATE TABLE poets (name text, id integer)")))
(is (= #{"create-view"}
(mutations "create view folk as select * from people where id > 10")))
(mutations "CREATE VIEW folk AS SELECT * FROM people WHERE id > 10")))
(is (= #{"delete"}
(mutations "delete from people")))
(mutations "DELETE FROM people")))
(is (= #{"drop"}
(mutations "drop table people")))
(mutations "DROP TABLE people")))
(is (= #{"grant"}
(mutations "grant select, update, insert on people to myself")))
(mutations "GRANT SELECT, UPDATE, INSERT ON people TO myself")))
(is (= #{"insert"}
(mutations "insert into people(name,source) values ('Robert Fergusson', 'Twitter'), ('Robert Burns',
(mutations "INSERT INTO people(name, source) VALUES ('Robert Fergusson', 'Twitter'), ('Robert Burns',
'Facebook')")))
(is (= #{"purge"}
(mutations "purge table people")))
(mutations "PURGE TABLE people")))
(is (= #{"rename-table"}
(mutations "rename table people to folk")))
(mutations "RENAME TABLE people TO folk")))
(is (= #{"truncate"}
(mutations "truncate table people")))
(mutations "TRUNCATE TABLE people")))
(is (= #{"update"}
(mutations "update people set name = 'Robert Fergusson' where id = 23"))))
(mutations "UPDATE people SET name = 'Robert Fergusson' WHERE id = 23"))))

(deftest alias-inclusion-test
(testing "Aliases are not included"
(is (= #{"orders" "foo"}
(tables "select id, o.id from orders o join foo on orders.id = foo.order_id")))))
(tables "SELECT id, o.id FROM orders o JOIN foo ON orders.id = foo.order_id")))))

(deftest resolve-columns-test
(let [cols ["name" "id" "email"]]
Expand All @@ -101,34 +101,34 @@
(m/resolve-columns ["core_user" "report_card"] cols)))))

(deftest select-*-test
(is (true? (has-wildcard? "select * from orders")))
(is (true? (has-wildcard? "select id, * from orders join foo on orders.id = foo.order_id"))))
(is (true? (has-wildcard? "SELECT * FROM orders")))
(is (true? (has-wildcard? "SELECT id, * FROM orders JOIN foo ON orders.id = foo.order_id"))))

(deftest table-wildcard-test-without-aliases
(is (= #{"orders"}
(table-wcs "select orders.* from orders join foo on orders.id = foo.order_id")))
(table-wcs "SELECT orders.* FROM orders JOIN foo ON orders.id = foo.order_id")))
(is (= #{"foo"}
(table-wcs "select foo.* from orders join foo on orders.id = foo.order_id"))))
(table-wcs "SELECT foo.* FROM orders JOIN foo ON orders.id = foo.order_id"))))

(deftest table-star-test-with-aliases
(is (= #{"orders"}
(table-wcs "select o.* from orders o join foo on orders.id = foo.order_id")))
(table-wcs "SELECT o.* FROM orders o JOIN foo ON orders.id = foo.order_id")))
(is (= #{"foo"}
(table-wcs "select f.* from orders o join foo f on orders.id = foo.order_id"))))
(table-wcs "SELECT f.* FROM orders o JOIN foo f ON orders.id = foo.order_id"))))

(defn test-replacement [before replacements after]
(is (= after (m/replace-names before replacements))))

(deftest replace-names-test
(test-replacement "select a.x, b.y from a, b;"
(test-replacement "SELECT a.x, b.y FROM a, b;"
{:tables {"a" "aa"}
:columns {"x" "xx"}}
"select aa.xx, b.y from aa, b;")
"SELECT aa.xx, b.y FROM aa, b;")

(test-replacement
"select *, boink
, yoink as oink
from /* /* lore */
"SELECT *, boink
, yoink AS oink
FROM /* /* lore */
core_user,
bore_user, /* more */ snore_user ;"

Expand All @@ -140,8 +140,8 @@
"yoink" "oink"
"hoi" "polloi"}}

"select *, sturmunddrang
, oink as oink
from /* /* lore */
"SELECT *, sturmunddrang
, oink AS oink
FROM /* /* lore */
floor_muser,
user, /* more */ vigilant_user ;"))

0 comments on commit 055569c

Please sign in to comment.