File: resolve_test.go

package info (click to toggle)
golang-github-humanlogio-api 0.0~git20250305.fa41d14-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,352 kB
  • sloc: sh: 45; makefile: 8
file content (101 lines) | stat: -rw-r--r-- 2,470 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package logql

import (
	"testing"

	"github.com/google/go-cmp/cmp"
	typesv1 "github.com/humanlogio/api/go/types/v1"
	"github.com/stretchr/testify/require"
)

func TestResolveVal(t *testing.T) {
	tests := []struct {
		name    string
		in      *typesv1.Val
		mkMap   MakeMap
		mkSlice MakeSlice
		want    any
	}{
		{
			name: "flatmap",
			in: typesv1.ValObj(
				typesv1.KeyVal("composite", typesv1.ValObj(
					typesv1.KeyVal("depth1", typesv1.ValObj(
						typesv1.KeyVal("depth2", typesv1.ValStr("deep-value")),
					)),
				)),
				typesv1.KeyVal("simple", typesv1.ValStr("value")),
			),
			mkMap:   MakeFlatGoMap,
			mkSlice: MakeFlatMapGoSlice,
			want: map[string]any{
				"composite.depth1.depth2": "deep-value",
				"simple":                  "value",
			},
		},
		{
			name: "flatmapslice",
			in: typesv1.ValArr(
				typesv1.ValStr("value1"),
				typesv1.ValStr("value2"),
				typesv1.ValObj(
					typesv1.KeyVal("composite", typesv1.ValObj(
						typesv1.KeyVal("depth1", typesv1.ValObj(
							typesv1.KeyVal("depth2", typesv1.ValStr("deep-value")),
						)),
					)),
					typesv1.KeyVal("simple", typesv1.ValStr("value")),
				),
			),
			mkMap:   MakeFlatGoMap,
			mkSlice: MakeFlatMapGoSlice,
			want: map[string]any{
				"0":                         "value1",
				"1":                         "value2",
				"2.composite.depth1.depth2": "deep-value",
				"2.simple":                  "value",
			},
		},
		{
			name: "flatslice",
			in: typesv1.ValArr(
				typesv1.ValStr("value1"),
				typesv1.ValStr("value2"),
				typesv1.ValArr(typesv1.ValStr("el0"), typesv1.ValStr("el1")),
				typesv1.ValObj(
					typesv1.KeyVal("composite", typesv1.ValObj(
						typesv1.KeyVal("depth1", typesv1.ValObj(
							typesv1.KeyVal("depth2", typesv1.ValStr("deep-value")),
						)),
					)),
					typesv1.KeyVal("simple", typesv1.ValStr("value")),
				),
			),
			mkMap:   MakeFlatGoMap,
			mkSlice: MakeFlatGoSlice,
			want: []any{
				"value1",
				"value2",
				[]any{"el0", "el1"},
				map[string]any{
					"composite.depth1.depth2": "deep-value",
					"simple":                  "value",
				},
			},
		},
		{
			name:    "string",
			in:      typesv1.ValStr("a-string"),
			mkMap:   MakeFlatGoMap,
			mkSlice: MakeFlatGoSlice,
			want:    "a-string",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := ResolveVal(tt.in, tt.mkMap, tt.mkSlice)
			require.NoError(t, err)
			require.Empty(t, cmp.Diff(tt.want, got))
		})
	}
}