(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]] [document-storage.testdata :refer [generate-document]])) (defn test-save-document [ds-atom] (testing "save basic structure and load it" (let [structure (generate-document) 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))) (.delete-document @ds-atom id repo) (is (not (.load-document @ds-atom id repo)) "Deleted documents should not be loadable with .load-document") (is (not (some #{id} (.list-documents @ds-atom repo))) "Deleted documents should not be listed with .list-documents")))) (defn test-versioning [ds-atom] (testing "update document and check if changes are versioned" (let [structure-v1 (generate-document) #_{: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)))))))) (defn test-find-documents [ds-atom] (testing "create document with specific value and see if find-documents will find it" (let [needle-attributes {:title (str (gensym "unique")) :id (str (gensym "unique")) :x 35000} needle (-> (generate-document) (merge needle-attributes)) haystack (repeatedly 200 #(-> (generate-document) (assoc :id (str (gensym "random-id"))))) repo :testrepo] (doseq [d haystack] (.save-document @ds-atom (:id d) d repo)) (.save-document @ds-atom (:id needle) needle repo) (is (= needle (.load-document @ds-atom (:id needle) repo)) "Check if testdocument was saved correctly") (let [find-results (.find-documents @ds-atom [[:title [= (:title needle-attributes)]] [:x [= (:x needle-attributes)]]] repo)] (is (= 1 (count find-results)) "Search should return only one document") (is (= needle (first (vals find-results))) "Search should return testdocument"))))) ;;;TODO deftest delete-document ;;;TODO shutdown ;;;TODO get-document-size