Merge branch 'implement-common-test' into 'master'

Add common-test namespace with map-diff function

See merge request domaindrivenarchitecture/c4k-common!1
This commit is contained in:
Mattis Böckle 2022-06-17 06:54:06 +00:00
commit 0ea8a86c9e
2 changed files with 52 additions and 0 deletions

View file

@ -0,0 +1,26 @@
(ns dda.c4k-common.common-test)
; Heavily assumes c1 and c2 have an identical structure but maybe different values
; This function finds the diff of two nested maps/vectors
(defn map-diff
([c1 c2]
(into {}
(cond (or (map? c1) (vector? c1))
(map
#(if (= (% c1) (% c2))
nil
(let [c1-value (% c1)
c2-value (% c2)
key-name (name %)]
(cond
(map? c1-value) (map-diff c1-value c2-value)
(vector? c1-value) (map-diff c1-value c2-value key-name)
:else {(keyword (str key-name "-c1")) c1-value
(keyword (str key-name "-c2")) c2-value})))
(keys c1)))))
([c1 c2 last-name]
(first (for [x c1 y c2] (cond
(map? x) (map-diff x y)
(vector? x) (map-diff x y last-name)
:else {(keyword (str last-name "-c1")) x
(keyword (str last-name "-c2")) y})))))

View file

@ -3,6 +3,7 @@
#?(:clj [clojure.test :refer [deftest is are testing run-tests]] #?(:clj [clojure.test :refer [deftest is are testing run-tests]]
:cljs [cljs.test :refer-macros [deftest is are testing run-tests]]) :cljs [cljs.test :refer-macros [deftest is are testing run-tests]])
[clojure.spec.test.alpha :as st] [clojure.spec.test.alpha :as st]
[dda.c4k-common.common-test :as ct]
[dda.c4k-common.postgres :as cut])) [dda.c4k-common.postgres :as cut]))
(st/instrument `cut/generate-config) (st/instrument `cut/generate-config)
@ -27,6 +28,13 @@
(:data (cut/generate-config {:db-name "test"})))) (:data (cut/generate-config {:db-name "test"}))))
) )
(deftest should-generate-config-diff
(is (= {:postgres-db-c1 "postgres",
:postgres-db-c2 "test",
:postgresql.conf-c1 "max_connections = 100\nwork_mem = 4MB\nshared_buffers = 512MB\n",
:postgresql.conf-c2 "max_connections = 700\nwork_mem = 3MB\nshared_buffers = 2048MB\n"}
(ct/map-diff (cut/generate-config) (cut/generate-config {:db-name "test" :postgres-size :8gb})))))
(deftest should-generate-persistent-volume (deftest should-generate-persistent-volume
(is (= {:kind "PersistentVolume" (is (= {:kind "PersistentVolume"
:apiVersion "v1" :apiVersion "v1"
@ -50,6 +58,13 @@
(cut/generate-persistent-volume {:postgres-data-volume-path "xx" (cut/generate-persistent-volume {:postgres-data-volume-path "xx"
:pv-storage-size-gb 20})))) :pv-storage-size-gb 20}))))
(deftest should-generate-persistent-volume-diff
(is (= {:storage-c1 "10Gi", :storage-c2 "20Gi",
:path-c1 "/var/postgres", :path-c2 "xx"}
(ct/map-diff (cut/generate-persistent-volume {})
(cut/generate-persistent-volume {:postgres-data-volume-path "xx"
:pv-storage-size-gb 20})))))
(deftest should-generate-persistent-volume-claim (deftest should-generate-persistent-volume-claim
(is (= {:apiVersion "v1" (is (= {:apiVersion "v1"
:kind "PersistentVolumeClaim" :kind "PersistentVolumeClaim"
@ -71,6 +86,13 @@
(cut/generate-pvc {:pv-storage-size-gb 20 (cut/generate-pvc {:pv-storage-size-gb 20
:pvc-storage-class-name :local-path})))) :pvc-storage-class-name :local-path}))))
(deftest should-generate-persistent-volume-claim-diff
(is (= {:storageClassName-c1 "manual", :storageClassName-c2 "local-path",
:storage-c1 "10Gi", :storage-c2 "20Gi"}
(ct/map-diff (cut/generate-pvc {})
(cut/generate-pvc {:pv-storage-size-gb 20
:pvc-storage-class-name :local-path})))))
(deftest should-generate-secret (deftest should-generate-secret
(is (= {:apiVersion "v1" (is (= {:apiVersion "v1"
:kind "Secret" :kind "Secret"
@ -106,3 +128,7 @@
:mountPath "/var/lib/postgresql/data"}]}] :mountPath "/var/lib/postgresql/data"}]}]
(get-in (cut/generate-deployment {:postgres-image "postgres:14"}) (get-in (cut/generate-deployment {:postgres-image "postgres:14"})
[:spec :template :spec :containers])))) [:spec :template :spec :containers]))))
(deftest should-generate-deployment-diff
(is (= {:image-c1 "postgres:13", :image-c2 "postgres:14"}
(ct/map-diff (cut/generate-deployment) (cut/generate-deployment {:postgres-image "postgres:14"})))))