Skip to content

Commit

Permalink
add only failing test for deep-merge, simplify the solution
Browse files Browse the repository at this point in the history
  • Loading branch information
namenu committed Jul 3, 2024
1 parent b7b8d5e commit ca15d2d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/com/walmartlabs/lacinia/executor.clj
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
[left-value right-value]
(if (su/is-result-tuple? right-value)
(let [{:keys [alias value]} right-value
left-alias-value (alias left-value)]
left-alias-value (alias left-value)]
(cond
(= left-alias-value :com.walmartlabs.lacinia.schema/null)
left-value
Expand Down
3 changes: 0 additions & 3 deletions src/com/walmartlabs/lacinia/internal_utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,6 @@
(null? left)
left

(null? right)
right

(and (map? left) (map? right))
(merge-with deep-merge left right)

Expand Down
67 changes: 16 additions & 51 deletions test/com/walmartlabs/lacinia/internal_utils_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
(:require
[clojure.test :refer [deftest testing is]]
[com.walmartlabs.lacinia.internal-utils :refer [assoc-in! update-in! deep-merge]]
[clojure.string :as str])
[clojure.string :as str]
[flatland.ordered.map :refer [ordered-map]])
(:import
(clojure.lang ExceptionInfo)))

Expand Down Expand Up @@ -65,54 +66,18 @@
(ex-data e)))))

(deftest test-deep-merge
(testing "Basic map merge"
(is (= (deep-merge {:a 1} {:b 2})
{:a 1, :b 2}))
(is (= (deep-merge {:a 1} {:a 2})
{:a 2})))
(= (ordered-map [[:author :com.walmartlabs.lacinia.schema/null]])
(deep-merge
(ordered-map [[:author (ordered-map [[:name "John Doe"]])]])
(ordered-map [[:author :com.walmartlabs.lacinia.schema/null]]))
(deep-merge
(ordered-map [[:author :com.walmartlabs.lacinia.schema/null]])
(ordered-map [[:author (ordered-map [[:name "John Doe"]])]])))

(testing "Nested map merge"
(is (= (deep-merge {:a {:b 1}} {:a {:c 2}})
{:a {:b 1, :c 2}}))
(is (= (deep-merge {:a {:b 1}} {:a {:b 2}})
{:a {:b 2}})))

(testing "Mixed map and sequential"
(is (thrown-with-msg? ExceptionInfo #"unable to deep merge"
(deep-merge {:a 1} [1 2 3])))
(is (thrown-with-msg? ExceptionInfo #"unable to deep merge"
(deep-merge [1 2 3] {:a 1}))))

#_(testing "Merge with nil values"
(is (thrown-with-msg? ExceptionInfo #"unable to deep merge" (deep-merge {:a 1} nil)
{:a 1}))
(is (thrown-with-msg? ExceptionInfo #"unable to deep merge" (deep-merge nil {:a 1})
{:a 1})))

(testing "Merging with :com.walmartlabs.lacinia.schema/null values"
(is (= (deep-merge {:a 1} :com.walmartlabs.lacinia.schema/null)
:com.walmartlabs.lacinia.schema/null))
(is (= (deep-merge :com.walmartlabs.lacinia.schema/null {:a 1})
:com.walmartlabs.lacinia.schema/null))
(is (= (deep-merge :com.walmartlabs.lacinia.schema/null :com.walmartlabs.lacinia.schema/null)
:com.walmartlabs.lacinia.schema/null)))

(testing "Merging with empty maps"
(is (= (deep-merge {} {:a 1})
{:a 1}))
(is (= (deep-merge {:a 1} {})
{:a 1})))

(testing "Complex nested structures"
(is (= (deep-merge {:a {:b [1 2]}} {:a {:b [3 4]}})
{:a {:b [3 4]}}))
(is (= (deep-merge {:a [{:b 1} {:c 2}]} {:a [{:d 3} {:e 4}]})
{:a [{:b 1, :d 3} {:c 2, :e 4}]})))

(testing "Nested :com.walmartlabs.lacinia.schema/null values"
(is (= (deep-merge {:a {:b :com.walmartlabs.lacinia.schema/null}} {:a {:b 1}})
{:a {:b :com.walmartlabs.lacinia.schema/null}}))
(is (= (deep-merge {:a {:b 1}} {:a {:b :com.walmartlabs.lacinia.schema/null}})
{:a {:b :com.walmartlabs.lacinia.schema/null}}))
(is (= (deep-merge {:a {:b :com.walmartlabs.lacinia.schema/null, :c 2}} {:a {:b 1, :c :com.walmartlabs.lacinia.schema/null}})
{:a {:b :com.walmartlabs.lacinia.schema/null, :c :com.walmartlabs.lacinia.schema/null}}))))
(= (ordered-map [[:author nil]])
(deep-merge
(ordered-map [[:author (ordered-map [[:name "John Doe"]])]])
(ordered-map [[:author nil]]))
(deep-merge
(ordered-map [[:author nil]])
(ordered-map [[:author (ordered-map [[:name "John Doe"]])]]))))

0 comments on commit ca15d2d

Please sign in to comment.