The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
(ns metabase-enterprise.remote-sync.test-helpers-test
"Tests for the MockSource implementation in test-helpers."
(:require
[clojure.test :refer :all]
[metabase-enterprise.remote-sync.source.protocol :as source.p]
[metabase-enterprise.remote-sync.test-helpers :as th]))
(defn- write-files!
"Wholesale-write `files` ({:path :content}) to `snapshot` via the commit builder (clear managed dirs,
stage every file, push)."
[snapshot message files]
(let [c (source.p/open-commit snapshot)]
(source.p/replace-all! c)
(doseq [f files] (source.p/stage-upsert! c f))
(source.p/finish-commit! c message)))
(deftest mock-source-write-files-managed-dir-cleanup-test
(testing "MockSource removes files in managed dirs not in write set"
(let [source (th/create-mock-source
:initial-files {"main" {"collections/abc/file1.yaml" "content1"
"collections/abc/file2.yaml" "content2"
"collections/def/file3.yaml" "content3"
"other/file4.yaml" "content4"}}
:managed-dirs #{"collections"})
snapshot (source.p/snapshot source)]
(write-files! snapshot "Write only abc"
[{:path "collections/abc/file1.yaml" :content "new-content1"}])
(is (= #{"collections/abc/file1.yaml" "other/file4.yaml"}
(set (source.p/list-files snapshot)))
"Only written files in managed dirs should remain; unmanaged dirs untouched"))))
(deftest mock-source-write-files-unmanaged-preserved-test
(testing "MockSource preserves files in unmanaged directories"
(let [source (th/create-mock-source
:initial-files {"main" {"collections/abc/file1.yaml" "content1"
"unmanaged/file2.yaml" "content2"}}
:managed-dirs #{"collections"})
snapshot (source.p/snapshot source)]
(write-files! snapshot "Write collections"
[{:path "collections/abc/file1.yaml" :content "new-content"}])
(is (= #{"collections/abc/file1.yaml" "unmanaged/file2.yaml"}
(set (source.p/list-files snapshot)))
"Unmanaged directory files should be preserved"))))
(deftest mock-source-write-files-empty-managed-dir-cleanup-test
(testing "MockSource cleans managed dir even when no files written to it"
(let [source (th/create-mock-source
:initial-files {"main" {"collections/abc/file1.yaml" "content1"
"snippets/old.yaml" "old-snippet"}}
:managed-dirs #{"collections" "snippets"})
snapshot (source.p/snapshot source)]
;; Write only to collections, nothing to snippets
(write-files! snapshot "Write only collections"
[{:path "collections/abc/file1.yaml" :content "new-content"}])
(is (= #{"collections/abc/file1.yaml"}
(set (source.p/list-files snapshot)))
"Snippets dir should be cleaned even though no snippet files were written"))))