Skip to content
Snippets Groups Projects
Commit 1fed381d authored by Bruno Burke's avatar Bruno Burke :hamburger:
Browse files

generic unit tests based on protocol, test for versioning

parent 6d6f872d
No related branches found
No related tags found
1 merge request!1Draft: Migration
Pipeline #80955 passed
......@@ -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"]]}})
......@@ -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 :? :*
......
......@@ -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)]
......
......@@ -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)
......
......@@ -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")
......
......@@ -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
......
......@@ -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))
(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))))))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment