-
Notifications
You must be signed in to change notification settings - Fork 67
/
complex_test.go
134 lines (126 loc) · 3.54 KB
/
complex_test.go
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package super_test
import (
"bytes"
"strings"
"testing"
"github.com/brimdata/super"
"github.com/brimdata/super/zcode"
"github.com/brimdata/super/zio/zsonio"
"github.com/brimdata/super/zson"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRecordAccessNamed(t *testing.T) {
const input = `{foo:"hello" (=zfile),bar:true (=zbool)} (=0)`
rec := zson.MustParseValue(super.NewContext(), input)
s := rec.Deref("foo").AsString()
assert.Equal(t, s, "hello")
b := rec.Deref("bar").AsBool()
assert.Equal(t, b, true)
}
func TestNonRecordDeref(t *testing.T) {
const input = `
1
192.168.1.1
null
[1,2,3]
|[1,2,3]|`
reader := zsonio.NewReader(super.NewContext(), strings.NewReader(input))
for {
val, err := reader.Read()
if val == nil {
break
}
require.NoError(t, err)
v := val.Deref("foo")
require.Nil(t, v)
}
}
func TestNormalizeSet(t *testing.T) {
t.Run("duplicate-element", func(t *testing.T) {
b := zcode.NewBuilder()
b.BeginContainer()
b.Append([]byte("dup"))
b.Append([]byte("dup"))
b.TransformContainer(super.NormalizeSet)
b.EndContainer()
set := zcode.Append(nil, []byte("dup"))
expected := zcode.Append(nil, set)
require.Exactly(t, expected, b.Bytes())
})
t.Run("unsorted-elements", func(t *testing.T) {
b := zcode.NewBuilder()
b.BeginContainer()
b.Append([]byte("z"))
b.Append([]byte("a"))
b.TransformContainer(super.NormalizeSet)
b.EndContainer()
set := zcode.Append(nil, []byte("a"))
set = zcode.Append(set, []byte("z"))
expected := zcode.Append(nil, set)
require.Exactly(t, expected, b.Bytes())
})
t.Run("unsorted-and-duplicate-elements", func(t *testing.T) {
b := zcode.NewBuilder()
big := bytes.Repeat([]byte("x"), 256)
small := []byte("small")
b.Append(big)
b.BeginContainer()
// Append duplicate elements in reverse of set-normal order.
for i := 0; i < 3; i++ {
b.Append(big)
b.Append(big)
b.Append(small)
b.Append(small)
b.Append(nil)
b.Append(nil)
}
b.TransformContainer(super.NormalizeSet)
b.EndContainer()
set := zcode.Append(nil, nil)
set = zcode.Append(set, small)
set = zcode.Append(set, big)
expected := zcode.Append(nil, big)
expected = zcode.Append(expected, set)
require.Exactly(t, expected, b.Bytes())
})
}
func TestDuplicates(t *testing.T) {
ctx := super.NewContext()
setType := ctx.LookupTypeSet(super.TypeInt32)
typ1, err := ctx.LookupTypeRecord([]super.Field{
{"a", super.TypeString},
{"b", setType},
})
require.NoError(t, err)
typ2, err := zson.ParseType(ctx, "{a:string,b:|[int32]|}")
require.NoError(t, err)
assert.EqualValues(t, typ1.ID(), typ2.ID())
assert.EqualValues(t, setType.ID(), typ2.(*super.TypeRecord).Fields[1].Type.ID())
typ3, err := ctx.LookupByValue(super.EncodeTypeValue(setType))
require.NoError(t, err)
assert.Equal(t, setType.ID(), typ3.ID())
}
func TestTranslateNamed(t *testing.T) {
c1 := super.NewContext()
c2 := super.NewContext()
set1, err := zson.ParseType(c1, "|[int64]|")
require.NoError(t, err)
set2, err := zson.ParseType(c2, "|[int64]|")
require.NoError(t, err)
named1, err := c1.LookupTypeNamed("foo", set1)
require.NoError(t, err)
named2, err := c2.LookupTypeNamed("foo", set2)
require.NoError(t, err)
named3, err := c2.TranslateType(named1)
require.NoError(t, err)
assert.Equal(t, named2, named3)
}
func TestCopyMutateFields(t *testing.T) {
c := super.NewContext()
fields := []super.Field{{"foo", super.TypeString}, {"bar", super.TypeInt64}}
typ, err := c.LookupTypeRecord(fields)
require.NoError(t, err)
fields[0].Type = nil
require.NotNil(t, typ.Fields[0].Type)
}