-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1482 from ClickHouse/json_nested_map_fix
fix: JSON NestedMap + add tests
- Loading branch information
Showing
2 changed files
with
104 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package chcol | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNestedMap(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
input *JSON | ||
expected map[string]any | ||
}{ | ||
{ | ||
name: "nested object with values present", | ||
input: &JSON{ | ||
valuesByPath: map[string]any{ | ||
"x": NewVariant(nil), | ||
"x.a": NewVariant(42), | ||
"x.b": NewVariant(64), | ||
"x.b.c.d": NewVariant(96), | ||
"a.b.c": NewVariant(128), | ||
}, | ||
}, | ||
expected: map[string]any{ | ||
"x": map[string]any{ | ||
"a": NewVariant(42), | ||
"b": NewVariant(64), | ||
"c": map[string]any{ | ||
"d": NewVariant(96), | ||
}, | ||
}, | ||
"a": map[string]any{ | ||
"b": map[string]any{ | ||
"c": NewVariant(128), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "nested object with only top level path present", | ||
input: &JSON{ | ||
valuesByPath: map[string]any{ | ||
"x": NewVariant(42), | ||
"x.a": NewVariant(nil), | ||
"x.b": NewVariant(nil), | ||
"x.b.c.d": NewVariant(nil), | ||
"a.b.c": NewVariant(nil), | ||
}, | ||
}, | ||
expected: map[string]any{ | ||
"x": NewVariant(42), | ||
}, | ||
}, | ||
{ | ||
name: "nested object with typed paths", | ||
input: &JSON{ | ||
valuesByPath: map[string]any{ | ||
"x": 42, | ||
"a.b": "test value", | ||
}, | ||
}, | ||
expected: map[string]any{ | ||
"x": 42, | ||
"a": map[string]any{ | ||
"b": "test value", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "empty object", | ||
input: NewJSON(), | ||
expected: map[string]any{}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
actual := c.input.NestedMap() | ||
require.Equal(t, c.expected, actual) | ||
}) | ||
} | ||
} |