Skip to content

Commit

Permalink
Process all examples as integration tests. See #121
Browse files Browse the repository at this point in the history
  • Loading branch information
katauber committed Sep 12, 2023
1 parent e89c552 commit 2d2bb5a
Showing 1 changed file with 55 additions and 45 deletions.
100 changes: 55 additions & 45 deletions test/clj/metafacture_playground/process_test.clj
Original file line number Diff line number Diff line change
@@ -1,49 +1,59 @@
(ns metafacture-playground.process-test
(:require [clojure.test :refer :all]
[metafacture-playground.process :refer [process]]))
(:require [clojure.test :refer [deftest testing is]]
[metafacture-playground.process :refer [process]]
[clojure.java.io :as io]
[lambdaisland.uri :refer [uri query-string->map]]))

; TODO: we should extract the samples here and in db.cljs to files
(def sample-data {:data "1{a: Faust, b {n: Goethe, v: JW}, c: Weimar}\n2{a: Räuber, b {n: Schiller, v: F}, c: Weimar}"
:flux-with-fix "inputFile|\nopen-file\n|as-lines\n|decode-formeta\n|fix(transformationFile)\n|encode-xml(rootTag=\"collection\")\n|print\n;"
:flux-with-morph "inputFile|\nopen-file\n|as-lines\n|decode-formeta\n|morph(transformationFile)\n|encode-xml(rootTag=\"collection\")\n|print\n;"
:fix "move_field(_id, id)\nmove_field(a, title)\npaste(author, b.v, b.n, '~aus', c)\nretain(id, title, author)"
:morph (str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<metamorph xmlns=\"http://www.culturegraph.org/metamorph\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
"\tversion=\"1\">\n"
"\t<rules>\n"
"\t\t<data source=\"_id\" name=\"id\"/>\n"
"\t\t<data source=\"a\" name=\"title\"/>\n"
"\t\t<combine value=\"${first} ${last} aus ${place}\" name=\"author\">\n"
"\t\t\t<data source=\"b.v\" name=\"first\" />\n"
"\t\t\t<data source=\"b.n\" name=\"last\" />\n"
"\t\t\t<data source=\"c\" name=\"place\" />\n"
"\t\t</combine>\n"
"\t</rules>\n"
"</metamorph>\n")})
(deftest processing-correctly-test
(let [test-data {:data "1{a: Faust, b {n: Goethe, v: JW}, c: Weimar}\n2{a: Räuber, b {n: Schiller, v: F}, c: Weimar}"
:flux-with-fix "inputFile|\nopen-file\n|as-lines\n|decode-formeta\n|fix(transformationFile)\n|encode-xml(rootTag=\"collection\")\n|print\n;"
:flux-with-morph "inputFile|\nopen-file\n|as-lines\n|decode-formeta\n|morph(transformationFile)\n|encode-xml(rootTag=\"collection\")\n|print\n;"
:fix "move_field(_id, id)\nmove_field(a, title)\npaste(author, b.v, b.n, '~aus', c)\nretain(id, title, author)"
:morph (str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<metamorph xmlns=\"http://www.culturegraph.org/metamorph\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
"\tversion=\"1\">\n"
"\t<rules>\n"
"\t\t<data source=\"_id\" name=\"id\"/>\n"
"\t\t<data source=\"a\" name=\"title\"/>\n"
"\t\t<combine value=\"${first} ${last} aus ${place}\" name=\"author\">\n"
"\t\t\t<data source=\"b.v\" name=\"first\" />\n"
"\t\t\t<data source=\"b.n\" name=\"last\" />\n"
"\t\t\t<data source=\"c\" name=\"place\" />\n"
"\t\t</combine>\n"
"\t</rules>\n"
"</metamorph>\n")
:result (str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"\n<collection>"
"\n"
"\n <record>"
"\n <id>1</id>"
"\n <title>Faust</title>"
"\n <author>JW Goethe aus Weimar</author>"
"\n </record>"
"\n"
"\n <record>"
"\n <id>2</id>"
"\n <title>Räuber</title>"
"\n <author>F Schiller aus Weimar</author>"
"\n </record>"
"\n"
"\n</collection>\n")}]
(testing "Process sample data (with fix) with correct result."
(let [result (process (:flux-with-fix test-data) (:data test-data) (:fix test-data))]
(is (= result (:result test-data)))))

(def test-data (merge sample-data
{:result (str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"\n<collection>"
"\n"
"\n <record>"
"\n <id>1</id>"
"\n <title>Faust</title>"
"\n <author>JW Goethe aus Weimar</author>"
"\n </record>"
"\n"
"\n <record>"
"\n <id>2</id>"
"\n <title>Räuber</title>"
"\n <author>F Schiller aus Weimar</author>"
"\n </record>"
"\n"
"\n</collection>\n")}))
(testing "Process sample data (with morph) with correct result."
(let [result (process (:flux-with-morph test-data) (:data test-data) (:morph test-data))]
(is (= result (:result test-data)))))))

(deftest processing-test
(testing "Process sample data (with fix) correctly."
(let [result (process (:flux-with-fix test-data) (:data test-data) (:fix test-data))]
(is (= result (:result test-data)))))

(testing "Process sample data (with morph) correctly."
(let [result (process (:flux-with-morph test-data) (:data test-data) (:morph test-data))]
(is (= result (:result test-data))))))
(deftest process-examples-test
(testing "Process all examples in resources/examples without exceptions."
(let [examples (->> (io/resource "examples")
io/file
.listFiles
;(map io/file)
(keep #(when (.isFile %)
(slurp %)))
(map #(-> % uri :query query-string->map)))]
(doseq [example examples]
(is (process (:flux example) (:data example) (:transformation example)))))))

0 comments on commit 2d2bb5a

Please sign in to comment.