From ca15d2ded7e87d4a9d6bea23c455bf3f978a3287 Mon Sep 17 00:00:00 2001 From: Hyunwoo Nam Date: Wed, 3 Jul 2024 14:19:45 +0900 Subject: [PATCH] add only failing test for deep-merge, simplify the solution --- src/com/walmartlabs/lacinia/executor.clj | 2 +- .../walmartlabs/lacinia/internal_utils.clj | 3 - .../lacinia/internal_utils_tests.clj | 67 +++++-------------- 3 files changed, 17 insertions(+), 55 deletions(-) diff --git a/src/com/walmartlabs/lacinia/executor.clj b/src/com/walmartlabs/lacinia/executor.clj index f81a4d6e..d7bc0c62 100644 --- a/src/com/walmartlabs/lacinia/executor.clj +++ b/src/com/walmartlabs/lacinia/executor.clj @@ -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 diff --git a/src/com/walmartlabs/lacinia/internal_utils.clj b/src/com/walmartlabs/lacinia/internal_utils.clj index 9f1c0885..b11c5a2d 100644 --- a/src/com/walmartlabs/lacinia/internal_utils.clj +++ b/src/com/walmartlabs/lacinia/internal_utils.clj @@ -420,9 +420,6 @@ (null? left) left - (null? right) - right - (and (map? left) (map? right)) (merge-with deep-merge left right) diff --git a/test/com/walmartlabs/lacinia/internal_utils_tests.clj b/test/com/walmartlabs/lacinia/internal_utils_tests.clj index 37e27c9a..083931da 100644 --- a/test/com/walmartlabs/lacinia/internal_utils_tests.clj +++ b/test/com/walmartlabs/lacinia/internal_utils_tests.clj @@ -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))) @@ -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"]])]]))))