From 1fed381dd395a06b06c904f023bca64b50c5ff4c Mon Sep 17 00:00:00 2001 From: Bruno Burke <burke@fh-muenster.de> Date: Sat, 31 Jul 2021 12:31:58 +0200 Subject: [PATCH] generic unit tests based on protocol, test for versioning --- project.clj | 7 ++- resources/sql/_ds_postgres.sql | 4 +- src/document_storage/git/core.clj | 10 +++-- src/document_storage/postgres/core.clj | 23 +++++----- src/document_storage/protocol.clj | 2 +- test/document_storage/git/core_test.clj | 26 +++++------ test/document_storage/postgres/core_test.clj | 20 ++++----- test/document_storage/protocol_test.clj | 47 ++++++++++++++++++++ 8 files changed, 93 insertions(+), 46 deletions(-) create mode 100644 test/document_storage/protocol_test.clj diff --git a/project.clj b/project.clj index 0668a98..0d9980e 100644 --- a/project.clj +++ b/project.clj @@ -16,8 +16,11 @@ [wwsoftware/clj-helper "0.0.3.5"] [org.postgresql/postgresql "42.2.14"]] :profiles {:test [:project/test :profiles/test] + :dev [:project/dev :profiles/dev] + :profiles/dev {} + :project/dev {:dependencies [[environ "1.2.0"]] + :plugins [[lein-environ "1.2.0"]]} :profiles/test {} :project/test {:dependencies [[environ "1.2.0"]] - :plugins [[lein-environ "1.2.0"]]} - }) + :plugins [[lein-environ "1.2.0"]]}}) diff --git a/resources/sql/_ds_postgres.sql b/resources/sql/_ds_postgres.sql index a2b8401..111e905 100644 --- a/resources/sql/_ds_postgres.sql +++ b/resources/sql/_ds_postgres.sql @@ -19,14 +19,14 @@ SELECT * FROM :i:repository-table WHERE id = :id -- :name get-document-versions :? :* -- :doc retrieve all document-versions given the document-id. -SELECT version, data_type, created_at, id +SELECT version, data_type, created_at, id, created_at as "date" FROM :i:repository-table WHERE document_id = :document-id ORDER BY version DESC -- :name get-document-frame-by-version :? :1 -- :doc retrieve a document-frame given the document-id and version. -SELECT * FROM :i:repository-table WHERE document_id = :id AND version = :version +SELECT * FROM :i:repository-table WHERE document_id = :document-id AND version = :version -- :name get-all-document-ids :? :* diff --git a/src/document_storage/git/core.clj b/src/document_storage/git/core.clj index c57c4ae..9776450 100644 --- a/src/document_storage/git/core.clj +++ b/src/document_storage/git/core.clj @@ -24,6 +24,11 @@ (defonce cache (atom {})) +(defn create-storage [dir config] + (let [config-file (str dir "/" "config.edn")] + (io/make-parents config-file) + (spit config-file (with-out-str (fipp config))))) + (defn load-config [dir] (-> dir (str "/config.edn") @@ -89,7 +94,6 @@ (defrecord git-storage [options] protocol/storage (init-storage [{{:keys [dir] :as options} :options :as this}] - (println options) (log/info "Init Storage in " dir) (reset! storage-directory dir) (if-let [config (load-config dir)] @@ -181,10 +185,10 @@ (list-versions [this id repository] (let [repo (get-repo repository)] - (versioning/list-commits repo id))) + (sort-by :date (versioning/list-commits repo id)))) (load-version [this id version repository] - (let [versions (.list-versions this id :repository repository) + (let [versions (.list-versions this id repository) repo (get-repo repository)] (when-let [commit (-> (filter #(= (:version %) version) versions) first :commit)] diff --git a/src/document_storage/postgres/core.clj b/src/document_storage/postgres/core.clj index 7544f1e..6507ad8 100644 --- a/src/document_storage/postgres/core.clj +++ b/src/document_storage/postgres/core.clj @@ -36,11 +36,11 @@ table-names {:schema-name storage-schema :repository-name repository-name :ds-encoding-fqn (str storage-schema - "." - "ds_encoding") + "." + "ds_encoding") :ds-type-fqn (str storage-schema - "." - "ds_type") + "." + "ds_type") :repositories-table (str storage-schema "." "repositories") @@ -68,8 +68,7 @@ (run-query this :get-repositories {})) (load-document [this id repository] - (documents/load-document this id repository) - ) + (documents/load-document this id repository)) (get-document-size [this id repository] (count (documents/load-document this id repository))) @@ -90,9 +89,8 @@ (let [result (into {} (keep (fn [fname] (let [doc (.load-document this fname :repository repository)] (when (query-fits? doc query) - [fname doc] - ) - )) + [fname doc]))) + docs))] result))) @@ -100,12 +98,11 @@ (db/disconnect! (:connection this))) (list-versions [this id repository] - (run-query this :get-document-versions {:repository repository - :document-id id})) + (sort-by :version (run-query this :get-document-versions {:repository repository + :document-id id}))) (load-version [this id version repository] - (documents/load-document this id version repository)) - ) + (documents/load-document this id version repository))) (defn make-postgres-ds [config] (let [conn (db/connect! config) diff --git a/src/document_storage/protocol.clj b/src/document_storage/protocol.clj index 6ed90e4..f819bea 100644 --- a/src/document_storage/protocol.clj +++ b/src/document_storage/protocol.clj @@ -8,7 +8,7 @@ (list-documents [ds repository] "List all documents in a repository") (find-documents [ds query repository] "Find documents with a given query in a repository") (list-repositories [ds] "List all repositories") - (list-versions [ds id repository] "List all version of a specific document in a repository") + (list-versions [ds id repository] "List all version of a specific document in a repository. The result is expected to be sorted in ascending order.") (load-version [ds id version repository] "Load a specific document version in a repository") (get-document-size [ds id repository] "Calculate the current document size") (init-storage [ds] "Start a document storage") diff --git a/test/document_storage/git/core_test.clj b/test/document_storage/git/core_test.clj index bc1b21d..e6c0f09 100644 --- a/test/document_storage/git/core_test.clj +++ b/test/document_storage/git/core_test.clj @@ -2,19 +2,21 @@ (:require [clojure.test :refer :all] [document-storage.core :as ds] [document-storage.git.views :as dsv] + [document-storage.git.core :as gstorage] [clojure.java.io :as io] [fipp.edn :refer [pprint] :rename {pprint fipp}] + [clj-helper.string :refer [get-unique-id]] + [clojure.data :refer [diff]] [document-storage.git.core :as dsgit] - [document-storage.protocol :as dsprot])) + [document-storage.protocol :as dsprot] + [clojure.tools.logging :as log] + [document-storage.protocol-test :as protocol-tests])) (def ds-atom (atom nil)) (defn create-storage [directory] - (let [config-file (str directory "/" "config.edn") - config {:repositories - {:testrepo {}}}] - (io/make-parents config-file) - (spit config-file (with-out-str (fipp config))))) + (gstorage/create-storage directory {:repositories + {:testrepo {}}})) (defn delete-folder @@ -36,13 +38,11 @@ (use-fixtures :once storage-fixture) (deftest simple-test - (testing "save basic structure and load it" - (let [structure {:a 7 :c [1 2 3 4 99 5 -4] :v 3} - id "simple-test1" - repo :testrepo] - (.save-document @ds-atom id structure repo) - (is (= structure - (.load-document @ds-atom id repo)))))) + (protocol-tests/test-save-document ds-atom)) + + +(deftest versioning-test + (protocol-tests/test-versioning ds-atom)) (deftest views-test diff --git a/test/document_storage/postgres/core_test.clj b/test/document_storage/postgres/core_test.clj index db68f58..2a0f7a1 100644 --- a/test/document_storage/postgres/core_test.clj +++ b/test/document_storage/postgres/core_test.clj @@ -5,28 +5,24 @@ [clojure.java.io :as io] [fipp.edn :refer [pprint] :rename {pprint fipp}] [document-storage.protocol :as dsprot] - [environ.core :refer [env]])) + [environ.core :refer [env]] + [document-storage.protocol-test :as protocol-tests])) (def ds-atom (atom nil)) (defn storage-fixture [f] - (println "ENV:" ) - (println (env :database-url)) (reset! ds-atom (dspostgres/make-postgres-ds {:id :testing - :repositories [:testing] + :repositories [:testrepo] :jdbc-url (env :database-url)})) (f) - (.delete-repository @ds-atom :testing)) + (.delete-repository @ds-atom :testrepo)) (use-fixtures :once storage-fixture) (deftest simple-test - (testing "save basic structure and load it" - (let [structure {:a 7 :c [1 2 3 4 99 5 -4] :v 3} - id "simple-test1" - repo :testing] - (.save-document @ds-atom id structure repo) - (is (= structure - (.load-document @ds-atom id repo)))))) + (protocol-tests/test-save-document ds-atom)) + +(deftest versioning-test + (protocol-tests/test-versioning ds-atom)) diff --git a/test/document_storage/protocol_test.clj b/test/document_storage/protocol_test.clj new file mode 100644 index 0000000..b37d07e --- /dev/null +++ b/test/document_storage/protocol_test.clj @@ -0,0 +1,47 @@ +(ns document-storage.protocol-test + (:require [document-storage.protocol :as sut] + [clojure.test :refer :all] + [clj-helper.string :refer [get-unique-id]] + [clojure.data :refer [diff]])) + + +(defn test-save-document [ds-atom] + (testing "save basic structure and load it" + (let [structure {:a 7 :c [1 2 3 4 99 5 -4] :v 3} + id "simple-test1" + repo :testrepo] + (.save-document @ds-atom id structure repo) + (is (some #{id} (.list-documents @ds-atom :testrepo))) + (is (= structure + (.load-document @ds-atom id repo)))))) + +(defn test-versioning [ds-atom] + (testing "update document and check if changes are versioned" + (let [structure-v1 {:a 7 :c [1 2 3 4 99 5 -4] :v 3} + structure-v2 (-> structure-v1 + (assoc :a 1 :v -3) + (assoc-in [:c 2] 1258)) + structure-v3 (-> structure-v2 + (assoc :a -5 :v -6) + (assoc-in [:c 0] 121)) + id (get-unique-id "tdoc") + repo :testrepo] + (.save-document @ds-atom id structure-v1 repo) + (.save-document @ds-atom id structure-v2 repo) + (.save-document @ds-atom id structure-v3 repo) + (let [versions (reverse (.list-versions @ds-atom id repo))] + (is (= (count versions) 3) "Check if there are the 3 expected versions") + (let [v1 (:version (nth versions 2)) + v2 (:version (nth versions 1)) + v3 (:version (nth versions 0)) + storage-doc-v1 (.load-version @ds-atom id v1 repo) + storage-doc-v2 (.load-version @ds-atom id v2 repo) + storage-doc-v3 (.load-version @ds-atom id v3 repo)] + (is (= storage-doc-v1 structure-v1) "check if temporary and stored data is equal") + (is (= storage-doc-v2 structure-v2)) + (is (= storage-doc-v3 structure-v3)) + (is (= (diff structure-v1 structure-v2) + (diff storage-doc-v1 storage-doc-v2))) + (is (= (diff structure-v2 structure-v3) + (diff storage-doc-v2 storage-doc-v3)))))))) + -- GitLab