From 21b03e2ef8e3c5108e4938e68fd13d3b586a5b60 Mon Sep 17 00:00:00 2001 From: jem Date: Mon, 20 May 2019 08:58:43 +0200 Subject: [PATCH] load test-data dynamicaly --- main/src/data_test/runner.clj | 32 +++++++++++-------- .../data_test/runner_test/test_it.edn | 1 + .../should_test_with_data_record_version.edn | 2 ++ test/src/data_test/runner_test.clj | 6 +++- 4 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 test/resources/data_test/runner_test/test_it.edn create mode 100644 test/resources/data_test_test/should_test_with_data_record_version.edn diff --git a/main/src/data_test/runner.clj b/main/src/data_test/runner.clj index ff082fc..4ebe689 100644 --- a/main/src/data_test/runner.clj +++ b/main/src/data_test/runner.clj @@ -31,8 +31,9 @@ (defprotocol RunTest "Protocol for data driven tests" + (name-prefix [dda-test]) (run-test [dda-test]) - (name-prefix [dda-test])) + ) (s/defn dispatch-by-name :- s/Keyword "Dispatcher for data-tests." @@ -50,24 +51,27 @@ (aero/read-config resource-url)) (s/defn data-file-prefix :- s/Str - [runner :- TestRunner] - (let [name-key (:name runner)] - (str/replace - (str/replace (str (namespace name-key) "/" (name name-key)) - #"-" "_") - #"\." "/"))) + [name-key :- s/Keyword] + (str/replace + (str/replace (str (namespace name-key) "/" (name name-key)) + #"-" "_") + #"\." "/")) + +(s/defn load-test-data + [file-prefix :- s/Str] + (let [file-path (str (data-file-prefix file-prefix) ".edn")] + (println file-path) + (read-data + (io/resource file-path)))) (extend-type TestRunner RunTest (name-prefix [_] - (data-file-prefix _)) + (data-file-prefix (:name _))) (run-test [_] - (println (data-file-prefix _)) - (let [testdata (read-data - (io/resource "data_test_test/should-test-with-data-record-version.edn")) - ;(str (data-file-prefix _) ".edn"))) - {:keys [input expectation]} testdata] - (data-test _ input expectation)))) + (let [testdata (load-test-data (data-file-prefix (:name _))) + {:keys [input expectation]} testdata] + (data-test _ input expectation)))) (defn create-test-runner [test-name] diff --git a/test/resources/data_test/runner_test/test_it.edn b/test/resources/data_test/runner_test/test_it.edn new file mode 100644 index 0000000..e85d33d --- /dev/null +++ b/test/resources/data_test/runner_test/test_it.edn @@ -0,0 +1 @@ +{:test "data"} \ No newline at end of file diff --git a/test/resources/data_test_test/should_test_with_data_record_version.edn b/test/resources/data_test_test/should_test_with_data_record_version.edn new file mode 100644 index 0000000..030c81f --- /dev/null +++ b/test/resources/data_test_test/should_test_with_data_record_version.edn @@ -0,0 +1,2 @@ +{:input 1 + :expectation 1} \ No newline at end of file diff --git a/test/src/data_test/runner_test.clj b/test/src/data_test/runner_test.clj index 8855c1c..c395d9d 100644 --- a/test/src/data_test/runner_test.clj +++ b/test/src/data_test/runner_test.clj @@ -29,7 +29,11 @@ (deftest should-calculate-data-file-prefix (is (= "data_test/runner_test/test_it" - (sut/data-file-prefix (sut/create-test-runner ::test-it))))) + (sut/data-file-prefix ::test-it)))) + +(deftest should-load-data + (is (= {:test "data"} + (sut/load-test-data ::test-it)))) (s/defmethod sut/data-test ::test-it [_ input :- s/Any expectation :- s/Any]