Skip to content

Commit

Permalink
Merge most queries into a shared SQL file (#103)
Browse files Browse the repository at this point in the history
I'm finding creating and jumping between tiny files to be quite a chore.

This leaves all the EDN for now. We probably want an option to put that inline in the SQL file, but for larger files I want a rich editor experience, without needing a [fancy Emacs mode](https://github.com/polymode/polymode).
  • Loading branch information
crisptrutski authored Oct 11, 2024
1 parent 828d90b commit a1f3f41
Show file tree
Hide file tree
Showing 32 changed files with 338 additions and 243 deletions.
48 changes: 42 additions & 6 deletions test/macaw/acceptance_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns macaw.acceptance-test
(:require
[clojure.java.io :as io]
[clojure.set :as set]
[clojure.string :as str]
[clojure.test :refer :all]
[macaw.core :as m]
Expand All @@ -26,7 +27,7 @@
(some-> fixture (ct/fixture->filename "acceptance" ".renames.edn") io/resource slurp read-string))

(defn- fixture-rewritten [fixture]
(some-> fixture (ct/fixture->filename "acceptance" ".rewritten.sql") io/resource slurp))
(some-> fixture (ct/fixture->filename "acceptance" ".rewritten.sql") io/resource slurp str/trim))

(defn- get-component [cs k]
(case k
Expand All @@ -41,6 +42,18 @@
(def global-overrides
{:basic-select :macaw.error/not-implemented})

(def ^:private merged-fixtures-file "test/resources/acceptance/queries.sql")

(defn- merged-fixtures
"The fixtures in merged fixtures file, mapped by their identifiers."
[]
(->> (str/split (slurp merged-fixtures-file) #"-- FIXTURE: ")
(keep (fn [named-query]
(when-not (str/blank? named-query)
(let [[nm qry] (.split ^String named-query "\n" 2)]
[(keyword nm) (str/trim qry)]))))
(into {})))

(defn- validate-analysis [correct override actual]
(let [expected (or override correct)]
(when override
Expand Down Expand Up @@ -71,7 +84,8 @@
"Test that we can parse a given fixture, and compare against expected analysis and rewrites, where they are defined."
[fixture]
(let [prefix (str "(fixture: " (subs (str fixture) 1) ")")
sql (ct/query-fixture fixture)
merged (merged-fixtures)
sql (or (ct/query-fixture fixture) (get merged fixture))
expected-cs (fixture-analysis fixture)
renames (fixture-renames fixture)
expected-rw (fixture-rewritten fixture)
Expand Down Expand Up @@ -104,15 +118,15 @@
(when renames
(let [broken? (:broken? renames)
rewritten (testing (str prefix " rewriting does not throw")
(is (m/replace-names sql (dissoc renames :broken?) base-opts)))]
(is (str/trim (m/replace-names sql (dissoc renames :broken?) base-opts))))]
(when expected-rw
(testing (str prefix " rewritten SQL is correct")
(if broken?
(is (not= expected-rw rewritten))
(is (= expected-rw rewritten)))))))))

(defn find-fixtures
"Find all the fixture symbols within our test resources."
(defn isolated-fixtures
"Find all the fixture symbols for stand-alone sql files within our test resources."
[]
(->> (io/resource "acceptance")
io/file
Expand All @@ -122,13 +136,21 @@
(when (.endsWith n ".sql")
(str/replace n #"\.sql$" "")))))
(remove #(.contains ^String % "."))
(remove #{"queries"})
(map ct/stem->fixture)
(sort-by str)))

(defn- all-fixtures []
(let [isolated (isolated-fixtures)
merged (keys (merged-fixtures))]
(assert (empty? (set/intersection (set isolated) (set merged)))
"No fixtures should be in both the isolated and merged files")
(sort-by str (distinct (concat isolated merged)))))

(defmacro create-fixture-tests!
"Find all the fixture files and for each of them run all the tests we can construct from the related files."
[]
(let [fixtures (find-fixtures)]
(let [fixtures (all-fixtures)]
(cons 'do
(for [f fixtures
:let [test-name (symbol (str/replace (ct/fixture->filename f "-test") #"(?<!_)_(?!_)" "-"))]]
Expand All @@ -143,6 +165,20 @@
(when (:test (meta ns-var))
(ns-unmap *ns* sym)))

(merged-fixtures)
;; Append all the isolated fixtures to the merged file.
;; For now, we keep the stress-testing fixtures separate, because OH LAWDY they HUGE.
(spit merged-fixtures-file
(str/join "\n\n"
(for [fixture (isolated-fixtures)]
(str "-- FIXTURE: "
(when-let [nms (namespace fixture)]
(str nms "/"))
(name fixture) "\n"
(str/trim
(ct/query-fixture fixture))))))


(test-fixture :compound/cte)
(test-fixture :compound/cte-nonambiguous)
(test-fixture :literal/with-table)
Expand Down
3 changes: 0 additions & 3 deletions test/resources/acceptance/between__working.sql

This file was deleted.

7 changes: 0 additions & 7 deletions test/resources/acceptance/bigquery__table_wildcard.sql

This file was deleted.

7 changes: 0 additions & 7 deletions test/resources/acceptance/broken__between.sql

This file was deleted.

7 changes: 0 additions & 7 deletions test/resources/acceptance/broken__filter_where.sql

This file was deleted.

6 changes: 0 additions & 6 deletions test/resources/acceptance/compound__correlated_subquery.sql

This file was deleted.

23 changes: 0 additions & 23 deletions test/resources/acceptance/compound__cte.sql

This file was deleted.

4 changes: 0 additions & 4 deletions test/resources/acceptance/compound__cte_deadscope.sql

This file was deleted.

4 changes: 0 additions & 4 deletions test/resources/acceptance/compound__cte_masking.sql

This file was deleted.

4 changes: 0 additions & 4 deletions test/resources/acceptance/compound__cte_nonambiguous.sql

This file was deleted.

15 changes: 0 additions & 15 deletions test/resources/acceptance/compound__cte_pun.sql

This file was deleted.

10 changes: 0 additions & 10 deletions test/resources/acceptance/compound__cte_recursive.sql

This file was deleted.

1 change: 0 additions & 1 deletion test/resources/acceptance/compound__cte_simple.sql

This file was deleted.

13 changes: 0 additions & 13 deletions test/resources/acceptance/compound__subselect.sql

This file was deleted.

10 changes: 0 additions & 10 deletions test/resources/acceptance/compound__subselect_table_masking.sql

This file was deleted.

18 changes: 0 additions & 18 deletions test/resources/acceptance/compound__union.sql

This file was deleted.

20 changes: 0 additions & 20 deletions test/resources/acceptance/cycle__cte.sql

This file was deleted.

9 changes: 0 additions & 9 deletions test/resources/acceptance/duplicate_scopes.sql

This file was deleted.

2 changes: 0 additions & 2 deletions test/resources/acceptance/generate_series.sql

This file was deleted.

1 change: 0 additions & 1 deletion test/resources/acceptance/literal__with_table.sql

This file was deleted.

1 change: 0 additions & 1 deletion test/resources/acceptance/literal__without_table.sql

This file was deleted.

2 changes: 0 additions & 2 deletions test/resources/acceptance/no_source_columns.sql

This file was deleted.

5 changes: 0 additions & 5 deletions test/resources/acceptance/oracle__open_for.sql

This file was deleted.

Loading

0 comments on commit a1f3f41

Please sign in to comment.